`
baobaoupup
  • 浏览: 470618 次
文章分类
社区版块
存档分类
最新评论

实现自定义消息对话框控件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);

运行结果如下图所示:


分享到:
评论

相关推荐

    VB程序设计及应用

    8.2.1 通用对话框控件 8.2.2 通用对话框的属性与方 8.3 通用对话框的使用 8.3.1 打开(Open)对话框 8.3.2 另存为(SaveAs)对话框 8.3.3 颜色(Color)对话框 8.3.4 字体(Font)对话框 ...

    vb控件开发 开发ocx

    如果自定义 MsgBox 的按钮,标题等 51 , 51.txt 如何创建数据绑定控件 52 , 52.txt 如何检查您的电脑是否打开 ActiveDesktop? 53 , 53.txt 如何解决VB中的Grid控件的打印问题 54 , 54.txt 如何强制文件对话框再次...

    VBA常用技巧

    技巧73使用Msgbox函数165 73-1显示简单的提示信息165 73-2定制个性化的消息框166 73-3获得消息框的返回值167 73-4在消息框中排版169 73-5对齐消息框中显示的信息170 技巧74自动关闭的消息框172 74-1使用WshShell....

    中文版Excel.2007高级VBA编程宝典.part1

     6.3.4 创建自定义的对话框  6.3.5 在工作表上使用ActiveX控件  6.3.6 执行开发的成果  6.4 使自己关心终端用户  6.4.1 测试应用程序  6.4.2 尽量完善应用程序的安全性  6.4.3 让应用程序变得美观和直观  ...

    VBA 帮助文档(摘自Office2000 VBA帮助)

    22、创建自定义对话框 23、在窗体上使用 Microsoft Office Web 组件 24、内置对话框参数表 25、使用 Microsoft Excel 对象的事件 26、在 Visual Basic 中使用 Microsoft Excel 工作表函数 27、处理形状(图形对象) ...

    VBA编程技巧大全

    技巧73 使用Msgbox函数 167 73-1 显示简单的提示信息 167 73-2 定制个性化的消息框 168 73-3 获得消息框的返回值 169 73-4 在消息框中排版 171 73-5 对齐消息框中显示的信息 172 技巧74 自动关闭的消息框 174 74-1 ...

    ExcelVBA程序设计.doc

    第十章 对话框和自定义窗体 189 1.文件打开和另存为对话框 192 2.GETOPENFILENAME和GETSAVEASFILENAME方法 196 3.创建窗体 197 4.创建用户窗体的工具 199 5.标签 200 6.文字框 200 7.框架 200 8.选项按钮 201 9.复选...

    Excel_VBA教程

    第十章 对话框和自定义窗体 189 1.文件打开和另存为对话框 192 2.GETOPENFILENAME和GETSAVEASFILENAME方法 196 3.创建窗体 197 4.创建用户窗体的工具 199 5.标签 200 6.文字框 200 7.框架 200 8.选项按钮 201 9.复选...

    Excel VBA 基础教程

    目 录 一、VBA语言基础...................................................................................................................1 第一节 标识符....................................................

    EXCEL编程VBA高级教程

    目录 一、VBA语言基础...................................................................................................................1 第一节标识符.....................................................

Global site tag (gtag.js) - Google Analytics