对话框消息在c#中为。net Framework 4.5

表的内容 在我们继续实现表单设计代码之前介绍 结构事件和方法 结论历史 介绍 在我开始编程的时候,我在标准的。net框架库中发现了MessageBox类。这是令人兴奋的,因为它允许我试验不同的方式来提供我自己的信息,关于我的应用程序正在发生什么。这很好,直到我在个人使用Windows时看到了一个特殊的消息框。我最终了解到这个消息框实际上叫做任务对话框,它是在Windows Vista中引入的。它的顶部有蓝色的文字,底部有一些较小的黑色文字。了解如何使用TaskDialog超出了我的技能范围,所以我决定尝试使用我当时拥有的技能创建一个类似的。 在这个项目中,我们将使用Visual Studio和使用c#的设计器编写我们自己的对话框消息框。它将支持两种文本样式、三种按钮配置和六种图标配置。图标的使用来自SystemIcons类。 在我们继续之前 这并不是关于如何使用Visual Studio或c#编程语言的一步一步的教程,而是开发自己的消息框所需逻辑的概述。代码部分中的代码片段有许多注释,以帮助理解每个片段的作用。以下是假设您熟悉的主题列表: Windows窗体设计if-else开关枚举类型方法与返回值传递值类型参数静态类和静态类成员动态链接库(DLL) 实现 下面是消息的实现。这段代码将显示出现在本文开头图像中的消息。 隐藏,收缩,复制Code

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!");

表单设计 下图是MainForm.Designer.cs的表单设计器视图,带有控件和一些值得注意的属性。需要注意的重要属性有Anchor、MaximumSize和FormBorderStyle。 锚确保窗体上的控件在窗体重新调整大小时适当移动。 标签的最大尺寸确保文本不会溢出窗体,并将换行到新行。 FormBorderStyle设置为FixedDialog可确保用户不能根据提供的文本量调整表单的大小。 代码 结构 消息框被分成两个主文件;MainForm.cs DialogMessage.cs。 cs包含以下表单。加载事件: 隐藏,复制Code

// 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
}

cs包含下面的三个代码块;a 公共静态方法和两个枚举: 隐藏,复制Code

/// <summary>
/// A public static method with a return value of System.Windows.Forms.DialogResult
/// </summary>
/// <paramname="_windowTitle"></param>
/// <paramname="_mainInstruction"></param>
/// <paramname="_msgButtons"></param>
/// <paramname="_msgIcons"></param> // Optional parameter with default value of None
/// <paramname="_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
}

隐藏,复制Code

// 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
}

隐藏,复制Code

// 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中的加载事件: 隐藏,收缩,复制Code

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方法: 隐藏,收缩,复制Code

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的消息框替代品和包装器(正是它们激发了这个项目的灵感),但是,我想把它作为学习如何编写自己的消息框的参考。 历史 2020年4月25日 添加图标功能。图标的使用来自SystemIcons类。 2020年4月18日 重新设计代码以使用静态类和方法。这样就不需要创建一个新的DialogMessage实例。 2020年4月13日 初始版本 本文转载于:http://www.diyabc.com/frontweb/news2561.html

原文地址:https://www.cnblogs.com/Dincat/p/13458033.html