http://www.hzhcontrols.com
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
https://www.cnblogs.com/bfyx/p/11364884.html
这是一个提示消息的窗体,他继承自基类窗体FrmBase,如果你对FrmBase还不了解,请移步 (十七)c#Winform自定义控件-基类窗体 查看
提示消息窗体支持有确定取消按钮及单取消按钮,更多操作按钮暂没有增加
添加一个Form命名为FrmDialog ,继承FrmBase
私有的构造函数
1 private FrmDialog( 2 string strMessage, 3 string strTitle, 4 bool blnShowCancel = false, 5 bool blnShowClose = false, 6 bool blnisEnterClose = true) 7 { 8 InitializeComponent(); 9 if (!string.IsNullOrWhiteSpace(strTitle)) 10 lblTitle.Text = strTitle; 11 lblMsg.Text = strMessage; 12 if (blnShowCancel) 13 { 14 this.tableLayoutPanel1.ColumnStyles[1].Width = 1; 15 this.tableLayoutPanel1.ColumnStyles[2].Width = 50; 16 } 17 else 18 { 19 this.tableLayoutPanel1.ColumnStyles[1].Width = 0; 20 this.tableLayoutPanel1.ColumnStyles[2].Width = 0; 21 } 22 btnClose.Visible = blnShowClose; 23 blnEnterClose = blnisEnterClose; 24 }
搭配一个静态的公共函数
1 #region 显示一个模式信息框 2 ///3 /// 功能描述:显示一个模式信息框 4 /// 作 者:HZH 5 /// 创建日期:2019-03-04 15:49:48 6 /// 任务编号:POS 7 /// 8 /// owner 9 /// strMessage 10 /// strTitle 11 /// blnShowCancel 12 /// isShowMaskDialog 13 /// blnShowClose 14 /// isEnterClose 15 /// 返回值 16 public static DialogResult ShowDialog( 17 IWin32Window owner, 18 string strMessage, 19 string strTitle = "提示", 20 bool blnShowCancel = false, 21 bool isShowMaskDialog = true, 22 bool blnShowClose = false, 23 bool blnIsEnterClose = true) 24 { 25 DialogResult result = DialogResult.Cancel; 26 if (owner == null || (owner is Control && (owner as Control).IsDisposed)) 27 { 28 result = new FrmDialog(strMessage, strTitle, blnShowCancel, blnShowClose, blnIsEnterClose) 29 { 30 StartPosition = FormStartPosition.CenterScreen, 31 IsShowMaskDialog = isShowMaskDialog, 32 TopMost = true 33 }.ShowDialog(); 34 } 35 else 36 { 37 if (owner is Control) 38 { 39 owner = (owner as Control).FindForm(); 40 } 41 result = new FrmDialog(strMessage, strTitle, blnShowCancel, blnShowClose, blnIsEnterClose) 42 { 43 StartPosition = (owner != null) ? FormStartPosition.CenterParent : FormStartPosition.CenterScreen, 44 IsShowMaskDialog = isShowMaskDialog, 45 TopMost = true 46 }.ShowDialog(owner); 47 } 48 return result; 49 } 50 #endregion
一些小事件
1 private void btnOK_BtnClick(object sender, EventArgs e) 2 { 3 this.DialogResult = System.Windows.Forms.DialogResult.OK; 4 } 5 6 private void btnCancel_BtnClick(object sender, EventArgs e) 7 { 8 this.DialogResult = System.Windows.Forms.DialogResult.Cancel; 9 } 10 11 private void btnClose_MouseDown(object sender, MouseEventArgs e) 12 { 13 this.DialogResult = System.Windows.Forms.DialogResult.Cancel; 14 } 15 16 protected override void DoEnter() 17 { 18 if (blnEnterClose) 19 this.DialogResult = System.Windows.Forms.DialogResult.OK; 20 }
代码就这么多,看下完整代码
1 // 版权所有 黄正辉 交流群:568015492 QQ:623128629 2 // 文件名称:FrmDialog.cs 3 // 创建日期:2019-08-15 16:04:36 4 // 功能描述:FrmDialog 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.Linq; 12 using System.Text; 13 using System.Windows.Forms; 14 15 namespace HZH_Controls.Forms 16 { 17 public partial class FrmDialog : FrmBase 18 { 19 bool blnEnterClose = true; 20 private FrmDialog( 21 string strMessage, 22 string strTitle, 23 bool blnShowCancel = false, 24 bool blnShowClose = false, 25 bool blnisEnterClose = true) 26 { 27 InitializeComponent(); 28 if (!string.IsNullOrWhiteSpace(strTitle)) 29 lblTitle.Text = strTitle; 30 lblMsg.Text = strMessage; 31 if (blnShowCancel) 32 { 33 this.tableLayoutPanel1.ColumnStyles[1].Width = 1; 34 this.tableLayoutPanel1.ColumnStyles[2].Width = 50; 35 } 36 else 37 { 38 this.tableLayoutPanel1.ColumnStyles[1].Width = 0; 39 this.tableLayoutPanel1.ColumnStyles[2].Width = 0; 40 } 41 //btnCancel.Visible = blnShowCancel; 42 //ucSplitLine_V1.Visible = blnShowCancel; 43 btnClose.Visible = blnShowClose; 44 blnEnterClose = blnisEnterClose; 45 //if (blnShowCancel) 46 //{ 47 // btnOK.BtnForeColor = Color.FromArgb(255, 85, 51); 48 //} 49 } 50 51 #region 显示一个模式信息框 52 ///53 /// 功能描述:显示一个模式信息框 54 /// 作 者:HZH 55 /// 创建日期:2019-03-04 15:49:48 56 /// 任务编号:POS 57 /// 58 /// owner 59 /// strMessage 60 /// strTitle 61 /// blnShowCancel 62 /// isShowMaskDialog 63 /// blnShowClose 64 /// isEnterClose 65 /// 返回值 66 public static DialogResult ShowDialog( 67 IWin32Window owner, 68 string strMessage, 69 string strTitle = "提示", 70 bool blnShowCancel = false, 71 bool isShowMaskDialog = true, 72 bool blnShowClose = false, 73 bool blnIsEnterClose = true) 74 { 75 DialogResult result = DialogResult.Cancel; 76 if (owner == null || (owner is Control && (owner as Control).IsDisposed)) 77 { 78 result = new FrmDialog(strMessage, strTitle, blnShowCancel, blnShowClose, blnIsEnterClose) 79 { 80 StartPosition = FormStartPosition.CenterScreen, 81 IsShowMaskDialog = isShowMaskDialog, 82 TopMost = true 83 }.ShowDialog(); 84 } 85 else 86 { 87 if (owner is Control) 88 { 89 owner = (owner as Control).FindForm(); 90 } 91 result = new FrmDialog(strMessage, strTitle, blnShowCancel, blnShowClose, blnIsEnterClose) 92 { 93 StartPosition = (owner != null) ? FormStartPosition.CenterParent : FormStartPosition.CenterScreen, 94 IsShowMaskDialog = isShowMaskDialog, 95 TopMost = true 96 }.ShowDialog(owner); 97 } 98 return result; 99 } 100 #endregion 101 102 private void btnOK_BtnClick(object sender, EventArgs e) 103 { 104 this.DialogResult = System.Windows.Forms.DialogResult.OK; 105 } 106 107 private void btnCancel_BtnClick(object sender, EventArgs e) 108 { 109 this.DialogResult = System.Windows.Forms.DialogResult.Cancel; 110 } 111 112 private void btnClose_MouseDown(object sender, MouseEventArgs e) 113 { 114 this.DialogResult = System.Windows.Forms.DialogResult.Cancel; 115 } 116 117 protected override void DoEnter() 118 { 119 if (blnEnterClose) 120 this.DialogResult = System.Windows.Forms.DialogResult.OK; 121 } 122 123 private void FrmDialog_VisibleChanged(object sender, EventArgs e) 124 { 125 126 } 127 } 128 }