.NET Framework 4.5的C#中的对话框消息

目录

介绍

在我们继续之前

实现

Form设计

代码

结构体

事件与方法

结论


介绍

在开始编程的那段时间,我在标准.NET Framework库中发现了MessageBox类。这令人兴奋,因为它允许我尝试各种不同的方式来向自己提供有关应用程序中正在发生的事情的信息。这很好,直到我在个人使用Windows时看到一个特定的消息框为止。我最终将得知该消息框实际上称为TaskDialog,它是Windows Vista中引入的。它的顶部是蓝色文本,底部是一些较小的黑色文本。当时,理解如何使用TaskDialog超出了我的能力范围,所以我决定尝试使用我当时拥有的技能创建一个类似的TaskDialog

在这个项目中,我们将使用Visual Studio和带有C#的Designer编写自己的对话框消息。它将支持两种样式的文本,三种按钮配置和六种图标配置。所使用的图标来自SystemIcons类。

在我们继续之前

这并不意味着它是有关如何使用Visual StudioC#编程语言的分步教程,而是概述了开发自己的消息框所必需的逻辑。代码部分中的代码片段有很多注释,以帮助您了解每个部分的操作。以下是假定您熟悉的主题列表:

实现

这是消息的实现。此代码将显示文章开头图像中显示的消息。

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设计器视图,其中包含控件和一些值得注意的属性。重要属性通知是AnchorMaximumSizeFormBorderStyle

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

LabelMaximumSize确保文本不会从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 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消息框替代方法和包装器的深入文章(这启发了该项目),但是,我希望这可以作为学习如何编写自己的文章的参考。

TaskDialog信息框支持库。 操作系统支持: Windows 注意:TaskDialog信息框的API是在Vista之后才被加入的,所以,在Vista以下电脑慎用!! 前言 本程序基于CTaskDialog类,前阵子发布过一个不是很完善的程序。本次开放了:“TaskDialogCallbackProc”接口,可以对TaskDialog进行消息处理。 增加内容 增加的常量: 通知消息 wParam 值 lParam 值 TDN_CREATED 未使用。 未使用。 TDN_NAVIGATED 未使用。 未使用。 TDN_BUTTON_CLICKED 命令按钮控件ID. 未使用。 TDN_HYPERLINK_CLICKED 命令按钮控件ID. 包含指向的 LPCWSTR 结构。 TDN_TIMER 重置毫秒,因为 CTaskDialog 创建或计时器的时间。 未使用。 TDN_DESTROYED 未使用。 未使用。 TDN_RADIO_BUTTON_CLICKED 单选按钮的ID. 未使用。 TDN_DIALOG_CONSTRUCTED 未使用. 未使用。 TDN_VERIFICATION_CLICKED 1,复选框处于选状态,0;未启用。 未使用。 TDN_HELP 未使用。 未使用。 TDN_EXPANDO_BUTTON_CLICKED 0,展开区域处于折叠状态;非零,外接文本显示。 未使用。 功能常量名 作用 S_OK 对于TaskDialogCallbackProc处理完成后返回,并把执行权交回TaskDialog S_FALSE 对于TaskDialogCallbackProc处理完成后,不把执行权交回TaskDialog 备注 TaskDialogCallback 处理的默认实现特定消息然后调用适当在 CTaskDialog类选件的方法。 例如,在响应 TDN_BUTTON_CLICKED 消息TaskDialogCallback 调用 CTaskDialog::OnCommandControlClick。 wParam 和 lParam 的值取决于特定生成的消息。 可以为或这两个值可以为空。 下表列出了支持的默认值通知,以及 wParam 和 lParam 的值表示。 如果您具有派生类重写此方法,则应实现每个消息的回调代码在下表。 效果 解释: TaskDialog设置了回调事件“TaskDialogCallbackProc2”,然后取出进度条句柄,修改进度条的进度。 更多例子: 链接测试 月历测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值