UCControlBase.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCControlBase.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. using System.Drawing.Drawing2D;
  25. using System.Reflection;
  26. namespace HZH_Controls.Controls
  27. {
  28. /// <summary>
  29. /// Class UCControlBase.
  30. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  31. /// Implements the <see cref="System.Windows.Forms.IContainerControl" />
  32. /// </summary>
  33. /// <seealso cref="System.Windows.Forms.UserControl" />
  34. /// <seealso cref="System.Windows.Forms.IContainerControl" />
  35. [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))]
  36. public partial class UCControlBase : UserControl, IContainerControl
  37. {
  38. /// <summary>
  39. /// The is radius
  40. /// </summary>
  41. private bool _isRadius = false;
  42. /// <summary>
  43. /// The corner radius
  44. /// </summary>
  45. private int _cornerRadius = 24;
  46. /// <summary>
  47. /// The is show rect
  48. /// </summary>
  49. private bool _isShowRect = false;
  50. /// <summary>
  51. /// The rect color
  52. /// </summary>
  53. private Color _rectColor = Color.FromArgb(220, 220, 220);
  54. /// <summary>
  55. /// The rect width
  56. /// </summary>
  57. private int _rectWidth = 1;
  58. /// <summary>
  59. /// The fill color
  60. /// </summary>
  61. private Color _fillColor = Color.Transparent;
  62. /// <summary>
  63. /// 是否圆角
  64. /// </summary>
  65. /// <value><c>true</c> if this instance is radius; otherwise, <c>false</c>.</value>
  66. [Description("是否圆角"), Category("自定义")]
  67. public virtual bool IsRadius
  68. {
  69. get
  70. {
  71. return this._isRadius;
  72. }
  73. set
  74. {
  75. this._isRadius = value;
  76. Refresh();
  77. }
  78. }
  79. /// <summary>
  80. /// 圆角角度
  81. /// </summary>
  82. /// <value>The coner radius.</value>
  83. [Description("圆角角度"), Category("自定义")]
  84. public virtual int ConerRadius
  85. {
  86. get
  87. {
  88. return this._cornerRadius;
  89. }
  90. set
  91. {
  92. this._cornerRadius = Math.Max(value, 1);
  93. Refresh();
  94. }
  95. }
  96. /// <summary>
  97. /// 是否显示边框
  98. /// </summary>
  99. /// <value><c>true</c> if this instance is show rect; otherwise, <c>false</c>.</value>
  100. [Description("是否显示边框"), Category("自定义")]
  101. public virtual bool IsShowRect
  102. {
  103. get
  104. {
  105. return this._isShowRect;
  106. }
  107. set
  108. {
  109. this._isShowRect = value;
  110. Refresh();
  111. }
  112. }
  113. /// <summary>
  114. /// 边框颜色
  115. /// </summary>
  116. /// <value>The color of the rect.</value>
  117. [Description("边框颜色"), Category("自定义")]
  118. public virtual Color RectColor
  119. {
  120. get
  121. {
  122. return this._rectColor;
  123. }
  124. set
  125. {
  126. this._rectColor = value;
  127. this.Refresh();
  128. }
  129. }
  130. /// <summary>
  131. /// 边框宽度
  132. /// </summary>
  133. /// <value>The width of the rect.</value>
  134. [Description("边框宽度"), Category("自定义")]
  135. public virtual int RectWidth
  136. {
  137. get
  138. {
  139. return this._rectWidth;
  140. }
  141. set
  142. {
  143. this._rectWidth = value;
  144. Refresh();
  145. }
  146. }
  147. /// <summary>
  148. /// 当使用边框时填充颜色,当值为背景色或透明色或空值则不填充
  149. /// </summary>
  150. /// <value>The color of the fill.</value>
  151. [Description("当使用边框时填充颜色,当值为背景色或透明色或空值则不填充"), Category("自定义")]
  152. public virtual Color FillColor
  153. {
  154. get
  155. {
  156. return this._fillColor;
  157. }
  158. set
  159. {
  160. this._fillColor = value;
  161. Refresh();
  162. }
  163. }
  164. /// <summary>
  165. /// Initializes a new instance of the <see cref="UCControlBase" /> class.
  166. /// </summary>
  167. public UCControlBase()
  168. {
  169. this.InitializeComponent();
  170. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  171. this.SetStyle(ControlStyles.DoubleBuffer, true);
  172. this.SetStyle(ControlStyles.ResizeRedraw, true);
  173. this.SetStyle(ControlStyles.Selectable, true);
  174. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  175. this.SetStyle(ControlStyles.UserPaint, true);
  176. }
  177. /// <summary>
  178. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  179. /// </summary>
  180. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  181. protected override void OnPaint(PaintEventArgs e)
  182. {
  183. if (this.Visible)
  184. {
  185. if (this._isRadius)
  186. {
  187. this.SetWindowRegion();
  188. }
  189. else
  190. {
  191. //关闭圆角后显示为原矩形
  192. GraphicsPath g = new GraphicsPath();
  193. g.AddRectangle(base.ClientRectangle);
  194. g.CloseFigure();
  195. base.Region = new Region(g);
  196. }
  197. GraphicsPath graphicsPath = new GraphicsPath();
  198. if (this._isShowRect || (_fillColor != Color.Empty && _fillColor != Color.Transparent && _fillColor != this.BackColor))
  199. {
  200. Rectangle clientRectangle = base.ClientRectangle;
  201. if (_isRadius)
  202. {
  203. graphicsPath.AddArc(0, 0, _cornerRadius, _cornerRadius, 180f, 90f);
  204. graphicsPath.AddArc(clientRectangle.Width - _cornerRadius - 1, 0, _cornerRadius, _cornerRadius, 270f, 90f);
  205. graphicsPath.AddArc(clientRectangle.Width - _cornerRadius - 1, clientRectangle.Height - _cornerRadius - 1, _cornerRadius, _cornerRadius, 0f, 90f);
  206. graphicsPath.AddArc(0, clientRectangle.Height - _cornerRadius - 1, _cornerRadius, _cornerRadius, 90f, 90f);
  207. graphicsPath.CloseFigure();
  208. }
  209. else
  210. {
  211. graphicsPath.AddRectangle(clientRectangle);
  212. }
  213. }
  214. e.Graphics.SetGDIHigh();
  215. if (_fillColor != Color.Empty && _fillColor != Color.Transparent && _fillColor != this.BackColor)
  216. e.Graphics.FillPath(new SolidBrush(this._fillColor), graphicsPath);
  217. if (this._isShowRect)
  218. {
  219. Color rectColor = this._rectColor;
  220. Pen pen = new Pen(rectColor, (float)this._rectWidth);
  221. e.Graphics.DrawPath(pen, graphicsPath);
  222. }
  223. }
  224. base.OnPaint(e);
  225. }
  226. /// <summary>
  227. /// Sets the window region.
  228. /// </summary>
  229. private void SetWindowRegion()
  230. {
  231. GraphicsPath path = new GraphicsPath();
  232. Rectangle rect = new Rectangle(-1, -1, base.Width + 1, base.Height);
  233. path = this.GetRoundedRectPath(rect, this._cornerRadius);
  234. base.Region = new Region(path);
  235. }
  236. /// <summary>
  237. /// Gets the rounded rect path.
  238. /// </summary>
  239. /// <param name="rect">The rect.</param>
  240. /// <param name="radius">The radius.</param>
  241. /// <returns>GraphicsPath.</returns>
  242. private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
  243. {
  244. Rectangle rect2 = new Rectangle(rect.Location, new Size(radius, radius));
  245. GraphicsPath graphicsPath = new GraphicsPath();
  246. graphicsPath.AddArc(rect2, 180f, 90f);//左上角
  247. rect2.X = rect.Right - radius;
  248. graphicsPath.AddArc(rect2, 270f, 90f);//右上角
  249. rect2.Y = rect.Bottom - radius;
  250. rect2.Width += 1;
  251. rect2.Height += 1;
  252. graphicsPath.AddArc(rect2, 360f, 90f);//右下角
  253. rect2.X = rect.Left;
  254. graphicsPath.AddArc(rect2, 90f, 90f);//左下角
  255. graphicsPath.CloseFigure();
  256. return graphicsPath;
  257. }
  258. /// <summary>
  259. /// WNDs the proc.
  260. /// </summary>
  261. /// <param name="m">要处理的 Windows <see cref="T:System.Windows.Forms.Message" />。</param>
  262. protected override void WndProc(ref Message m)
  263. {
  264. if (m.Msg != 20)
  265. {
  266. base.WndProc(ref m);
  267. }
  268. }
  269. }
  270. }