现在的位置: 首页 > 综合 > 正文

MsgBox-自定义消息对话框控件(非重写MessageBox)

2012年10月09日 ⁄ 综合 ⁄ 共 7990字 ⁄ 字号 评论关闭

  再次声明一下:对于这个程序,我并不觉得它的功能比MessageBox强,完善。我发布这个程序的目的,并不是要跟MS比什么,也没有能力去比,我只是一个做应用软件开发的,对底层的东西知晓的并不多。它存在的意义就是在于开源,开源意味着什么?有代码!我只提供了这个类似MessageBox最基本的实现,任何一个有一定.net编程基础的程序员都可以去改写它,可以定制自己需要的功能和外观;如果你能力强,基础好,你甚至可以去重新绘制这个对话框,做成你想要的任意外观,三角形还是半圆,甚至3D图形.....也许在一般情况下,我所做的是有些多余;但并不排除做某些项目的时候,MessageBox的功能的确有太多限制,功能上未必够用。这也就是我当初编写这个程序的目的,当然,你想重写MessageBox也是可以的,只是,一般的程序员未必有此功底。

  此外,我还做了许多可以方便日常开发用的小程序和小控件,还有一些小平台。其中有一个项目,目的是将系统的功能授权,使用授权,功能动态加载,权限控制等一系列软件开发中常用的技术和功能整合到一起,我个人已经基本完成代码的编写,不过由于本人能力有限,框架设计不尽人意,目前想着把这个平台,和有兴趣的网友一起做成开源的项目,供个人和团体参考学习,有时间到时候我会发布这个项目的开发邀请,希望有兴趣的网友参与。

  本文主要介绍的是一个自己编写的,类似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 // 指定若干常数,用以定义显示按钮的文本
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();

抱歉!评论已关闭.