玩转控件:重写/重绘Dev中MessageBox弹窗控件

        很久没有更新博客了,本想着直接发一篇《手撕ERP》系列,从控件重写、重绘,到框架搭建,再到部分模块实现+业务的。但是每次动手的时候,都觉得难以下手。直接从数据库设计开始吧,模块设计还没定下来,从模块设计开始吧,winform自带控件和DevExpress控件用起来布局实在太难看了。算了,从低做起吧。接着6-7年前的玩转控件系列开始,工欲善其事必先利其器!利器备好,框架搭建完毕,模块设计就是拖控件而已!

    

        Talk is Cheap,Show me the Code!

        首先,项目中新建一个窗体(用于后面的弹窗载体),按自己意愿做好布局效果,当然关于皮肤方面,大家可以应用界内很成熟的皮肤控件(具体就不列举了,避免打广告的嫌疑),或者后期自己代码实现。本篇主要介绍如何重写/重绘控件,磨自己的利器,至于利器上贴个动漫图片还是其他花里胡哨的图案,请根据自己的喜好来。大概效果如图(有洁癖的请自己细心布局):

        窗体后台代码分析如下:

        首先窗体集成DevExpress:

 public partial class frm_MessageBox : DevExpress.XtraEditors.XtraForm

        其余初始化动作代码如下,备注很详细就不一一列举了:

 /// <summary>
/// 确定按钮
/// </summary>
private SimpleButton btn_OK;/// <summary>
/// 取消按钮
/// </summary>
private SimpleButton btn_Cancel;/// <summary>
/// 中止按钮
/// </summary>
private SimpleButton btn_Abort;/// <summary>
/// 重试按钮
/// </summary>
private SimpleButton btn_Retry;/// <summary>
/// 忽略按钮
/// </summary>
private SimpleButton btn_Ignore;/// <summary>
/// 是按钮
/// </summary>
private SimpleButton btn_Yes;/// <summary>
/// 否按钮
/// </summary>
private SimpleButton btn_No;/// <summary>
/// 要在消息框中显示的文本
/// </summary>
private string text;/// <summary>
/// 要在消息框的标题栏中显示的文本
/// </summary>
private string caption;/// <summary>
///  System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中显示哪些按钮
/// </summary>
private MessageBoxButtons buttons;/// <summary>
/// System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中显示哪个图标
/// </summary>
private MessageBoxIcon icon;/// <summary>
/// System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默认按钮。
/// </summary>
private MessageBoxDefaultButton defaultButton;/// <summary>
/// 消息弹出框参数实体
/// </summary>
MessageBoxModel _MessageBoxModel = default(MessageBoxModel);

        界面初始化:

/// <summary>
/// 支持修改弹出框的按钮标题描述
/// </summary>
/// <param name="pMessageBoxModel"></param>
public frm_MessageBox(MessageBoxModel pMessageBoxModel)
{InitializeComponent();if (pMessageBoxModel == null)pMessageBoxModel = new MessageBoxModel();this.ControlBox = false;this.text = pMessageBoxModel.MsgText;this.Text = pMessageBoxModel.FormText ?? "Stephen's UserControl";this.caption = pMessageBoxModel.FormText;this.buttons = pMessageBoxModel.MsgButton;this.icon = pMessageBoxModel.MsgIcon;this.defaultButton = pMessageBoxModel.MsgxDefaultButton;this._MessageBoxModel = pMessageBoxModel;
}/// <summary>
/// 显示一个具有指定文本、标题、按钮、图标、默认按钮的消息框
/// </summary>
/// <param name="text"></param>
/// <param name="caption"></param>
/// <param name="buttons"></param>
/// <param name="icon"></param>
/// <param name="defaultButton"></param>
public frm_MessageBox(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{InitializeComponent();this.ControlBox = false;this.text = text;this.Text = caption ?? "Stephen's UserControl";this.caption = caption;this.buttons = buttons;this.icon = icon;this.defaultButton = defaultButton;
}

        窗体Load事件绑定弹窗按钮事件:

private void frm_MessageBox_Load(object sender, EventArgs e)
{int pannelLength = panelButton.Size.Width;switch (buttons){case MessageBoxButtons.OK:#region OKthis.btn_OK = new SimpleButton();this.panelButton.SuspendLayout();//btn_OKthis.btn_OK.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_OK.Name = "btn_OK";this.btn_OK.Size = new System.Drawing.Size(75, 27);this.btn_OK.Location = new Point(pannelLength - 85, 10);this.btn_OK.TabIndex = 0;if (_MessageBoxModel != null)this.btn_OK.Text = _MessageBoxModel.YesButtonText;elsethis.btn_OK.Text = sysClass.ssLoadMsgOrDefault("SYS000001", "确定(O)");//确定(O)this.btn_OK.Margin = new Padding(0, 2, 2, 2);this.btn_OK.Click += btn_OK_Click;this.panelButton.Controls.Add(this.btn_OK);this.panelButton.ResumeLayout();#endregionbreak;case MessageBoxButtons.OKCancel:#region OKCancelthis.btn_OK = new SimpleButton();this.btn_Cancel = new SimpleButton();this.panelButton.SuspendLayout();//btn_OKthis.btn_OK.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_OK.Name = "btn_OK";this.btn_OK.Size = new System.Drawing.Size(75, 27);this.btn_OK.Location = new Point(pannelLength - 170, 10);this.btn_OK.TabIndex = 0;if (_MessageBoxModel != null)this.btn_OK.Text = _MessageBoxModel.YesButtonText;elsethis.btn_OK.Text = sysClass.ssLoadMsgOrDefault("SYS000001", "确定(O)");//确定(O)this.btn_OK.Margin = new Padding(0, 2, 2, 2);this.btn_OK.Click += btn_OK_Click;this.panelButton.Controls.Add(this.btn_OK);//btn_Cancelthis.btn_Cancel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_Cancel.Name = "btn_Cancel";this.btn_Cancel.Size = new System.Drawing.Size(75, 27);this.btn_Cancel.Location = new Point(pannelLength - 85, 10);this.btn_Cancel.TabIndex = 1;if (_MessageBoxModel != null)this.btn_Cancel.Text = _MessageBoxModel.CancleButtonText;elsethis.btn_Cancel.Text = sysClass.ssLoadMsgOrDefault("SYS000002", "取消(C)");//取消(C)this.btn_Cancel.Margin = new Padding(0, 2, 2, 2);this.btn_Cancel.Click += btn_Cancel_Click;this.panelButton.Controls.Add(this.btn_Cancel);this.panelButton.ResumeLayout();if (defaultButton == MessageBoxDefaultButton.Button1){this.btn_OK.Select();}else{this.btn_Cancel.Select();}#endregionbreak;case MessageBoxButtons.AbortRetryIgnore:#region AbortRetryIgnorethis.btn_Abort = new SimpleButton();this.btn_Retry = new SimpleButton();this.btn_Ignore = new SimpleButton();this.panelButton.SuspendLayout();//btn_Abortthis.btn_Abort.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_Abort.Name = "btn_Abort";this.btn_Abort.Size = new System.Drawing.Size(75, 27);this.btn_Abort.Location = new Point(pannelLength - 255, 10);this.btn_Abort.TabIndex = 0;this.btn_Abort.Text = sysClass.ssLoadMsgOrDefault("SYS000003", "中止(A)");//中止(A)this.btn_Abort.Margin = new Padding(0, 2, 2, 2);this.btn_Abort.Click += btn_Abort_Click;this.panelButton.Controls.Add(this.btn_Abort);//btn_Retrythis.btn_Retry.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_Retry.Name = "btn_Retry";this.btn_Retry.Size = new System.Drawing.Size(75, 27);this.btn_Retry.Location = new Point(pannelLength - 170, 10);this.btn_Retry.TabIndex = 1;this.btn_Retry.Text = sysClass.ssLoadMsgOrDefault("SYS000004", "重试(R)");//重试(R)this.btn_Retry.Margin = new Padding(0, 2, 2, 2);this.btn_Retry.Click += btn_Retry_Click;this.panelButton.Controls.Add(this.btn_Retry);//btn_Ignorethis.btn_Ignore.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_Ignore.Name = "btn_Ignore";this.btn_Ignore.Size = new System.Drawing.Size(75, 27);this.btn_Ignore.Location = new Point(pannelLength - 85, 10);this.btn_Ignore.TabIndex = 2;this.btn_Ignore.Text = sysClass.ssLoadMsgOrDefault("SYS000005", "忽略(I)");//忽略(I)this.btn_Ignore.Margin = new Padding(0, 2, 2, 2);this.btn_Ignore.Click += btn_Ignore_Click;this.panelButton.Controls.Add(this.btn_Ignore);this.panelButton.ResumeLayout();if (defaultButton == MessageBoxDefaultButton.Button1){this.btn_Abort.Select();}else if (defaultButton == MessageBoxDefaultButton.Button2){this.btn_Retry.Select();}else if (defaultButton == MessageBoxDefaultButton.Button3){this.btn_Ignore.Select();}#endregionbreak;case MessageBoxButtons.YesNoCancel:#region YesNoCancelthis.btn_Yes = new SimpleButton();this.btn_No = new SimpleButton();this.btn_Cancel = new SimpleButton();this.panelButton.SuspendLayout();//btn_Yesthis.btn_Yes.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_Yes.Name = "btn_Yes";this.btn_Yes.Size = new System.Drawing.Size(75, 27);this.btn_Yes.Location = new Point(pannelLength - 255, 10);this.btn_Yes.TabIndex = 0;if (_MessageBoxModel != null)this.btn_Yes.Text = _MessageBoxModel.YesButtonText;elsethis.btn_Yes.Text = sysClass.ssLoadMsgOrDefault("SYS000006", "是(Y)");//是(Y)this.btn_Yes.Margin = new Padding(0, 2, 2, 2);this.btn_Yes.Click += btn_Yes_Click;this.panelButton.Controls.Add(this.btn_Yes);//btn_Nothis.btn_No.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_No.Name = "btn_No";this.btn_No.Size = new System.Drawing.Size(75, 27);this.btn_No.Location = new Point(pannelLength - 170, 10);this.btn_No.TabIndex = 1;if (_MessageBoxModel != null)this.btn_No.Text = _MessageBoxModel.NoButtonText;elsethis.btn_No.Text = sysClass.ssLoadMsgOrDefault("SYS000007", "否(N)");//否(N)this.btn_No.Margin = new Padding(0, 2, 2, 2);this.btn_No.Click += btn_No_Click;this.panelButton.Controls.Add(this.btn_No);this.btn_Cancel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_Cancel.Name = "btn_Cancel";this.btn_Cancel.Size = new System.Drawing.Size(75, 27);this.btn_Cancel.Location = new Point(pannelLength - 85, 10);this.btn_Cancel.TabIndex = 2;if (_MessageBoxModel != null)this.btn_Cancel.Text = _MessageBoxModel.CancleButtonText;elsethis.btn_Cancel.Text = sysClass.ssLoadMsgOrDefault("SYS000002", "取消(C)");//取消(C)this.btn_Cancel.Margin = new Padding(0, 2, 2, 2);this.btn_Cancel.Click += btn_Cancel_Click;this.panelButton.Controls.Add(this.btn_Cancel);this.panelButton.ResumeLayout();if (defaultButton == MessageBoxDefaultButton.Button1){this.btn_Yes.Select();}else if (defaultButton == MessageBoxDefaultButton.Button2){this.btn_No.Select();}else if (defaultButton == MessageBoxDefaultButton.Button3){this.btn_Cancel.Select();}#endregionbreak;case MessageBoxButtons.YesNo:#region YesNothis.btn_Yes = new SimpleButton();this.btn_No = new SimpleButton();this.panelButton.SuspendLayout();//btn_Yesthis.btn_Yes.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_Yes.Name = "btn_Yes";this.btn_Yes.Size = new System.Drawing.Size(75, 27);this.btn_Yes.Location = new Point(pannelLength - 170, 10);this.btn_Yes.TabIndex = 0;if (_MessageBoxModel != null)this.btn_Yes.Text = _MessageBoxModel.YesButtonText;elsethis.btn_Yes.Text = sysClass.ssLoadMsgOrDefault("SYS000006", "是(Y)");//是(Y)this.btn_Yes.Margin = new Padding(0, 2, 2, 2);this.btn_Yes.Click += btn_Yes_Click;this.panelButton.Controls.Add(this.btn_Yes);//btn_Nothis.btn_No.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_No.Name = "btn_No";this.btn_No.Size = new System.Drawing.Size(75, 27);this.btn_No.Location = new Point(pannelLength - 85, 10);this.btn_No.TabIndex = 1;if (_MessageBoxModel != null)this.btn_No.Text = _MessageBoxModel.NoButtonText;elsethis.btn_No.Text = sysClass.ssLoadMsgOrDefault("SYS000007", "否(N)");//否(N)this.btn_No.Margin = new Padding(0, 2, 2, 2);this.btn_No.Click += btn_No_Click;this.panelButton.Controls.Add(this.btn_No);this.panelButton.ResumeLayout();if (defaultButton == MessageBoxDefaultButton.Button1){this.btn_Yes.Select();}else{this.btn_No.Select();}#endregionbreak;case MessageBoxButtons.RetryCancel:#region RetryCancelthis.btn_Retry = new SimpleButton();this.btn_Cancel = new SimpleButton();this.panelButton.SuspendLayout();//btn_Retrythis.btn_Retry.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_Retry.Name = "btn_Retry";this.btn_Retry.Size = new System.Drawing.Size(75, 27);this.btn_Retry.Location = new Point(pannelLength - 170, 10);this.btn_Retry.TabIndex = 0;this.btn_Retry.Text = sysClass.ssLoadMsgOrDefault("SYS000004", "重试(R)");//重试(R)this.btn_Retry.Margin = new Padding(0, 2, 2, 2);this.btn_Retry.Click += btn_Retry_Click;this.panelButton.Controls.Add(this.btn_Retry);//btn_Cancelthis.btn_Cancel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| (System.Windows.Forms.AnchorStyles.Right)))));this.btn_Cancel.Name = "btn_Cancel";this.btn_Cancel.Size = new System.Drawing.Size(75, 27);this.btn_Cancel.Location = new Point(pannelLength - 85, 10);this.btn_Cancel.TabIndex = 1;this.btn_Cancel.Text = sysClass.ssLoadMsgOrDefault("SYS000002", "取消(C)");//取消(C)this.btn_Cancel.Margin = new Padding(0, 2, 2, 2);this.btn_Cancel.Click += btn_Cancel_Click;this.panelButton.Controls.Add(this.btn_Cancel);this.panelButton.ResumeLayout();if (defaultButton == MessageBoxDefaultButton.Button1){this.btn_Retry.Select();}else{this.btn_Cancel.Select();}#endregionbreak;}this.Text = caption;this.lblMsg.Text = text;int moreHeight = this.lblMsg.Height - 35;if (moreHeight > 0){this.Height += moreHeight;}}

        代码比较简单,就是把初始化按钮事件和把初始化的弹窗中的按钮添加到布局中的Panel容器里面和一些细节调整,关于方法sysClass.ssLoadMsgOrDefault目前可以不用在意,是我的通用类库,主要是用来实现国际化的,后续会断断续续为大家介绍这块代码。

        按钮绑定事件和键盘响应事件代码如下:

private void PaintIcon(Icon icon, int x, int y){Graphics g = this.CreateGraphics();g.DrawIcon(icon, x, y);}void btn_No_Click(object sender, EventArgs e){this.DialogResult = DialogResult.No;}void btn_Yes_Click(object sender, EventArgs e){this.DialogResult = DialogResult.Yes;}void btn_Ignore_Click(object sender, EventArgs e){this.DialogResult = DialogResult.Ignore;}void btn_Retry_Click(object sender, EventArgs e){this.DialogResult = DialogResult.Retry;}void btn_Abort_Click(object sender, EventArgs e){this.DialogResult = DialogResult.Abort;}void btn_Cancel_Click(object sender, EventArgs e){this.DialogResult = DialogResult.Cancel;}void btn_OK_Click(object sender, EventArgs e){this.DialogResult = DialogResult.OK;}private void frm_MessageBox_KeyUp(object sender, KeyEventArgs e){if (e.Modifiers == Keys.None){if (e.KeyCode == Keys.O && btn_OK != null){btn_OK_Click(null, null);}if (e.KeyCode == Keys.C && btn_Cancel != null){btn_Cancel_Click(null, null);}if (e.KeyCode == Keys.A && btn_Abort != null){btn_Abort_Click(null, null);}if (e.KeyCode == Keys.R && btn_Retry != null){btn_Retry_Click(null, null);}if (e.KeyCode == Keys.I && btn_Ignore != null){btn_Ignore_Click(null, null);}if (e.KeyCode == Keys.Y && btn_Yes != null){btn_Yes_Click(null, null);}if (e.KeyCode == Keys.N && btn_No != null){btn_No_Click(null, null);}}if (e.Modifiers == Keys.Control && e.KeyCode == Keys.C){string mCopyText = "-------------------------------";mCopyText += "\n";mCopyText += lblMsg.Text + "\n";mCopyText += "-------------------------------";Clipboard.SetText(mCopyText);}}private void frm_MessageBox_Paint(object sender, PaintEventArgs e){Icon msgIcon;switch (icon){case MessageBoxIcon.Error:msgIcon = System.Drawing.SystemIcons.Error;break;case MessageBoxIcon.Question:msgIcon = System.Drawing.SystemIcons.Question;break;case MessageBoxIcon.Exclamation:msgIcon = System.Drawing.SystemIcons.Exclamation;break;default:msgIcon = System.Drawing.SystemIcons.Information;break;}e.Graphics.DrawIcon(msgIcon, 40, 20);}}

           以及弹窗实体类:

 /// <summary>
/// 弹出框实体
/// </summary>
public class MessageBoxModel
{/// <summary>/// 弹出框标题/// </summary>public string FormText { get; set; }/// <summary>/// 弹出框宽度/// </summary>public int FormWidth { get; set; }/// <summary>/// 弹出框高度/// </summary>public int FormHeight { get; set; }/// <summary>/// 弹出框消息内容/// </summary>public string MsgText { get; set; }/// <summary>/// 文字大小/// </summary>public int FontSize { get; set; }/// <summary>/// “是”按钮标题/// </summary>public string YesButtonText { get; set; }/// <summary>/// “否”按钮标题/// </summary>public string NoButtonText { get; set; }/// <summary>/// “取消”按钮标题/// </summary>public string CancleButtonText { get; set; }/// <summary>/// 弹出框类型(提示型、选择型等)/// </summary>public MessageBoxButtons MsgButton = MessageBoxButtons.OK;/// <summary>/// 弹出框中显示的图标/// </summary>public MessageBoxIcon MsgIcon = MessageBoxIcon.Information;/// <summary>/// 弹出框默认选中的按钮/// </summary>public MessageBoxDefaultButton MsgxDefaultButton = MessageBoxDefaultButton.Button1;

        细心的读者会发现,博主在实例弹窗实体的时候,有个语法糖:

    /// <summary>/// 消息弹出框参数实体/// </summary>MessageBoxModel _MessageBoxModel = default(MessageBoxModel);

        default(T) 这是C# 7.1的关键字新用法,主要用法是默认值表达式,default对应各种类型生成默认值列表如下:

类型
默认值
任何引用类型
null
数值类型
0
bool
false
enum表达式(E)0生成的值,其中E是枚举标识符
struct
通过如下设置生成的值:将所有值类型的字段设置为其默认值,将所有引用类型的字段设置为null    ‍
可以为null的类型
HasValue属性为false且Value属性未定义的实例

罗列一下上述列表中常见类型对应的值

default(string) // null
default(int) // 0
default(int?) // null
default(dynamic) // null
default(DateTime) // 0001/01/01 0:00:00
default(DateTime?) // null

        好了,篇幅有限,具体深入了解请大家自行百度看看微软文档的解释。

     以上是窗体代码解析,窗体创建好了,最后一步,创建一个实体类(暂时命名KzxMessageBox)用来调用弹窗的窗体显示,具体代码如下:

public class KzxMessageBox
{/// <summary>/// 标题 /// </summary>private static string caption;/// <summary>/// 按钮(默认“OK”)/// </summary>private static MessageBoxButtons buttons;/// <summary>/// 图标(默认“information”)/// </summary>private static MessageBoxIcon icon;/// <summary>/// 默认按钮(默认“button1”)/// </summary>private static MessageBoxDefaultButton defaultButton;/// <summary>/// 静态构造函数,初始化数据/// </summary>static KzxMessageBox(){if (SysVar.loginType == 1){caption = "Stephen's UserControl";}else{caption = sysClass.ssLoadMsgOrDefault("SYS000008", "Stephen's UserControl");}buttons = MessageBoxButtons.OK;icon = MessageBoxIcon.Information;defaultButton = MessageBoxDefaultButton.Button1;}/// <summary>/// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框/// </summary>/// <param name="text">文本</param>/// <returns></returns>public static DialogResult Show(string text){return Show(text, buttons, icon, defaultButton, null);}/// <summary>/// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框,add by zhang.jz 2019.05.10/// </summary>/// <param name="text">文本</param>/// <returns></returns>public static DialogResult Show(string text, int pFormWidth = 0, int pFormHeight = 0){return Show(text, buttons, icon, defaultButton, null, pFormWidth, pFormHeight);}/// <summary>/// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框/// </summary>/// <param name="text"></param>/// <param name="parent"></param>/// <returns></returns>public static DialogResult Show(string text, Form parent){return Show(text, buttons, icon, defaultButton, parent);}/// <summary>/// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框/// </summary>/// <param name="text">文本</param>/// <param name="buttons">按钮</param>/// <returns></returns>public static DialogResult Show(string text, MessageBoxButtons buttons){return Show(text, buttons, icon, defaultButton, null);}/// <summary>/// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框/// </summary>/// <param name="text"></param>/// <param name="buttons"></param>/// <param name="parent"></param>/// <returns></returns>public static DialogResult Show(string text, MessageBoxButtons buttons, Form parent){return Show(text, buttons, icon, defaultButton, parent);}/// <summary>/// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框/// </summary>/// <param name="text">文本</param>/// <param name="caption">标题</param>/// <param name="buttons">按钮</param>/// <param name="icon">图标</param>/// <returns></returns>public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon){return Show(text, buttons, icon, defaultButton, null);}/// <summary>/// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框/// </summary>/// <param name="text"></param>/// <param name="buttons"></param>/// <param name="icon"></param>/// <param name="parent"></param>/// <returns></returns>public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon, Form parent){return Show(text, buttons, icon, defaultButton, parent);}/// <summary>/// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框/// </summary>/// <param name="text"></param>/// <param name="buttons"></param>/// <param name="icon"></param>/// <param name="defaultButton"></param>/// <returns></returns>public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton){return Show(text, buttons, icon, defaultButton, null);}/// <summary>/// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框/// </summary>/// <param name="text">文本</param>/// <param name="caption">标题</param>/// <param name="buttons">按钮</param>/// <param name="icon">图标</param>/// <param name="defaultButton">默认按钮</param>/// <returns>System.Windows.Forms.DialogResult 值之一</returns>public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, Form parent, int pFormWidth = 0, int pFormHeight = 0){using (frm_MessageBox frm = new frm_MessageBox(text, caption, buttons, icon, defaultButton)){if (parent == null || parent.IsDisposed){frm.StartPosition = FormStartPosition.CenterScreen; if (pFormWidth != 0) frm.Width = pFormWidth;if (pFormHeight != 0) frm.Height = pFormHeight;return frm.ShowDialog();}else{frm.StartPosition = FormStartPosition.CenterParent; if (pFormWidth != 0) frm.Width = pFormWidth;if (pFormHeight != 0) frm.Height = pFormHeight;return frm.ShowDialog(parent);}}}public static DialogResult Show(Form parent, MessageBoxModel pMessageBoxModel){using (frm_MessageBox frm = new frm_MessageBox(pMessageBoxModel)){if (parent == null || parent.IsDisposed){frm.StartPosition = FormStartPosition.CenterScreen; if (pMessageBoxModel.FormWidth != 0) frm.Width = pMessageBoxModel.FormWidth;if (pMessageBoxModel.FormHeight != 0) frm.Height = pMessageBoxModel.FormHeight;return frm.ShowDialog();}else{frm.StartPosition = FormStartPosition.CenterParent; if (pMessageBoxModel.FormWidth != 0) frm.Width = pMessageBoxModel.FormWidth;if (pMessageBoxModel.FormHeight != 0) frm.Height = pMessageBoxModel.FormHeight;return frm.ShowDialog(parent);}}}
}

        代码比较简单,创建一个公共类,以及类型Messagebox的方法重载而已!

        最后一步,调用:

  private void button1_Click(object sender, EventArgs e){KzxMessageBox.Show("this is a test!");KzxMessageBox.Show("This is a test!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);}

        一起看下效果:

        最后,由于后续所有重写/重绘控件都在同一个项目使用,而且Dev系统引用文件较多,压缩后源码文件仍然很大,如果有需要源码的朋友,可以微信公众号联系博主,源码可以免费赠予~!有疑问的也可以CALL我一起探讨,最最后,如果觉得本篇博文对您或者身边朋友有帮助的,麻烦点个关注!赠人玫瑰,手留余香,您的支持就是我写作最大的动力,感谢您的关注,期待和您一起探讨!再会!

    

        

        

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/311032.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

.NET Core开发实战(第26课:工程结构概览:定义应用分层及依赖关系)--学习笔记...

26 | 工程结构概览&#xff1a;定义应用分层及依赖关系从这一节开始进入微服务实战部分这一节主要讲解工程的结构和应用的分层在应用的分层这里定义了四个层次&#xff1a;1、领域模型层2、基础设施层3、应用层4、共享层可以通过代码来看一下源码链接&#xff1a;https://githu…

容器网络是如何影响Kubernetes中数据库性能的?

关于Kubernetes中的数据库&#xff0c;大家最关心的第一个问题是性能。由于这种担心的存在&#xff0c;许多使用Kubernetes进行生产应用程序工作的客户正在Kubernetes之外的裸机或VM上运行数据库。因此&#xff0c;我们致力于深入研究Kubernetes抽象层&#xff0c;确定值得测试…

mysql php状态函数_mysql_stat()查询MySQL服务器当前系统状态

mysql教程&#xff1a;mysql_stat()查询MySQL服务器当前系统状态 定义和用法 mysql_stat() 函数返回 MySQL 服务器的当前系统状态。 如果成功&#xff0c;则该函数返回状态。如果失败&#xff0c;则返回 false。 语法 mysql_stat(connection)参数 描述 connection 可mysql教程&…

聊聊面试的事(应聘方)

这里是Z哥的个人公众号每周五11&#xff1a;45 按时送达当然了&#xff0c;也会时不时加个餐&#xff5e;我的第「134」篇原创敬上原本春节长假之后会有不少人开始新的面试之旅。但是疫情的到来打乱了这个节奏&#xff0c;包括招聘方的计划。因此&#xff0c;在以往是金三银四的…

mysql边备份边导入么_MySQL 怎么导入导出操作

mysql 如何导入导出操作1、MySQL 如何导入导出个别需要数据记录&#xff1f;– 先导出数据所在的表结构&#xff1a;mysql> show create table CHARBASE into outfile “/db/mysql/RS.sql” ;– 将需要单独导出的内容存放在文本文件里&#xff0c;这里一定要注意存放的位置必…

Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(三)

在前面两篇文章中&#xff0c;我介绍了基于IdentityServer4的一个Identity Service的实现&#xff0c;并且实现了一个Weather API和基于Ocelot的API网关&#xff0c;然后实现了通过Ocelot API网关整合Identity Service做身份认证的API请求。今天&#xff0c;我们进入前端开发&a…

[蓝桥杯][算法提高VIP]因式分解

解题思路&#xff1a; 水题&#xff01;&#xff01;&#xff01; 代码如下&#xff1a; #include <iostream> using namespace std; const int N 100010; int p[N]; int k; bool vis[N];void init() {for (int i 2;i<N-1;i){if (!vis[i])p[k] i;for (int j 2*i…

win10 64位操作系统安装mysql_win10,64位操作系统安装mysql-8.0.16经验总结(图文详细,保证一次安装成功)...

机器配置&#xff1a;win10&#xff0c;64位&#xff1b;mysql-8.0.161.mysql下载首先在mysql下载链接下载安装包&#xff1a;点击 Download 按钮进入下载页面&#xff0c;点击下图中的 No thanks, just start my download. 就可立即下载&#xff1a;2.解压及配置文件下载完后&…

基于 Roslyn 实现一个简单的条件解析引擎

基于 Roslyn 实现一个简单的条件解析引擎Intro最近在做一个勋章的服务&#xff0c;我们想定义一些勋章的获取条件&#xff0c;满足条件之后就给用户颁发一个勋章&#xff0c;定义条件的时候会定义需要哪些参数&#xff0c;参数的类型&#xff0c;获取勋章的时候会提供所需要的参…

BeetleX之vue-autoui自匹配UI插件

vue-autoui 是一款基于vue和element扩展的一个自动化UI控件&#xff0c;它主要提供两个控件封装分别是auto-form和auto-grid; 通过这两个控件可以完成大多数的信息输入和查询输出的需要.auto-form和auto-grid是通过json来描述展示的结构&#xff0c;在处理上要比写html标签来得…

protobufjs 命令执行_protobufjs简单使用

npm i protobufjs -D添加两个proto文件User.protosyntax "proto3";package login;message PBUser {string uid 1;string pwd 2;int64 age 3;}Login.protosyntax "proto3";package login;import "./User.proto";message LoginReq {PBUser us…

.NET Core开发实战(第27课:定义Entity:区分领域模型的内在逻辑和外在行为)--学习笔记...

27 | 定义Entity&#xff1a;区分领域模型的内在逻辑和外在行为上一节讲到领域模型分为两层一层是抽象层&#xff0c;定义了公共的接口和类另一层就是领域模型的定义层先看一下抽象层的定义1、实体接口 IEntitynamespace GeekTime.Domain {public interface IEntity{object[] G…

Abp vNext发布v2.3!

在全球范围内病毒笼罩的日子里,我们发布了ABP框架v2.3, 这篇文章将说明本次发布新增内容和过去的两周我们做了什么.关于新冠病毒和我们的团队关于冠状病毒的状况我们很难过.在Volosoft的团队,我们有不同国家的远程工作者在自己家里工作.从上周开始,我们已经完全开始在家远程工作…

ASP.NET Core 中间件分类

ASP.NET Core 中间件的配置方法可以分为以上三种&#xff0c;对应的Helper方法分别是&#xff1a;Run(), Use(), Map()。Run()&#xff0c;使用Run调用中间件的时候&#xff0c;会直接返回一个响应&#xff0c;所以后续的中间件将不会被执行了。Use()&#xff0c;它会对请求做一…

redis持久化到mysql的方案_redis进阶: 数据持久化

redis是内存数据库&#xff0c;即数据库状态都是存储于内存中&#xff0c;因此&#xff0c;当服务器重启或者断开后&#xff0c;数据便会丢失&#xff1b;为了解决数据丢失问题&#xff0c;便需要将数据从内存保持到磁盘中&#xff0c;这就是redis的数据持久化目前&#xff0c;…

如何创建一个自定义的`ErrorHandlerMiddleware`方法

在本文中&#xff0c;我将讲解如何通过自定义ExceptionHandlerMiddleware&#xff0c;以便在中间件管道中发生错误时创建自定义响应&#xff0c;而不是提供一个“重新执行”管道的路径。作者&#xff1a;依乐祝译文&#xff1a;https://www.cnblogs.com/yilezhu/p/12497937.htm…

mysql or中有空查询慢_MySQL 慢查询日志

1.定义2.相关参数2.开启3.原因4.慢查询日志工具mysqldumpslow1.定义作用:用来记录在MySQL中响应时间超过阀值的语句。2.相关参数mysql> show variables like %slow_query%;------------------------------------------------------------| Variable_name | Value …

从业务需求抽象成模型解决方案

从业务需求调研&#xff0c;通过抽象转换成模型技术方案&#xff0c;本文将对这个过程做个拆解&#xff0c;供大家参考。以下我所说的可能都是错的&#xff0c;只是一家之见&#xff0c;欢迎大家在留言区多提意见和看法&#xff0c;互相共勉。一、订单对象-信息需求公司的运营都…

《C++ Primer》7.1.4节练习

练习7.11: #include <iostream> #include <cstring> using namespace std;class Sales_data {public:Sales_data() default;Sales_data(const std::string &book): bookNo(book) {}Sales_data(const std::string &book, const unsigned num, const doubl…

DotNetCore Web应用程序中的Session管理

原文来自互联网&#xff0c;由长沙DotNET技术社区编译。如译文侵犯您的署名权或版权&#xff0c;请联系小编&#xff0c;小编将在24小时内删除。限于译者的能力有限&#xff0c;个别语句翻译略显生硬&#xff0c;还请见谅。作者简介&#xff1a;Jon&#xff08;Jonathan&#x…