UCCheckBox.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCCheckBox.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 UCCheckBox.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. [DefaultEvent("CheckedChangeEvent")]
  32. public partial class UCCheckBox : UserControl
  33. {
  34. /// <summary>
  35. /// 选中改变事件
  36. /// </summary>
  37. [Description("选中改变事件"), Category("自定义")]
  38. public event EventHandler CheckedChangeEvent;
  39. /// <summary>
  40. /// 字体
  41. /// </summary>
  42. /// <value>The font.</value>
  43. /// <PermissionSet>
  44. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  45. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  46. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  47. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  48. /// </PermissionSet>
  49. [Description("字体"), Category("自定义")]
  50. public override Font Font
  51. {
  52. get
  53. {
  54. return base.Font;
  55. }
  56. set
  57. {
  58. base.Font = value;
  59. label1.Font = value;
  60. }
  61. }
  62. /// <summary>
  63. /// The fore color
  64. /// </summary>
  65. private Color _ForeColor = Color.FromArgb(62, 62, 62);
  66. /// <summary>
  67. /// 字体颜色
  68. /// </summary>
  69. /// <value>The color of the fore.</value>
  70. /// <PermissionSet>
  71. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  72. /// </PermissionSet>
  73. [Description("字体颜色"), Category("自定义")]
  74. public new Color ForeColor
  75. {
  76. get { return _ForeColor; }
  77. set
  78. {
  79. base.ForeColor = value;
  80. label1.ForeColor = value;
  81. _ForeColor = value;
  82. }
  83. }
  84. /// <summary>
  85. /// The text
  86. /// </summary>
  87. private string _Text = "复选框";
  88. /// <summary>
  89. /// 文本
  90. /// </summary>
  91. /// <value>The text value.</value>
  92. [Description("文本"), Category("自定义")]
  93. public string TextValue
  94. {
  95. get { return _Text; }
  96. set
  97. {
  98. label1.Text = value;
  99. _Text = value;
  100. }
  101. }
  102. /// <summary>
  103. /// The checked
  104. /// </summary>
  105. private bool _checked = false;
  106. /// <summary>
  107. /// 是否选中
  108. /// </summary>
  109. /// <value><c>true</c> if checked; otherwise, <c>false</c>.</value>
  110. [Description("是否选中"), Category("自定义")]
  111. public bool Checked
  112. {
  113. get
  114. {
  115. return _checked;
  116. }
  117. set
  118. {
  119. if (_checked != value)
  120. {
  121. _checked = value;
  122. if (base.Enabled)
  123. {
  124. if (_checked)
  125. {
  126. panel1.BackgroundImage = Properties.Resources.checkbox1;
  127. }
  128. else
  129. {
  130. panel1.BackgroundImage = Properties.Resources.checkbox0;
  131. }
  132. }
  133. else
  134. {
  135. if (_checked)
  136. {
  137. panel1.BackgroundImage = Properties.Resources.checkbox10;
  138. }
  139. else
  140. {
  141. panel1.BackgroundImage = Properties.Resources.checkbox00;
  142. }
  143. }
  144. if (CheckedChangeEvent != null)
  145. {
  146. CheckedChangeEvent(this, null);
  147. }
  148. }
  149. }
  150. }
  151. /// <summary>
  152. /// 获取或设置一个值,该值指示控件是否可以对用户交互作出响应。
  153. /// </summary>
  154. /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
  155. /// <PermissionSet>
  156. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  157. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  158. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  159. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  160. /// </PermissionSet>
  161. public new bool Enabled
  162. {
  163. get
  164. {
  165. return base.Enabled;
  166. }
  167. set
  168. {
  169. base.Enabled = value;
  170. if (value)
  171. {
  172. if (_checked)
  173. {
  174. panel1.BackgroundImage = Properties.Resources.checkbox1;
  175. }
  176. else
  177. {
  178. panel1.BackgroundImage = Properties.Resources.checkbox0;
  179. }
  180. }
  181. else
  182. {
  183. if (_checked)
  184. {
  185. panel1.BackgroundImage = Properties.Resources.checkbox10;
  186. }
  187. else
  188. {
  189. panel1.BackgroundImage = Properties.Resources.checkbox00;
  190. }
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// Initializes a new instance of the <see cref="UCCheckBox" /> class.
  196. /// </summary>
  197. public UCCheckBox()
  198. {
  199. InitializeComponent();
  200. }
  201. /// <summary>
  202. /// Handles the MouseDown event of the CheckBox control.
  203. /// </summary>
  204. /// <param name="sender">The source of the event.</param>
  205. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  206. private void CheckBox_MouseDown(object sender, MouseEventArgs e)
  207. {
  208. Checked = !Checked;
  209. }
  210. }
  211. }