实现自定义消息对话框控件MsgBox简介

实现自定义消息对话框控件MsgBox简介

2011-10-23 来自:CNBLOGS 字体大小:【    
  • 摘要:本文介绍实现自定义消息对话框控件MsgBox,并提供完整的示例代码供参考。

  本文主要介绍的是一个自己编写的,类似MessageBox消息对话框的自定义控件。这个控件不是重写MessageBox类,完全是参照MessageBox的功能,并在此的基础上砍掉了一部分功能,如IOwer;但是,因为有源码,我们自己完全可以定义这个消息对话框显示的文字,图片,按钮,尺寸等等。极大的方便了我们在特殊情况下,需要一些另类的消息对话框。

  首先,我们来看一下这个自定义控件的访问接口:MsgBox.cs,这个类提供了一系列重载的静态方法Show,并通过不同的参数个数和类型,控制消息对话框外观和内容,代码如下:

 1 /// <summary>
 2     /// 显示包含文本,按钮,图标内的消息对话框
 3     /// </summary>
 4     public class MsgBox
 5     {
 6         // 参数为空时的默认值
 7         private static string defaultCaption = string.Empty;
 8         private static MsgBoxButtons defaultButtons = MsgBoxButtons.OK;
 9         private static MsgBoxIcon defaultIcon = MsgBoxIcon.None;
10         /// <summary>
11         /// 消息对话的结果
12         /// </summary>
13         internal static MsgBoxDialogResult result = MsgBoxDialogResult.None;
14         /// <summary>
15         /// 显示可包括文本,符号和按钮的消息框
16         /// </summary>
17         /// <param name="text">文本</param>
18         /// <returns>结果</returns>
19         public static MsgBoxDialogResult Show(string text)
20         {
21             return Show(text, defaultCaption, defaultButtons, defaultIcon);
22         }
23         /// <summary>
24         /// 显示可包括文本,符号和按钮的消息框
25         /// </summary>
26         /// <param name="text">文本</param>
27         /// <param name="caption">标题</param>
28         /// <returns>结果</returns>
29         public static MsgBoxDialogResult Show(string text,string caption)
30         {
31             return Show(text, caption, defaultButtons, defaultIcon);
32         }
33         
34         /// <summary>
35         /// 显示可包括文本,符号和按钮的消息框
36         /// </summary>
37         /// <param name="text">文本</param>
38         /// <param name="caption">标题</param>
39         /// <param name="buttons">按钮</param>
40         /// <returns>结果</returns>
41         public static MsgBoxDialogResult Show(string text, string caption, MsgBoxButtons buttons)
42         {
43             return Show(text, caption, buttons, defaultIcon);
44         }
45         /// <summary>
46         /// 显示可包括文本,符号和按钮的消息框
47         /// </summary>
48         /// <param name="text">文本</param>
49         /// <param name="caption">标题</param>
50         /// <param name="buttons">按钮</param>
51         /// <param name="icon">图标</param>
52         /// <returns>结果</returns>
53         public static MsgBoxDialogResult Show(string text, string caption, MsgBoxButtons buttons, MsgBoxIcon icon)
54         {
55             //初始化消息框
56             DialogForm dialogForm = new DialogForm(text, caption, buttons, icon);
57             //弹出消息框
58             dialogForm.ShowDialog();
59             //返回结果
60             return result;
61         }
62     }

然后,我们来看一下提供消息对话框按钮,图片,结果等信息常量的Enum:

  1 // 摘要:
  2     //     指定若干常数,用以定义 System.Windows.Forms.MessageBox 上将显示按钮的文本
  3     [ComVisible(true)]
  4     internal class MsgBoxButtonText
  5     {
  6         // 摘要:
  7         //     消息框包含“确定”按钮的文本。
  8         public const string OK = "确认";
  9         // 摘要:
 10         //     消息框包含“是”按钮的文本。
 11         public const string Yes = "";
 12         // 摘要:
 13         //     消息框包含“否”按钮的文本。
 14         public const string No = "";
 15         // 摘要:
 16         //     消息框包含“取消”按钮的文本。
 17         public const string Cancel = "取消";
 18         // 摘要:
 19         //     消息框包含“重试”按钮的文本。
 20         public const string Retry = "重试";
 21         // 摘要:
 22         //     消息框包含“忽略”按钮的文本。
 23         public const string Ignore = "忽略";
 24         // 摘要:
 25         //     消息框包含“终止”按钮的文本。
 26         public const string Abort = "终止";
 27         // 摘要:
 28         //     消息框包含“复制”按钮的文本。
 29         public const string Copy = "复制";
 30     }
 31 
 32     // 摘要:
 33     //     指定标识符以指示对话框的返回值。
 34     [ComVisible(true)]
 35     public enum MsgBoxDialogResult
 36     {
 37         // 摘要:
 38         //     从对话框返回了 Nothing。这表明有模式对话框继续运行。
 39         None = 0,
 40         //
 41         // 摘要:
 42         //     对话框的返回值是 OK(通常从标签为“确定”的按钮发送)。
 43         OK = 1,
 44         //
 45         // 摘要:
 46         //     对话框的返回值是 Cancel(通常从标签为“取消”的按钮发送)。
 47         Cancel = 2,
 48         //
 49         // 摘要:
 50         //     对话框的返回值是 Abort(通常从标签为“中止”的按钮发送)。
 51         Abort = 3,
 52         //
 53         // 摘要:
 54         //     对话框的返回值是 Retry(通常从标签为“重试”的按钮发送)。
 55         Retry = 4,
 56         //
 57         // 摘要:
 58         //     对话框的返回值是 Ignore(通常从标签为“忽略”的按钮发送)。
 59         Ignore = 5,
 60         //
 61         // 摘要:
 62         //     对话框的返回值是 Yes(通常从标签为“是”的按钮发送)。
 63         Yes = 6,
 64         //
 65         // 摘要:
 66         //     对话框的返回值是 No(通常从标签为“否”的按钮发送)。
 67         No = 7,
 68     }
 69     
 70 
 71     // 摘要:
 72     //     指定定义哪些信息要显示的常数。
 73     [ComVisible(true)]
 74     public enum MsgBoxIcon
 75     {
 76         /// <summary>
 77         /// 78         /// </summary>
 79         None = 0,
 80         /// <summary>
 81         /// 错误
 82         /// </summary>
 83         Error = 1,
 84         /// <summary>
 85         /// 问题
 86         /// </summary>
 87         Question = 2,
 88         /// <summary>
 89         /// 停止
 90         /// </summary>
 91         Stop = 3,
 92         /// <summary>
 93         /// 警告
 94         /// </summary>
 95         Warning = 4,
 96         /// <summary>
 97         /// 信息提示
 98         /// </summary>
 99         Information = 5,
100         /// <summary>
101         /// 崩溃
102         /// </summary>
103         SysBreak = 6,
104         /// <summary>
105         /// 空值
106         /// </summary>
107         NoRecord = 7,
108     }
109 
110     // 摘要:
111     //     指定若干常数,用以定义 System.Windows.Forms.MessageBox 上将显示哪些按钮
112     [ComVisible(true)]
113     public enum MsgBoxButtons
114     {
115 
116         // 摘要:
117         //     消息框包含“确定”按钮。
118         OK = 0,
119         //
120         // 摘要:
121         //     消息框包含“确定”和“取消”按钮。
122         OKCancel = 1,
123         //
124         // 摘要:
125         //     消息框包含“中止”、“重试”和“忽略”按钮。
126         AbortRetryIgnore = 2,
127         //
128         // 摘要:
129         //     消息框包含“是”、“否”和“取消”按钮。
130         YesNoCancel = 3,
131         //
132         // 摘要:
133         //     消息框包含“是”和“否”按钮。
134         YesNo = 4,
135         //
136         // 摘要:
137         //     消息框包含“重试”和“取消”按钮。
138         RetryCancel = 5,
139         //
140         // 摘要:
141         //     消息框包含“确认”和“复制”按钮。
142         OKCopy = 6,
143     }

 

  下面,我们来看最重要的一个部分,消息对话框:DialogForm窗体:

  在这个窗体中,我们定义各种控件,并给他们的属性赋上有意义且正确的值,比如:要显示消息的内容,那么就需要Lable或TextBox控件,并设置它们的Font,Localtion,Text,Name等属性;要获取选择的结果,需要一个或数个按钮控件;在特定条件下,某个控件的某个事件是否会触发另外一个事件等等。设置好某一个新建的控件的所有属性后,将其添加到一个容器中;所有控件都创建并添加到容器中后,再将窗体显示出来,这就是我们看到的消息对话框。下面的代码是创建按钮的方法:

  1 // <summary>
  2         /// 设置按钮
  3         /// </summary>
  4         /// <param name="button">需产生的按钮组</param>
  5         private void SetButton(MsgBoxButtons buttons)
  6         {
  7             int width = this.pnlButtonContainer.Width / 2;
  8             int height = this.pnlButtonContainer.Height;
  9             int x = 0, y = 0;
 10             switch (buttons)
 11             {
 12                 case MsgBoxButtons.OK:
 13                     {
 14                       
 15                         x=width- 30;
 16                         y=height- 30;
 17                         //“确认”按钮
 18                         Button btn1 = new Button();
 19                         btn1.Size = new Size(75, 23);
 20                         btn1.Font = new Font("宋体", 9);
 21                         btn1.Location = new Point(x,y);
 22                         btn1.Name = "btnOK";
 23                         btn1.Text = MsgBoxButtonText.OK;
 24                         btn1.Click += btnOK_Click;
 25                         pnlButtonContainer.Controls.Add(btn1);
 26                         
 27                     }
 28                     break;
 29                 case MsgBoxButtons.OKCancel:
 30                     {
 31                         x=width- 80;
 32                         y=height- 30;
 33                         //“确认”按钮
 34                         Button btn1 = new Button();
 35                         btn1.Size = new Size(75, 23);
 36                         btn1.Font = new Font("宋体", 9);
 37                         btn1.Location = new Point(x, y);
 38                         btn1.Name = "btnOK";
 39                         btn1.Text = MsgBoxButtonText.OK;
 40                         btn1.Click += btnOK_Click;
 41                         pnlButtonContainer.Controls.Add(btn1);
 42 
 43 
 44                         x=width+ 5;
 45                         y=height- 30;
 46                         //“取消”按钮
 47                         Button btn2 = new Button();
 48                         btn2.Size = new Size(75, 23);
 49                         btn2.Font = new Font("宋体", 9);
 50                         btn2.Location = new Point(x, y);
 51                         btn2.Name = "btnCancel";
 52                         btn2.Text = MsgBoxButtonText.Cancel;
 53                         btn2.Click += btnCancel_Click;
 54                         pnlButtonContainer.Controls.Add(btn2);
 55                                                 
 56                     }
 57                     break;
 58                 case MsgBoxButtons.YesNo:
 59                     {
 60                         x=width- 80;
 61                         y=height- 30;
 62                         //“是”按钮
 63                         Button btn1 = new Button();
 64                         btn1.Size = new Size(75, 23);
 65                         btn1.Font = new Font("宋体", 9);
 66                         btn1.Location = new Point(x, y); 
 67                         btn1.Name = "btnYes";
 68                         btn1.Text = MsgBoxButtonText.Yes;
 69                         btn1.Click += btnYes_Click;
 70                         pnlButtonContainer.Controls.Add(btn1);
 71 
 72                         x=width+ 5;
 73                         y=height- 30;
 74                         //“否”按钮
 75                         Button btn2 = new Button();
 76                         btn2.Size = new Size(75, 23);
 77                         btn2.Font = new Font("宋体", 9);
 78                         btn2.Location = new Point(x, y);
 79                         btn2.Name = "btnNo";
 80                         btn2.Text = MsgBoxButtonText.No;
 81                         btn2.Click += btnNo_Click;
 82                         pnlButtonContainer.Controls.Add(btn2);
 83                          
 84                     }
 85                     break;
 86                 case MsgBoxButtons.YesNoCancel:
 87                     {
 88                         x=width- 120;
 89                         y=height- 30;
 90                         //“是”按钮
 91                         Button btn1 = new Button();
 92                         btn1.Size = new Size(75, 23);
 93                         btn1.Font = new Font("宋体", 9);
 94                         btn1.Location = new Point(x, y);
 95                         btn1.Name = "btnYes";
 96                         btn1.Text = MsgBoxButtonText.Yes;
 97                         btn1.Click += btnYes_Click;
 98                         pnlButtonContainer.Controls.Add(btn1);
 99 
100                         x=width- 35;
101                         y=height- 30;
102                         //“否”按钮
103                         Button btn2 = new Button();
104                         btn2.Size = new Size(75, 23);
105                         btn2.Font = new Font("宋体", 9);
106                         btn2.Location = new Point(x, y);
107                         btn2.Name = "btnNo";
108                         btn2.Text = MsgBoxButtonText.No;
109                         btn2.Click += btnNo_Click;
110                         pnlButtonContainer.Controls.Add(btn2);
111 
112                         x=width+50;
113                         y=height- 30;
114                         //“取消”按钮
115                         Button btn3 = new Button();
116                         btn3.Size = new Size(75, 23);
117                         btn3.Font = new Font("宋体", 9);
118                         btn3.Location = new Point(x, y);
119                         btn3.Name = "btnCancel";
120                         btn3.Text = MsgBoxButtonText.Cancel;
121                         btn3.Click += btnCancel_Click;
122                         pnlButtonContainer.Controls.Add(btn3);
123                          
124                     }
125                     break;
126                 case MsgBoxButtons.AbortRetryIgnore:
127                     {
128                         x=width- 120;
129                         y=height- 30;
130                         //“终止”按钮
131                         Button btn1 = new Button();
132                         btn1.Size = new Size(75, 23);
133                         btn1.Font = new Font("宋体", 9);
134                         btn1.Location = new Point(x, y);
135                         btn1.Name = "btnAbort";
136                         btn1.Text = MsgBoxButtonText.Abort;
137                         btn1.Click += btnAbort_Click;
138                         pnlButtonContainer.Controls.Add(btn1);
139 
140                         x=width- 35;
141                         y=height- 30;
142                         //“重试”按钮
143                         Button btn2 = new Button();
144                         btn2.Size = new Size(75, 23);
145                         btn2.Font = new Font("宋体", 9);
146                         btn2.Location = new Point(x, y);
147                         btn2.Name = "btnRetry";
148                         btn2.Text = MsgBoxButtonText.Retry;
149                         btn2.Click += btnRetry_Click;
150                         pnlButtonContainer.Controls.Add(btn2);
151 
152                         x=width+ 50;
153                         y=height- 30;
154                         //“忽略”按钮
155                         Button btn3 = new Button();
156                         btn3.Size = new Size(75, 23);
157                         btn3.Font = new Font("宋体", 9);
158                         btn3.Location = new Point(x, y);
159                         btn3.Name = "btnIgnore";
160                         btn3.Text = MsgBoxButtonText.Ignore;
161                         btn3.Click += btnIgnore_Click;
162                         pnlButtonContainer.Controls.Add(btn3);
163                          
164                         
165                     }
166                     break;
167                 case MsgBoxButtons.RetryCancel:
168                     {
169                         x=width- 80;
170                         y=height- 30;
171                         //“重试”按钮
172                         Button btn1 = new Button();
173                         btn1.Size = new Size(75, 23);
174                         btn1.Font = new Font("宋体", 9);
175                         btn1.Location = new Point(x, y);
176                         btn1.Name = "btnRetry";
177                         btn1.Text = MsgBoxButtonText.Retry;
178                         btn1.Click += btnRetry_Click;
179                         pnlButtonContainer.Controls.Add(btn1);
180 
181                         x=width+ 5;
182                         y=height- 30;
183                         //“取消”按钮
184                         Button btn2 = new Button();
185                         btn2.Size = new Size(75, 23);
186                         btn2.Font = new Font("宋体", 9);
187                         btn2.Location = new Point(x, y);
188                         btn2.Name = "btnCancel";
189                         btn2.Text = MsgBoxButtonText.Cancel;
190                         btn2.Click += btnCancel_Click;
191                         pnlButtonContainer.Controls.Add(btn2);
192                          
193                     }
194                     break;
195                 case MsgBoxButtons.OKCopy:
196                     {
197                         x=width-80;
198                         y=height- 30;
199                         //“确认”按钮
200                         Button btn1 = new Button();
201                         btn1.Size = new Size(75, 23);
202                         btn1.Font = new Font("宋体", 9);
203                         btn1.Location = new Point(x, y);
204                         btn1.Name = "btnOK";
205                         btn1.Text = MsgBoxButtonText.OK;
206                         btn1.Click += btnOK_Click;
207                         pnlButtonContainer.Controls.Add(btn1);
208 
209                         x =width+ 5;
210                         y=height- 30;
211                         //“复制”按钮
212                         Button btn2 = new Button();
213                         btn2.Size = new Size(75, 23);
214                         btn2.Font = new Font("宋体", 9);
215                         btn2.Location = new Point(x, y); 
216                         btn2.Name = "btnCopy";
217                         btn2.Text = MsgBoxButtonText.Copy;
218                         btn2.Click += btnCopy_Click;
219                         pnlButtonContainer.Controls.Add(btn2);
220                          
221                     }
222                     break;
223                 default:
224                     {
225                         x=width- 30;
226                         y=height- 30;
227                         //“确认”按钮
228                         Button btn1 = new Button();
229                         btn1.Size = new Size(75, 23);
230                         btn1.Font = new Font("宋体", 9);
231                         btn1.Location = new Point(x, y);
232                         btn1.Name = "btnOK";
233                         btn1.Text = MsgBoxButtonText.OK;
234                         btn1.Click += btnOK_Click;
235                         pnlButtonContainer.Controls.Add(btn1);
236                     }
237                     break;
238             }
239         }
240         #endregion

最后,完成了MsgBox这个自定义控件后,就可以将它添加工具箱中,用于其他项目了,使用方式跟MessageBox类似。

1  MsgBox.Show("程序已崩溃,请重启!", "系统崩溃", MsgBoxButtons.OKCancel, MsgBoxIcon.SysBreak);
2             MsgBox.Show("密码错误!", "错误", MsgBoxButtons.OK, MsgBoxIcon.Error);
3             MsgBox.Show("您确定要退出系统?", "警告", MsgBoxButtons.YesNo, MsgBoxIcon.Warning);

运行结果如下图所示:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值