• (十七)c#Winform自定义控件-基类窗体-HZHControls


    官网

    http://www.hzhcontrols.com

    前提

    入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

    GitHub:https://github.com/kwwwvagaa/NetWinformControl

    码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

    如果觉得写的还行,请点个 star 支持一下吧

    欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

    目录

    https://www.cnblogs.com/bfyx/p/11364884.html

    准备工作

    前面介绍了那么多控件(虽然重要的文本框还没有出现),终于轮到窗体上场了

    首先我们需要一个基类窗体,所有的窗体都将继承基类窗体

    基类窗体需要实现哪些功能呢?

    1. 圆角
    2. 边框
    3. 热键
    4. 蒙版

    开始

    添加一个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         /// 边框颜色
     49         /// </summary>
     50         private Color _borderStyleColor;
     51         /// <summary>
     52         /// 边框宽度
     53         /// </summary>
     54         private int _borderStyleSize;
     55         /// <summary>
     56         /// 边框样式
     57         /// </summary>
     58         private ButtonBorderStyle _borderStyleType;
     59         /// <summary>
     60         /// 是否显示模态
     61         /// </summary>
     62         private bool _isShowMaskDialog = false;
     63         /// <summary>
     64         /// 蒙版窗体
     65         /// </summary>
     66         //private FrmTransparent _frmTransparent = null;
     67         /// <summary>
     68         /// 是否显示蒙版窗体
     69         /// </summary>
     70         [Description("是否显示蒙版窗体")]
     71         public bool IsShowMaskDialog
     72         {
     73             get
     74             {
     75                 return this._isShowMaskDialog;
     76             }
     77             set
     78             {
     79                 this._isShowMaskDialog = value;
     80             }
     81         }
     82         /// <summary>
     83         /// 边框宽度
     84         /// </summary>
     85         [Description("边框宽度")]
     86         public int BorderStyleSize
     87         {
     88             get
     89             {
     90                 return this._borderStyleSize;
     91             }
     92             set
     93             {
     94                 this._borderStyleSize = value;
     95             }
     96         }
     97         /// <summary>
     98         /// 边框颜色
     99         /// </summary>
    100         [Description("边框颜色")]
    101         public Color BorderStyleColor
    102         {
    103             get
    104             {
    105                 return this._borderStyleColor;
    106             }
    107             set
    108             {
    109                 this._borderStyleColor = value;
    110             }
    111         }
    112         /// <summary>
    113         /// 边框样式
    114         /// </summary>
    115         [Description("边框样式")]
    116         public ButtonBorderStyle BorderStyleType
    117         {
    118             get
    119             {
    120                 return this._borderStyleType;
    121             }
    122             set
    123             {
    124                 this._borderStyleType = value;
    125             }
    126         }
    127         /// <summary>
    128         /// 边框圆角
    129         /// </summary>
    130         [Description("边框圆角")]
    131         public int RegionRadius
    132         {
    133             get
    134             {
    135                 return this._regionRadius;
    136             }
    137             set
    138             {
    139                 this._regionRadius = value;
    140             }
    141         }
    142         /// <summary>
    143         /// 是否显示自定义绘制内容
    144         /// </summary>
    145         [Description("是否显示自定义绘制内容")]
    146         public bool IsShowRegion
    147         {
    148             get
    149             {
    150                 return this._isShowRegion;
    151             }
    152             set
    153             {
    154                 this._isShowRegion = value;
    155             }
    156         }
    157         /// <summary>
    158         /// 是否显示重绘边框
    159         /// </summary>
    160         [Description("是否显示重绘边框")]
    161         public bool Redraw
    162         {
    163             get
    164             {
    165                 return this._redraw;
    166             }
    167             set
    168             {
    169                 this._redraw = value;
    170             }
    171         }
    172 
    173         private bool _isFullSize = true;
    174         /// <summary>
    175         /// 是否全屏
    176         /// </summary>
    177         [Description("是否全屏")]
    178         public bool IsFullSize
    179         {
    180             get { return _isFullSize; }
    181             set { _isFullSize = value; }
    182         }
    183         /// <summary>
    184         /// 失去焦点自动关闭
    185         /// </summary>
    186         [Description("失去焦点自动关闭")]
    187         public bool IsLoseFocusClose
    188         {
    189             get
    190             {
    191                 return this._isLoseFocusClose;
    192             }
    193             set
    194             {
    195                 this._isLoseFocusClose = value;
    196             }
    197         }
    198         #endregion
    199 
    200         private bool IsDesingMode
    201         {
    202             get
    203             {
    204                 bool ReturnFlag = false;
    205                 if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
    206                     ReturnFlag = true;
    207                 else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
    208                     ReturnFlag = true;
    209                 return ReturnFlag;
    210             }
    211         }
    212 
    213         #region 初始化
    214         public FrmBase()
    215         {
    216             InitializeComponent();
    217             base.SetStyle(ControlStyles.UserPaint, true);
    218             base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    219             base.SetStyle(ControlStyles.DoubleBuffer, true);
    220             //base.HandleCreated += new EventHandler(this.FrmBase_HandleCreated);
    221             //base.HandleDestroyed += new EventHandler(this.FrmBase_HandleDestroyed);        
    222             this.KeyDown += FrmBase_KeyDown;
    223             this.FormClosing += FrmBase_FormClosing;
    224         }
    225 
    226         void FrmBase_FormClosing(object sender, FormClosingEventArgs e)
    227         {
    228             if (_isLoseFocusClose)
    229             {
    230                 MouseHook.OnMouseActivity -= hook_OnMouseActivity;
    231             }
    232         }
    233 
    234 
    235         private void FrmBase_Load(object sender, EventArgs e)
    236         {
    237             if (!IsDesingMode)
    238             {
    239                 if (_isFullSize)
    240                     SetFullSize();
    241             }
    242             if (_isLoseFocusClose)
    243             {
    244                 MouseHook.OnMouseActivity += hook_OnMouseActivity;
    245             }
    246         }
    247 
    248         #endregion
    249 
    250         #region 方法区
    251 
    252 
    253         void hook_OnMouseActivity(object sender, MouseEventArgs e)
    254         {
    255             try
    256             {
    257                 if (this._isLoseFocusClose && e.Clicks > 0)
    258                 {
    259                     if (e.Button == System.Windows.Forms.MouseButtons.Left || e.Button == System.Windows.Forms.MouseButtons.Right)
    260                     {
    261                         if (!this.IsDisposed)
    262                         {
    263                             if (!this.ClientRectangle.Contains(this.PointToClient(e.Location)))
    264                             {
    265                                 base.Close();
    266                             }
    267                         }
    268                     }
    269                 }
    270             }
    271             catch { }
    272         }
    273 
    274 
    275         /// <summary>
    276         /// 全屏
    277         /// </summary>
    278         public void SetFullSize()
    279         {
    280             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    281 
    282             this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
    283         }
    284         protected virtual void DoEsc()
    285         {
    286             base.Close();
    287         }
    288 
    289         protected virtual void DoEnter()
    290         {
    291         }
    292     
    293         /// <summary>
    294         /// 设置重绘区域
    295         /// </summary>
    296         public void SetWindowRegion()
    297         {
    298             GraphicsPath path = new GraphicsPath();
    299             Rectangle rect = new Rectangle(-1, -1, base.Width + 1, base.Height);
    300             path = this.GetRoundedRectPath(rect, this._regionRadius);
    301             base.Region = new Region(path);
    302         }
    303         /// <summary>
    304         /// 获取重绘区域
    305         /// </summary>
    306         /// <param name="rect"></param>
    307         /// <param name="radius"></param>
    308         /// <returns></returns>
    309         private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
    310         {
    311             Rectangle rect2 = new Rectangle(rect.Location, new Size(radius, radius));
    312             GraphicsPath graphicsPath = new GraphicsPath();
    313             graphicsPath.AddArc(rect2, 180f, 90f);
    314             rect2.X = rect.Right - radius;
    315             graphicsPath.AddArc(rect2, 270f, 90f);
    316             rect2.Y = rect.Bottom - radius;
    317             rect2.Width += 1;
    318             rect2.Height += 1;
    319             graphicsPath.AddArc(rect2, 360f, 90f);
    320             rect2.X = rect.Left;
    321             graphicsPath.AddArc(rect2, 90f, 90f);
    322             graphicsPath.CloseFigure();
    323             return graphicsPath;
    324         }
    325 
    326         public new DialogResult ShowDialog(IWin32Window owner)
    327         {
    328             try
    329             {
    330                 if (this._isShowMaskDialog && owner != null)
    331                 {
    332                     var frmOwner = (Control)owner;
    333                     FrmTransparent _frmTransparent = new FrmTransparent();
    334                     _frmTransparent.Width = frmOwner.Width;
    335                     _frmTransparent.Height = frmOwner.Height;
    336                     Point location = frmOwner.PointToScreen(new Point(0, 0));
    337                     _frmTransparent.Location = location;
    338                     _frmTransparent.frmchild = this;
    339                     _frmTransparent.IsShowMaskDialog = false;
    340                     return _frmTransparent.ShowDialog(owner);
    341                 }
    342                 else
    343                 {
    344                     return base.ShowDialog(owner);
    345                 }
    346             }
    347             catch (NullReferenceException)
    348             {
    349                 return System.Windows.Forms.DialogResult.None;
    350             }
    351         }
    352 
    353         public new DialogResult ShowDialog()
    354         {
    355             return base.ShowDialog();
    356         }
    357         #endregion
    358 
    359         #region 事件区
    360 
    361       
    362         /// <summary>
    363         /// 关闭时发生
    364         /// </summary>
    365         /// <param name="e"></param>
    366         protected override void OnClosed(EventArgs e)
    367         {
    368             base.OnClosed(e);
    369             if (base.Owner != null && base.Owner is FrmTransparent)
    370             {
    371                 (base.Owner as FrmTransparent).Close();
    372             }
    373         }
    374       
    375         /// <summary>
    376         /// 快捷键
    377         /// </summary>
    378         /// <param name="msg"></param>
    379         /// <param name="keyData"></param>
    380         /// <returns></returns>
    381         protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    382         {
    383             int num = 256;
    384             int num2 = 260;
    385             bool result;
    386             if (msg.Msg == num | msg.Msg == num2)
    387             {
    388                 if (keyData == (Keys)262259)
    389                 {
    390                     result = true;
    391                     return result;
    392                 }
    393                 if (keyData != Keys.Enter)
    394                 {
    395                     if (keyData == Keys.Escape)
    396                     {
    397                         this.DoEsc();
    398                     }
    399                 }
    400                 else
    401                 {
    402                     this.DoEnter();
    403                 }
    404             }
    405             result = false;
    406             if (result)
    407                 return result;
    408             else
    409                 return base.ProcessCmdKey(ref msg, keyData);
    410         }
    411 
    412         protected void FrmBase_KeyDown(object sender, KeyEventArgs e)
    413         {
    414             if (HotKeyDown != null && HotKeys != null)
    415             {
    416                 bool blnCtrl = false;
    417                 bool blnAlt = false;
    418                 bool blnShift = false;
    419                 if (e.Control)
    420                     blnCtrl = true;
    421                 if (e.Alt)
    422                     blnAlt = true;
    423                 if (e.Shift)
    424                     blnShift = true;
    425                 if (HotKeys.ContainsKey(e.KeyValue))
    426                 {
    427                     string strKey = string.Empty;
    428                     if (blnCtrl)
    429                     {
    430                         strKey += "Ctrl+";
    431                     }
    432                     if (blnAlt)
    433                     {
    434                         strKey += "Alt+";
    435                     }
    436                     if (blnShift)
    437                     {
    438                         strKey += "Shift+";
    439                     }
    440                     strKey += HotKeys[e.KeyValue];
    441 
    442                     if (HotKeyDown(strKey))
    443                     {
    444                         e.Handled = true;
    445                         e.SuppressKeyPress = true;
    446                     }
    447                 }
    448             }
    449         }
    450 
    451         /// <summary>
    452         /// 重绘事件
    453         /// </summary>
    454         /// <param name="e"></param>
    455         protected override void OnPaint(PaintEventArgs e)
    456         {
    457             if (this._isShowRegion)
    458             {
    459                 this.SetWindowRegion();
    460             }
    461             base.OnPaint(e);
    462             if (this._redraw)
    463             {
    464                 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);
    465             }
    466         }
    467         #endregion
    468 
    469     }
    470 }
    View Code
     1 namespace HZH_Controls.Forms
     2 {
     3     partial class FrmBase
     4     {
     5         /// <summary>
     6         /// Required designer variable.
     7         /// </summary>
     8         private System.ComponentModel.IContainer components = null;
     9 
    10         /// <summary>
    11         /// Clean up any resources being used.
    12         /// </summary>
    13         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    14         protected override void Dispose(bool disposing)
    15         {
    16             if (disposing && (components != null))
    17             {
    18                 components.Dispose();
    19             }
    20             base.Dispose(disposing);
    21         }
    22 
    23         #region Windows Form Designer generated code
    24 
    25         /// <summary>
    26         /// Required method for Designer support - do not modify
    27         /// the contents of this method with the code editor.
    28         /// </summary>
    29         private void InitializeComponent()
    30         {
    31             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmBase));
    32             this.SuspendLayout();
    33             // 
    34             // FrmBase
    35             // 
    36             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
    37             this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
    38             this.ClientSize = new System.Drawing.Size(331, 371);
    39             this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
    40             this.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(66)))), ((int)(((byte)(66)))), ((int)(((byte)(66)))));
    41             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    42             this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    43             this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
    44             this.Name = "FrmBase";
    45             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    46             this.Text = "FrmBase";
    47             this.Load += new System.EventHandler(this.FrmBase_Load);
    48             this.ResumeLayout(false);
    49 
    50         }
    51 
    52         #endregion
    53     }
    54 }
    View Code

    设计效果就是这样的

    用处及效果

    一般来说,这个基类窗体不直接使用,不过你高兴用的话 也是可以的 ,比如设计个圆角窗体什么的 

    最后的话

    如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧

  • 相关阅读:
    通过注册表获取计算机相关信息
    GetTickCount()函数
    SystemInfo获取计算机相关信息
    CriticalSection 临界区
    mysql慢查询问题
    mysql show processlist详解
    nginx及php版本号隐藏
    安装psutil模块报错&安装python-devel
    ubuntu ssh重启
    php-fpm配置优化
  • 原文地址:https://www.cnblogs.com/bfyx/p/11363673.html
Copyright © 2020-2023  润新知