UCArrow.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-10
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCArrow.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.Linq;
  19. using System.Text;
  20. using System.Windows.Forms;
  21. using System.Drawing;
  22. using System.Drawing.Drawing2D;
  23. using System.ComponentModel;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCArrow.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public class UCArrow : UserControl
  32. {
  33. /// <summary>
  34. /// The arrow color
  35. /// </summary>
  36. private Color arrowColor = Color.FromArgb(255, 77, 59);
  37. /// <summary>
  38. /// Gets or sets the color of the arrow.
  39. /// </summary>
  40. /// <value>The color of the arrow.</value>
  41. [Description("箭头颜色"), Category("自定义")]
  42. public Color ArrowColor
  43. {
  44. get { return arrowColor; }
  45. set
  46. {
  47. arrowColor = value;
  48. Refresh();
  49. }
  50. }
  51. /// <summary>
  52. /// The border color
  53. /// </summary>
  54. private Color? borderColor = null;
  55. /// <summary>
  56. /// Gets or sets the color of the border.
  57. /// </summary>
  58. /// <value>The color of the border.</value>
  59. [Description("箭头边框颜色,为空则无边框"), Category("自定义")]
  60. public Color? BorderColor
  61. {
  62. get { return borderColor; }
  63. set
  64. {
  65. borderColor = value;
  66. Refresh();
  67. }
  68. }
  69. /// <summary>
  70. /// The direction
  71. /// </summary>
  72. private ArrowDirection direction = ArrowDirection.Right;
  73. /// <summary>
  74. /// Gets or sets the direction.
  75. /// </summary>
  76. /// <value>The direction.</value>
  77. [Description("箭头方向"), Category("自定义")]
  78. public ArrowDirection Direction
  79. {
  80. get { return direction; }
  81. set
  82. {
  83. direction = value;
  84. ResetPath();
  85. Refresh();
  86. }
  87. }
  88. /// <summary>
  89. /// 获取或设置控件显示的文字的字体。
  90. /// </summary>
  91. /// <value>The font.</value>
  92. /// <PermissionSet>
  93. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  94. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  95. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  96. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  97. /// </PermissionSet>
  98. public override Font Font
  99. {
  100. get
  101. {
  102. return base.Font;
  103. }
  104. set
  105. {
  106. base.Font = value;
  107. Refresh();
  108. }
  109. }
  110. /// <summary>
  111. /// 获取或设置控件的前景色。
  112. /// </summary>
  113. /// <value>The color of the fore.</value>
  114. /// <PermissionSet>
  115. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  116. /// </PermissionSet>
  117. public override Color ForeColor
  118. {
  119. get
  120. {
  121. return base.ForeColor;
  122. }
  123. set
  124. {
  125. base.ForeColor = value;
  126. Refresh();
  127. }
  128. }
  129. /// <summary>
  130. /// The text
  131. /// </summary>
  132. private string text;
  133. /// <summary>
  134. /// Gets or sets the text.
  135. /// </summary>
  136. /// <value>The text.</value>
  137. [Bindable(true)]
  138. [Browsable(true)]
  139. [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  140. [EditorBrowsable(EditorBrowsableState.Always)]
  141. [Localizable(true)]
  142. [Description("箭头文字"), Category("自定义")]
  143. public override string Text
  144. {
  145. get
  146. {
  147. return text;
  148. }
  149. set
  150. {
  151. text = value;
  152. Refresh();
  153. }
  154. }
  155. /// <summary>
  156. /// The m path
  157. /// </summary>
  158. GraphicsPath m_path;
  159. /// <summary>
  160. /// Initializes a new instance of the <see cref="UCArrow" /> class.
  161. /// </summary>
  162. public UCArrow()
  163. {
  164. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  165. this.SetStyle(ControlStyles.DoubleBuffer, true);
  166. this.SetStyle(ControlStyles.ResizeRedraw, true);
  167. this.SetStyle(ControlStyles.Selectable, true);
  168. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  169. this.SetStyle(ControlStyles.UserPaint, true);
  170. this.ForeColor = Color.White;
  171. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  172. this.SizeChanged += UCArrow_SizeChanged;
  173. this.Size = new Size(100, 50);
  174. }
  175. /// <summary>
  176. /// Handles the SizeChanged event of the UCArrow control.
  177. /// </summary>
  178. /// <param name="sender">The source of the event.</param>
  179. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  180. void UCArrow_SizeChanged(object sender, EventArgs e)
  181. {
  182. ResetPath();
  183. }
  184. /// <summary>
  185. /// Resets the path.
  186. /// </summary>
  187. private void ResetPath()
  188. {
  189. Point[] ps = null;
  190. switch (direction)
  191. {
  192. case ArrowDirection.Left:
  193. ps = new Point[]
  194. {
  195. new Point(0,this.Height/2),
  196. new Point(40,0),
  197. new Point(40,this.Height/4),
  198. new Point(this.Width-1,this.Height/4),
  199. new Point(this.Width-1,this.Height-this.Height/4),
  200. new Point(40,this.Height-this.Height/4),
  201. new Point(40,this.Height),
  202. new Point(0,this.Height/2)
  203. };
  204. break;
  205. case ArrowDirection.Right:
  206. ps = new Point[]
  207. {
  208. new Point(0,this.Height/4),
  209. new Point(this.Width-40,this.Height/4),
  210. new Point(this.Width-40,0),
  211. new Point(this.Width-1,this.Height/2),
  212. new Point(this.Width-40,this.Height),
  213. new Point(this.Width-40,this.Height-this.Height/4),
  214. new Point(0,this.Height-this.Height/4),
  215. new Point(0,this.Height/4)
  216. };
  217. break;
  218. case ArrowDirection.Top:
  219. ps = new Point[]
  220. {
  221. new Point(this.Width/2,0),
  222. new Point(this.Width,40),
  223. new Point(this.Width-this.Width/4,40),
  224. new Point(this.Width-this.Width/4,this.Height-1),
  225. new Point(this.Width/4,this.Height-1),
  226. new Point(this.Width/4,40),
  227. new Point(0,40),
  228. new Point(this.Width/2,0),
  229. };
  230. break;
  231. case ArrowDirection.Bottom:
  232. ps = new Point[]
  233. {
  234. new Point(this.Width-this.Width/4,0),
  235. new Point(this.Width-this.Width/4,this.Height-40),
  236. new Point(this.Width,this.Height-40),
  237. new Point(this.Width/2,this.Height-1),
  238. new Point(0,this.Height-40),
  239. new Point(this.Width/4,this.Height-40),
  240. new Point(this.Width/4,0),
  241. new Point(this.Width-this.Width/4,0),
  242. };
  243. break;
  244. case ArrowDirection.Left_Right:
  245. ps = new Point[]
  246. {
  247. new Point(0,this.Height/2),
  248. new Point(40,0),
  249. new Point(40,this.Height/4),
  250. new Point(this.Width-40,this.Height/4),
  251. new Point(this.Width-40,0),
  252. new Point(this.Width-1,this.Height/2),
  253. new Point(this.Width-40,this.Height),
  254. new Point(this.Width-40,this.Height-this.Height/4),
  255. new Point(40,this.Height-this.Height/4),
  256. new Point(40,this.Height),
  257. new Point(0,this.Height/2),
  258. };
  259. break;
  260. case ArrowDirection.Top_Bottom:
  261. ps = new Point[]
  262. {
  263. new Point(this.Width/2,0),
  264. new Point(this.Width,40),
  265. new Point(this.Width-this.Width/4,40),
  266. new Point(this.Width-this.Width/4,this.Height-40),
  267. new Point(this.Width,this.Height-40),
  268. new Point(this.Width/2,this.Height-1),
  269. new Point(0,this.Height-40),
  270. new Point(this.Width/4,this.Height-40),
  271. new Point(this.Width/4,40),
  272. new Point(0,40),
  273. new Point(this.Width/2,0),
  274. };
  275. break;
  276. }
  277. m_path = new GraphicsPath();
  278. m_path.AddLines(ps);
  279. m_path.CloseAllFigures();
  280. }
  281. /// <summary>
  282. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  283. /// </summary>
  284. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  285. protected override void OnPaint(PaintEventArgs e)
  286. {
  287. base.OnPaint(e);
  288. var g = e.Graphics;
  289. g.SetGDIHigh();
  290. base.Region = new Region(m_path);
  291. g.FillPath(new SolidBrush(arrowColor), m_path);
  292. if (borderColor != null && borderColor != Color.Empty)
  293. g.DrawPath(new Pen(new SolidBrush(borderColor.Value)), m_path);
  294. if (!string.IsNullOrEmpty(text))
  295. {
  296. var size = g.MeasureString(Text, Font);
  297. g.DrawString(Text, Font, new SolidBrush(ForeColor), new PointF((this.Width - size.Width) / 2, (this.Height - size.Height) / 2));
  298. }
  299. }
  300. }
  301. /// <summary>
  302. /// Enum ArrowDirection
  303. /// </summary>
  304. public enum ArrowDirection
  305. {
  306. /// <summary>
  307. /// The left
  308. /// </summary>
  309. Left,
  310. /// <summary>
  311. /// The right
  312. /// </summary>
  313. Right,
  314. /// <summary>
  315. /// The top
  316. /// </summary>
  317. Top,
  318. /// <summary>
  319. /// The bottom
  320. /// </summary>
  321. Bottom,
  322. /// <summary>
  323. /// The left right
  324. /// </summary>
  325. Left_Right,
  326. /// <summary>
  327. /// The top bottom
  328. /// </summary>
  329. Top_Bottom
  330. }
  331. }