QQButton.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using TestProject.Properties;
  11. using System.Drawing.Text;
  12. using System.Drawing.Drawing2D;
  13. namespace TestProject
  14. {
  15. public partial class QQButton : Button
  16. {
  17. private EMouseEnum _mouseState = EMouseEnum.Normal;
  18. private bool _isShowBorder = true;
  19. private TextFormatFlags _textAlign = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
  20. public QQButton()
  21. {
  22. InitializeComponent();
  23. }
  24. internal EMouseEnum MouseState
  25. {
  26. get { return this._mouseState; }
  27. set
  28. {
  29. this._mouseState = value;
  30. base.Invalidate();
  31. }
  32. }
  33. /// <summary>
  34. /// 整个按钮的区域
  35. /// </summary>
  36. internal Rectangle AllRect
  37. {
  38. get { return new Rectangle(0, 0, this.Width, this.Height); }
  39. }
  40. /// <summary>
  41. /// 是否显示发光边框
  42. /// </summary>
  43. [Description("是否显示发光边框")]
  44. public virtual bool IsShowBorder
  45. {
  46. get { return this._isShowBorder; }
  47. set { this._isShowBorder = value; }
  48. }
  49. /// <summary>
  50. /// 文字区域
  51. /// </summary>
  52. internal Rectangle TextRect
  53. {
  54. get { return new Rectangle(2, 2, this.AllRect.Width - 4, this.AllRect.Height - 4); }
  55. }
  56. /// <summary>
  57. /// 按钮上文字的对齐方式
  58. /// </summary>
  59. [Description("按钮上文字的对齐方式")]
  60. new public ContentAlignment TextAlign
  61. {
  62. get { return base.TextAlign; }
  63. set
  64. {
  65. base.TextAlign = value;
  66. switch (base.TextAlign)
  67. {
  68. case ContentAlignment.BottomCenter:
  69. this._textAlign = TextFormatFlags.Bottom |
  70. TextFormatFlags.HorizontalCenter |
  71. TextFormatFlags.SingleLine;
  72. break;
  73. case ContentAlignment.BottomLeft:
  74. this._textAlign = TextFormatFlags.Bottom |
  75. TextFormatFlags.Left |
  76. TextFormatFlags.SingleLine;
  77. break;
  78. case ContentAlignment.BottomRight:
  79. this._textAlign = TextFormatFlags.Bottom |
  80. TextFormatFlags.Right |
  81. TextFormatFlags.SingleLine;
  82. break;
  83. case ContentAlignment.MiddleCenter:
  84. this._textAlign = TextFormatFlags.SingleLine |
  85. TextFormatFlags.HorizontalCenter |
  86. TextFormatFlags.VerticalCenter;
  87. break;
  88. case ContentAlignment.MiddleLeft:
  89. this._textAlign = TextFormatFlags.Left |
  90. TextFormatFlags.VerticalCenter |
  91. TextFormatFlags.SingleLine;
  92. break;
  93. case ContentAlignment.MiddleRight:
  94. this._textAlign = TextFormatFlags.Right |
  95. TextFormatFlags.VerticalCenter |
  96. TextFormatFlags.SingleLine;
  97. break;
  98. case ContentAlignment.TopCenter:
  99. this._textAlign = TextFormatFlags.Top |
  100. TextFormatFlags.HorizontalCenter |
  101. TextFormatFlags.SingleLine;
  102. break;
  103. case ContentAlignment.TopLeft:
  104. this._textAlign = TextFormatFlags.Top |
  105. TextFormatFlags.Left |
  106. TextFormatFlags.SingleLine;
  107. break;
  108. case ContentAlignment.TopRight:
  109. this._textAlign = TextFormatFlags.Top |
  110. TextFormatFlags.Right |
  111. TextFormatFlags.SingleLine;
  112. break;
  113. }
  114. base.Invalidate(this.TextRect);
  115. }
  116. }
  117. protected override void OnPaint(PaintEventArgs pevent)
  118. {
  119. Graphics g = pevent.Graphics;
  120. g.SmoothingMode = SmoothingMode.AntiAlias;
  121. g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  122. switch (MouseState)
  123. {
  124. case EMouseEnum.Leave:
  125. case EMouseEnum.Normal:
  126. if (base.Focused)
  127. {
  128. if (this.IsShowBorder)
  129. {
  130. using (Image focus = Resources.focus)
  131. {
  132. DrawHelper.RendererBackground(g, this.TextRect, focus, true);
  133. }
  134. }
  135. else
  136. {
  137. DrawHelper.RendererBackground(g, this.TextRect, Resources.normal, true);
  138. }
  139. }
  140. else
  141. {
  142. DrawHelper.RendererBackground(g, this.TextRect, Resources.normal, true);
  143. }
  144. break;
  145. case EMouseEnum.Up:
  146. case EMouseEnum.Move:
  147. DrawHelper.RendererBackground(g, this.TextRect, Resources.highlight, true);
  148. break;
  149. case EMouseEnum.Down:
  150. DrawHelper.RendererBackground(g, this.TextRect, Resources.Light, true);
  151. break;
  152. }
  153. TextRenderer.DrawText(g, this.Text, this.Font, this.TextRect, this.ForeColor, this._textAlign);
  154. }
  155. /// <summary>
  156. /// 引发 System.Windows.Forms.Form.MouseEnter 事件。
  157. /// </summary>
  158. /// <param name="e">包含事件数据的 System.EventArgs。</param>
  159. protected override void OnMouseEnter(EventArgs e)
  160. {
  161. base.OnMouseEnter(e);
  162. this.MouseState = EMouseEnum.Move;
  163. }
  164. /// <summary>
  165. /// 引发 System.Windows.Forms.Form.MouseLeave 事件。
  166. /// </summary>
  167. /// <param name="e">包含事件数据的 System.EventArgs。</param>
  168. protected override void OnMouseLeave(EventArgs e)
  169. {
  170. base.OnMouseLeave(e);
  171. this.MouseState = EMouseEnum.Leave;
  172. }
  173. /// <summary>
  174. /// 引发 System.Windows.Forms.Form.MouseDown 事件。
  175. /// </summary>
  176. /// <param name="mevent">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
  177. protected override void OnMouseDown(MouseEventArgs mevent)
  178. {
  179. base.OnMouseDown(mevent);
  180. this.MouseState = EMouseEnum.Down;
  181. }
  182. /// <summary>
  183. /// 引发 System.Windows.Forms.Form.MouseUp 事件。
  184. /// </summary>
  185. /// <param name="mevent">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
  186. protected override void OnMouseUp(MouseEventArgs mevent)
  187. {
  188. base.OnMouseUp(mevent);
  189. this.MouseState = EMouseEnum.Up;
  190. }
  191. }
  192. }