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

窗体后台代码分析如下:首先窗体集成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
bool false
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);  }

一起看下效果:

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

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

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

    很久没有更新博客了,本想着直接发一篇<手撕ERP>系列,从控件重写.重绘,到框架搭建,再到部分模块实现+业务的.但是每次动手的时候,都觉得难以下手.直接从数据库设计开始吧,模块设计还没定下 ...

  2. C# .NET 6.0已经取消了Framework 4.8的WebBrowser控件,几款NuGet中的浏览器控件介绍

    即使在C# .NET 6.0..NET 7.0在选择工具箱项COM组件添加Microsoft Web Browser,工具箱也没有WebBrowser控件. WebBrowser控件: WebBrow ...

  3. 中达优控触摸屏编程视频教程_YKBuilder中文版(中达优控触摸屏编程系统)V5.0.300 正式版...

    YKBuilder中文版(中达优控触摸屏编程系统)是中达优控YKHMI系列人机界面的集成开发环境软件包.非常专业的触摸屏组态编程软件,可以通过对现场数据的采集处理,YKBuilder以动画显示.报警处 ...

  4. python设计程序输出一个带文本框和图像的窗体_设计一个文本框 用户在文本框中输入什么 窗体标题就同步显示什么 怎么设计啊...

    展开全部 要求 1.掌握算法的基本概念. 2.掌握基本数据结构及其操作. 3.掌握基本排序和查找算法. 4.掌握逐e68a8462616964757a686964616f31333264626533步 ...

  5. 5单个编译总会编译全部_玩转Android10(五)源码编译开发中常用命令

    源码开发编译中,熟练掌握常用命令,可以提高开发工作效率.Android源码中,将相关的命令分为如下几类: 1.初始化源码编译环境 初始化编译环境,为后续提供如lunch.make.xxgrep.god ...

  6. 从文件中读出数据显示在表格中_玩转表格:如何在Word表格中进行数据计算?...

    Word文档中的表格也可以进行数据运算的. 例如,要对表格中第1季度的数据进行求和计算时,具体操作方法如下. 1 第1步:将光标置于要输入合计值的单元格中,切换到[表格工具]栏中的[布局]选项卡中,在 ...

  7. MFC 对Button控件的重绘方法(多种)

    EosPro 之MFC 对 Button的绘制 自绘按钮 一.位图按钮的实现方法:首先,我们创建一个基于对话框的应用程序CmyDialogA.MFC的CBitmapButton类,这也是最简单的功能最 ...

  8. 使用vs2008c++语言开发activex控件教程,VS2008 在IE中 调试 ActiveX控件

    不知道是不是微软的粗心大意,VS2008中竟然没有ActiveX控件测试容器. 幸好需要在IE中测试控件,就不需要那个测试容器啦. 1. 生产测试控件的HTML: Test CLASSID=" ...

  9. qt5中重绘工具栏_Qt ------ QPainter 和控件组件的重绘

    使用 QPainter 修改 QPaintDevice 的子类,如果 QPaintDevice 的子类也是 QWidget 的子类,比如自定义QWidget子类.QLabel等,需要把 QPainte ...

最新文章

  1. xubuntu18.04安装Google拼音输入法
  2. 算法导论一个让人很不爽的地方
  3. android studio 单步调试jni,android studio实现Jni单步调试-标准配置教程
  4. 图像处理(一)——使用matlab放缩图像
  5. CNN 与 LSTM 模型复杂度分析
  6. 上传项目到gitHub,上传报错和删除gitHub上的项目
  7. python多级网址爬取_python-29:多级页面爬取源码
  8. python显示界面后1秒自动隐藏_使用Python+Qt时解决QTreeWidget中的内容超出边界后自动隐藏的问题...
  9. 搭建nfs共享存储服务之三客户端配置
  10. 当今流行的HTML5技术门户网站——HTML5星空
  11. html滚动字幕制作教程,抖音上的字幕怎么弄的?滚动字幕制作方法教程[多图]
  12. 《Processing SPARQL queries over distributed RDF graphs》——读书笔记
  13. 头歌HTML实训笔录
  14. 怎么给HTML文件加背景,设置文件夹背景,如何设置文件夹背景颜色
  15. SudokuGame 记软工第二次作业
  16. 国家强制性产品认证实施规则汇总(更新日期:2020年12月)
  17. S7-1200变量地址
  18. jQuery学习小征途
  19. 第一章 MEMS惯性器件-加速度计误差分析
  20. Google Earth 4.0官方正式版发布

热门文章

  1. php 判断类存在,PHP怎么判断类是否存在
  2. Caused by java.lang.ClassNotFoundException javax.xml.bind.ValidationException异常
  3. Handler消息传递机制(一)
  4. 05--MySQL自学教程:DDL(Data Definition Language:数据库定义语言)操作数据库中的表(二)
  5. struts2 页面取值_struts2.1如何在页面上取值?
  6. android 支付宝月账单 统计图_2019年你抢了多少微信红包?花了多少?(微信账单查看教程)...
  7. java项目打成jar和war_maven打包web项目时同时打包为war和jar文件的方法
  8. java 接口 返回值_java api返回值的标准化详解
  9. python的垃圾处理机制_Python语法入门之垃圾回收机制
  10. C语言去括号编程题,数据结构课件.ppt