winform 异步弹窗窗体_玩转控件:重写/重绘Dev中MessageBox弹窗控件

        很久没有更新博客了,本想着直接发一篇《手撕ERP》系列,从控件重写、重绘,到框架搭建,再到部分模块实现+业务的。但是每次动手的时候,都觉得难以下手。直接从数据库设计开始吧,模块设计还没定下来,从模块设计开始吧,winform自带控件和DevExpress控件用起来布局实在太难看了。算了,从低做起吧。接着6-7年前的玩转控件系列开始,工欲善其事必先利其器!利器备好,框架搭建完毕,模块设计就是拖控件而已!              Talk is Cheap,Show me the Code!         首先,项目中新建一个窗体(用于后面的弹窗载体),按自己意愿做好布局效果,当然关于皮肤方面,大家可以应用界内很成熟的皮肤控件(具体就不列举了,避免打广告的嫌疑),或者后期自己代码实现。本篇主要介绍如何重写/重绘控件,磨自己的利器,至于利器上贴个动漫图片还是其他花里胡哨的图案,请根据自己的喜好来。大概效果如图(有洁癖的请自己细心布局):

3ad57e1e2e0f7d160bed48da93bf04a0.png

        窗体后台代码分析如下:         首先窗体集成DevExpress:
 public partial class frm_MessageBox : DevExpress.XtraEditors.XtraForm
        其余初始化动作代码如下,备注很详细就不一一列举了:
 /// /// 确定按钮/// private SimpleButton btn_OK;/// /// 取消按钮/// private SimpleButton btn_Cancel;/// /// 中止按钮/// private SimpleButton btn_Abort;/// /// 重试按钮/// private SimpleButton btn_Retry;/// /// 忽略按钮/// private SimpleButton btn_Ignore;/// /// 是按钮/// private SimpleButton btn_Yes;/// /// 否按钮/// private SimpleButton btn_No;/// /// 要在消息框中显示的文本/// private string text;/// /// 要在消息框的标题栏中显示的文本/// private string caption;/// ///  System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中显示哪些按钮/// private MessageBoxButtons buttons;/// /// System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中显示哪个图标/// private MessageBoxIcon icon;/// /// System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默认按钮。/// private MessageBoxDefaultButton defaultButton;/// /// 消息弹出框参数实体/// MessageBoxModel _MessageBoxModel = default(MessageBoxModel);
        界面初始化:
/// /// 支持修改弹出框的按钮标题描述/// /// 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;}/// /// 显示一个具有指定文本、标题、按钮、图标、默认按钮的消息框/// /// /// /// /// /// 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 OK            this.btn_OK = new SimpleButton();            this.panelButton.SuspendLayout();            //btn_OK            this.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;            else                this.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();            #endregion            break;        case MessageBoxButtons.OKCancel:            #region OKCancel            this.btn_OK = new SimpleButton();            this.btn_Cancel = new SimpleButton();            this.panelButton.SuspendLayout();            //btn_OK            this.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;            else                this.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_Cancel            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 = 1;            if (_MessageBoxModel != null)                this.btn_Cancel.Text = _MessageBoxModel.CancleButtonText;            else                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_OK.Select();            }            else            {                this.btn_Cancel.Select();            }            #endregion            break;        case MessageBoxButtons.AbortRetryIgnore:            #region AbortRetryIgnore            this.btn_Abort = new SimpleButton();            this.btn_Retry = new SimpleButton();            this.btn_Ignore = new SimpleButton();            this.panelButton.SuspendLayout();            //btn_Abort            this.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_Retry            this.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_Ignore            this.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();            }            #endregion            break;        case MessageBoxButtons.YesNoCancel:            #region YesNoCancel            this.btn_Yes = new SimpleButton();            this.btn_No = new SimpleButton();            this.btn_Cancel = new SimpleButton();            this.panelButton.SuspendLayout();            //btn_Yes            this.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;            else                this.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_No            this.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;            else                this.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;            else                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_Yes.Select();            }            else if (defaultButton == MessageBoxDefaultButton.Button2)            {                this.btn_No.Select();            }            else if (defaultButton == MessageBoxDefaultButton.Button3)            {                this.btn_Cancel.Select();            }            #endregion            break;        case MessageBoxButtons.YesNo:            #region YesNo            this.btn_Yes = new SimpleButton();            this.btn_No = new SimpleButton();            this.panelButton.SuspendLayout();            //btn_Yes            this.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;            else                this.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_No            this.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;            else                this.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();            }            #endregion            break;        case MessageBoxButtons.RetryCancel:            #region RetryCancel            this.btn_Retry = new SimpleButton();            this.btn_Cancel = new SimpleButton();            this.panelButton.SuspendLayout();            //btn_Retry            this.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_Cancel            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 = 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();            }            #endregion            break;    }    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);  }}
           以及弹窗实体类:
 /// /// 弹出框实体/// public class MessageBoxModel{    ///     /// 弹出框标题    ///     public string FormText { get; set; }    ///     /// 弹出框宽度    ///     public int FormWidth { get; set; }    ///     /// 弹出框高度    ///     public int FormHeight { get; set; }    ///     /// 弹出框消息内容    ///     public string MsgText { get; set; }    ///     /// 文字大小    ///     public int FontSize { get; set; }    ///     /// “是”按钮标题    ///     public string YesButtonText { get; set; }    ///     /// “否”按钮标题    ///     public string NoButtonText { get; set; }    ///     /// “取消”按钮标题    ///     public string CancleButtonText { get; set; }    ///     /// 弹出框类型(提示型、选择型等)    ///     public MessageBoxButtons MsgButton = MessageBoxButtons.OK;    ///     /// 弹出框中显示的图标    ///     public MessageBoxIcon MsgIcon = MessageBoxIcon.Information;    ///     /// 弹出框默认选中的按钮    ///     public MessageBoxDefaultButton MsgxDefaultButton = MessageBoxDefaultButton.Button1;
        细心的读者会发现,博主在实例弹窗实体的时候,有个语法糖:
    ///     /// 消息弹出框参数实体    ///     MessageBoxModel _MessageBoxModel = default(MessageBoxModel);
        default(T) 这是C# 7.1的关键字新用法,主要用法是默认值表达式,default对应各种类型生成默认值列表如下:
类型默认值
任何引用类型null
数值类型0
boolfalse
enum表达式(E)0生成的值,其中E是枚举标识符
struct通过如下设置生成的值:将所有值类型的字段设置为其默认值,将所有引用类型的字段设置为null    ‍
可以为null的类型HasValue属性为false且Value属性未定义的实例
罗列一下上述列表中常见类型对应的值
default(string) // nulldefault(int) // 0default(int?) // nulldefault(dynamic) // nulldefault(DateTime) // 0001/01/01 0:00:00default(DateTime?) // null
        好了,篇幅有限,具体深入了解请大家自行百度看看微软文档的解释。      以上是窗体代码解析,窗体创建好了,最后一步,创建一个实体类(暂时命名KzxMessageBox)用来调用弹窗的窗体显示,具体代码如下:
public class KzxMessageBox{    ///     /// 标题     ///     private static string caption;    ///     /// 按钮(默认“OK”)    ///     private static MessageBoxButtons buttons;    ///     /// 图标(默认“information”)    ///     private static MessageBoxIcon icon;    ///     /// 默认按钮(默认“button1”)    ///     private static MessageBoxDefaultButton defaultButton;    ///     /// 静态构造函数,初始化数据    ///     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;    }    ///     /// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框    ///     /// 文本    ///     public static DialogResult Show(string text)    {        return Show(text, buttons, icon, defaultButton, null);    }    ///     /// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框,add by zhang.jz 2019.05.10    ///     /// 文本    ///     public static DialogResult Show(string text, int pFormWidth = 0, int pFormHeight = 0)    {        return Show(text, buttons, icon, defaultButton, null, pFormWidth, pFormHeight);    }    ///     /// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框    ///     ///     ///     ///     public static DialogResult Show(string text, Form parent)    {        return Show(text, buttons, icon, defaultButton, parent);    }    ///     /// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框    ///     /// 文本    /// 按钮    ///     public static DialogResult Show(string text, MessageBoxButtons buttons)    {        return Show(text, buttons, icon, defaultButton, null);    }    ///     /// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框    ///     ///     ///     ///     ///     public static DialogResult Show(string text, MessageBoxButtons buttons, Form parent)    {        return Show(text, buttons, icon, defaultButton, parent);    }    ///     /// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框    ///     /// 文本    /// 标题    /// 按钮    /// 图标    ///     public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon)    {        return Show(text, buttons, icon, defaultButton, null);    }    ///     /// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框    ///     ///     ///     ///     ///     ///     public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon, Form parent)    {        return Show(text, buttons, icon, defaultButton, parent);    }    ///     /// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框    ///     ///     ///     ///     ///     ///     public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)    {        return Show(text, buttons, icon, defaultButton, null);    }    ///     /// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框    ///     /// 文本    /// 标题    /// 按钮    /// 图标    /// 默认按钮    /// System.Windows.Forms.DialogResult 值之一    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);  }
        一起看下效果:

d371211d3fba4acdf666650eebc0660e.png

04cf1e603c6e87f09630cb5651af8c44.png

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

5d88fcad6b09d701d4d1cf0d108fbb6e.png

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值