UserButton.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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.Windows.Forms;
  9. using System.Drawing.Drawing2D;
  10. namespace HslCommunication.Controls
  11. {
  12. /// <summary>
  13. /// 一个自定义的按钮控件
  14. /// </summary>
  15. [DefaultEvent("Click")]
  16. public partial class UserButton : UserControl
  17. {
  18. /// <summary>
  19. /// 实例化一个按钮对象
  20. /// </summary>
  21. public UserButton()
  22. {
  23. InitializeComponent();
  24. }
  25. private void UserButton_Load(object sender, EventArgs e)
  26. {
  27. sf = new StringFormat();
  28. sf.Alignment = StringAlignment.Center;
  29. sf.LineAlignment = StringAlignment.Center;
  30. SizeChanged += UserButton_SizeChanged;
  31. Font = new Font("微软雅黑", Font.Size, Font.Style);
  32. MouseEnter += UserButton_MouseEnter;
  33. MouseLeave += UserButton_MouseLeave;
  34. MouseDown += UserButton_MouseDown;
  35. MouseUp += UserButton_MouseUp;
  36. SetStyle(ControlStyles.ResizeRedraw, true);
  37. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  38. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  39. }
  40. private void UserButton_SizeChanged(object sender, EventArgs e)
  41. {
  42. if (Width > 1)
  43. {
  44. Invalidate();
  45. }
  46. }
  47. private int RoundCorner = 3;
  48. private StringFormat sf = null;
  49. private string _text = "button";
  50. /// <summary>
  51. /// 设置或获取显示的文本
  52. /// </summary>
  53. [Category("外观")]
  54. [DefaultValue("button")]
  55. [Description("用来设置显示的文本信息")]
  56. public string UIText
  57. {
  58. get { return _text; }
  59. set { _text = value; Invalidate(); }
  60. }
  61. /// <summary>
  62. /// 设置或获取显示文本的颜色
  63. /// </summary>
  64. [Category("外观")]
  65. [DefaultValue(typeof(Color), "Black")]
  66. [Description("用来设置显示的文本的颜色")]
  67. public Color TextColor { get; set; } = Color.Black;
  68. /// <summary>
  69. /// 设置按钮的圆角
  70. /// </summary>
  71. [Category("外观")]
  72. [DefaultValue(3)]
  73. [Description("按钮框的圆角属性")]
  74. public int CornerRadius
  75. {
  76. get { return RoundCorner; }
  77. set { RoundCorner = value; Invalidate(); }
  78. }
  79. private bool m_Selected = false;
  80. /// <summary>
  81. /// 用来设置按钮的选中状态
  82. /// </summary>
  83. [Category("外观")]
  84. [DefaultValue(false)]
  85. [Description("指示按钮的选中状态")]
  86. public bool Selected
  87. {
  88. get { return m_Selected; }
  89. set { m_Selected = value; Invalidate(); }
  90. }
  91. /// <summary>
  92. /// 已经弃用
  93. /// </summary>
  94. [Browsable(false)]
  95. public override Color ForeColor
  96. {
  97. get { return base.ForeColor; }
  98. set { base.ForeColor = value; }
  99. }
  100. /// <summary>
  101. /// 已经弃用
  102. /// </summary>
  103. [Browsable(false)]
  104. public override string Text
  105. {
  106. get { return base.Text; }
  107. set { base.Text = value; }
  108. }
  109. private Color m_backcor = Color.Lavender;
  110. /// <summary>
  111. /// 按钮的背景色
  112. /// </summary>
  113. [Category("外观")]
  114. [DefaultValue(typeof(Color), "Lavender")]
  115. [Description("按钮的背景色")]
  116. public Color OriginalColor
  117. {
  118. get{return m_backcor; }
  119. set{m_backcor = value;Invalidate(); }
  120. }
  121. private Color m_enablecolor = Color.FromArgb(190, 190, 190);
  122. /// <summary>
  123. /// 按钮的背景色
  124. /// </summary>
  125. [Category("外观")]
  126. [Description("按钮的活动色")]
  127. public Color EnableColor
  128. {
  129. get { return m_enablecolor; }
  130. set { m_enablecolor = value; Invalidate(); }
  131. }
  132. private Color m_active = Color.AliceBlue;
  133. /// <summary>
  134. /// 鼠标挪动时的活动颜色
  135. /// </summary>
  136. [Category("外观")]
  137. [DefaultValue(typeof(Color), "AliceBlue")]
  138. [Description("按钮的活动色")]
  139. public Color ActiveColor
  140. {
  141. get { return m_active; }
  142. set { m_active = value; Invalidate(); }
  143. }
  144. private bool m_BorderVisiable = true;
  145. /// <summary>
  146. /// 设置按钮的边框是否可见
  147. /// </summary>
  148. [Category("外观")]
  149. [Browsable(true)]
  150. [DefaultValue(true)]
  151. [Description("指示按钮是否存在边框")]
  152. public bool BorderVisiable
  153. {
  154. get { return m_BorderVisiable; }
  155. set { m_BorderVisiable = value; Invalidate(); }
  156. }
  157. /// <summary>
  158. /// 存放用户需要保存的一些额外的信息
  159. /// </summary>
  160. [Browsable(false)]
  161. public string CustomerInformation { get; set; } = "";
  162. #region 光标移动块
  163. private bool is_mouse_on { get; set; } = false;
  164. private void UserButton_MouseLeave(object sender, EventArgs e)
  165. {
  166. is_mouse_on = false;
  167. Invalidate();
  168. }
  169. private void UserButton_MouseEnter(object sender, EventArgs e)
  170. {
  171. is_mouse_on = true;
  172. Invalidate();
  173. }
  174. #endregion
  175. #region 鼠标点击块
  176. private bool is_left_mouse_down { get; set; } = false;
  177. private void UserButton_MouseUp(object sender, MouseEventArgs e)
  178. {
  179. if (e.Button == MouseButtons.Left)
  180. {
  181. is_left_mouse_down = false;
  182. Invalidate();
  183. }
  184. }
  185. private void UserButton_MouseDown(object sender, MouseEventArgs e)
  186. {
  187. if (e.Button == MouseButtons.Left)
  188. {
  189. is_left_mouse_down = true;
  190. Invalidate();
  191. }
  192. }
  193. #endregion
  194. /// <summary>
  195. /// 触发一次点击的事件
  196. /// </summary>
  197. public void PerformClick()
  198. {
  199. OnClick(new EventArgs());
  200. }
  201. /// <summary>
  202. /// 重绘数据区
  203. /// </summary>
  204. /// <param name="e"></param>
  205. protected override void OnPaint(PaintEventArgs e)
  206. {
  207. e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
  208. e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  209. GraphicsPath path = new GraphicsPath();
  210. path.AddLine(RoundCorner, 0, Width - RoundCorner - 1, 0);
  211. path.AddArc(Width - RoundCorner * 2 - 1, 0, RoundCorner * 2, RoundCorner * 2, 270f, 90f);
  212. path.AddLine(Width - 1, RoundCorner, Width - 1, Height - RoundCorner - 1);
  213. path.AddArc(Width - RoundCorner * 2 - 1, Height - RoundCorner * 2 - 1, RoundCorner * 2, RoundCorner * 2, 0f, 90f);
  214. path.AddLine(Width - RoundCorner - 1, Height - 1, RoundCorner, Height - 1);
  215. path.AddArc(0, Height - RoundCorner * 2 - 1, RoundCorner * 2, RoundCorner * 2, 90f, 90f);
  216. path.AddLine(0, Height - RoundCorner - 1, 0, RoundCorner);
  217. path.AddArc(0, 0, RoundCorner * 2, RoundCorner * 2, 180f, 90f);
  218. Brush brush_fore_text = null;
  219. Brush brush_back_text = null;
  220. Rectangle rect_text = new Rectangle(ClientRectangle.X, ClientRectangle.Y,
  221. ClientRectangle.Width, ClientRectangle.Height);
  222. if (Enabled)
  223. {
  224. brush_fore_text = new SolidBrush(TextColor);
  225. if(Selected)
  226. {
  227. brush_back_text = new SolidBrush(Color.DodgerBlue);
  228. }
  229. else if (is_mouse_on)
  230. {
  231. brush_back_text = new SolidBrush(ActiveColor);
  232. }
  233. else
  234. {
  235. brush_back_text = new SolidBrush(OriginalColor);
  236. }
  237. if (is_left_mouse_down)
  238. {
  239. rect_text.Offset(1, 1);
  240. }
  241. }
  242. else
  243. {
  244. brush_fore_text = new SolidBrush(Color.Gray);
  245. brush_back_text = new SolidBrush(EnableColor);
  246. }
  247. e.Graphics.FillPath(brush_back_text, path);
  248. Pen pen_border = new Pen(Color.FromArgb(170, 170, 170));
  249. if (BorderVisiable)
  250. {
  251. e.Graphics.DrawPath(pen_border, path);
  252. }
  253. e.Graphics.DrawString(UIText, Font, brush_fore_text, rect_text, sf);
  254. //base.OnPaint(e);
  255. brush_fore_text.Dispose();
  256. brush_back_text.Dispose();
  257. pen_border.Dispose();
  258. path.Dispose();
  259. }
  260. private void UserButton_KeyDown(object sender, KeyEventArgs e)
  261. {
  262. if (e.KeyCode == Keys.Enter)
  263. {
  264. OnClick(new EventArgs());
  265. }
  266. }
  267. /// <summary>
  268. /// 点击按钮的触发事件
  269. /// </summary>
  270. /// <param name="e"></param>
  271. protected override void OnClick(EventArgs e)
  272. {
  273. if (Enabled)
  274. {
  275. base.OnClick(e);
  276. }
  277. }
  278. /// <summary>
  279. /// 点击的时候触发事件
  280. /// </summary>
  281. /// <param name="e"></param>
  282. protected override void OnMouseClick(MouseEventArgs e)
  283. {
  284. if (Enabled)
  285. {
  286. base.OnMouseClick(e);
  287. }
  288. }
  289. }
  290. }