????????很久沒(méi)有更新博客了,本想著直接發(fā)一篇《手撕ERP》系列,從控件重寫(xiě)、重繪,到框架搭建,再到部分模塊實(shí)現(xiàn)+業(yè)務(wù)的。但是每次動(dòng)手的時(shí)候,都覺(jué)得難以下手。直接從數(shù)據(jù)庫(kù)設(shè)計(jì)開(kāi)始吧,模塊設(shè)計(jì)還沒(méi)定下來(lái),從模塊設(shè)計(jì)開(kāi)始吧,winform自帶控件和DevExpress控件用起來(lái)布局實(shí)在太難看了。算了,從低做起吧。接著6-7年前的玩轉(zhuǎn)控件系列開(kāi)始,工欲善其事必先利其器!利器備好,框架搭建完畢,模塊設(shè)計(jì)就是拖控件而已!
????
????????Talk is Cheap,Show me the Code!
????????首先,項(xiàng)目中新建一個(gè)窗體(用于后面的彈窗載體),按自己意愿做好布局效果,當(dāng)然關(guān)于皮膚方面,大家可以應(yīng)用界內(nèi)很成熟的皮膚控件(具體就不列舉了,避免打廣告的嫌疑),或者后期自己代碼實(shí)現(xiàn)。本篇主要介紹如何重寫(xiě)/重繪控件,磨自己的利器,至于利器上貼個(gè)動(dòng)漫圖片還是其他花里胡哨的圖案,請(qǐng)根據(jù)自己的喜好來(lái)。大概效果如圖(有潔癖的請(qǐng)自己細(xì)心布局):
????????窗體后臺(tái)代碼分析如下:
????????首先窗體集成DevExpress:
public partial class frm_MessageBox : DevExpress.XtraEditors.XtraForm
????????其余初始化動(dòng)作代碼如下,備注很詳細(xì)就不一一列舉了:
/// <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>
/// 要在消息框的標(biāo)題欄中顯示的文本
/// </summary>
private string caption;/// <summary>
/// System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中顯示哪些按鈕
/// </summary>
private MessageBoxButtons buttons;/// <summary>
/// System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中顯示哪個(gè)圖標(biāo)
/// </summary>
private MessageBoxIcon icon;/// <summary>
/// System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默認(rèn)按鈕。
/// </summary>
private MessageBoxDefaultButton defaultButton;/// <summary>
/// 消息彈出框參數(shù)實(shí)體
/// </summary>
MessageBoxModel _MessageBoxModel = default(MessageBoxModel);
????????界面初始化:
/// <summary>
/// 支持修改彈出框的按鈕標(biāo)題描述
/// </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>
/// 顯示一個(gè)具有指定文本、標(biāo)題、按鈕、圖標(biāo)、默認(rèn)按鈕的消息框
/// </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;}}
????????代碼比較簡(jiǎn)單,就是把初始化按鈕事件和把初始化的彈窗中的按鈕添加到布局中的Panel容器里面和一些細(xì)節(jié)調(diào)整,關(guān)于方法sysClass.ssLoadMsgOrDefault目前可以不用在意,是我的通用類(lèi)庫(kù),主要是用來(lái)實(shí)現(xiàn)國(guó)際化的,后續(xù)會(huì)斷斷續(xù)續(xù)為大家介紹這塊代碼。
????????按鈕綁定事件和鍵盤(pán)響應(yīng)事件代碼如下:
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);}}
? ?????????以及彈窗實(shí)體類(lèi):
/// <summary>
/// 彈出框?qū)嶓w
/// </summary>
public class MessageBoxModel
{/// <summary>/// 彈出框標(biāo)題/// </summary>public string FormText { get; set; }/// <summary>/// 彈出框?qū)挾?// </summary>public int FormWidth { get; set; }/// <summary>/// 彈出框高度/// </summary>public int FormHeight { get; set; }/// <summary>/// 彈出框消息內(nèi)容/// </summary>public string MsgText { get; set; }/// <summary>/// 文字大小/// </summary>public int FontSize { get; set; }/// <summary>/// “是”按鈕標(biāo)題/// </summary>public string YesButtonText { get; set; }/// <summary>/// “否”按鈕標(biāo)題/// </summary>public string NoButtonText { get; set; }/// <summary>/// “取消”按鈕標(biāo)題/// </summary>public string CancleButtonText { get; set; }/// <summary>/// 彈出框類(lèi)型(提示型、選擇型等)/// </summary>public MessageBoxButtons MsgButton = MessageBoxButtons.OK;/// <summary>/// 彈出框中顯示的圖標(biāo)/// </summary>public MessageBoxIcon MsgIcon = MessageBoxIcon.Information;/// <summary>/// 彈出框默認(rèn)選中的按鈕/// </summary>public MessageBoxDefaultButton MsgxDefaultButton = MessageBoxDefaultButton.Button1;
????????細(xì)心的讀者會(huì)發(fā)現(xiàn),博主在實(shí)例彈窗實(shí)體的時(shí)候,有個(gè)語(yǔ)法糖:
/// <summary>/// 消息彈出框參數(shù)實(shí)體/// </summary>MessageBoxModel _MessageBoxModel = default(MessageBoxModel);
????????default(T) 這是C# 7.1的關(guān)鍵字新用法,主要用法是默認(rèn)值表達(dá)式,default對(duì)應(yīng)各種類(lèi)型生成默認(rèn)值列表如下:
類(lèi)型 默認(rèn)值 任何引用類(lèi)型 null 數(shù)值類(lèi)型 0 bool false enum 表達(dá)式(E)0生成的值,其中E是枚舉標(biāo)識(shí)符 struct 通過(guò)如下設(shè)置生成的值:將所有值類(lèi)型的字段設(shè)置為其默認(rèn)值,將所有引用類(lèi)型的字段設(shè)置為null????? 可以為null的類(lèi)型 HasValue屬性為false且Value屬性未定義的實(shí)例
羅列一下上述列表中常見(jiàn)類(lèi)型對(duì)應(yīng)的值
default(string) // null
default(int) // 0
default(int?) // null
default(dynamic) // null
default(DateTime) // 0001/01/01 0:00:00
default(DateTime?) // null
????????好了,篇幅有限,具體深入了解請(qǐng)大家自行百度看看微軟文檔的解釋。
?????以上是窗體代碼解析,窗體創(chuàng)建好了,最后一步,創(chuàng)建一個(gè)實(shí)體類(lèi)(暫時(shí)命名KzxMessageBox)用來(lái)調(diào)用彈窗的窗體顯示,具體代碼如下:
public class KzxMessageBox
{/// <summary>/// 標(biāo)題 /// </summary>private static string caption;/// <summary>/// 按鈕(默認(rèn)“OK”)/// </summary>private static MessageBoxButtons buttons;/// <summary>/// 圖標(biāo)(默認(rèn)“information”)/// </summary>private static MessageBoxIcon icon;/// <summary>/// 默認(rèn)按鈕(默認(rèn)“button1”)/// </summary>private static MessageBoxDefaultButton defaultButton;/// <summary>/// 靜態(tài)構(gòu)造函數(shù),初始化數(shù)據(jù)/// </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>/// 顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)和默認(rèn)按鈕的消息框/// </summary>/// <param name="text">文本</param>/// <returns></returns>public static DialogResult Show(string text){return Show(text, buttons, icon, defaultButton, null);}/// <summary>/// 顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)和默認(rèn)按鈕的消息框,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>/// 顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)和默認(rèn)按鈕的消息框/// </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>/// 顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)和默認(rèn)按鈕的消息框/// </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>/// 顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)和默認(rèn)按鈕的消息框/// </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>/// 顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)和默認(rèn)按鈕的消息框/// </summary>/// <param name="text">文本</param>/// <param name="caption">標(biāo)題</param>/// <param name="buttons">按鈕</param>/// <param name="icon">圖標(biāo)</param>/// <returns></returns>public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon){return Show(text, buttons, icon, defaultButton, null);}/// <summary>/// 顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)和默認(rèn)按鈕的消息框/// </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>/// 顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)和默認(rèn)按鈕的消息框/// </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>/// 顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)和默認(rèn)按鈕的消息框/// </summary>/// <param name="text">文本</param>/// <param name="caption">標(biāo)題</param>/// <param name="buttons">按鈕</param>/// <param name="icon">圖標(biāo)</param>/// <param name="defaultButton">默認(rèn)按鈕</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);}}}
}
????????代碼比較簡(jiǎn)單,創(chuàng)建一個(gè)公共類(lèi),以及類(lèi)型Messagebox的方法重載而已!
????????最后一步,調(diào)用:
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ù)所有重寫(xiě)/重繪控件都在同一個(gè)項(xiàng)目使用,而且Dev系統(tǒng)引用文件較多,壓縮后源碼文件仍然很大,如果有需要源碼的朋友,可以微信公眾號(hào)聯(lián)系博主,源碼可以免費(fèi)贈(zèng)予~!有疑問(wèn)的也可以CALL我一起探討,最最后,如果覺(jué)得本篇博文對(duì)您或者身邊朋友有幫助的,麻煩點(diǎn)個(gè)關(guān)注!贈(zèng)人玫瑰,手留余香,您的支持就是我寫(xiě)作最大的動(dòng)力,感謝您的關(guān)注,期待和您一起探討!再會(huì)!
????
????????
????????
總結(jié)
以上是生活随笔 為你收集整理的玩转控件:重写/重绘Dev中MessageBox弹窗控件 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。