1. 程式人生 > >玩轉控制元件:重寫/重繪Dev中MessageBox彈窗控制元件

玩轉控制元件:重寫/重繪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 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我一起探討,最最後,如果覺得本篇博文對您或者身邊朋友有幫助的,麻煩點個關注!贈人玫瑰,手留餘香,您的支援就是我寫作最大的動力,感謝您的關注,期待和您一起探討!再會!