前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果覺得寫的還行,請點個 star 支持一下吧 歡迎前來交流探討: 企鵝群568015492 目錄 ...
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
如果覺得寫的還行,請點個 star 支持一下吧
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
準備工作
前面介紹了那麼多控制項(雖然重要的文本框還沒有出現),終於輪到窗體上場了
首先我們需要一個基類窗體,所有的窗體都將繼承基類窗體
基類窗體需要實現哪些功能呢?
- 圓角
- 邊框
- 熱鍵
- 蒙版
開始
添加一個Form,命名FrmBase
寫上一些屬性
1 [Description("定義的熱鍵列表"), Category("自定義")] 2 public Dictionary<int, string> HotKeys { get; set; } 3 public delegate bool HotKeyEventHandler(string strHotKey); 4 /// <summary> 5 /// 熱鍵事件 6 /// </summary> 7 [Description("熱鍵事件"), Category("自定義")] 8 public event HotKeyEventHandler HotKeyDown; 9 #region 欄位屬性 10 11 /// <summary> 12 /// 失去焦點關閉 13 /// </summary> 14 bool _isLoseFocusClose = false; 15 /// <summary> 16 /// 是否重繪邊框樣式 17 /// </summary> 18 private bool _redraw = false; 19 /// <summary> 20 /// 是否顯示圓角 21 /// </summary> 22 private bool _isShowRegion = false; 23 /// <summary> 24 /// 邊圓角大小 25 /// </summary> 26 private int _regionRadius = 10; 27 /// <summary> 28 /// 邊框顏色 29 /// </summary> 30 private Color _borderStyleColor; 31 /// <summary> 32 /// 邊框寬度 33 /// </summary> 34 private int _borderStyleSize; 35 /// <summary> 36 /// 邊框樣式 37 /// </summary> 38 private ButtonBorderStyle _borderStyleType; 39 /// <summary> 40 /// 是否顯示模態 41 /// </summary> 42 private bool _isShowMaskDialog = false; 43 /// <summary> 44 /// 蒙版窗體 45 /// </summary> 46 //private FrmTransparent _frmTransparent = null; 47 /// <summary> 48 /// 是否顯示蒙版窗體 49 /// </summary> 50 [Description("是否顯示蒙版窗體")] 51 public bool IsShowMaskDialog 52 { 53 get 54 { 55 return this._isShowMaskDialog; 56 } 57 set 58 { 59 this._isShowMaskDialog = value; 60 } 61 } 62 /// <summary> 63 /// 邊框寬度 64 /// </summary> 65 [Description("邊框寬度")] 66 public int BorderStyleSize 67 { 68 get 69 { 70 return this._borderStyleSize; 71 } 72 set 73 { 74 this._borderStyleSize = value; 75 } 76 } 77 /// <summary> 78 /// 邊框顏色 79 /// </summary> 80 [Description("邊框顏色")] 81 public Color BorderStyleColor 82 { 83 get 84 { 85 return this._borderStyleColor; 86 } 87 set 88 { 89 this._borderStyleColor = value; 90 } 91 } 92 /// <summary> 93 /// 邊框樣式 94 /// </summary> 95 [Description("邊框樣式")] 96 public ButtonBorderStyle BorderStyleType 97 { 98 get 99 { 100 return this._borderStyleType; 101 } 102 set 103 { 104 this._borderStyleType = value; 105 } 106 } 107 /// <summary> 108 /// 邊框圓角 109 /// </summary> 110 [Description("邊框圓角")] 111 public int RegionRadius 112 { 113 get 114 { 115 return this._regionRadius; 116 } 117 set 118 { 119 this._regionRadius = value; 120 } 121 } 122 /// <summary> 123 /// 是否顯示自定義繪製內容 124 /// </summary> 125 [Description("是否顯示自定義繪製內容")] 126 public bool IsShowRegion 127 { 128 get 129 { 130 return this._isShowRegion; 131 } 132 set 133 { 134 this._isShowRegion = value; 135 } 136 } 137 /// <summary> 138 /// 是否顯示重繪邊框 139 /// </summary> 140 [Description("是否顯示重繪邊框")] 141 public bool Redraw 142 { 143 get 144 { 145 return this._redraw; 146 } 147 set 148 { 149 this._redraw = value; 150 } 151 } 152 153 private bool _isFullSize = true; 154 /// <summary> 155 /// 是否全屏 156 /// </summary> 157 [Description("是否全屏")] 158 public bool IsFullSize 159 { 160 get { return _isFullSize; } 161 set { _isFullSize = value; } 162 } 163 /// <summary> 164 /// 失去焦點自動關閉 165 /// </summary> 166 [Description("失去焦點自動關閉")] 167 public bool IsLoseFocusClose 168 { 169 get 170 { 171 return this._isLoseFocusClose; 172 } 173 set 174 { 175 this._isLoseFocusClose = value; 176 } 177 } 178 #endregion 179 180 private bool IsDesingMode 181 { 182 get 183 { 184 bool ReturnFlag = false; 185 if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) 186 ReturnFlag = true; 187 else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv") 188 ReturnFlag = true; 189 return ReturnFlag; 190 } 191 }
快捷鍵處理
1 /// <summary> 2 /// 快捷鍵 3 /// </summary> 4 /// <param name="msg"></param> 5 /// <param name="keyData"></param> 6 /// <returns></returns> 7 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 8 { 9 int num = 256; 10 int num2 = 260; 11 bool result; 12 if (msg.Msg == num | msg.Msg == num2) 13 { 14 if (keyData == (Keys)262259) 15 { 16 result = true; 17 return result; 18 } 19 if (keyData != Keys.Enter) 20 { 21 if (keyData == Keys.Escape) 22 { 23 this.DoEsc(); 24 } 25 } 26 else 27 { 28 this.DoEnter(); 29 } 30 } 31 result = false; 32 if (result) 33 return result; 34 else 35 return base.ProcessCmdKey(ref msg, keyData); 36 }
1 protected void FrmBase_KeyDown(object sender, KeyEventArgs e) 2 { 3 if (HotKeyDown != null && HotKeys != null) 4 { 5 bool blnCtrl = false; 6 bool blnAlt = false; 7 bool blnShift = false; 8 if (e.Control) 9 blnCtrl = true; 10 if (e.Alt) 11 blnAlt = true; 12 if (e.Shift) 13 blnShift = true; 14 if (HotKeys.ContainsKey(e.KeyValue)) 15 { 16 string strKey = string.Empty; 17 if (blnCtrl) 18 { 19 strKey += "Ctrl+"; 20 } 21 if (blnAlt) 22 { 23 strKey += "Alt+"; 24 } 25 if (blnShift) 26 { 27 strKey += "Shift+"; 28 } 29 strKey += HotKeys[e.KeyValue]; 30 31 if (HotKeyDown(strKey)) 32 { 33 e.Handled = true; 34 e.SuppressKeyPress = true; 35 } 36 } 37 } 38 }
重繪
1 /// <summary> 2 /// 重繪事件 3 /// </summary> 4 /// <param name="e"></param> 5 protected override void OnPaint(PaintEventArgs e) 6 { 7 if (this._isShowRegion) 8 { 9 this.SetWindowRegion(); 10 } 11 base.OnPaint(e); 12 if (this._redraw) 13 { 14 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); 15 } 16 } 17 /// <summary> 18 /// 設置重繪區域 19 /// </summary> 20 public void SetWindowRegion() 21 { 22 GraphicsPath path = new GraphicsPath(); 23 Rectangle rect = new Rectangle(-1, -1, base.Width + 1, base.Height); 24 path = this.GetRoundedRectPath(rect, this._regionRadius); 25 base.Region = new Region(path); 26 } 27 /// <summary> 28 /// 獲取重繪區域 29 /// </summary> 30 /// <param name="rect"></param> 31 /// <param name="radius"></param> 32 /// <returns></returns> 33 private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius) 34 { 35 Rectangle rect2 = new Rectangle(rect.Location, new Size(radius, radius)); 36 GraphicsPath graphicsPath = new GraphicsPath(); 37 graphicsPath.AddArc(rect2, 180f, 90f); 38 rect2.X = rect.Right - radius; 39 graphicsPath.AddArc(rect2, 270f, 90f); 40 rect2.Y = rect.Bottom - radius; 41 rect2.Width += 1; 42 rect2.Height += 1; 43 graphicsPath.AddArc(rect2, 360f, 90f); 44 rect2.X = rect.Left; 45 graphicsPath.AddArc(rect2, 90f, 90f); 46 graphicsPath.CloseFigure(); 47 return graphicsPath; 48 }
還有為了點擊窗體外區域關閉的鉤子功能
1 void FrmBase_FormClosing(object sender, FormClosingEventArgs e) 2 { 3 if (_isLoseFocusClose) 4 { 5 MouseHook.OnMouseActivity -= hook_OnMouseActivity; 6 } 7 } 8 9 10 private void FrmBase_Load(object sender, EventArgs e) 11 { 12 if (!IsDesingMode) 13 { 14 if (_isFullSize) 15 SetFullSize(); 16 } 17 if (_isLoseFocusClose) 18 { 19 MouseHook.OnMouseActivity += hook_OnMouseActivity; 20 } 21 } 22 23 #endregion 24 25 #region 方法區 26 27 28 void hook_OnMouseActivity(object sender, MouseEventArgs e) 29 { 30 try 31 { 32 if (this._isLoseFocusClose && e.Clicks > 0) 33 { 34 if (e.Button == System.Windows.Forms.MouseButtons.Left || e.Button == System.Windows.Forms.MouseButtons.Right) 35 { 36 if (!this.IsDisposed) 37 { 38 if (!this.ClientRectangle.Contains(this.PointToClient(e.Location))) 39 { 40 base.Close(); 41 } 42 } 43 } 44 } 45 } 46 catch { } 47 }
為了實現蒙版,覆蓋ShowDialog函數
1 public new DialogResult ShowDialog(IWin32Window owner) 2 { 3 try 4 { 5 if (this._isShowMaskDialog && owner != null) 6 { 7 var frmOwner = (Control)owner; 8 FrmTransparent _frmTransparent = new FrmTransparent(); 9 _frmTransparent.Width = frmOwner.Width; 10 _frmTransparent.Height = frmOwner.Height; 11 Point location = frmOwner.PointToScreen(new Point(0, 0)); 12 _frmTransparent.Location = location; 13 _frmTransparent.frmchild = this; 14 _frmTransparent.IsShowMaskDialog = false; 15 return _frmTransparent.ShowDialog(owner); 16 } 17 else 18 { 19 return base.ShowDialog(owner); 20 } 21 } 22 catch (NullReferenceException) 23 { 24 return System.Windows.Forms.DialogResult.None; 25 } 26 } 27 28 public new DialogResult ShowDialog() 29 { 30 return base.ShowDialog(); 31 }
最後看下完整代碼
1 // 版權所有 黃正輝 交流群:568015492 QQ:623128629 2 // 文件名稱:FrmBase.cs 3 // 創建日期:2019-08-15 16:04:31 4 // 功能描述:FrmBase 5 // 項目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 6 using System; 7 using System.Collections.Generic; 8 using System.ComponentModel; 9 using System.Data; 10 using System.Drawing; 11 using System.Drawing.Drawing2D; 12 using System.Linq; 13 using System.Text; 14 using System.Windows.Forms; 15 16 namespace HZH_Controls.Forms 17 { 18 [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))] 19 public partial class FrmBase : Form 20 { 21 [Description("定義的熱鍵列表"), Category("自定義")] 22 public Dictionary<int, string> HotKeys { get; set; } 23 public delegate bool HotKeyEventHandler(string strHotKey); 24 /// <summary> 25 /// 熱鍵事件 26 /// </summary> 27 [Description("熱鍵事件"), Category("自定義")] 28 public event HotKeyEventHandler HotKeyDown; 29 #region 欄位屬性 30 31 /// <summary> 32 /// 失去焦點關閉 33 /// </summary> 34 bool _isLoseFocusClose = false; 35 /// <summary> 36 /// 是否重繪邊框樣式 37 /// </summary> 38 private bool _redraw = false; 39 /// <summary> 40 /// 是否顯示圓角 41 /// </summary> 42 private bool _isShowRegion = false; 43 /// <summary> 44 /// 邊圓角大小 45 /// </summary> 46 private int _regionRadius = 10; 47 /// <summary> 48