【问题标题】:Custom MessageBox自定义消息框
【发布时间】:2011-01-16 10:27:51
【问题描述】:

我想创建一个自定义消息框 但我想在显示消息框期间使用自定义图标和自定义声音 我如何创建这个消息框? 我不想使用 shell32.dll 和 user32.dll

一个与 windows 7 for windows xp 相同的消息框

【问题讨论】:

  • 您到底想定制什么?

标签: c#


【解决方案1】:

我使用了一个带有静态方法 ShowCustomButtonsDialog 的简单对话框。我在左上角放置了一个文本标签,并将边框样式更改为对话框。方法只返回按钮索引或 -1。

public partial class CustomButtonsDialog : Form
{
    private const int ButtonHeight = 24;
    private const int ButtonPadding = 6;
    private const int ButtonInnerPadding = 5;
    private const int MaxFormWidth = 700;

    private int buttonIndex = -1;

    public int ButtonIndex
    {
        get { return buttonIndex; }
        private set { buttonIndex = value; }
    }

    public static int ShowCustomButtonsDialog(string text, string title, params string[] buttonsText)
    {
        var dlg = new CustomButtonsDialog(text, title, buttonsText.ToList());
        dlg.ShowDialog();
        return dlg.ButtonIndex;
    }

    public static int ShowCustomButtonsDialog(string text, string title, List<string> buttonsText)
    {
        var dlg = new CustomButtonsDialog(text, title, buttonsText);
        dlg.ShowDialog();
        return dlg.ButtonIndex;
    }

    public CustomButtonsDialog()
    {
        InitializeComponent();
    }

    private CustomButtonsDialog(string text, string title, List<string> buttonsText)
    {
        InitializeComponent();
        Text = title;
        labelText.Text = text;

        // добавить кнопки
        var formWidth = ClientSize.Width;
        List<int> buttonWidths;            
        using (var gr = CreateGraphics())
        {
            buttonWidths = buttonsText.Select(b => (int)gr.MeasureString(b, Font).Width + 2 * ButtonInnerPadding).ToList();                
        }
        var totalButtonWd = buttonWidths.Sum() + (buttonWidths.Count - 1) * ButtonPadding;
        if (totalButtonWd > formWidth)
        {
            if (totalButtonWd <= MaxFormWidth)
                Width = Width - ClientSize.Width + totalButtonWd + ButtonPadding * 2;
            else
            {// trim some buttons
                Width = Width - ClientSize.Width + MaxFormWidth;
                totalButtonWd = ClientSize.Width - ButtonPadding * 2;
                var avgWidth = (totalButtonWd - (buttonsText.Count - 1) * ButtonPadding) / buttonsText.Count;

                var sumThins = buttonWidths.Sum(w => w <= avgWidth ? w : 0);
                var countThins = buttonWidths.Count(w => w <= avgWidth);
                var countFat = buttonsText.Count - countThins;
                var spareRoom = totalButtonWd - sumThins;
                var fatWidth = (countThins == 0) || (countFat == 0)
                                   ? avgWidth
                                   : (spareRoom - (countThins - 1)*ButtonPadding)/countFat;
                for (var i = 0; i < buttonWidths.Count; i++)
                    if (buttonWidths[i] > avgWidth) buttonWidths[i] = fatWidth;                    
            }
        }
        // buttons' Y-coords and height
        labelText.MaximumSize = new Size(totalButtonWd,
            labelText.MaximumSize.Height);
        var buttonTop = labelText.Bottom + ButtonPadding;
        var formHeight = buttonTop + ButtonHeight + ButtonPadding;
        Height = Height - ClientSize.Height + formHeight;

        // do make buttons
        var buttonLeft = ButtonPadding;
        var tag = 0;
        for (var i = 0; i < buttonWidths.Count; i++)
        {
            var button = new Button
                             {
                                 Parent = this,
                                 Width = buttonWidths[i],
                                 Height = ButtonHeight,
                                 Left = buttonLeft,
                                 Top = buttonTop,
                                 Text = buttonsText[i],
                                 Tag = tag++
                             };
            button.Click += ButtonClick;
            buttonLeft = button.Right + ButtonPadding;
            Controls.Add(button);
        }
    }

    private void ButtonClick(object sender, EventArgs e)
    {
        ButtonIndex = (int) ((Button) sender).Tag;
        Close();
    }
}

【讨论】:

    【解决方案2】:

    最简单的方法是从头开始创建您自己的 MessageBox 窗口。如果您正在寻找默认 Windows MessageBox 的挂钩,您需要考虑到以后可能会遇到与其他 Windows 操作系统的兼容性等问题。

    这里有几个示例如何创建您自己的 MessageBox:

    这将使您了解逻辑以及如何开始编写自己的自定义 MessageBox。

    【讨论】:

    • 你的第二个链接也失效了
    猜你喜欢
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多