UCDropDownBtn.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-27-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCDropDownBtn.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;
  25. using System.Drawing.Drawing2D;
  26. namespace HZH_Controls.Controls
  27. {
  28. /// <summary>
  29. /// Class UCDropDownBtn.
  30. /// Implements the <see cref="HZH_Controls.Controls.UCBtnImg" />
  31. /// </summary>
  32. /// <seealso cref="HZH_Controls.Controls.UCBtnImg" />
  33. [DefaultEvent("BtnClick")]
  34. public partial class UCDropDownBtn : UCBtnImg
  35. {
  36. /// <summary>
  37. /// The FRM anchor
  38. /// </summary>
  39. Forms.FrmAnchor _frmAnchor;
  40. /// <summary>
  41. /// The drop panel height
  42. /// </summary>
  43. private int _dropPanelHeight = -1;
  44. /// <summary>
  45. /// 按钮点击事件
  46. /// </summary>
  47. public new event EventHandler BtnClick;
  48. /// <summary>
  49. /// 下拉框高度
  50. /// </summary>
  51. /// <value>The height of the drop panel.</value>
  52. [Description("下拉框高度"), Category("自定义")]
  53. public int DropPanelHeight
  54. {
  55. get { return _dropPanelHeight; }
  56. set { _dropPanelHeight = value; }
  57. }
  58. /// <summary>
  59. /// The BTNS
  60. /// </summary>
  61. private string[] btns;
  62. /// <summary>
  63. /// 需要显示的按钮文字
  64. /// </summary>
  65. /// <value>The BTNS.</value>
  66. [Description("需要显示的按钮文字"), Category("自定义")]
  67. public string[] Btns
  68. {
  69. get { return btns; }
  70. set { btns = value; }
  71. }
  72. /// <summary>
  73. /// 图片
  74. /// </summary>
  75. /// <value>The image.</value>
  76. [Obsolete("不再可用的属性")]
  77. [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  78. public override Image Image
  79. {
  80. get;
  81. set;
  82. }
  83. /// <summary>
  84. /// 图片位置
  85. /// </summary>
  86. /// <value>The image align.</value>
  87. [Obsolete("不再可用的属性")]
  88. [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  89. public override ContentAlignment ImageAlign
  90. {
  91. get;
  92. set;
  93. }
  94. /// <summary>
  95. /// 按钮字体颜色
  96. /// </summary>
  97. /// <value>The color of the BTN fore.</value>
  98. [Description("按钮字体颜色"), Category("自定义")]
  99. public override Color BtnForeColor
  100. {
  101. get
  102. {
  103. return base.BtnForeColor;
  104. }
  105. set
  106. {
  107. base.BtnForeColor = value;
  108. Bitmap bit = new Bitmap(12, 10);
  109. Graphics g = Graphics.FromImage(bit);
  110. g.SetGDIHigh();
  111. GraphicsPath path = new GraphicsPath();
  112. path.AddLines(new Point[]
  113. {
  114. new Point(1,1),
  115. new Point(11,1),
  116. new Point(6,10),
  117. new Point(1,1)
  118. });
  119. g.FillPath(new SolidBrush(value), path);
  120. g.Dispose();
  121. this.lbl.Image = bit;
  122. }
  123. }
  124. /// <summary>
  125. /// Initializes a new instance of the <see cref="UCDropDownBtn" /> class.
  126. /// </summary>
  127. public UCDropDownBtn()
  128. {
  129. InitializeComponent();
  130. IsShowTips = false;
  131. this.lbl.ImageAlign = ContentAlignment.MiddleRight;
  132. base.BtnClick += UCDropDownBtn_BtnClick;
  133. }
  134. /// <summary>
  135. /// Handles the BtnClick event of the UCDropDownBtn control.
  136. /// </summary>
  137. /// <param name="sender">The source of the event.</param>
  138. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  139. void UCDropDownBtn_BtnClick(object sender, EventArgs e)
  140. {
  141. if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
  142. {
  143. if (Btns != null && Btns.Length > 0)
  144. {
  145. int intRow = 0;
  146. int intCom = 1;
  147. var p = this.PointToScreen(this.Location);
  148. while (true)
  149. {
  150. int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
  151. if ((p.Y + this.Height + Btns.Length / intCom * 50 < intScreenHeight || p.Y - Btns.Length / intCom * 50 > 0)
  152. && (_dropPanelHeight <= 0 ? true : (Btns.Length / intCom * 50 <= _dropPanelHeight)))
  153. {
  154. intRow = Btns.Length / intCom + (Btns.Length % intCom != 0 ? 1 : 0);
  155. break;
  156. }
  157. intCom++;
  158. }
  159. UCTimePanel ucTime = new UCTimePanel();
  160. ucTime.IsShowBorder = true;
  161. int intWidth = this.Width / intCom;
  162. Size size = new Size(intCom * intWidth, intRow * 50);
  163. ucTime.Size = size;
  164. ucTime.FirstEvent = true;
  165. ucTime.SelectSourceEvent += ucTime_SelectSourceEvent;
  166. ucTime.Row = intRow;
  167. ucTime.Column = intCom;
  168. List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>();
  169. foreach (var item in Btns)
  170. {
  171. lst.Add(new KeyValuePair<string, string>(item, item));
  172. }
  173. ucTime.Source = lst;
  174. _frmAnchor = new Forms.FrmAnchor(this, ucTime);
  175. _frmAnchor.Load += (a, b) => { (a as Form).Size = size; };
  176. _frmAnchor.Show(this.FindForm());
  177. }
  178. }
  179. else
  180. {
  181. _frmAnchor.Close();
  182. }
  183. }
  184. /// <summary>
  185. /// Handles the SelectSourceEvent event of the ucTime control.
  186. /// </summary>
  187. /// <param name="sender">The source of the event.</param>
  188. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  189. void ucTime_SelectSourceEvent(object sender, EventArgs e)
  190. {
  191. if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible)
  192. {
  193. _frmAnchor.Close();
  194. if (BtnClick != null)
  195. {
  196. BtnClick(sender.ToString(), e);
  197. }
  198. }
  199. }
  200. }
  201. }