很久没有更新博客了,本想着直接发一篇《手撕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(, );
this.btn_OK.Location = new Point(pannelLength - , );
this.btn_OK.TabIndex = ;
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(, , , );
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(, );
this.btn_OK.Location = new Point(pannelLength - , );
this.btn_OK.TabIndex = ;
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(, , , );
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(, );
this.btn_Cancel.Location = new Point(pannelLength - , );
this.btn_Cancel.TabIndex = ;
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(, , , );
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(, );
this.btn_Abort.Location = new Point(pannelLength - , );
this.btn_Abort.TabIndex = ;
this.btn_Abort.Text = sysClass.ssLoadMsgOrDefault("SYS000003", "中止(A)");//中止(A)
this.btn_Abort.Margin = new Padding(, , , );
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(, );
this.btn_Retry.Location = new Point(pannelLength - , );
this.btn_Retry.TabIndex = ;
this.btn_Retry.Text = sysClass.ssLoadMsgOrDefault("SYS000004", "重试(R)");//重试(R)
this.btn_Retry.Margin = new Padding(, , , );
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(, );
this.btn_Ignore.Location = new Point(pannelLength - , );
this.btn_Ignore.TabIndex = ;
this.btn_Ignore.Text = sysClass.ssLoadMsgOrDefault("SYS000005", "忽略(I)");//忽略(I)
this.btn_Ignore.Margin = new Padding(, , , );
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(, );
this.btn_Yes.Location = new Point(pannelLength - , );
this.btn_Yes.TabIndex = ;
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(, , , );
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(, );
this.btn_No.Location = new Point(pannelLength - , );
this.btn_No.TabIndex = ;
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(, , , );
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(, );
this.btn_Cancel.Location = new Point(pannelLength - , );
this.btn_Cancel.TabIndex = ;
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(, , , );
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(, );
this.btn_Yes.Location = new Point(pannelLength - , );
this.btn_Yes.TabIndex = ;
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(, , , );
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(, );
this.btn_No.Location = new Point(pannelLength - , );
this.btn_No.TabIndex = ;
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(, , , );
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(, );
this.btn_Retry.Location = new Point(pannelLength - , );
this.btn_Retry.TabIndex = ; this.btn_Retry.Text = sysClass.ssLoadMsgOrDefault("SYS000004", "重试(R)");//重试(R)
this.btn_Retry.Margin = new Padding(, , , );
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(, );
this.btn_Cancel.Location = new Point(pannelLength - , );
this.btn_Cancel.TabIndex = ;
this.btn_Cancel.Text = sysClass.ssLoadMsgOrDefault("SYS000002", "取消(C)");//取消(C)
this.btn_Cancel.Margin = new Padding(, , , );
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 - ;
if (moreHeight > )
{
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, , );
} }

以及弹窗实体类:

/// <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) //
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 == )
{
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 = , int pFormHeight = )
{
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 = , int pFormHeight = )
{
using (frm_MessageBox frm = new frm_MessageBox(text, caption, buttons, icon, defaultButton))
{
if (parent == null || parent.IsDisposed)
{
frm.StartPosition = FormStartPosition.CenterScreen;
if (pFormWidth != ) frm.Width = pFormWidth;
if (pFormHeight != ) frm.Height = pFormHeight; return frm.ShowDialog();
}
else
{
frm.StartPosition = FormStartPosition.CenterParent;
if (pFormWidth != ) frm.Width = pFormWidth;
if (pFormHeight != ) 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 != ) frm.Width = pMessageBoxModel.FormWidth;
if (pMessageBoxModel.FormHeight != ) frm.Height = pMessageBoxModel.FormHeight; return frm.ShowDialog();
}
else
{
frm.StartPosition = FormStartPosition.CenterParent;
if (pMessageBoxModel.FormWidth != ) frm.Width = pMessageBoxModel.FormWidth;
if (pMessageBoxModel.FormHeight != ) 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我一起探讨,最最后,如果觉得本篇博文对您或者身边朋友有帮助的,麻烦点个关注!赠人玫瑰,手留余香,您的支持就是我写作最大的动力,感谢您的关注,期待和您一起探讨!再会!

玩转控件:重写/重绘Dev中MessageBox弹窗控件的更多相关文章

  1. 玩转控件:重绘DEVEXPRESS中DateEdit控件 —— 让DateEdit支持只选择年月 (提供源码下载)

      前言 上一篇博文<玩转控件:重绘ComboBox —— 让ComboBox多列显示>中,根据大家的回馈,ComboBox已经支持筛选了,更新见博文最后最后最后面.   奇葩 这两天遇到 ...

  2. 《ASP.NET1200例》解决母版页报错“内容控件必须是内容页中的顶级控件,或是引用母版页的嵌套母版页。”

    VS2005下,添加了母版页这个控件,我们可以讲N个页面中共同的部分放在母版页来实现,并让WEB窗体集成自我们的母版页,就可以让我们的站点具有统一的风格了.在VS2005SP1之前的版本中,我们只能创 ...

  3. Winfrom中From控件的重绘

    重绘目的: 1. 满足非默认主题下的标题栏样式 2. 在保留停靠功能的同时进行重绘. 代码如下: public partial class FormEx: Form { public FormEx() ...

  4. DEV中的TreeList控件应用的一个小效果实现——个人总结

    我使用最多的DEV控件就是这个TreeList啦,当然用好它很不简单,如果用好它,能做出很精彩的树形层次结构图.TreeList控件很强大,以至于你看DEV自带的DEMO,也得浪费你很长时间应用.DE ...

  5. DEV中dx:ASPxPopupControl 控件的使用(在窗口关闭或隐藏时,清楚文本框中的内容)

    //在窗口关闭或隐藏时,清楚文本框中的内容(核心代码) function(s, e) { ASPxClientEdit.ClearGroup('entryGroup'); } <asp:Cont ...

  6. dev中 使用一些控件后,窗体屏蔽右键某些菜单

    使用Ribbon时,ribbonControl1.ShowToolbarCustomizeItem=false; 使用LayoutControl时,layoutControl1.AllowCustom ...

  7. 如何遍历tabcontrol控件的所有的tabpage中的所有控件

    foreach(Control c in tabControl1.TabPages)这个循环的意思是说,遍历tabControl1中所有的TabPages,TabPages是包含在tabControl ...

  8. 玩转控件:扩展Dev中SimpleButton

    何为扩展,顾名思义,就是在原有控件属性.事件的基础上拓展自己需要或实用的属性.事件等等.或者可以理解为,现有的控件已经不能完全满足我(的需求)了.好的扩展会使控件更加完善,实用,好用.不好的扩展,说白 ...

  9. 玩转控件:对Dev中GridControl控件的封装和扩展

    又是一年清明节至,细雨绵绵犹如泪光,树叶随风摆动.... 转眼间,一年又过去了三分之一,疫情的严峻让不少企业就跟清明时节的树叶一样,摇摇欲坠.裁员的裁员,降薪的降薪,996的996~~说起来都是泪,以 ...

随机推荐

  1. DOCKER中centos7的中文支持

    直接编写看下能否改变成识别中文字体 写到你的~/.bashrc里吧,然后重启终端(我写的是英文的啊,改成你要的) export LC_ALL=en_US.UTF-8 export LANGUAGE=e ...

  2. make的工作方式

    摘自<跟我一起写Makefile> GUN的make工作时的执行步骤如下: 1)读入所有的Makefile. 2)读入被include的其他Makeifle. 3)初始化文件中的变量. 4 ...

  3. IOC @Autowired/@Resource/@Qulified的用法实例

    首先要知道另一个东西,default-autowire,它是在xml文件中进行配置的,可以设置为byName.byType.constructor和autodetect:比如byName,不用显式的在 ...

  4. java 使用poi导入Excel通用方法

    需要的jar: [XML] 纯文本查看 复制代码 ? 1 2 3 4 5 <dependency>             <groupId>org.apache.poi< ...

  5. 使用java列举所有给定数组中和为定值的组合

    import java.util.Arrays; public class SolveProb { ]; ;// 记录当前 public SolveProb() { } public static v ...

  6. 第一次安装vs2010无法运行程序,系统找不到exe文件,LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    最近在看数据结构的一些书籍,怎奈代码是c写的,所以安装一个编译器vs2010来测试代码,但是建完文件后编译ok,f5却出现错误:无法启动程序,系统找不到指定文件.上网找了一些解决办法,但是仍然无法解决 ...

  7. Tozan and Gezan(x*y<a*b)

    E - Tozan and Gezan Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement Yo ...

  8. HF Java Chap 1

    介绍了java的工作方式以及几个有趣的小程序 Java的工作模式 大体来说有四个步骤: 源代码 编译器 编译器的输出 Java虚拟机 源代码 这是我们程序员接触到的部分.根据我们面临的问题,编写一个符 ...

  9. hexo文章编写部分语法总结以及hexo使用

    一.hexo的使用 1.1 新建一篇文章 1 $ hexo new [layout] <title> 1.2. 生成静态文件 1 $ hexo generate 可简写为 1 $ hexo ...

  10. 由uploadfive看servlet

    一.uploadfive的使用 上传工具是程序设计中最常用的功能,其中,uploadfive插件使用比较多,此处该插件进行文件的上传操作.该插件是基于HTML5的,因此PC端和移动端都可以使用. 使用 ...