目录

介绍

在我们继续之前

实现

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 buttonsDMessage.MsgButtons.YesNo,// Dialog IconsDMessage.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 resultMessageBox.Show("You clicked Yes!");elseMessageBox.Show("You clicked No!");

Form设计

下图是MainForm.Designer.cs的“Form设计器”视图,其中包含控件和一些值得注意的属性。重要属性通知是Anchor、MaximumSize和FormBorderStyle。

Anchor确保在Form调整大小时对其进行适当的移动。

Label的MaximumSize确保文本不会从Form溢出,并且将换行。

FormBorderStyle设置为FixedDialog确保用户无法调整大小,从而允许其Form根据提供的文本量来调整大小。

代码

结构体

消息框分为两个主要文件;MainForm.csDialogMessage.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:

/// <summary>
/// A public static method with a return value of System.Windows.Forms.DialogResult
/// </summary>
/// <param name="_windowTitle"></param>
/// <param name="_mainInstruction"></param>
/// <param name="_msgButtons"></param>
/// <param name="_msgIcons"></param> // Optional parameter with default value of None
/// <param name="_content"></param> // Optional parameter with empty default value
/// <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
// 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 MainInstructionint mainInstructionBottom = mainInstruction.Location.Y + mainInstruction.Height;// Gets the Y location of the bottom of Contentint contentBottom = content.Location.Y + content.Height;// Offsets the top of Content from the bottom of MainInstructionint contentTop = mainInstructionBottom + 18; // 18 just looked nice to me// Sets new location of the top of Contentcontent.Location = new Point(content.Location.X, contentTop);if (content.Text == string.Empty)// If only MainInstruction is provided then make the form a little shorterHeight += (mainInstruction.Location.Y + mainInstruction.Height) - 50;elseHeight += (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 controlsMainForm main = new MainForm();// Sets the initial height of the formmain.Height = 157;// Sets Window Titlemain.Text = _windowTitle;// Sets MainInstructionmain.mainInstruction.Text = _mainInstruction;// Sets Contentmain.content.Text = _content;// Sets the properties of the buttons based on which enum was providedswitch (_msgButtons){// Button1 is the left button// Button2 is the right buttoncase 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 providedif (_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 userreturn main.ShowDialog();
}

结论

希望本文对您有所帮助。我意识到在CodeProject上已经有一些有关Windows TaskDialog的消息框替代方法和包装器的深入文章(这启发了该项目),但是,我希望这可以作为学习如何编写自己的文章的参考。

.NET Framework 4.5的C#中的对话框消息相关推荐

  1. .NET Framework 4.0 和 Dublin 中的 WCF 和 WF 服务 - z

    在 2008 年 10 月份召开的专业开发人员大会 (PDC) 上,Microsoft 发布了有关 Microsoft .NET Framework 4.0 中将要提供的大量改进的详细信息,尤其是在 ...

  2. Framework 1.0/1.1中NotifyIcon的不足

    .NET Framework 1.0/1.1中给我们提供了一个NotifyIcon类,使用这个类我们可以非常方便的实现系统托盘(SystemTray)图标.可是不知道微软是为了兼容性还是为了偷懒,只实 ...

  3. 将.net framework 4 部署在docker中的全过程(支持4.0 到 4.8,3.5应该也可以)

    前言: docker自从诞生之初,就是运行在linux系统中,后来windows上也可以运行docker了,但是微软是通过自身的hyper-v技术,在你的windows系统中虚拟出来了一个小的linu ...

  4. 中怎么撤回消息_微信消息撤回也能看到,这个开源神器牛x!语音、图片、文字都支持!...

    1.前言 微信在2014年的时候,发布的v5.3.1 版本中推出了消息撤回功能,用户可以选择撤回 2 分钟内发送的最后一条信息. 现在很多即时通讯的软件都有撤回这个功能. 腾讯为了照顾手残党,在微信和 ...

  5. 详解Linux交互式shell脚本中创建对话框实例教程

    详解Linux交互式shell脚本中创建对话框实例教程 本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一 ...

  6. C#中的MessageBox消息对话框

    关键字:C# MessageBox 消息对话框 在程序中,我们经常使用消息对话框给用户一定的信息提示,如在操作过程中遇到错误或程序异常,经常会使用这种方式给用于以提示.在C#中,MessageBox消 ...

  7. GTK+重拾--08 GTK+中的对话框

    版权声明:您好,转载请留下本人博客的地址,谢谢 https://blog.csdn.net/hongbochen1223/article/details/50351564 (一):写在前面 在这一个小 ...

  8. 如何禁用请求库中的日志消息?

    本文翻译自:How do I disable log messages from the Requests library? By default, the Requests python libra ...

  9. 自定义函数或者回调函数中调用对话框对象

    经常会在程序中写自己的类,或者回调函数之类,要在这些类中操控对话框上的控件必须得有对话框对象的指针 可以这样来做: 申明一个全局变量CXXXDlg* P 在CXXXDlg::OnInitDialog( ...

最新文章

  1. zabbix对一台主机监控的操作
  2. 剑指offer 算法(树的两个节点的最低祖先)
  3. php计算时间差js,JavaScript如何计算时间差(引入外部字体文件)?
  4. socket编程--sockaddr_in结构体操作
  5. python csv文件读取行列_使用Numpy读取CSV文件,并进行行列删除的操作方法
  6. .git文件夹_Git幸存者指南
  7. 从数据库读取数据后输出XML
  8. 4----apache主配置文件模板和基于域名虚拟主机配置文件模板
  9. NHibernate Configuring
  10. ctf 改变图片高度_在Unity中 改变地形(Terrain),并加上水面、树、草地、材质(地板上色)...
  11. 笔记本更新网卡驱动后,出现:上网图标消失、网络连接为空、设备管理器中网络适配器全部为叹号、有线无线均无法链接的情况
  12. JAVA实现UDP通信
  13. 如何解决电脑任务栏无故不见了的问题 ?
  14. Android camera相机开发拍照功能
  15. 关于ROS功能包里package.xml和CMakeList.txt的源码分析
  16. 隐藏计算机文件夹中,怎样显示电脑中已隐藏的文件夹
  17. C语言——Hello World
  18. android 坐标系 旋转,android IMU旋转矩阵横屏矫正(remapCoordinateSystem函数原理)
  19. Win10 使用python和ffmpeg批量合并音视频
  20. 黄金三年,我看浪潮存储的崛起

热门文章

  1. 为什么let在php中报错,ES6系列之声明变量let与const
  2. bootstrapr表格父子框_JS组件系列——表格组件神器:bootstrap table(二:父子表和行列调序)...
  3. node升级命令_Vue CLI 4 发布:自动化升级过程,支持自定义包管理器
  4. python的numpy是什么_python中numpy是什么
  5. 设计类导航,为设计师提供最简单便捷的设计网址
  6. 导入数据_导入外部数据的三个技巧
  7. C++ HOOK 详解
  8. Intel 中断和异常处理 - 目录
  9. Linux:C GNU Obstack内存池
  10. MNIST机器学习入门