UCBtnExt.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCBtnExt.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCBtnExt.
  28. /// Implements the <see cref="HZH_Controls.Controls.UCControlBase" />
  29. /// </summary>
  30. /// <seealso cref="HZH_Controls.Controls.UCControlBase" />
  31. [DefaultEvent("BtnClick")]
  32. public partial class UCBtnExt : UCControlBase
  33. {
  34. #region 字段属性
  35. private bool enabledMouseEffect = false;
  36. [Description("是否启用鼠标效果"), Category("自定义")]
  37. public bool EnabledMouseEffect
  38. {
  39. get { return enabledMouseEffect; }
  40. set { enabledMouseEffect = value; }
  41. }
  42. /// <summary>
  43. /// 是否显示角标
  44. /// </summary>
  45. /// <value><c>true</c> if this instance is show tips; otherwise, <c>false</c>.</value>
  46. [Description("是否显示角标"), Category("自定义")]
  47. public bool IsShowTips
  48. {
  49. get
  50. {
  51. return this.lblTips.Visible;
  52. }
  53. set
  54. {
  55. this.lblTips.Visible = value;
  56. }
  57. }
  58. /// <summary>
  59. /// 角标文字
  60. /// </summary>
  61. /// <value>The tips text.</value>
  62. [Description("角标文字"), Category("自定义")]
  63. public string TipsText
  64. {
  65. get
  66. {
  67. return this.lblTips.Text;
  68. }
  69. set
  70. {
  71. this.lblTips.Text = value;
  72. }
  73. }
  74. /// <summary>
  75. /// The BTN back color
  76. /// </summary>
  77. private Color _btnBackColor = Color.White;
  78. /// <summary>
  79. /// 按钮背景色
  80. /// </summary>
  81. /// <value>The color of the BTN back.</value>
  82. [Description("按钮背景色"), Category("自定义")]
  83. public Color BtnBackColor
  84. {
  85. get { return _btnBackColor; }
  86. set
  87. {
  88. _btnBackColor = value;
  89. this.BackColor = value;
  90. }
  91. }
  92. /// <summary>
  93. /// The BTN fore color
  94. /// </summary>
  95. private Color _btnForeColor = Color.White;
  96. /// <summary>
  97. /// 按钮字体颜色
  98. /// </summary>
  99. /// <value>The color of the BTN fore.</value>
  100. [Description("按钮字体颜色"), Category("自定义")]
  101. public virtual Color BtnForeColor
  102. {
  103. get { return _btnForeColor; }
  104. set
  105. {
  106. _btnForeColor = value;
  107. this.lbl.ForeColor = value;
  108. }
  109. }
  110. /// <summary>
  111. /// The BTN font
  112. /// </summary>
  113. private Font _btnFont = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  114. /// <summary>
  115. /// 按钮字体
  116. /// </summary>
  117. /// <value>The BTN font.</value>
  118. [Description("按钮字体"), Category("自定义")]
  119. public Font BtnFont
  120. {
  121. get { return _btnFont; }
  122. set
  123. {
  124. _btnFont = value;
  125. this.lbl.Font = value;
  126. }
  127. }
  128. /// <summary>
  129. /// 按钮点击事件
  130. /// </summary>
  131. [Description("按钮点击事件"), Category("自定义")]
  132. public event EventHandler BtnClick;
  133. /// <summary>
  134. /// The BTN text
  135. /// </summary>
  136. private string _btnText;
  137. /// <summary>
  138. /// 按钮文字
  139. /// </summary>
  140. /// <value>The BTN text.</value>
  141. [Description("按钮文字"), Category("自定义")]
  142. public virtual string BtnText
  143. {
  144. get { return _btnText; }
  145. set
  146. {
  147. _btnText = value;
  148. lbl.Text = value;
  149. }
  150. }
  151. /// <summary>
  152. /// The m tips color
  153. /// </summary>
  154. private Color m_tipsColor = Color.FromArgb(232, 30, 99);
  155. /// <summary>
  156. /// 角标颜色
  157. /// </summary>
  158. /// <value>The color of the tips.</value>
  159. [Description("角标颜色"), Category("自定义")]
  160. public Color TipsColor
  161. {
  162. get { return m_tipsColor; }
  163. set { m_tipsColor = value; }
  164. }
  165. [Description("鼠标效果生效时发生,需要和MouseEffected同时使用,否则无效"), Category("自定义")]
  166. public event EventHandler MouseEffecting;
  167. [Description("鼠标效果结束时发生,需要和MouseEffecting同时使用,否则无效"), Category("自定义")]
  168. public event EventHandler MouseEffected;
  169. #endregion
  170. /// <summary>
  171. /// Initializes a new instance of the <see cref="UCBtnExt" /> class.
  172. /// </summary>
  173. public UCBtnExt()
  174. {
  175. InitializeComponent();
  176. this.TabStop = false;
  177. lblTips.Paint += lblTips_Paint;
  178. this.lbl.MouseEnter += lbl_MouseEnter;
  179. this.lbl.MouseLeave += lbl_MouseLeave;
  180. }
  181. Color m_cacheColor = Color.Empty;
  182. void lbl_MouseLeave(object sender, EventArgs e)
  183. {
  184. if (enabledMouseEffect)
  185. {
  186. if (MouseEffecting != null && MouseEffected != null)
  187. {
  188. MouseEffected(this, e);
  189. }
  190. else
  191. {
  192. if (m_cacheColor != Color.Empty)
  193. {
  194. this.FillColor = m_cacheColor;
  195. m_cacheColor = Color.Empty;
  196. }
  197. }
  198. }
  199. }
  200. void lbl_MouseEnter(object sender, EventArgs e)
  201. {
  202. if (enabledMouseEffect)
  203. {
  204. if (MouseEffecting != null && MouseEffected != null)
  205. {
  206. MouseEffecting(this, e);
  207. }
  208. else
  209. {
  210. if (FillColor != Color.Empty && FillColor != null)
  211. {
  212. m_cacheColor = this.FillColor;
  213. this.FillColor = Color.FromArgb(230, this.FillColor);
  214. }
  215. }
  216. }
  217. }
  218. /// <summary>
  219. /// Handles the Paint event of the lblTips control.
  220. /// </summary>
  221. /// <param name="sender">The source of the event.</param>
  222. /// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
  223. void lblTips_Paint(object sender, PaintEventArgs e)
  224. {
  225. e.Graphics.SetGDIHigh();
  226. e.Graphics.FillEllipse(new SolidBrush(m_tipsColor), new Rectangle(0, 0, lblTips.Width - 1, lblTips.Height - 1));
  227. System.Drawing.SizeF sizeEnd = e.Graphics.MeasureString(TipsText, lblTips.Font);
  228. e.Graphics.DrawString(TipsText, lblTips.Font, new SolidBrush(lblTips.ForeColor), new PointF((lblTips.Width - sizeEnd.Width) / 2, (lblTips.Height - sizeEnd.Height) / 2 + 1));
  229. }
  230. /// <summary>
  231. /// Handles the MouseDown event of the lbl control.
  232. /// </summary>
  233. /// <param name="sender">The source of the event.</param>
  234. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  235. private void lbl_MouseDown(object sender, MouseEventArgs e)
  236. {
  237. if (this.BtnClick != null)
  238. BtnClick(this, e);
  239. }
  240. }
  241. }