????????很久沒有更新博客了,本想著直接發(fā)一篇《手撕ERP》系列,從控件重寫、重繪,到框架搭建,再到部分模塊實現(xiàn)+業(yè)務的。但是每次動手的時候,都覺得難以下手。直接從數(shù)據庫設計開始吧,模塊設計還沒定下來,從模塊設計開始吧,winform自帶控件和DevExpress控件用起來布局實在太難看了。算了,從低做起吧。接著6-7年前的玩轉控件系列開始,工欲善其事必先利其器!利器備好,框架搭建完畢,模塊設計就是拖控件而已!
????
????????Talk is Cheap,Show me the Code!
????????首先,項目中新建一個窗體(用于后面的彈窗載體),按自己意愿做好布局效果,當然關于皮膚方面,大家可以應用界內很成熟的皮膚控件(具體就不列舉了,避免打廣告的嫌疑),或者后期自己代碼實現(xiàn)。本篇主要介紹如何重寫/重繪控件,磨自己的利器,至于利器上貼個動漫圖片還是其他花里胡哨的圖案,請根據自己的喜好來。大概效果如圖(有潔癖的請自己細心布局):
????????窗體后臺代碼分析如下:
????????首先窗體集成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>
/// 消息彈出框參數(shù)實體
/// </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 OKthis.btn_OK = new SimpleButton();this.panelButton.SuspendLayout();//btn_OKthis.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;elsethis.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();#endregionbreak;case MessageBoxButtons.OKCancel:#region OKCancelthis.btn_OK = new SimpleButton();this.btn_Cancel = new SimpleButton();this.panelButton.SuspendLayout();//btn_OKthis.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;elsethis.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_Cancelthis.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;elsethis.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();}#endregionbreak;case MessageBoxButtons.AbortRetryIgnore:#region AbortRetryIgnorethis.btn_Abort = new SimpleButton();this.btn_Retry = new SimpleButton();this.btn_Ignore = new SimpleButton();this.panelButton.SuspendLayout();//btn_Abortthis.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_Retrythis.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_Ignorethis.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();}#endregionbreak;case MessageBoxButtons.YesNoCancel:#region YesNoCancelthis.btn_Yes = new SimpleButton();this.btn_No = new SimpleButton();this.btn_Cancel = new SimpleButton();this.panelButton.SuspendLayout();//btn_Yesthis.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;elsethis.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_Nothis.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;elsethis.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;elsethis.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();}#endregionbreak;case MessageBoxButtons.YesNo:#region YesNothis.btn_Yes = new SimpleButton();this.btn_No = new SimpleButton();this.panelButton.SuspendLayout();//btn_Yesthis.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;elsethis.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_Nothis.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;elsethis.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();}#endregionbreak;case MessageBoxButtons.RetryCancel:#region RetryCancelthis.btn_Retry = new SimpleButton();this.btn_Cancel = new SimpleButton();this.panelButton.SuspendLayout();//btn_Retrythis.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_Cancelthis.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();}#endregionbreak;}this.Text = caption;this.lblMsg.Text = text;int moreHeight = this.lblMsg.Height - 35;if (moreHeight > 0){this.Height += moreHeight;}}
????????代碼比較簡單,就是把初始化按鈕事件和把初始化的彈窗中的按鈕添加到布局中的Panel容器里面和一些細節(jié)調整,關于方法sysClass.ssLoadMsgOrDefault目前可以不用在意,是我的通用類庫,主要是用來實現(xiàn)國際化的,后續(xù)會斷斷續(xù)續(xù)為大家介紹這塊代碼。
????????按鈕綁定事件和鍵盤響應事件代碼如下:
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;
????????細心的讀者會發(fā)現(xiàn),博主在實例彈窗實體的時候,有個語法糖:
/// <summary>/// 消息彈出框參數(shù)實體/// </summary>MessageBoxModel _MessageBoxModel = default(MessageBoxModel);
????????default(T) 這是C# 7.1的關鍵字新用法,主要用法是默認值表達式,default對應各種類型生成默認值列表如下:
類型 默認值 任何引用類型 null 數(shù)值類型 0 bool false enum 表達式(E)0生成的值,其中E是枚舉標識符 struct 通過如下設置生成的值:將所有值類型的字段設置為其默認值,將所有引用類型的字段設置為null????? 可以為null的類型 HasValue屬性為false且Value屬性未定義的實例
羅列一下上述列表中常見類型對應的值
default(string) // null
default(int) // 0
default(int?) // null
default(dynamic) // null
default(DateTime) // 0001/01/01 0:00:00
default(DateTime?) // null
????????好了,篇幅有限,具體深入了解請大家自行百度看看微軟文檔的解釋。
?????以上是窗體代碼解析,窗體創(chuàng)建好了,最后一步,創(chuàng)建一個實體類(暫時命名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>/// 靜態(tài)構造函數(shù),初始化數(shù)據/// </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);}}}
}
????????代碼比較簡單,創(chuàng)建一個公共類,以及類型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);}
????????一起看下效果:
????????最后,由于后續(xù)所有重寫/重繪控件都在同一個項目使用,而且Dev系統(tǒng)引用文件較多,壓縮后源碼文件仍然很大,如果有需要源碼的朋友,可以微信公眾號聯(lián)系博主,源碼可以免費贈予~!有疑問的也可以CALL我一起探討,最最后,如果覺得本篇博文對您或者身邊朋友有幫助的,麻煩點個關注!贈人玫瑰,手留余香,您的支持就是我寫作最大的動力,感謝您的關注,期待和您一起探討!再會!
????
????????
????????
總結
以上是生活随笔 為你收集整理的玩转控件:重写/重绘Dev中MessageBox弹窗控件 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。