MessageBox模板的问题

时间:2015-07-30 04:57:26

标签: c#

我有MessageBox的这个模板。我已经改变,所以你可以有一个滚动条,我现在唯一的问题是标题与消息非常分开,我不知道如何将它们联合起来。尝试一切,但我不能。

    using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Point = System.Drawing.Point;
using Size = System.Drawing.Size;

namespace WpfApplication2.xDialog
{
    class MsgBox : Form
    {
        private const int CS_DROPSHADOW = 0x00020000;
        private static MsgBox _msgBox;
        private readonly Panel _plHeader = new Panel();
        private readonly Panel _plButton = new Panel();
        private readonly Panel _plIcon = new Panel();
        private readonly PictureBox _picIcon = new PictureBox();
        private readonly FlowLayoutPanel _flpButtons = new FlowLayoutPanel();
        // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
        private readonly Label _lblTitle;
        // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
        private readonly RichTextBox _lblMessage;
        private readonly List<Button> _buttonCollection = new List<Button>();
        private static DialogResult _buttonResult;
        private static Timer _timer;
        private static Point _lastMousePos;

    //[DllImport("user32.dll", CharSet = CharSet.Auto)]
    //private static extern bool MessageBeep(uint type);

    private MsgBox()
    {
        FormBorderStyle = FormBorderStyle.None;
        BackColor = Color.FromArgb(190, 190, 190);
        //BackColor = Color.DarkGreen;
        StartPosition = FormStartPosition.CenterScreen;
        //Padding = new Padding(3);
        //Width = 400;

        _lblTitle = new Label
        {
            ForeColor = Color.Black,
            TextAlign = ContentAlignment.MiddleLeft,
            Font = new Font("Segoe UI", 15),
            BackColor = Color.Red,
            AutoSize = true
        };

        _lblMessage = new RichTextBox
        {
            ForeColor = Color.Black,
            Font = new Font("Segoe UI", 12),
            Multiline = true,
            WordWrap = true,
            Dock = DockStyle.Fill,
            ReadOnly = true,
            BackColor = Color.Yellow,
            BorderStyle = BorderStyle.None
        };

        _flpButtons.FlowDirection = FlowDirection.RightToLeft;
        _flpButtons.Dock = DockStyle.Fill;

        //PANEL DEL TITULO
        _plHeader.Dock = DockStyle.Top;
        _plHeader.Controls.Add(_lblTitle);

        // PANEL DE LOS BOTONES
        _plButton.Dock = DockStyle.Bottom;
        _plButton.Padding = new Padding(20);
        _plButton.BackColor = Color.FromArgb(170, 170, 170);
        _plButton.Height = 80;
        _plButton.Controls.Add(_flpButtons);

        // ICONO
        _picIcon.Width = 32;
        _picIcon.Height = 32;
        _picIcon.Location = new Point(20, 60);

        // PANEL DEL ICONO
        _plIcon.Dock = DockStyle.Left;
        _plIcon.Padding = new Padding(20);
        _plIcon.Width = 70;
        _plIcon.BackColor = Color.Blue;
        _plIcon.Controls.Add(_picIcon);

        var controlCollection = new List<Control>
        {
            this,
            _lblTitle,
            _flpButtons,
            _plHeader,
            _plButton,
            _plIcon,
            _picIcon
        };

        foreach (var control in controlCollection)
        {
            control.MouseDown += MsgBox_MouseDown;
            control.MouseMove += MsgBox_MouseMove;
        }

        Controls.Add(_lblMessage);
        Controls.Add(_plHeader);
        Controls.Add(_plIcon);
        Controls.Add(_plButton);

    }

    public override sealed Color BackColor
    {
        get { return base.BackColor; }
        set { base.BackColor = value; }
    }

    private static void MsgBox_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _lastMousePos = new Point(e.X, e.Y);
        }
    }


    private static void MsgBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left) return;
        _msgBox.Left += e.X - _lastMousePos.X;
        _msgBox.Top += e.Y - _lastMousePos.Y;
    }

    public static DialogResult Show(string message)
    {
        _msgBox = new MsgBox { _lblMessage = { Text = message } };

        InitButtons(Buttons.OK);

        _msgBox.ShowDialog();
        return _buttonResult;
    }

    public static DialogResult Show(string message, string title)
    {
        _msgBox = new MsgBox
        {
            _lblMessage = { Text = message },
            _lblTitle = { Text = title },
            Size = MessageSize(message, title)
        };

        InitButtons(Buttons.OK);

        _msgBox.ShowDialog();
        return _buttonResult;
    }

    public static DialogResult Show(string message, string title, Buttons buttons)
    {
        _msgBox = new MsgBox { _lblMessage = { Text = message }, _lblTitle = { Text = title } };
        _msgBox._plIcon.Hide();

        InitButtons(buttons);

        _msgBox.Size = MessageSize(message, title);
        _msgBox.ShowDialog();
        return _buttonResult;
    }

    public static DialogResult Show(string message, string title, Buttons buttons, Icon icon)
    {
        _msgBox = new MsgBox { _lblMessage = { Text = message }, _lblTitle = { Text = title } };

        InitButtons(buttons);
        InitIcon(icon);

        _msgBox.Size = MessageSize(message, title);
        _msgBox.ShowDialog();
        //MessageBeep(0);
        return _buttonResult;
    }

    public static DialogResult Show(string message, string title, Buttons buttons, Icon icon, AnimateStyle style)
    {
        _msgBox = new MsgBox { _lblMessage = { Text = message }, _lblTitle = { Text = title }, Height = 0 };

        InitButtons(buttons);
        InitIcon(icon);

        _timer = new Timer();
        var formSize = MessageSize(message, title);

        switch (style)
        {
            case AnimateStyle.SlideDown:
                _msgBox.Size = new Size(formSize.Width, 0);
                _timer.Interval = 1;
                _timer.Tag = new AnimateMsgBox(formSize, style);
                break;

            case AnimateStyle.FadeIn:
                _msgBox.Size = formSize;
                _msgBox.Opacity = 0;
                _timer.Interval = 20;
                _timer.Tag = new AnimateMsgBox(formSize, style);
                break;

            case AnimateStyle.ZoomIn:
                _msgBox.Size = new Size(formSize.Width + 100, formSize.Height + 100);
                _timer.Tag = new AnimateMsgBox(formSize, style);
                _timer.Interval = 1;
                break;
        }

        _timer.Tick += timer_Tick;
        _timer.Start();

        _msgBox.ShowDialog();
        //MessageBeep(0);
        return _buttonResult;
    }

    static void timer_Tick(object sender, EventArgs e)
    {
        var timer = (Timer)sender;
        var animate = (AnimateMsgBox)timer.Tag;

        switch (animate.Style)
        {
            case AnimateStyle.SlideDown:
                if (_msgBox.Height < animate.FormSize.Height)
                {
                    _msgBox.Height += 17;
                    _msgBox.Invalidate();
                }
                else
                {
                    _timer.Stop();
                    _timer.Dispose();
                }
                break;

            case AnimateStyle.FadeIn:
                if (_msgBox.Opacity < 1)
                {
                    _msgBox.Opacity += 0.1;
                    _msgBox.Invalidate();
                }
                else
                {
                    _timer.Stop();
                    _timer.Dispose();
                }
                break;

            case AnimateStyle.ZoomIn:
                if (_msgBox.Width > animate.FormSize.Width)
                {
                    _msgBox.Width -= 17;
                    _msgBox.Invalidate();
                }
                if (_msgBox.Height > animate.FormSize.Height)
                {
                    _msgBox.Height -= 17;
                    _msgBox.Invalidate();
                }
                break;
        }
    }

    private static void InitButtons(Buttons buttons)
    {
        switch (buttons)
        {
            case Buttons.AbortRetryIgnore:
                _msgBox.InitAbortRetryIgnoreButtons();
                break;

            case Buttons.OK:
                _msgBox.InitOkButton();
                break;

            case Buttons.OKCancel:
                _msgBox.InitOkCancelButtons();
                break;

            case Buttons.RetryCancel:
                _msgBox.InitRetryCancelButtons();
                break;

            case Buttons.YesNo:
                _msgBox.InitYesNoButtons();
                break;

            case Buttons.YesNoCancel:
                _msgBox.InitYesNoCancelButtons();
                break;
        }

        foreach (var btn in _msgBox._buttonCollection)
        {
            btn.ForeColor = Color.FromArgb(170, 170, 170);
            btn.ForeColor = Color.Black;
            btn.Font = new Font("Segoe UI", 8);
            btn.Padding = new Padding(3);
            btn.FlatStyle = FlatStyle.Flat;
            btn.Height = 30;
            btn.FlatAppearance.BorderColor = Color.FromArgb(99, 99, 98);

            _msgBox._flpButtons.Controls.Add(btn);
        }
    }

    private static void InitIcon(Icon icon)
    {
        switch (icon)
        {
            case Icon.Application:
                _msgBox._picIcon.Image = SystemIcons.Application.ToBitmap();
                break;

            case Icon.Exclamation:
                _msgBox._picIcon.Image = SystemIcons.Exclamation.ToBitmap();
                break;

            case Icon.Error:
                _msgBox._picIcon.Image = SystemIcons.Error.ToBitmap();
                break;

            case Icon.Info:
                _msgBox._picIcon.Image = SystemIcons.Information.ToBitmap();
                break;

            case Icon.Question:
                _msgBox._picIcon.Image = SystemIcons.Question.ToBitmap();
                break;

            case Icon.Shield:
                _msgBox._picIcon.Image = SystemIcons.Shield.ToBitmap();
                break;

            case Icon.Warning:
                _msgBox._picIcon.Image = SystemIcons.Warning.ToBitmap();
                break;
        }
    }

    private void InitAbortRetryIgnoreButtons()
    {
        var btnAbort = new Button { Text = @"Abortar" };
        btnAbort.Click += ButtonClick;
        CancelButton = btnAbort;

        var btnRetry = new Button { Text = @"Reintentar" };
        btnRetry.Click += ButtonClick;
        btnRetry.Width = 80;

        var btnIgnore = new Button { Text = @"Ignorar" };
        btnIgnore.Click += ButtonClick;

        _buttonCollection.Add(btnIgnore);
        _buttonCollection.Add(btnAbort);
        _buttonCollection.Add(btnRetry);

    }

    private void InitOkButton()
    {
        var btnOk = new Button { Text = @"OK" };
        btnOk.Click += ButtonClick;
        CancelButton = btnOk;

        _buttonCollection.Add(btnOk);
    }

    private void InitOkCancelButtons()
    {
        var btnOk = new Button { Text = @"OK" };
        btnOk.Click += ButtonClick;

        var btnCancel = new Button { Text = @"Cancelar" };
        btnCancel.Click += ButtonClick;
        CancelButton = btnCancel;

        _buttonCollection.Add(btnCancel);
        _buttonCollection.Add(btnOk);

    }

    private void InitRetryCancelButtons()
    {
        var btnRetry = new Button { Text = @"Reintentar" };
        btnRetry.Click += ButtonClick;

        var btnCancel = new Button { Text = @"Cancelar" };
        btnCancel.Click += ButtonClick;
        CancelButton = btnCancel;

        _buttonCollection.Add(btnCancel);
        _buttonCollection.Add(btnRetry);

    }

    private void InitYesNoButtons()
    {
        var btnYes = new Button { Text = @"Sí" };
        btnYes.Click += ButtonClick;

        var btnNo = new Button { Text = @"No" };
        btnNo.Click += ButtonClick;
        CancelButton = btnNo;

        _buttonCollection.Add(btnNo);
        _buttonCollection.Add(btnYes);

    }

    private void InitYesNoCancelButtons()
    {
        var btnYes = new Button { Text = @"Sí" };
        btnYes.Click += ButtonClick;

        var btnNo = new Button { Text = @"No" };
        btnNo.Click += ButtonClick;

        var btnCancel = new Button { Text = @"Cancelar" };
        btnCancel.Click += ButtonClick;
        CancelButton = btnCancel;

        _buttonCollection.Add(btnCancel);
        _buttonCollection.Add(btnNo);
        _buttonCollection.Add(btnYes);
    }

    private static void ButtonClick(object sender, EventArgs e)
    {
        var btn = (Button)sender;

        switch (btn.Text)
        {
            case "Abortar":
                _buttonResult = DialogResult.Abort;
                break;

            case "Reintentar":
                _buttonResult = DialogResult.Retry;
                break;

            case "Ignorar":
                _buttonResult = DialogResult.Ignore;
                break;

            case "OK":
                _buttonResult = DialogResult.OK;
                break;

            case "Cancelar":
                _buttonResult = DialogResult.Cancel;
                break;

            case "Sí":
                _buttonResult = DialogResult.Yes;
                break;

            case "No":
                _buttonResult = DialogResult.No;
                break;
        }

        _msgBox.Dispose();
    }

    private static Size MessageSize(string message, string title)
    {
        var g = _msgBox.CreateGraphics();
        var width = 320;
        var height = 320;

        var messageSize = g.MeasureString(message, new Font("Segoe UI", 10));
        var titleSize = g.MeasureString(title, new Font("Segoe UI", 15));

        if (message.Length < 150)
        {
            if ((int)messageSize.Width > 350 || (int)titleSize.Width > 350)
            {
                width = (messageSize.Width > titleSize.Width)
                    ? (int)messageSize.Width
                    : (int)(titleSize.Width + titleSize.Width / 4);
            }
        }
        else
        {
            //var groups = (from Match m in Regex.Matches(message, ".{1,180}") select m.Value).ToArray();
            //var lines = groups.Length + 1;
            width = 775;
            height += (int)(messageSize.Height + 50);
            if (height > 575)
                height = 575;
        }
        return new Size(width, height);
    }

    protected override CreateParams CreateParams
    {
        get
        {
            var cp = base.CreateParams;
            cp.ClassStyle |= CS_DROPSHADOW;
            return cp;
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        var g = e.Graphics;
        var rect = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));
        var pen = new Pen(Color.FromArgb(0, 151, 251));

        g.DrawRectangle(pen, rect);
    }

    public enum Buttons
    {
        AbortRetryIgnore = 1,
        // ReSharper disable once InconsistentNaming
        OK = 2,
        // ReSharper disable once InconsistentNaming
        OKCancel = 3,
        RetryCancel = 4,
        YesNo = 5,
        YesNoCancel = 6
    }

    public new enum Icon
    {
        Application = 1,
        Exclamation = 2,
        Error = 3,
        Warning = 4,
        Info = 5,
        Question = 6,
        Shield = 7,
        Search = 8
    }

    public enum AnimateStyle
    {
        SlideDown = 1,
        FadeIn = 2,
        ZoomIn = 3
    }

    //private void InitializeComponent()
    //{
    //    SuspendLayout();
    //    // 
    //    // MsgBox
    //    // 
    //    ClientSize = new Size(300, 375);
    //    Name = "MsgBox";
    //    ResumeLayout(false);

    //}

}

class AnimateMsgBox
{
    public Size FormSize;
    public readonly MsgBox.AnimateStyle Style;

    public AnimateMsgBox(Size formSize, MsgBox.AnimateStyle style)
    {
        FormSize = formSize;
        Style = style;
    }
}

}

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要设置_plHeader

的高度属性

如果需要动态更改高度,可以使用Graphics.MeasureString,TextRenderer.MeasureText或自动调整大小TextBox来获取面板所需的高度