前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
目录
https://blog.csdn.net/kwwwvagaa/article/details/100586547
用处及效果
用处:一般用在一个需要用户确定的提示上
效果:
调用示例
1 if (FrmDialog.ShowDialog(this, "是否再显示一个没有取消按钮的提示框?", "模式窗体测试", true) == System.Windows.Forms.DialogResult.OK) 2 { 3 FrmDialog.ShowDialog(this, "这是一个没有取消按钮的提示框", "模式窗体测试"); 4 }
准备工作
这是一个提示消息的窗体,他继承自基类窗体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 }
代码就这么多,看下完整代码
// 版权所有 黄正辉 交流群:568015492 QQ:623128629
// 文件名称:FrmDialog.cs
// 创建日期:2019-08-15 16:04:36
// 功能描述:FrmDialog
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HZH_Controls.Forms
{
public partial class FrmDialog : FrmBase
{
bool blnEnterClose = true;
private FrmDialog(
string strMessage,
string strTitle,
bool blnShowCancel = false,
bool blnShowClose = false,
bool blnisEnterClose = true)
{
InitializeComponent();
if (!string.IsNullOrWhiteSpace(strTitle))
lblTitle.Text = strTitle;
lblMsg.Text = strMessage;
if (blnShowCancel)
{
this.tableLayoutPanel1.ColumnStyles[1].Width = 1;
this.tableLayoutPanel1.ColumnStyles[2].Width = 50;
}
else
{
this.tableLayoutPanel1.ColumnStyles[1].Width = 0;
this.tableLayoutPanel1.ColumnStyles[2].Width = 0;
}
//btnCancel.Visible = blnShowCancel;
//ucSplitLine_V1.Visible = blnShowCancel;
btnClose.Visible = blnShowClose;
blnEnterClose = blnisEnterClose;
//if (blnShowCancel)
//{
// btnOK.BtnForeColor = Color.FromArgb(255, 85, 51);
//}
}
#region 显示一个模式信息框
/// <summary>
/// 功能描述:显示一个模式信息框
/// 作 者:HZH
/// 创建日期:2019-03-04 15:49:48
/// 任务编号:POS
/// </summary>
/// <param name="owner">owner</param>
/// <param name="strMessage">strMessage</param>
/// <param name="strTitle">strTitle</param>
/// <param name="blnShowCancel">blnShowCancel</param>
/// <param name="isShowMaskDialog">isShowMaskDialog</param>
/// <param name="blnShowClose">blnShowClose</param>
/// <param name="isEnterClose">isEnterClose</param>
/// <returns>返回值</returns>
public static DialogResult ShowDialog(
IWin32Window owner,
string strMessage,
string strTitle = "提示",
bool blnShowCancel = false,
bool isShowMaskDialog = true,
bool blnShowClose = false,
bool blnIsEnterClose = true)
{
DialogResult result = DialogResult.Cancel;
if (owner == null || (owner is Control && (owner as Control).IsDisposed))
{
result = new FrmDialog(strMessage, strTitle, blnShowCancel, blnShowClose, blnIsEnterClose)
{
StartPosition = FormStartPosition.CenterScreen,
IsShowMaskDialog = isShowMaskDialog,
TopMost = true
}.ShowDialog();
}
else
{
if (owner is Control)
{
owner = (owner as Control).FindForm();
}
result = new FrmDialog(strMessage, strTitle, blnShowCancel, blnShowClose, blnIsEnterClose)
{
StartPosition = (owner != null) ? FormStartPosition.CenterParent : FormStartPosition.CenterScreen,
IsShowMaskDialog = isShowMaskDialog,
TopMost = true
}.ShowDialog(owner);
}
return result;
}
#endregion
private void btnOK_BtnClick(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void btnCancel_BtnClick(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
}
private void btnClose_MouseDown(object sender, MouseEventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
}
protected override void DoEnter()
{
if (blnEnterClose)
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void FrmDialog_VisibleChanged(object sender, EventArgs e)
{
}
}
}
namespace HZH_Controls.Forms
{
partial class FrmDialog
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmDialog));
this.btnClose = new System.Windows.Forms.Panel();
this.panel1 = new System.Windows.Forms.Panel();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.btnCancel = new HZH_Controls.Controls.UCBtnExt();
this.ucSplitLine_V1 = new HZH_Controls.Controls.UCSplitLine_V();
this.btnOK = new HZH_Controls.Controls.UCBtnExt();
this.lblMsg = new System.Windows.Forms.Label();
this.lblTitle = new System.Windows.Forms.Label();
this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
this.ucSplitLine_H2 = new HZH_Controls.Controls.UCSplitLine_H();
this.panel1.SuspendLayout();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// btnClose
//
this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnClose.BackgroundImage = global::HZH_Controls.Properties.Resources.dialog_close;
this.btnClose.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btnClose.Location = new System.Drawing.Point(383, 0);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(28, 60);
this.btnClose.TabIndex = 4;
this.btnClose.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnClose_MouseDown);
//
// panel1
//
this.panel1.Controls.Add(this.tableLayoutPanel1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 214);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(427, 64);
this.panel1.TabIndex = 3;
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 3;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Controls.Add(this.btnCancel, 2, 0);
this.tableLayoutPanel1.Controls.Add(this.ucSplitLine_V1, 1, 0);
this.tableLayoutPanel1.Controls.Add(this.btnOK, 0, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 1;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(427, 64);
this.tableLayoutPanel1.TabIndex = 5;
//
// btnCancel
//
this.btnCancel.BackColor = System.Drawing.Color.Transparent;
this.btnCancel.BtnBackColor = System.Drawing.Color.Transparent;
this.btnCancel.BtnFont = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnCancel.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(73)))), ((int)(((byte)(119)))), ((int)(((byte)(232)))));
this.btnCancel.BtnText = "取消";
this.btnCancel.ConerRadius = 5;
this.btnCancel.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnCancel.Dock = System.Windows.Forms.DockStyle.Fill;
this.btnCancel.FillColor = System.Drawing.Color.White;
this.btnCancel.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
this.btnCancel.IsRadius = false;
this.btnCancel.IsShowRect = false;
this.btnCancel.IsShowTips = false;
this.btnCancel.Location = new System.Drawing.Point(214, 0);
this.btnCancel.Margin = new System.Windows.Forms.Padding(0);
this.btnCancel.Name = "btnCancel";
this.btnCancel.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
this.btnCancel.RectWidth = 1;
this.btnCancel.Size = new System.Drawing.Size(213, 64);
this.btnCancel.TabIndex = 1;
this.btnCancel.TabStop = false;
this.btnCancel.TipsText = "";
this.btnCancel.BtnClick += new System.EventHandler(this.btnCancel_BtnClick);
//
// ucSplitLine_V1
//
this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Fill;
this.ucSplitLine_V1.Location = new System.Drawing.Point(213, 15);
this.ucSplitLine_V1.Margin = new System.Windows.Forms.Padding(0, 15, 0, 15);
this.ucSplitLine_V1.Name = "ucSplitLine_V1";
this.ucSplitLine_V1.Size = new System.Drawing.Size(1, 34);
this.ucSplitLine_V1.TabIndex = 2;
this.ucSplitLine_V1.TabStop = false;
//
// btnOK
//
this.btnOK.BackColor = System.Drawing.Color.Transparent;
this.btnOK.BtnBackColor = System.Drawing.Color.Transparent;
this.btnOK.BtnFont = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnOK.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
this.btnOK.BtnText = "确定";
this.btnOK.ConerRadius = 5;
this.btnOK.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnOK.Dock = System.Windows.Forms.DockStyle.Fill;
this.btnOK.FillColor = System.Drawing.Color.White;
this.btnOK.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
this.btnOK.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
this.btnOK.IsRadius = false;
this.btnOK.IsShowRect = false;
this.btnOK.IsShowTips = false;
this.btnOK.Location = new System.Drawing.Point(0, 0);
this.btnOK.Margin = new System.Windows.Forms.Padding(0);
this.btnOK.Name = "btnOK";
this.btnOK.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
this.btnOK.RectWidth = 1;
this.btnOK.Size = new System.Drawing.Size(213, 64);
this.btnOK.TabIndex = 0;
this.btnOK.TabStop = false;
this.btnOK.TipsText = "";
this.btnOK.BtnClick += new System.EventHandler(this.btnOK_BtnClick);
//
// lblMsg
//
this.lblMsg.BackColor = System.Drawing.Color.White;
this.lblMsg.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblMsg.Font = new System.Drawing.Font("微软雅黑", 12F);
this.lblMsg.Location = new System.Drawing.Point(0, 60);
this.lblMsg.Name = "lblMsg";
this.lblMsg.Padding = new System.Windows.Forms.Padding(20);
this.lblMsg.Size = new System.Drawing.Size(427, 218);
this.lblMsg.TabIndex = 2;
this.lblMsg.Text = "这是一个提示信息。";
//
// lblTitle
//
this.lblTitle.BackColor = System.Drawing.Color.Transparent;
this.lblTitle.Dock = System.Windows.Forms.DockStyle.Top;
this.lblTitle.Font = new System.Drawing.Font("微软雅黑", 17F);
this.lblTitle.Location = new System.Drawing.Point(0, 0);
this.lblTitle.Name = "lblTitle";
this.lblTitle.Size = new System.Drawing.Size(427, 60);
this.lblTitle.TabIndex = 0;
this.lblTitle.Text = "提示";
this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// ucSplitLine_H1
//
this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Top;
this.ucSplitLine_H1.Location = new System.Drawing.Point(0, 60);
this.ucSplitLine_H1.Name = "ucSplitLine_H1";
this.ucSplitLine_H1.Size = new System.Drawing.Size(427, 1);
this.ucSplitLine_H1.TabIndex = 5;
this.ucSplitLine_H1.TabStop = false;
//
// ucSplitLine_H2
//
this.ucSplitLine_H2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
this.ucSplitLine_H2.Dock = System.Windows.Forms.DockStyle.Bottom;
this.ucSplitLine_H2.Location = new System.Drawing.Point(0, 213);
this.ucSplitLine_H2.Name = "ucSplitLine_H2";
this.ucSplitLine_H2.Size = new System.Drawing.Size(427, 1);
this.ucSplitLine_H2.TabIndex = 6;
this.ucSplitLine_H2.TabStop = false;
//
// FrmDialog
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(427, 278);
this.Controls.Add(this.ucSplitLine_H2);
this.Controls.Add(this.ucSplitLine_H1);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.panel1);
this.Controls.Add(this.lblMsg);
this.Controls.Add(this.lblTitle);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.IsFullSize = false;
this.IsShowRegion = true;
this.Name = "FrmDialog";
this.Redraw = true;
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.Text = "FrmDialoag";
this.VisibleChanged += new System.EventHandler(this.FrmDialog_VisibleChanged);
this.panel1.ResumeLayout(false);
this.tableLayoutPanel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label lblTitle;
private Controls.UCBtnExt btnOK;
private Controls.UCBtnExt btnCancel;
private System.Windows.Forms.Label lblMsg;
private System.Windows.Forms.Panel panel1;
private Controls.UCSplitLine_V ucSplitLine_V1;
private System.Windows.Forms.Panel btnClose;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private Controls.UCSplitLine_H ucSplitLine_H1;
private Controls.UCSplitLine_H ucSplitLine_H2;
}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧