自定义MessageBox

时间:2011-01-16 10:27:51

标签: c#

我想创建一个自定义消息框 但我想在show messagebox中使用自定义图标和自定义声音 我该如何创建这个Messagebox? 我不想使用shell32.dll和user32.dll

与Windows XP for Windows XP相同的消息框

2 个答案:

答案 0 :(得分:3)

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

以下是一些示例如何创建自己的MessageBox:

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

答案 1 :(得分:3)

我使用了一个带有静态方法 ShowCustomButtonsDialog 的简单对话框。我在左上角放置了一个文本标签,并将边框样式更改为Dialog。方法只返回按钮索引或-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();
    }
}