在开始编程的时候,发现了.NET Framework库中的MessageBox类。这让非常兴奋,因为它允许尝试不同的方式向自己提供应用程序中正在发生的事情的信息。然而,直到在个人使用Windows时看到了一个特别的对话框,才意识到这种对话框实际上是叫做TaskDialog的,它在Windows Vista中被引入。当时还不了解如何使用TaskDialog,所以决定尝试使用当时的技能创建一个外观相似的对话框。
在这个项目中,将使用Visual Studio和C#的设计器编写自己的对话框消息框。它将支持两种文本样式、三种按钮配置和六种图标配置。使用的图标来自SystemIcons类。
这不是一个关于如何使用Visual Studio或C#编程语言的逐步教程,而是一个概述,介绍开发自己的消息框所需的逻辑。代码部分的代码片段有很多注释,以帮助理解每部分的作用。以下是一些假定已经熟悉的主题:
以下是消息的实现代码。这段代码将显示文章开头图片中的消息。
C# using DialogMessage; if (DMessage.ShowMessage( "Window Title", "Main Instruction", DMessage.MsgButtons.YesNo, DMessage.MsgIcons.Question, "Content" )) MessageBox.Show( "You clicked Yes!" ); else MessageBox.Show( "You clicked No!" );
下图是MainForm.Designer.cs的表单设计器视图,包含控件和一些值得注意的属性。需要注意的重要属性有Anchor、MaximumSize和FormBorderStyle。Anchor确保表单上的控件在调整大小时适当移动。Label的MaximumSize确保文本不会溢出表单并换行到新行。FormBorderStyle设置为FixedDialog,确保用户不能调整表单大小,允许它根据提供的文本量调整大小。
消息框分为两个主要文件:MainForm.cs和DialogMessage.cs。MainForm.cs包含以下Form.Load事件:
C# private void DialogMessage_Load(object sender, EventArgs e) { // Set the starting locations and sizes of the labels // Adjust the locations and sizes of the labels to properly show the information }
DialogMessage.cs包含以下三个代码块;一个public static方法和两个枚举:
C# ///
/// <summary>
/// ///
/// </summary>
/// ///
/// <param name="_windowTitle">
/// ///
/// </param>
/// ///
/// <param name="_mainInstruction">
/// ///
/// </param>
/// ///
/// <param name="_msgButtons">
/// ///
/// </param>
/// ///
/// <param name="_msgIcons">
/// ///
/// </param>
/// ///
/// <param name="_content">
/// ///
/// </param>
/// ///
/// <returns>
/// ///
/// </returns>
public static DialogResult ShowMessage(string _windowTitle, string _mainInstruction, MsgButtons _msgButtons, MsgIcons _msgIcons = MsgIcons.None, string _content = "") { // Set button and icon configurations and show the information to the user }
Message button enum for switch statement in ShowMessage:
C# public enum MsgButtons { OK = 0, OKCancel = 1, YesNo = 2 }
Message icon enum for switch statement in ShowMessage:
C# public enum MsgIcons { None = 0, Question = 1, Info = 2, Warning = 3, Error = 4, Shield = 5 }
让深入了解每个代码块,看看它们的作用。
Form.Load事件在MainForm.cs中:
C# private void DialogMessage_Load(object sender, EventArgs e) { // Once the ShowMessage function is called and the form appears // the code below makes the appropriate adjustments so the text appears properly // If no icon will be shown then shift the MainInstruction and Content left to an appropriate location // Adjust the MaximumSize to compensate for the shift left. if (msgIcon.Visible == false) { mainInstruction.Location = new Point(12, mainInstruction.Location.Y); mainInstruction.MaximumSize = new Size(353, 0); content.Location = new Point(12, content.Location.Y); content.MaximumSize = new Size(353, 0); } // Gets the Y location of the bottom of MainInstruction int mainInstructionBottom = mainInstruction.Location.Y + mainInstruction.Height; // Gets the Y location of the bottom of Content int contentBottom = content.Location.Y + content.Height; // Offsets the top of Content from the bottom of MainInstruction int contentTop = mainInstructionBottom + 18; // 18 just looked nice to me // Sets new location of the top of Content content.Location = new Point(content.Location.X, contentTop); if (content.Text == string.Empty) // If only MainInstruction is provided then make the form a little shorter Height += (mainInstruction.Location.Y + mainInstruction.Height) - 50; else Height += (content.Location.Y + content.Height) - 60; }
C# public static DialogResult ShowMessage(string _windowTitle, string _mainInstruction, MsgButtons _msgButtons, MsgIcons _msgIcons = MsgIcons.None, string _content = "") { // Creates a new instance of MainForm so we can set the properties of the controls MainForm main = new MainForm(); // Sets the initial height of the form main.Height = 157; // Sets Window Title main.Text = _windowTitle; // Sets MainInstruction main.mainInstruction.Text = _mainInstruction; // Sets Content main.content.Text = _content; // Sets the properties of the buttons based on which enum was provided switch (_msgButtons) { // Button1 is the left button // Button2 is the right button case MsgButtons.OK: main.Button1.Visible = false; main.Button2.DialogResult = DialogResult.OK; main.Button2.Text = "OK"; main.AcceptButton = main.Button2; main.Button2.TabIndex = 0; main.ActiveControl = main.Button2; break; case MsgButtons.OKCancel: main.Button1.DialogResult = DialogResult.OK; main.Button2.DialogResult = DialogResult.Cancel; main.Button1.Text = "OK"; main.Button2.Text = "Cancel"; main.AcceptButton = main.Button2; main.Button1.TabIndex = 1; main.Button2.TabIndex = 0; main.ActiveControl = main.Button2; break; case MsgButtons.YesNo: main.Button1.DialogResult = DialogResult.Yes; main.Button2.DialogResult = DialogResult.No; main.Button1.Text = "Yes"; main.Button2.Text = "No"; main.AcceptButton = main.Button2; main.Button1.TabIndex = 1; main.Button2.TabIndex = 0; main.ActiveControl = main.Button2; break; default: break; } // Sets the Image for the PictureBox based on which enum was provided if (_msgIcons != MsgIcons.None) { main.msgIcon.Visible = true; switch (_msgIcons) { case MsgIcons.Question: main.msgIcon.Image = SystemIcons.Question.ToBitmap(); break; case MsgIcons.Info: main.msgIcon.Image = SystemIcons.Information.ToBitmap(); break; case MsgIcons.Warning: main.msgIcon.Image = SystemIcons.Warning.ToBitmap(); break; case MsgIcons.Error: main.msgIcon.Image = SystemIcons.Error.ToBitmap(); break; case MsgIcons.Shield: main.msgIcon.Image = SystemIcons.Shield.ToBitmap(); break; default: break; } } else { main.msgIcon.Visible = false; } // Shows the message and gets the result selected by the user return main.ShowDialog(); }