RadioButtonEx.cs
上传用户:nnpulika
上传日期:2013-02-15
资源大小:597k
文件大小:5k
源码类别:

状态条

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Windows.Forms;
  7. using UtilityLibrary.General;
  8. using UtilityLibrary.Win32;
  9. namespace UtilityLibrary.WinControls
  10. {
  11. /// <summary>
  12. /// Summary description for RadioButtonEx.
  13. /// </summary>
  14. [ToolboxItem(false)]
  15. public class RadioButtonEx : System.Windows.Forms.RadioButton
  16. {
  17. #region Class Variables
  18. const int CIRCLE_DIAMETER = 11;
  19. DrawState drawState = DrawState.Normal;
  20. #endregion
  21. #region Constructors
  22. public RadioButtonEx()
  23. {
  24. // Our control needs to have Flat style set
  25. FlatStyle = FlatStyle.Flat;
  26. }
  27. #endregion
  28. #region Overrides
  29. protected override void OnMouseEnter(EventArgs e)
  30. {
  31. // Set state to hot
  32. base.OnMouseEnter(e);
  33. drawState = DrawState.Hot;
  34. Invalidate();
  35. }
  36. protected override void OnMouseLeave(EventArgs e)
  37. {
  38. // Set state to Normal
  39. base.OnMouseLeave(e);
  40. if ( !ContainsFocus )
  41. {
  42. drawState = DrawState.Normal;
  43. Invalidate();
  44. }
  45. }
  46.       
  47. protected override void OnGotFocus(EventArgs e)
  48. {
  49. // Set state to Hot
  50. base.OnGotFocus(e);
  51. drawState = DrawState.Hot;
  52. Invalidate();
  53. }
  54.         
  55. protected override void OnLostFocus(EventArgs e)
  56. {
  57. // Set state to Normal
  58. base.OnLostFocus(e);
  59. drawState = DrawState.Normal;
  60. Invalidate();
  61. }
  62. protected override  void WndProc(ref Message m)
  63. {
  64. bool callBase = true;
  65. switch(m.Msg)
  66. {
  67. case ((int)Msg.WM_PAINT):
  68. {
  69. // Let the edit control do its painting
  70. base.WndProc(ref m);
  71. // Now do our custom painting
  72. DrawRadioButtonState(Enabled?drawState:DrawState.Disable);
  73. callBase = false;
  74. }
  75. break;
  76. default:
  77. break;
  78. }
  79. if ( callBase )
  80. base.WndProc(ref m);
  81. }
  82. #endregion
  83. #region Properties
  84. public new FlatStyle FlatStyle 
  85. {
  86. // Don't let the user change this property
  87. // to anything other than flat, otherwise
  88. // there would be painting problems
  89. get { return base.FlatStyle; } 
  90. set 
  91. {
  92. if ( value != FlatStyle.Flat )
  93. {
  94. // Throw an exception to tell the user
  95. // that this property needs to be "Flat" 
  96. // if he is to use this class
  97. string message = "FlatStyle needs to be set to Flat for this class";
  98. ArgumentException argumentException = new ArgumentException("FlatStyle", message);
  99. throw(argumentException);
  100. }
  101. else 
  102. base.FlatStyle = value;
  103. }
  104. }
  105. #endregion
  106. #region Implementation
  107. void DrawRadioButtonState(DrawState state)
  108. {
  109. Rectangle rect = ClientRectangle;
  110. // Create DC for the whole edit window instead of just for the client area
  111. IntPtr hDC = WindowsAPI.GetDC(Handle);
  112. Rectangle circleRect = Rectangle.Empty;
  113. if ( RightToLeft == RightToLeft.No )
  114. {
  115. circleRect = new Rectangle(rect.Left, rect.Top + (rect.Height-CIRCLE_DIAMETER)/2, 
  116. CIRCLE_DIAMETER, CIRCLE_DIAMETER);
  117. }
  118. else
  119. {
  120. circleRect = new Rectangle(rect.Right-CIRCLE_DIAMETER-1, rect.Top + (rect.Height-CIRCLE_DIAMETER)/2, 
  121. CIRCLE_DIAMETER, CIRCLE_DIAMETER);
  122. }
  123. using (Graphics g = Graphics.FromHdc(hDC))
  124. {
  125. // Always paint the inner circle with the Window color
  126. g.FillEllipse(SystemBrushes.Window, circleRect);
  127. if ( state == DrawState.Normal )
  128. {
  129. // Draw normal black circle
  130. g.DrawEllipse(Pens.Black, circleRect);
  131. }
  132. else if ( state == DrawState.Hot )
  133. {
  134. g.DrawEllipse(SystemPens.Highlight, circleRect);
  135. }
  136. else if ( state == DrawState.Disable )
  137. {
  138. // draw highlighted rectangle
  139. g.DrawEllipse(SystemPens.ControlDark, circleRect);
  140. }
  141. if ( Checked )
  142. DrawCircleGlyph(g, state);
  143. }
  144. // Release DC
  145. WindowsAPI.ReleaseDC(Handle, hDC);
  146.             
  147. }
  148. void DrawCircleGlyph(Graphics g, DrawState state)
  149. {
  150. Rectangle rc = ClientRectangle;
  151. // Calculate coordinates
  152. Point point1 = Point.Empty;
  153. Point point2 = Point.Empty;
  154. Point point3 = Point.Empty;
  155. Point point4 = Point.Empty;
  156. if ( RightToLeft == RightToLeft.No )
  157. {
  158. point1 = new Point(rc.Left + 4, rc.Top + rc.Top + (rc.Height-4)/2 + 2);
  159. point2 = new Point(point1.X + 4, point1.Y);
  160. point3 = new Point(rc.Left + 6, rc.Top + rc.Top + (rc.Height-4)/2);
  161. point4 = new Point(point3.X, point3.Y + 4);
  162. }
  163. else
  164. {
  165. point1 = new Point(rc.Right - 4, rc.Top + rc.Top + (rc.Height-4)/2 + 2);
  166. point2 = new Point(point1.X - 4, point1.Y);
  167. point3 = new Point(rc.Right - 6, rc.Top + rc.Top + (rc.Height-4)/2);
  168. point4 = new Point(point3.X, point3.Y + 4);
  169. }
  170. // Choose color
  171. Color checkColor = Color.Empty;
  172. if ( state == DrawState.Normal )
  173. checkColor = Color.Black;
  174. else if (state == DrawState.Hot )
  175. checkColor = ColorUtil.VSNetBorderColor;
  176. else if ( state == DrawState.Disable )
  177. checkColor = SystemColors.ControlDark;
  178. // Draw the check mark
  179. using ( Pen pen = new Pen(checkColor, 2) )
  180. {
  181. g.DrawLine(pen, point1, point2);
  182. g.DrawLine(pen, point3, point4);
  183. }
  184. }
  185. #endregion
  186. }
  187. }