目录
介绍
在我们继续之前
实现
Form设计
代码
结构体
事件与方法
结论
- 从GitHub下载源代码
在开始编程的那段时间,我在标准.NET Framework库中发现了MessageBox类。这令人兴奋,因为它允许我尝试各种不同的方式来向自己提供有关应用程序中正在发生的事情的信息。这很好,直到我在个人使用Windows时看到一个特定的消息框为止。我最终将得知该消息框实际上称为TaskDialog,它是Windows Vista中引入的。它的顶部是蓝色文本,底部是一些较小的黑色文本。当时,理解如何使用TaskDialog超出了我的能力范围,所以我决定尝试使用我当时拥有的技能创建一个类似的TaskDialog。
在这个项目中,我们将使用Visual Studio和带有C#的Designer编写自己的对话框消息。它将支持两种样式的文本,三种按钮配置和六种图标配置。所使用的图标来自SystemIcons类。
在我们继续之前这并不意味着它是有关如何使用Visual Studio或C#编程语言的分步教程,而是概述了开发自己的消息框所必需的逻辑。代码部分中的代码片段有很多注释,以帮助您了解每个部分的操作。以下是假定您熟悉的主题列表:
- Windows Form设计
- if-else
- switch
- 枚举类型
- 具有返回值的方法
- 传递值类型参数
- 静态类和静态类成员
- 动态链接库(DLL)
这是消息的实现。此代码将显示文章开头图像中显示的消息。
using DialogMessage; if (DMessage.ShowMessage( // Window Title "Window Title", // Main Instruction "Want to learn how to write your own message box?", // Dialog buttons DMessage.MsgButtons.YesNo, // Dialog Icons DMessage.MsgIcons.Question, // Content "In this project we will learn the logic necessary " + "to write your own dialog message box in Windows") // Checks DialogResult of the button clicked by user == DialogResult.Yes) // Show the Windows standard MessageBox to test result MessageBox.Show("You clicked Yes!"); else MessageBox.Show("You clicked No!");Form设计
下图是MainForm.Designer.cs的“Form设计器”视图,其中包含控件和一些值得注意的属性。重要属性通知是Anchor、MaximumSize和FormBorderStyle。
Anchor确保在Form调整大小时对其进行适当的移动。
Label的MaximumSize确保文本不会从Form溢出,并且将换行。
FormBorderStyle设置为FixedDialog确保用户无法调整大小,从而允许其Form根据提供的文本量来调整大小。
消息框分为两个主要文件;MainForm.cs和DialogMessage.cs。
MainForm.cs包含以下Form.Load事件:
// Form.Load event 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方法和两个enum:
////// A public static method with a return value of System.Windows.Forms.DialogResult ///////////////// Optional parameter with default value of None ///// Optional parameter with empty default value ///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 // This will set the properties of the form buttons and their DialogResult public enum MsgButtons { OK = 0, OKCancel = 1, YesNo = 2 }
// Message icon enum for switch statement in ShowMessage // This will set the Image for the PictureBox public enum MsgIcons { None = 0, Question = 1, Info = 2, Warning = 3, Error = 4, Shield = 5 }事件与方法
让我们进入每个代码块,看看它能做什么。
MainForm.cs中的Form.Load事件:
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; }
DialogMessage.cs中的ShowMessage方法:
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(); }结论
希望本文对您有所帮助。我意识到在CodeProject上已经有一些有关Windows TaskDialog的消息框替代方法和包装器的深入文章(这启发了该项目),但是,我希望这可以作为学习如何编写自己的文章的参考。