FrmBase.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="FrmBase.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.Data;
  20. using System.Drawing;
  21. using System.Drawing.Drawing2D;
  22. using System.Linq;
  23. using System.Runtime.InteropServices;
  24. using System.Text;
  25. using System.Windows.Forms;
  26. namespace HZH_Controls.Forms
  27. {
  28. /// <summary>
  29. /// Class FrmBase.
  30. /// Implements the <see cref="System.Windows.Forms.Form" />
  31. /// </summary>
  32. /// <seealso cref="System.Windows.Forms.Form" />
  33. [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))]
  34. public partial class FrmBase : Form
  35. {
  36. /// <summary>
  37. /// Gets or sets the hot keys.
  38. /// </summary>
  39. /// <value>The hot keys.</value>
  40. [Description("定义的热键列表"), Category("自定义")]
  41. public Dictionary<int, string> HotKeys { get; set; }
  42. /// <summary>
  43. /// Delegate HotKeyEventHandler
  44. /// </summary>
  45. /// <param name="strHotKey">The string hot key.</param>
  46. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  47. public delegate bool HotKeyEventHandler(string strHotKey);
  48. /// <summary>
  49. /// 热键事件
  50. /// </summary>
  51. [Description("热键事件"), Category("自定义")]
  52. public event HotKeyEventHandler HotKeyDown;
  53. #region 字段属性
  54. /// <summary>
  55. /// 失去焦点关闭
  56. /// </summary>
  57. bool _isLoseFocusClose = false;
  58. /// <summary>
  59. /// 是否重绘边框样式
  60. /// </summary>
  61. private bool _redraw = false;
  62. /// <summary>
  63. /// 是否显示圆角
  64. /// </summary>
  65. private bool _isShowRegion = false;
  66. /// <summary>
  67. /// 边圆角大小
  68. /// </summary>
  69. private int _regionRadius = 10;
  70. /// <summary>
  71. /// 边框颜色
  72. /// </summary>
  73. private Color _borderStyleColor;
  74. /// <summary>
  75. /// 边框宽度
  76. /// </summary>
  77. private int _borderStyleSize;
  78. /// <summary>
  79. /// 边框样式
  80. /// </summary>
  81. private ButtonBorderStyle _borderStyleType;
  82. /// <summary>
  83. /// 是否显示模态
  84. /// </summary>
  85. private bool _isShowMaskDialog = false;
  86. /// <summary>
  87. /// 蒙版窗体
  88. /// </summary>
  89. /// <value><c>true</c> if this instance is show mask dialog; otherwise, <c>false</c>.</value>
  90. [Description("是否显示蒙版窗体")]
  91. public bool IsShowMaskDialog
  92. {
  93. get
  94. {
  95. return this._isShowMaskDialog;
  96. }
  97. set
  98. {
  99. this._isShowMaskDialog = value;
  100. }
  101. }
  102. /// <summary>
  103. /// 边框宽度
  104. /// </summary>
  105. /// <value>The size of the border style.</value>
  106. [Description("边框宽度")]
  107. public int BorderStyleSize
  108. {
  109. get
  110. {
  111. return this._borderStyleSize;
  112. }
  113. set
  114. {
  115. this._borderStyleSize = value;
  116. }
  117. }
  118. /// <summary>
  119. /// 边框颜色
  120. /// </summary>
  121. /// <value>The color of the border style.</value>
  122. [Description("边框颜色")]
  123. public Color BorderStyleColor
  124. {
  125. get
  126. {
  127. return this._borderStyleColor;
  128. }
  129. set
  130. {
  131. this._borderStyleColor = value;
  132. }
  133. }
  134. /// <summary>
  135. /// 边框样式
  136. /// </summary>
  137. /// <value>The type of the border style.</value>
  138. [Description("边框样式")]
  139. public ButtonBorderStyle BorderStyleType
  140. {
  141. get
  142. {
  143. return this._borderStyleType;
  144. }
  145. set
  146. {
  147. this._borderStyleType = value;
  148. }
  149. }
  150. /// <summary>
  151. /// 边框圆角
  152. /// </summary>
  153. /// <value>The region radius.</value>
  154. [Description("边框圆角")]
  155. public int RegionRadius
  156. {
  157. get
  158. {
  159. return this._regionRadius;
  160. }
  161. set
  162. {
  163. this._regionRadius = Math.Max(value, 1);
  164. }
  165. }
  166. /// <summary>
  167. /// 是否显示自定义绘制内容
  168. /// </summary>
  169. /// <value><c>true</c> if this instance is show region; otherwise, <c>false</c>.</value>
  170. [Description("是否显示自定义绘制内容")]
  171. public bool IsShowRegion
  172. {
  173. get
  174. {
  175. return this._isShowRegion;
  176. }
  177. set
  178. {
  179. this._isShowRegion = value;
  180. }
  181. }
  182. /// <summary>
  183. /// 是否显示重绘边框
  184. /// </summary>
  185. /// <value><c>true</c> if redraw; otherwise, <c>false</c>.</value>
  186. [Description("是否显示重绘边框")]
  187. public bool Redraw
  188. {
  189. get
  190. {
  191. return this._redraw;
  192. }
  193. set
  194. {
  195. this._redraw = value;
  196. }
  197. }
  198. /// <summary>
  199. /// The is full size
  200. /// </summary>
  201. private bool _isFullSize = true;
  202. /// <summary>
  203. /// 是否全屏
  204. /// </summary>
  205. /// <value><c>true</c> if this instance is full size; otherwise, <c>false</c>.</value>
  206. [Description("是否全屏")]
  207. public bool IsFullSize
  208. {
  209. get { return _isFullSize; }
  210. set { _isFullSize = value; }
  211. }
  212. /// <summary>
  213. /// 失去焦点自动关闭
  214. /// </summary>
  215. /// <value><c>true</c> if this instance is lose focus close; otherwise, <c>false</c>.</value>
  216. [Description("失去焦点自动关闭")]
  217. public bool IsLoseFocusClose
  218. {
  219. get
  220. {
  221. return this._isLoseFocusClose;
  222. }
  223. set
  224. {
  225. this._isLoseFocusClose = value;
  226. }
  227. }
  228. #endregion
  229. /// <summary>
  230. /// Gets a value indicating whether this instance is desing mode.
  231. /// </summary>
  232. /// <value><c>true</c> if this instance is desing mode; otherwise, <c>false</c>.</value>
  233. private bool IsDesingMode
  234. {
  235. get
  236. {
  237. bool ReturnFlag = false;
  238. if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
  239. ReturnFlag = true;
  240. else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
  241. ReturnFlag = true;
  242. return ReturnFlag;
  243. }
  244. }
  245. #region 初始化
  246. /// <summary>
  247. /// Initializes a new instance of the <see cref="FrmBase" /> class.
  248. /// </summary>
  249. public FrmBase()
  250. {
  251. InitializeComponent();
  252. base.SetStyle(ControlStyles.UserPaint, true);
  253. base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  254. base.SetStyle(ControlStyles.DoubleBuffer, true);
  255. //base.HandleCreated += new EventHandler(this.FrmBase_HandleCreated);
  256. //base.HandleDestroyed += new EventHandler(this.FrmBase_HandleDestroyed);
  257. this.KeyDown += FrmBase_KeyDown;
  258. this.FormClosing += FrmBase_FormClosing;
  259. }
  260. /// <summary>
  261. /// Handles the FormClosing event of the FrmBase control.
  262. /// </summary>
  263. /// <param name="sender">The source of the event.</param>
  264. /// <param name="e">The <see cref="FormClosingEventArgs" /> instance containing the event data.</param>
  265. void FrmBase_FormClosing(object sender, FormClosingEventArgs e)
  266. {
  267. if (_isLoseFocusClose)
  268. {
  269. MouseHook.OnMouseActivity -= hook_OnMouseActivity;
  270. }
  271. }
  272. /// <summary>
  273. /// Handles the Load event of the FrmBase control.
  274. /// </summary>
  275. /// <param name="sender">The source of the event.</param>
  276. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  277. private void FrmBase_Load(object sender, EventArgs e)
  278. {
  279. if (!IsDesingMode)
  280. {
  281. if (_isFullSize)
  282. SetFullSize();
  283. }
  284. if (_isLoseFocusClose)
  285. {
  286. MouseHook.OnMouseActivity += hook_OnMouseActivity;
  287. }
  288. }
  289. #endregion
  290. #region 方法区
  291. /// <summary>
  292. /// Handles the OnMouseActivity event of the hook control.
  293. /// </summary>
  294. /// <param name="sender">The source of the event.</param>
  295. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  296. void hook_OnMouseActivity(object sender, MouseEventArgs e)
  297. {
  298. try
  299. {
  300. if (this._isLoseFocusClose && e.Clicks > 0)
  301. {
  302. if (e.Button == System.Windows.Forms.MouseButtons.Left || e.Button == System.Windows.Forms.MouseButtons.Right)
  303. {
  304. if (!this.IsDisposed)
  305. {
  306. if (!this.ClientRectangle.Contains(this.PointToClient(e.Location)))
  307. {
  308. base.Close();
  309. }
  310. }
  311. }
  312. }
  313. }
  314. catch { }
  315. }
  316. /// <summary>
  317. /// 全屏
  318. /// </summary>
  319. public void SetFullSize()
  320. {
  321. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  322. this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
  323. }
  324. /// <summary>
  325. /// Does the escape.
  326. /// </summary>
  327. protected virtual void DoEsc()
  328. {
  329. base.Close();
  330. }
  331. /// <summary>
  332. /// Does the enter.
  333. /// </summary>
  334. protected virtual void DoEnter()
  335. {
  336. }
  337. /// <summary>
  338. /// 设置重绘区域
  339. /// </summary>
  340. public void SetWindowRegion()
  341. {
  342. GraphicsPath path = new GraphicsPath();
  343. Rectangle rect = new Rectangle(-1, -1, base.Width + 1, base.Height);
  344. path = this.GetRoundedRectPath(rect, this._regionRadius);
  345. base.Region = new Region(path);
  346. }
  347. /// <summary>
  348. /// 获取重绘区域
  349. /// </summary>
  350. /// <param name="rect">The rect.</param>
  351. /// <param name="radius">The radius.</param>
  352. /// <returns>GraphicsPath.</returns>
  353. private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
  354. {
  355. Rectangle rect2 = new Rectangle(rect.Location, new Size(radius, radius));
  356. GraphicsPath graphicsPath = new GraphicsPath();
  357. graphicsPath.AddArc(rect2, 180f, 90f);
  358. rect2.X = rect.Right - radius;
  359. graphicsPath.AddArc(rect2, 270f, 90f);
  360. rect2.Y = rect.Bottom - radius;
  361. rect2.Width += 1;
  362. rect2.Height += 1;
  363. graphicsPath.AddArc(rect2, 360f, 90f);
  364. rect2.X = rect.Left;
  365. graphicsPath.AddArc(rect2, 90f, 90f);
  366. graphicsPath.CloseFigure();
  367. return graphicsPath;
  368. }
  369. /// <summary>
  370. /// 将窗体显示为具有指定所有者的模式对话框。
  371. /// </summary>
  372. /// <param name="owner">任何实现 <see cref="T:System.Windows.Forms.IWin32Window" />(表示将拥有模式对话框的顶级窗口)的对象。</param>
  373. /// <returns><see cref="T:System.Windows.Forms.DialogResult" /> 值之一。</returns>
  374. /// <PermissionSet>
  375. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  376. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  377. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  378. /// <IPermission class="System.Security.Permissions.UIPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  379. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  380. /// </PermissionSet>
  381. public new DialogResult ShowDialog(IWin32Window owner)
  382. {
  383. try
  384. {
  385. if (this._isShowMaskDialog && owner != null)
  386. {
  387. var frmOwner = (Control)owner;
  388. FrmTransparent _frmTransparent = new FrmTransparent();
  389. _frmTransparent.Width = frmOwner.Width;
  390. _frmTransparent.Height = frmOwner.Height;
  391. Point location = frmOwner.PointToScreen(new Point(0, 0));
  392. _frmTransparent.Location = location;
  393. _frmTransparent.frmchild = this;
  394. _frmTransparent.IsShowMaskDialog = false;
  395. return _frmTransparent.ShowDialog(owner);
  396. }
  397. else
  398. {
  399. return base.ShowDialog(owner);
  400. }
  401. }
  402. catch (NullReferenceException)
  403. {
  404. return System.Windows.Forms.DialogResult.None;
  405. }
  406. }
  407. /// <summary>
  408. /// 将窗体显示为模式对话框,并将当前活动窗口设置为它的所有者。
  409. /// </summary>
  410. /// <returns><see cref="T:System.Windows.Forms.DialogResult" /> 值之一。</returns>
  411. /// <PermissionSet>
  412. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  413. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  414. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  415. /// <IPermission class="System.Security.Permissions.UIPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  416. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  417. /// </PermissionSet>
  418. public new DialogResult ShowDialog()
  419. {
  420. return base.ShowDialog();
  421. }
  422. #endregion
  423. #region 事件区
  424. /// <summary>
  425. /// 关闭时发生
  426. /// </summary>
  427. /// <param name="e">一个包含事件数据的 <see cref="T:System.EventArgs" />。</param>
  428. protected override void OnClosed(EventArgs e)
  429. {
  430. base.OnClosed(e);
  431. if (base.Owner != null && base.Owner is FrmTransparent)
  432. {
  433. (base.Owner as FrmTransparent).Close();
  434. }
  435. }
  436. /// <summary>
  437. /// 快捷键
  438. /// </summary>
  439. /// <param name="msg">通过引用传递的 <see cref="T:System.Windows.Forms.Message" />,它表示要处理的 Win32 消息。</param>
  440. /// <param name="keyData"><see cref="T:System.Windows.Forms.Keys" /> 值之一,它表示要处理的键。</param>
  441. /// <returns>如果控件处理并使用击键,则为 true;否则为 false,以允许进一步处理。</returns>
  442. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  443. {
  444. int num = 256;
  445. int num2 = 260;
  446. bool result;
  447. if (msg.Msg == num | msg.Msg == num2)
  448. {
  449. if (keyData == (Keys)262259)
  450. {
  451. result = true;
  452. return result;
  453. }
  454. if (keyData != Keys.Enter)
  455. {
  456. if (keyData == Keys.Escape)
  457. {
  458. this.DoEsc();
  459. }
  460. }
  461. else
  462. {
  463. this.DoEnter();
  464. }
  465. }
  466. result = false;
  467. if (result)
  468. return result;
  469. else
  470. return base.ProcessCmdKey(ref msg, keyData);
  471. }
  472. /// <summary>
  473. /// Handles the KeyDown event of the FrmBase control.
  474. /// </summary>
  475. /// <param name="sender">The source of the event.</param>
  476. /// <param name="e">The <see cref="KeyEventArgs" /> instance containing the event data.</param>
  477. protected void FrmBase_KeyDown(object sender, KeyEventArgs e)
  478. {
  479. if (HotKeyDown != null && HotKeys != null)
  480. {
  481. bool blnCtrl = false;
  482. bool blnAlt = false;
  483. bool blnShift = false;
  484. if (e.Control)
  485. blnCtrl = true;
  486. if (e.Alt)
  487. blnAlt = true;
  488. if (e.Shift)
  489. blnShift = true;
  490. if (HotKeys.ContainsKey(e.KeyValue))
  491. {
  492. string strKey = string.Empty;
  493. if (blnCtrl)
  494. {
  495. strKey += "Ctrl+";
  496. }
  497. if (blnAlt)
  498. {
  499. strKey += "Alt+";
  500. }
  501. if (blnShift)
  502. {
  503. strKey += "Shift+";
  504. }
  505. strKey += HotKeys[e.KeyValue];
  506. if (HotKeyDown(strKey))
  507. {
  508. e.Handled = true;
  509. e.SuppressKeyPress = true;
  510. }
  511. }
  512. }
  513. }
  514. /// <summary>
  515. /// 重绘事件
  516. /// </summary>
  517. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  518. protected override void OnPaint(PaintEventArgs e)
  519. {
  520. if (this._isShowRegion)
  521. {
  522. this.SetWindowRegion();
  523. }
  524. base.OnPaint(e);
  525. if (this._redraw)
  526. {
  527. ControlPaint.DrawBorder(e.Graphics, base.ClientRectangle, this._borderStyleColor, this._borderStyleSize, this._borderStyleType, this._borderStyleColor, this._borderStyleSize, this._borderStyleType, this._borderStyleColor, this._borderStyleSize, this._borderStyleType, this._borderStyleColor, this._borderStyleSize, this._borderStyleType);
  528. }
  529. }
  530. #endregion
  531. #region 窗体拖动 English:Form drag
  532. /// <summary>
  533. /// Releases the capture.
  534. /// </summary>
  535. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  536. [DllImport("user32.dll")]
  537. public static extern bool ReleaseCapture();
  538. /// <summary>
  539. /// Sends the message.
  540. /// </summary>
  541. /// <param name="hwnd">The HWND.</param>
  542. /// <param name="wMsg">The w MSG.</param>
  543. /// <param name="wParam">The w parameter.</param>
  544. /// <param name="lParam">The l parameter.</param>
  545. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  546. [DllImport("user32.dll")]
  547. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  548. /// <summary>
  549. /// The wm syscommand
  550. /// </summary>
  551. public const int WM_SYSCOMMAND = 0x0112;
  552. /// <summary>
  553. /// The sc move
  554. /// </summary>
  555. public const int SC_MOVE = 0xF010;
  556. /// <summary>
  557. /// The htcaption
  558. /// </summary>
  559. public const int HTCAPTION = 0x0002;
  560. /// <summary>
  561. /// 通过Windows的API控制窗体的拖动
  562. /// </summary>
  563. /// <param name="hwnd">The HWND.</param>
  564. public static void MouseDown(IntPtr hwnd)
  565. {
  566. ReleaseCapture();
  567. SendMessage(hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  568. }
  569. #endregion
  570. /// <summary>
  571. /// 在构造函数中调用设置窗体移动
  572. /// </summary>
  573. /// <param name="cs">The cs.</param>
  574. protected void InitFormMove(params Control[] cs)
  575. {
  576. foreach (Control c in cs)
  577. {
  578. if (c != null && !c.IsDisposed)
  579. c.MouseDown += c_MouseDown;
  580. }
  581. }
  582. /// <summary>
  583. /// Handles the MouseDown event of the c control.
  584. /// </summary>
  585. /// <param name="sender">The source of the event.</param>
  586. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  587. void c_MouseDown(object sender, MouseEventArgs e)
  588. {
  589. MouseDown(this.Handle);
  590. }
  591. }
  592. }