<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      【Talk is cheap. Show me the code.】 公眾號如有回復不及時的,麻煩點擊聯系關于我-聯系博主,微信我。謝謝!
      老帥哥

      Stephen-kzx的博客

      【Talk is cheap. Show me the code.】【公眾號如有回復不及時的,麻煩點擊聯系關于我-聯系博主,微信我。謝謝!】

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

      posted @ 2020-03-19 00:55  何以解憂唯有*碼  閱讀(1286)  評論(3)    收藏  舉報
      主站蜘蛛池模板: av一区二区中文字幕| 中文字幕乱码亚洲无线三区| 一区二区中文字幕久久| 在线无码免费的毛片视频| 亚欧洲乱码视频一二三区| 精品91在线| 亚洲综合色婷婷中文字幕| 18国产午夜福利一二区| 国产亚洲精品国产福APP| 曲阜市| 久久久久夜夜夜精品国产| 日本成人午夜一区二区三区| 激情自拍校园春色中文| 公喝错春药让我高潮| 99精品国产成人一区二区| 丰满的少妇被猛烈进入白浆| 资兴市| 国内自拍偷拍一区二区三区| 免费无码又爽又刺激高潮虎虎视频| 艳妇臀荡乳欲伦69调教视频| 色呦呦九九七七国产精品| 成人av午夜在线观看| 无码帝国www无码专区色综合| 国产av仑乱内谢| 色偷偷中文在线天堂中文| 在线A毛片免费视频观看| 国产精品午夜爆乳美女视频| 北岛玲中文字幕人妻系列| 精品国产伦理国产无遮挡| 国产精品一区二区AV| 国内自拍av在线免费| 亚洲一区二区av免费| 欧美国产综合视频| 欧美日韩亚洲国产| 国产剧情福利一区二区麻豆| 中文熟妇人妻av在线| 亚洲韩国精品无码一区二区三区| 日韩午夜福利片段在线观看| 精品无码人妻一区二区三区| 亚洲成年av天堂动漫网站| 亚洲中文字幕精品无人区|