UCPanelTitle.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-17-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCPanelTitle.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 UCPanelTitle.
  29. /// Implements the <see cref="HZH_Controls.Controls.UCControlBase" />
  30. /// </summary>
  31. /// <seealso cref="HZH_Controls.Controls.UCControlBase" />
  32. public partial class UCPanelTitle : UCControlBase, IContainerControl
  33. {
  34. /// <summary>
  35. /// The m int maximum height
  36. /// </summary>
  37. private int m_intMaxHeight = 0;
  38. /// <summary>
  39. /// The is can expand
  40. /// </summary>
  41. private bool isCanExpand = true;
  42. /// <summary>
  43. /// Gets or sets a value indicating whether this instance is can expand.
  44. /// </summary>
  45. /// <value><c>true</c> if this instance is can expand; otherwise, <c>false</c>.</value>
  46. [Description("是否可折叠"), Category("自定义")]
  47. public bool IsCanExpand
  48. {
  49. get { return isCanExpand; }
  50. set
  51. {
  52. isCanExpand = value;
  53. if (value)
  54. {
  55. lblTitle.Image = GetImg();
  56. }
  57. else
  58. {
  59. lblTitle.Image = null;
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// The is expand
  65. /// </summary>
  66. private bool isExpand = false;
  67. /// <summary>
  68. /// Gets or sets a value indicating whether this instance is expand.
  69. /// </summary>
  70. /// <value><c>true</c> if this instance is expand; otherwise, <c>false</c>.</value>
  71. [Description("是否已折叠"), Category("自定义")]
  72. public bool IsExpand
  73. {
  74. get { return isExpand; }
  75. set
  76. {
  77. isExpand = value;
  78. if (value)
  79. {
  80. m_intMaxHeight = this.Height;
  81. this.Height = lblTitle.Height;
  82. }
  83. else
  84. {
  85. this.Height = m_intMaxHeight;
  86. }
  87. if (isCanExpand)
  88. {
  89. lblTitle.Image = GetImg();
  90. }
  91. else
  92. {
  93. lblTitle.Image = null;
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// Gets or sets the color of the border.
  99. /// </summary>
  100. /// <value>The color of the border.</value>
  101. [Description("边框颜色"), Category("自定义")]
  102. public Color BorderColor
  103. {
  104. get { return this.RectColor; }
  105. set
  106. {
  107. this.RectColor = value;
  108. this.lblTitle.BackColor = value;
  109. }
  110. }
  111. /// <summary>
  112. /// Gets or sets the title.
  113. /// </summary>
  114. /// <value>The title.</value>
  115. [Description("面板标题"), Category("自定义")]
  116. public string Title
  117. {
  118. get { return lblTitle.Text; }
  119. set { lblTitle.Text = value; }
  120. }
  121. /// <summary>
  122. /// 获取或设置控件的前景色。
  123. /// </summary>
  124. /// <value>The color of the fore.</value>
  125. /// <PermissionSet>
  126. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  127. /// </PermissionSet>
  128. public override Color ForeColor
  129. {
  130. get
  131. {
  132. return this.lblTitle.ForeColor;
  133. }
  134. set
  135. {
  136. this.lblTitle.ForeColor = value;
  137. GetImg(true);
  138. if (isCanExpand)
  139. {
  140. lblTitle.Image = GetImg();
  141. }
  142. else
  143. {
  144. lblTitle.Image = null;
  145. }
  146. }
  147. }
  148. /// <summary>
  149. /// Initializes a new instance of the <see cref="UCPanelTitle" /> class.
  150. /// </summary>
  151. public UCPanelTitle()
  152. {
  153. InitializeComponent();
  154. this.SizeChanged += UCPanelTitle_SizeChanged;
  155. if (isCanExpand)
  156. {
  157. lblTitle.Image = GetImg();
  158. }
  159. else
  160. {
  161. lblTitle.Image = null;
  162. }
  163. }
  164. /// <summary>
  165. /// The bit down
  166. /// </summary>
  167. Bitmap bitDown = null;
  168. /// <summary>
  169. /// The bit up
  170. /// </summary>
  171. Bitmap bitUp = null;
  172. /// <summary>
  173. /// Gets the img.
  174. /// </summary>
  175. /// <param name="blnRefresh">if set to <c>true</c> [BLN refresh].</param>
  176. /// <returns>Bitmap.</returns>
  177. private Bitmap GetImg(bool blnRefresh = false)
  178. {
  179. if (isExpand)
  180. {
  181. if (bitDown == null || blnRefresh)
  182. {
  183. bitDown = new Bitmap(24, 24);
  184. Graphics g = Graphics.FromImage(bitDown);
  185. g.SetGDIHigh();
  186. GraphicsPath path = new GraphicsPath();
  187. path.AddLine(3, 5, 21, 5);
  188. path.AddLine(21, 5, 12, 19);
  189. path.AddLine(12, 19, 3, 5);
  190. g.FillPath(new SolidBrush(ForeColor), path);
  191. g.Dispose();
  192. }
  193. return bitDown;
  194. }
  195. else
  196. {
  197. if (bitUp == null || blnRefresh)
  198. {
  199. bitUp = new Bitmap(24, 24);
  200. Graphics g = Graphics.FromImage(bitUp);
  201. g.SetGDIHigh();
  202. GraphicsPath path = new GraphicsPath();
  203. path.AddLine(3, 19, 21, 19);
  204. path.AddLine(21, 19, 12, 5);
  205. path.AddLine(12, 5, 3, 19);
  206. g.FillPath(new SolidBrush(ForeColor), path);
  207. g.Dispose();
  208. }
  209. return bitUp;
  210. }
  211. }
  212. /// <summary>
  213. /// Handles the MouseDown event of the lblTitle control.
  214. /// </summary>
  215. /// <param name="sender">The source of the event.</param>
  216. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  217. private void lblTitle_MouseDown(object sender, MouseEventArgs e)
  218. {
  219. if (isCanExpand)
  220. {
  221. IsExpand = !IsExpand;
  222. }
  223. }
  224. /// <summary>
  225. /// Handles the SizeChanged event of the UCPanelTitle control.
  226. /// </summary>
  227. /// <param name="sender">The source of the event.</param>
  228. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  229. private void UCPanelTitle_SizeChanged(object sender, EventArgs e)
  230. {
  231. if (this.Height != lblTitle.Height)
  232. {
  233. m_intMaxHeight = this.Height;
  234. }
  235. lblTitle.Width = this.Width;
  236. }
  237. }
  238. }