UCSwitch.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-19-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCSwitch.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. namespace HZH_Controls.Controls
  26. {
  27. /// <summary>
  28. /// Class UCSwitch.
  29. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  30. /// </summary>
  31. /// <seealso cref="System.Windows.Forms.UserControl" />
  32. [DefaultEvent("CheckedChanged")]
  33. public partial class UCSwitch : UserControl
  34. {
  35. /// <summary>
  36. /// Occurs when [checked changed].
  37. /// </summary>
  38. [Description("选中改变事件"), Category("自定义")]
  39. public event EventHandler CheckedChanged;
  40. /// <summary>
  41. /// The m true color
  42. /// </summary>
  43. private Color m_trueColor = Color.FromArgb(255, 77, 59);
  44. /// <summary>
  45. /// Gets or sets the color of the true.
  46. /// </summary>
  47. /// <value>The color of the true.</value>
  48. [Description("选中时颜色"), Category("自定义")]
  49. public Color TrueColor
  50. {
  51. get { return m_trueColor; }
  52. set
  53. {
  54. m_trueColor = value;
  55. Refresh();
  56. }
  57. }
  58. private Color m_trueTextColr = Color.White;
  59. [Description("选中时文本颜色"), Category("自定义")]
  60. public Color TrueTextColr
  61. {
  62. get { return m_trueTextColr; }
  63. set
  64. {
  65. m_trueTextColr = value;
  66. Refresh();
  67. }
  68. }
  69. /// <summary>
  70. /// The m false color
  71. /// </summary>
  72. private Color m_falseColor = Color.FromArgb(189, 189, 189);
  73. /// <summary>
  74. /// Gets or sets the color of the false.
  75. /// </summary>
  76. /// <value>The color of the false.</value>
  77. [Description("没有选中时颜色"), Category("自定义")]
  78. public Color FalseColor
  79. {
  80. get { return m_falseColor; }
  81. set
  82. {
  83. m_falseColor = value;
  84. Refresh();
  85. }
  86. }
  87. private Color m_falseTextColr = Color.White;
  88. [Description("没有选中时文本颜色"), Category("自定义")]
  89. public Color FalseTextColr
  90. {
  91. get { return m_falseTextColr; }
  92. set
  93. {
  94. m_falseTextColr = value;
  95. Refresh();
  96. }
  97. }
  98. /// <summary>
  99. /// The m checked
  100. /// </summary>
  101. private bool m_checked;
  102. /// <summary>
  103. /// Gets or sets a value indicating whether this <see cref="UCSwitch" /> is checked.
  104. /// </summary>
  105. /// <value><c>true</c> if checked; otherwise, <c>false</c>.</value>
  106. [Description("是否选中"), Category("自定义")]
  107. public bool Checked
  108. {
  109. get { return m_checked; }
  110. set
  111. {
  112. m_checked = value;
  113. Refresh();
  114. if (CheckedChanged != null)
  115. {
  116. CheckedChanged(this, null);
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// The m texts
  122. /// </summary>
  123. private string[] m_texts;
  124. /// <summary>
  125. /// Gets or sets the texts.
  126. /// </summary>
  127. /// <value>The texts.</value>
  128. [Description("文本值,当选中或没有选中时显示,必须是长度为2的数组"), Category("自定义")]
  129. public string[] Texts
  130. {
  131. get { return m_texts; }
  132. set
  133. {
  134. m_texts = value;
  135. Refresh();
  136. }
  137. }
  138. /// <summary>
  139. /// The m switch type
  140. /// </summary>
  141. private SwitchType m_switchType = SwitchType.Ellipse;
  142. /// <summary>
  143. /// Gets or sets the type of the switch.
  144. /// </summary>
  145. /// <value>The type of the switch.</value>
  146. [Description("显示类型"), Category("自定义")]
  147. public SwitchType SwitchType
  148. {
  149. get { return m_switchType; }
  150. set
  151. {
  152. m_switchType = value;
  153. Refresh();
  154. }
  155. }
  156. /// <summary>
  157. /// 获取或设置控件显示的文字的字体。
  158. /// </summary>
  159. /// <value>The font.</value>
  160. /// <PermissionSet>
  161. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  162. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  163. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  164. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  165. /// </PermissionSet>
  166. public override Font Font
  167. {
  168. get
  169. {
  170. return base.Font;
  171. }
  172. set
  173. {
  174. base.Font = value;
  175. Refresh();
  176. }
  177. }
  178. /// <summary>
  179. /// Initializes a new instance of the <see cref="UCSwitch" /> class.
  180. /// </summary>
  181. public UCSwitch()
  182. {
  183. InitializeComponent();
  184. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  185. this.SetStyle(ControlStyles.DoubleBuffer, true);
  186. this.SetStyle(ControlStyles.ResizeRedraw, true);
  187. this.SetStyle(ControlStyles.Selectable, true);
  188. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  189. this.SetStyle(ControlStyles.UserPaint, true);
  190. this.MouseDown += UCSwitch_MouseDown;
  191. }
  192. /// <summary>
  193. /// Handles the MouseDown event of the UCSwitch control.
  194. /// </summary>
  195. /// <param name="sender">The source of the event.</param>
  196. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  197. void UCSwitch_MouseDown(object sender, MouseEventArgs e)
  198. {
  199. Checked = !Checked;
  200. }
  201. /// <summary>
  202. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  203. /// </summary>
  204. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  205. protected override void OnPaint(PaintEventArgs e)
  206. {
  207. base.OnPaint(e);
  208. var g = e.Graphics;
  209. g.SetGDIHigh();
  210. if (m_switchType == HZH_Controls.Controls.SwitchType.Ellipse)
  211. {
  212. var fillColor = m_checked ? m_trueColor : m_falseColor;
  213. GraphicsPath path = new GraphicsPath();
  214. path.AddLine(new Point(this.Height / 2, 1), new Point(this.Width - this.Height / 2, 1));
  215. path.AddArc(new Rectangle(this.Width - this.Height - 1, 1, this.Height - 2, this.Height - 2), -90, 180);
  216. path.AddLine(new Point(this.Width - this.Height / 2, this.Height - 1), new Point(this.Height / 2, this.Height - 1));
  217. path.AddArc(new Rectangle(1, 1, this.Height - 2, this.Height - 2), 90, 180);
  218. g.FillPath(new SolidBrush(fillColor), path);
  219. string strText = string.Empty;
  220. if (m_texts != null && m_texts.Length == 2)
  221. {
  222. if (m_checked)
  223. {
  224. strText = m_texts[0];
  225. }
  226. else
  227. {
  228. strText = m_texts[1];
  229. }
  230. }
  231. if (m_checked)
  232. {
  233. g.FillEllipse(Brushes.White, new Rectangle(this.Width - this.Height - 1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
  234. if (string.IsNullOrEmpty(strText))
  235. {
  236. g.DrawEllipse(new Pen(Color.White, 2), new Rectangle((this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2));
  237. }
  238. else
  239. {
  240. System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
  241. int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
  242. g.DrawString(strText, Font, new SolidBrush(m_trueTextColr), new Point((this.Height - 2 - 4) / 2, intTextY));
  243. }
  244. }
  245. else
  246. {
  247. g.FillEllipse(Brushes.White, new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
  248. if (string.IsNullOrEmpty(strText))
  249. {
  250. g.DrawEllipse(new Pen(Color.White, 2), new Rectangle(this.Width - 2 - (this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2));
  251. }
  252. else
  253. {
  254. System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
  255. int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
  256. g.DrawString(strText, Font, new SolidBrush(m_falseTextColr), new Point(this.Width - 2 - (this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2 - (int)sizeF.Width / 2, intTextY));
  257. }
  258. }
  259. }
  260. else if (m_switchType == HZH_Controls.Controls.SwitchType.Quadrilateral)
  261. {
  262. var fillColor = m_checked ? m_trueColor : m_falseColor;
  263. GraphicsPath path = new GraphicsPath();
  264. int intRadius = 5;
  265. path.AddArc(0, 0, intRadius, intRadius, 180f, 90f);
  266. path.AddArc(this.Width - intRadius - 1, 0, intRadius, intRadius, 270f, 90f);
  267. path.AddArc(this.Width - intRadius - 1, this.Height - intRadius - 1, intRadius, intRadius, 0f, 90f);
  268. path.AddArc(0, this.Height - intRadius - 1, intRadius, intRadius, 90f, 90f);
  269. g.FillPath(new SolidBrush(fillColor), path);
  270. string strText = string.Empty;
  271. if (m_texts != null && m_texts.Length == 2)
  272. {
  273. if (m_checked)
  274. {
  275. strText = m_texts[0];
  276. }
  277. else
  278. {
  279. strText = m_texts[1];
  280. }
  281. }
  282. if (m_checked)
  283. {
  284. GraphicsPath path2 = new GraphicsPath();
  285. path2.AddArc(this.Width - this.Height - 1 + 2, 1 + 2, intRadius, intRadius, 180f, 90f);
  286. path2.AddArc(this.Width - 1 - 2 - intRadius, 1 + 2, intRadius, intRadius, 270f, 90f);
  287. path2.AddArc(this.Width - 1 - 2 - intRadius, this.Height - 2 - intRadius - 1, intRadius, intRadius, 0f, 90f);
  288. path2.AddArc(this.Width - this.Height - 1 + 2, this.Height - 2 - intRadius - 1, intRadius, intRadius, 90f, 90f);
  289. g.FillPath(Brushes.White, path2);
  290. if (string.IsNullOrEmpty(strText))
  291. {
  292. g.DrawEllipse(new Pen(Color.White, 2), new Rectangle((this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2));
  293. }
  294. else
  295. {
  296. System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
  297. int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
  298. g.DrawString(strText, Font, new SolidBrush(m_trueTextColr), new Point((this.Height - 2 - 4) / 2, intTextY));
  299. }
  300. }
  301. else
  302. {
  303. GraphicsPath path2 = new GraphicsPath();
  304. path2.AddArc(1 + 2, 1 + 2, intRadius, intRadius, 180f, 90f);
  305. path2.AddArc(this.Height - 2 - intRadius, 1 + 2, intRadius, intRadius, 270f, 90f);
  306. path2.AddArc(this.Height - 2 - intRadius, this.Height - 2 - intRadius - 1, intRadius, intRadius, 0f, 90f);
  307. path2.AddArc(1 + 2, this.Height - 2 - intRadius - 1, intRadius, intRadius, 90f, 90f);
  308. g.FillPath(Brushes.White, path2);
  309. //g.FillEllipse(Brushes.White, new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
  310. if (string.IsNullOrEmpty(strText))
  311. {
  312. g.DrawEllipse(new Pen(Color.White, 2), new Rectangle(this.Width - 2 - (this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2));
  313. }
  314. else
  315. {
  316. System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
  317. int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
  318. g.DrawString(strText, Font, new SolidBrush(m_falseTextColr), new Point(this.Width - 2 - (this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2 - (int)sizeF.Width / 2, intTextY));
  319. }
  320. }
  321. }
  322. else
  323. {
  324. var fillColor = m_checked ? m_trueColor : m_falseColor;
  325. int intLineHeight = (this.Height - 2 - 4) / 2;
  326. GraphicsPath path = new GraphicsPath();
  327. path.AddLine(new Point(this.Height / 2, (this.Height - intLineHeight) / 2), new Point(this.Width - this.Height / 2, (this.Height - intLineHeight) / 2));
  328. path.AddArc(new Rectangle(this.Width - this.Height / 2 - intLineHeight - 1, (this.Height - intLineHeight) / 2, intLineHeight, intLineHeight), -90, 180);
  329. path.AddLine(new Point(this.Width - this.Height / 2, (this.Height - intLineHeight) / 2 + intLineHeight), new Point(this.Width - this.Height / 2, (this.Height - intLineHeight) / 2 + intLineHeight));
  330. path.AddArc(new Rectangle(this.Height / 2, (this.Height - intLineHeight) / 2, intLineHeight, intLineHeight), 90, 180);
  331. g.FillPath(new SolidBrush(fillColor), path);
  332. if (m_checked)
  333. {
  334. g.FillEllipse(new SolidBrush(fillColor), new Rectangle(this.Width - this.Height - 1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
  335. g.FillEllipse(Brushes.White, new Rectangle(this.Width - 2 - (this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2 - 4, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2));
  336. }
  337. else
  338. {
  339. g.FillEllipse(new SolidBrush(fillColor), new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
  340. g.FillEllipse(Brushes.White, new Rectangle((this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2 + 4, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2));
  341. }
  342. }
  343. }
  344. }
  345. /// <summary>
  346. /// Enum SwitchType
  347. /// </summary>
  348. public enum SwitchType
  349. {
  350. /// <summary>
  351. /// 椭圆
  352. /// </summary>
  353. Ellipse,
  354. /// <summary>
  355. /// 四边形
  356. /// </summary>
  357. Quadrilateral,
  358. /// <summary>
  359. /// 横线
  360. /// </summary>
  361. Line
  362. }
  363. }