DotControl.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (c) 2007-2012 Michael Chapman
  2. // http://ipaddresscontrollib.googlecode.com
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  15. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  16. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  17. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  18. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. using System;
  20. using System.Drawing;
  21. using System.Windows.Forms;
  22. using System.Windows.Forms.VisualStyles;
  23. namespace IPAddressControlLib
  24. {
  25. internal class DotControl : Control
  26. {
  27. #region Public Properties
  28. public override Size MinimumSize
  29. {
  30. get
  31. {
  32. using ( Graphics g = Graphics.FromHwnd( Handle ) )
  33. {
  34. _sizeText = g.MeasureString( Text, Font, -1, _stringFormat );
  35. }
  36. // MeasureString() cuts off the bottom pixel for descenders no matter
  37. // which StringFormatFlags are chosen. This doesn't matter for '.' but
  38. // it's here in case someone wants to modify the text.
  39. //
  40. _sizeText.Height += 1F;
  41. return _sizeText.ToSize();
  42. }
  43. }
  44. public bool ReadOnly
  45. {
  46. get
  47. {
  48. return _readOnly;
  49. }
  50. set
  51. {
  52. _readOnly = value;
  53. Invalidate();
  54. }
  55. }
  56. #endregion // Public Properties
  57. #region Public Methods
  58. public override string ToString()
  59. {
  60. return Text;
  61. }
  62. #endregion // Public Methods
  63. #region Constructors
  64. public DotControl()
  65. {
  66. Text = Properties.Resources.FieldSeparator;
  67. _stringFormat = StringFormat.GenericTypographic;
  68. _stringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
  69. BackColor = SystemColors.Window;
  70. Size = MinimumSize;
  71. TabStop = false;
  72. SetStyle( ControlStyles.AllPaintingInWmPaint, true );
  73. SetStyle( ControlStyles.OptimizedDoubleBuffer, true );
  74. SetStyle( ControlStyles.ResizeRedraw, true );
  75. SetStyle( ControlStyles.UserPaint, true );
  76. SetStyle( ControlStyles.FixedHeight, true );
  77. SetStyle( ControlStyles.FixedWidth, true );
  78. }
  79. #endregion // Constructors
  80. #region Protected Methods
  81. protected override void OnFontChanged( EventArgs e )
  82. {
  83. base.OnFontChanged( e );
  84. Size = MinimumSize;
  85. }
  86. protected override void OnPaint( PaintEventArgs e )
  87. {
  88. if ( null == e ) { throw new ArgumentNullException( "e" ); }
  89. base.OnPaint( e );
  90. Color backColor = BackColor;
  91. if ( !_backColorChanged )
  92. {
  93. if ( !Enabled || ReadOnly )
  94. {
  95. backColor = SystemColors.Control;
  96. }
  97. }
  98. Color textColor = ForeColor;
  99. if ( !Enabled )
  100. {
  101. textColor = SystemColors.GrayText;
  102. }
  103. else if ( ReadOnly )
  104. {
  105. if ( !_backColorChanged )
  106. {
  107. textColor = SystemColors.WindowText;
  108. }
  109. }
  110. using ( SolidBrush backgroundBrush = new SolidBrush( backColor ) )
  111. {
  112. e.Graphics.FillRectangle( backgroundBrush, ClientRectangle );
  113. }
  114. using ( SolidBrush foreBrush = new SolidBrush( textColor ) )
  115. {
  116. float x = (float)ClientRectangle.Width / 2F - _sizeText.Width / 2F;
  117. e.Graphics.DrawString( Text, Font, foreBrush,
  118. new RectangleF( x, 0F, _sizeText.Width, _sizeText.Height ), _stringFormat );
  119. }
  120. }
  121. protected override void OnParentBackColorChanged( EventArgs e )
  122. {
  123. base.OnParentBackColorChanged( e );
  124. BackColor = Parent.BackColor;
  125. _backColorChanged = true;
  126. }
  127. protected override void OnParentForeColorChanged( EventArgs e )
  128. {
  129. base.OnParentForeColorChanged( e );
  130. ForeColor = Parent.ForeColor;
  131. }
  132. protected override void OnSizeChanged( EventArgs e )
  133. {
  134. base.OnSizeChanged( e );
  135. base.Size = MinimumSize;
  136. }
  137. #endregion // Protected Methods
  138. #region Private Data
  139. private bool _backColorChanged;
  140. private bool _readOnly;
  141. private StringFormat _stringFormat;
  142. private SizeF _sizeText;
  143. #endregion // Private Data
  144. }
  145. }