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

2019/04/10 10:10
阅读数 37

  很久没有更新博客了,本想着直接发一篇《手撕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 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);
  }

}

 以及弹窗实体类:

/// <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对应各种类型生成默认值列表如下:

 

 

 

 

 

 

 

 

 

 

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

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我一起探讨,最最后,如果觉得本篇博文对您或者身边朋友有帮助的,麻烦点个关注!赠人玫瑰,手留余香,您的支持就是我写作最大的动力,感谢您的关注,期待和您一起探讨!再会!

原文出处:https://www.cnblogs.com/axing/p/12521589.html

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部