入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
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 /// <summary>
3 /// 功能描述:显示一个模式信息框
4 /// 作 者:HZH
5 /// 创建日期:2019-03-04 15:49:48
6 /// 任务编号:POS
7 /// </summary>
8 /// <param name="owner">owner</param>
9 /// <param name="strMessage">strMessage</param>
10 /// <param name="strTitle">strTitle</param>
11 /// <param name="blnShowCancel">blnShowCancel</param>
12 /// <param name="isShowMaskDialog">isShowMaskDialog</param>
13 /// <param name="blnShowClose">blnShowClose</param>
14 /// <param name="isEnterClose">isEnterClose</param>
15 /// <returns>返回值</returns>
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 /// <summary>
53 /// 功能描述:显示一个模式信息框
54 /// 作 者:HZH
55 /// 创建日期:2019-03-04 15:49:48
56 /// 任务编号:POS
57 /// </summary>
58 /// <param name="owner">owner</param>
59 /// <param name="strMessage">strMessage</param>
60 /// <param name="strTitle">strTitle</param>
61 /// <param name="blnShowCancel">blnShowCancel</param>
62 /// <param name="isShowMaskDialog">isShowMaskDialog</param>
63 /// <param name="blnShowClose">blnShowClose</param>
64 /// <param name="isEnterClose">isEnterClose</param>
65 /// <returns>返回值</returns>
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 }
1 namespace HZH_Controls.Forms
2 {
3 partial class FrmDialog
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(FrmDialog));
32 this.btnClose = new System.Windows.Forms.Panel();
33 this.panel1 = new System.Windows.Forms.Panel();
34 this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
35 this.btnCancel = new HZH_Controls.Controls.UCBtnExt();
36 this.ucSplitLine_V1 = new HZH_Controls.Controls.UCSplitLine_V();
37 this.btnOK = new HZH_Controls.Controls.UCBtnExt();
38 this.lblMsg = new System.Windows.Forms.Label();
39 this.lblTitle = new System.Windows.Forms.Label();
40 this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
41 this.ucSplitLine_H2 = new HZH_Controls.Controls.UCSplitLine_H();
42 this.panel1.SuspendLayout();
43 this.tableLayoutPanel1.SuspendLayout();
44 this.SuspendLayout();
45 //
46 // btnClose
47 //
48 this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
49 this.btnClose.BackgroundImage = global::HZH_Controls.Properties.Resources.dialog_close;
50 this.btnClose.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
51 this.btnClose.Location = new System.Drawing.Point(383, 0);
52 this.btnClose.Name = "btnClose";
53 this.btnClose.Size = new System.Drawing.Size(28, 60);
54 this.btnClose.TabIndex = 4;
55 this.btnClose.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnClose_MouseDown);
56 //
57 // panel1
58 //
59 this.panel1.Controls.Add(this.tableLayoutPanel1);
60 this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
61 this.panel1.Location = new System.Drawing.Point(0, 214);
62 this.panel1.Name = "panel1";
63 this.panel1.Size = new System.Drawing.Size(427, 64);
64 this.panel1.TabIndex = 3;
65 //
66 // tableLayoutPanel1
67 //
68 this.tableLayoutPanel1.ColumnCount = 3;
69 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
70 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
71 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
72 this.tableLayoutPanel1.Controls.Add(this.btnCancel, 2, 0);
73 this.tableLayoutPanel1.Controls.Add(this.ucSplitLine_V1, 1, 0);
74 this.tableLayoutPanel1.Controls.Add(this.btnOK, 0, 0);
75 this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
76 this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
77 this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0);
78 this.tableLayoutPanel1.Name = "tableLayoutPanel1";
79 this.tableLayoutPanel1.RowCount = 1;
80 this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
81 this.tableLayoutPanel1.Size = new System.Drawing.Size(427, 64);
82 this.tableLayoutPanel1.TabIndex = 5;
83 //
84 // btnCancel
85 //
86 this.btnCancel.BackColor = System.Drawing.Color.Transparent;
87 this.btnCancel.BtnBackColor = System.Drawing.Color.Transparent;
88 this.btnCancel.BtnFont = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
89 this.btnCancel.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(73)))), ((int)(((byte)(119)))), ((int)(((byte)(232)))));
90 this.btnCancel.BtnText = "取消";
91 this.btnCancel.ConerRadius = 5;
92 this.btnCancel.Cursor = System.Windows.Forms.Cursors.Hand;
93 this.btnCancel.Dock = System.Windows.Forms.DockStyle.Fill;
94 this.btnCancel.FillColor = System.Drawing.Color.White;
95 this.btnCancel.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
96 this.btnCancel.IsRadius = false;
97 this.btnCancel.IsShowRect = false;
98 this.btnCancel.IsShowTips = false;
99 this.btnCancel.Location = new System.Drawing.Point(214, 0);
100 this.btnCancel.Margin = new System.Windows.Forms.Padding(0);
101 this.btnCancel.Name = "btnCancel";
102 this.btnCancel.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
103 this.btnCancel.RectWidth = 1;
104 this.btnCancel.Size = new System.Drawing.Size(213, 64);
105 this.btnCancel.TabIndex = 1;
106 this.btnCancel.TabStop = false;
107 this.btnCancel.TipsText = "";
108 this.btnCancel.BtnClick += new System.EventHandler(this.btnCancel_BtnClick);
109 //
110 // ucSplitLine_V1
111 //
112 this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
113 this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Fill;
114 this.ucSplitLine_V1.Location = new System.Drawing.Point(213, 15);
115 this.ucSplitLine_V1.Margin = new System.Windows.Forms.Padding(0, 15, 0, 15);
116 this.ucSplitLine_V1.Name = "ucSplitLine_V1";
117 this.ucSplitLine_V1.Size = new System.Drawing.Size(1, 34);
118 this.ucSplitLine_V1.TabIndex = 2;
119 this.ucSplitLine_V1.TabStop = false;
120 //
121 // btnOK
122 //
123 this.btnOK.BackColor = System.Drawing.Color.Transparent;
124 this.btnOK.BtnBackColor = System.Drawing.Color.Transparent;
125 this.btnOK.BtnFont = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
126 this.btnOK.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
127 this.btnOK.BtnText = "确定";
128 this.btnOK.ConerRadius = 5;
129 this.btnOK.Cursor = System.Windows.Forms.Cursors.Hand;
130 this.btnOK.Dock = System.Windows.Forms.DockStyle.Fill;
131 this.btnOK.FillColor = System.Drawing.Color.White;
132 this.btnOK.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
133 this.btnOK.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
134 this.btnOK.IsRadius = false;
135 this.btnOK.IsShowRect = false;
136 this.btnOK.IsShowTips = false;
137 this.btnOK.Location = new System.Drawing.Point(0, 0);
138 this.btnOK.Margin = new System.Windows.Forms.Padding(0);
139 this.btnOK.Name = "btnOK";
140 this.btnOK.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
141 this.btnOK.RectWidth = 1;
142 this.btnOK.Size = new System.Drawing.Size(213, 64);
143 this.btnOK.TabIndex = 0;
144 this.btnOK.TabStop = false;
145 this.btnOK.TipsText = "";
146 this.btnOK.BtnClick += new System.EventHandler(this.btnOK_BtnClick);
147 //
148 // lblMsg
149 //
150 this.lblMsg.BackColor = System.Drawing.Color.White;
151 this.lblMsg.Dock = System.Windows.Forms.DockStyle.Fill;
152 this.lblMsg.Font = new System.Drawing.Font("微软雅黑", 12F);
153 this.lblMsg.Location = new System.Drawing.Point(0, 60);
154 this.lblMsg.Name = "lblMsg";
155 this.lblMsg.Padding = new System.Windows.Forms.Padding(20);
156 this.lblMsg.Size = new System.Drawing.Size(427, 218);
157 this.lblMsg.TabIndex = 2;
158 this.lblMsg.Text = "这是一个提示信息。";
159 //
160 // lblTitle
161 //
162 this.lblTitle.BackColor = System.Drawing.Color.Transparent;
163 this.lblTitle.Dock = System.Windows.Forms.DockStyle.Top;
164 this.lblTitle.Font = new System.Drawing.Font("微软雅黑", 17F);
165 this.lblTitle.Location = new System.Drawing.Point(0, 0);
166 this.lblTitle.Name = "lblTitle";
167 this.lblTitle.Size = new System.Drawing.Size(427, 60);
168 this.lblTitle.TabIndex = 0;
169 this.lblTitle.Text = "提示";
170 this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
171 //
172 // ucSplitLine_H1
173 //
174 this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
175 this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Top;
176 this.ucSplitLine_H1.Location = new System.Drawing.Point(0, 60);
177 this.ucSplitLine_H1.Name = "ucSplitLine_H1";
178 this.ucSplitLine_H1.Size = new System.Drawing.Size(427, 1);
179 this.ucSplitLine_H1.TabIndex = 5;
180 this.ucSplitLine_H1.TabStop = false;
181 //
182 // ucSplitLine_H2
183 //
184 this.ucSplitLine_H2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
185 this.ucSplitLine_H2.Dock = System.Windows.Forms.DockStyle.Bottom;
186 this.ucSplitLine_H2.Location = new System.Drawing.Point(0, 213);
187 this.ucSplitLine_H2.Name = "ucSplitLine_H2";
188 this.ucSplitLine_H2.Size = new System.Drawing.Size(427, 1);
189 this.ucSplitLine_H2.TabIndex = 6;
190 this.ucSplitLine_H2.TabStop = false;
191 //
192 // FrmDialog
193 //
194 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
195 this.BackColor = System.Drawing.Color.White;
196 this.ClientSize = new System.Drawing.Size(427, 278);
197 this.Controls.Add(this.ucSplitLine_H2);
198 this.Controls.Add(this.ucSplitLine_H1);
199 this.Controls.Add(this.btnClose);
200 this.Controls.Add(this.panel1);
201 this.Controls.Add(this.lblMsg);
202 this.Controls.Add(this.lblTitle);
203 this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
204 this.IsFullSize = false;
205 this.IsShowRegion = true;
206 this.Name = "FrmDialog";
207 this.Redraw = true;
208 this.ShowIcon = false;
209 this.ShowInTaskbar = false;
210 this.Text = "FrmDialoag";
211 this.VisibleChanged += new System.EventHandler(this.FrmDialog_VisibleChanged);
212 this.panel1.ResumeLayout(false);
213 this.tableLayoutPanel1.ResumeLayout(false);
214 this.ResumeLayout(false);
215
216 }
217
218 #endregion
219
220 private System.Windows.Forms.Label lblTitle;
221 private Controls.UCBtnExt btnOK;
222 private Controls.UCBtnExt btnCancel;
223 private System.Windows.Forms.Label lblMsg;
224 private System.Windows.Forms.Panel panel1;
225 private Controls.UCSplitLine_V ucSplitLine_V1;
226 private System.Windows.Forms.Panel btnClose;
227 private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
228 private Controls.UCSplitLine_H ucSplitLine_H1;
229 private Controls.UCSplitLine_H ucSplitLine_H2;
230 }
231 }
用处:一般用在一个需要用户确定的提示上
效果:
调用示例
1 if (FrmDialog.ShowDialog(this, "是否再显示一个没有取消按钮的提示框?", "模式窗体测试", true) == System.Windows.Forms.DialogResult.OK)
2 {
3 FrmDialog.ShowDialog(this, "这是一个没有取消按钮的提示框", "模式窗体测试");
4 }