自定义一个MessageBox

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Runtime.InteropServices;


partial class MessageBoxNew : Form
{


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


    [DllImport("Shell32.dll")]
    public extern static int ExtractIconEx(string libName, int iconIndex, IntPtr[] largeIcon, IntPtr[] smallIcon, int nIcons);
    static private int minHeight = 200;
    static private int maxHeihgt = 700;
    static private int minWidth = 400;
    static private int maxWidth = 1000;


    static private bool init = false;
    static private IntPtr[] largeIcon;
    static private IntPtr[] smallIcon;


    static private MessageBoxNew newMessageBox;
    static private Label frmTitle;
    static private Label frmMessage;
    static private PictureBox pIcon;
    static private FlowLayoutPanel flpButtons;
    static private Icon frmIcon;


    static MessageBoxButtons MButtons;
    static private Button btnOK;
    static private Button btnAbort;
    static private Button btnRetry;
    static private Button btnIgnore;
    static private Button btnCancel;
    static private Button btnYes;
    static private Button btnNo;
    static private MessageBoxDefaultButton defaultButton = MessageBoxDefaultButton.Button1;


    static private DialogResult CYReturnButton;


    static private void BuildMessageBox(string title)
    {
        newMessageBox = new MessageBoxNew();
        newMessageBox.Text = title;
        newMessageBox.Size = new System.Drawing.Size(400, 200);
        newMessageBox.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        newMessageBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        newMessageBox.Paint += new PaintEventHandler(newMessageBox_Paint);
        newMessageBox.KeyPreview = true;
        newMessageBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(MessageBoxNew.MessageBoxNew_KeyPress);
        newMessageBox.KeyDown += new System.Windows.Forms.KeyEventHandler(MessageBoxNew.MessageBoxNew_KeyDown);
        newMessageBox.BackColor = System.Drawing.Color.White;
        newMessageBox.Activated += new System.EventHandler(MessageBoxNew.MessageBoxNew_Activated);
        TableLayoutPanel tlp = new TableLayoutPanel();
        tlp.RowCount = 3;
        tlp.ColumnCount = 0;
        tlp.Dock = System.Windows.Forms.DockStyle.Fill;
        tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 22));
        tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
        tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 50));
        tlp.BackColor = System.Drawing.Color.Transparent;
        tlp.Padding = new Padding(2, 5, 2, 2);


        frmTitle = new Label();
        frmTitle.Dock = System.Windows.Forms.DockStyle.Fill;
        frmTitle.BackColor = System.Drawing.Color.Transparent;
        frmTitle.ForeColor = System.Drawing.Color.White;
        frmTitle.Font = new Font("宋体", 12, FontStyle.Bold);


        frmMessage = new Label();
        frmMessage.Dock = System.Windows.Forms.DockStyle.Fill;
        frmMessage.BackColor = System.Drawing.Color.White;
        frmMessage.Font = new Font("宋体", 14, FontStyle.Regular);
        frmMessage.Text = "hiii";
        pIcon = new PictureBox();
        if (!init)
        {
            largeIcon = new IntPtr[250];
            smallIcon = new IntPtr[250];


            ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 250);
            init = true;
        }




        flpButtons = new FlowLayoutPanel();
        flpButtons.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
        flpButtons.Padding = new Padding(0, 5, 5, 0);
        flpButtons.Dock = System.Windows.Forms.DockStyle.Fill;
        flpButtons.BackColor = System.Drawing.Color.FromArgb(240, 240, 240);


        TableLayoutPanel tlpMessagePanel = new TableLayoutPanel();
        tlpMessagePanel.BackColor = System.Drawing.Color.White;
        tlpMessagePanel.Dock = System.Windows.Forms.DockStyle.Fill;
        tlpMessagePanel.ColumnCount = 2;
        tlpMessagePanel.RowCount = 0;
        tlpMessagePanel.Padding = new Padding(4, 5, 4, 4);
        tlpMessagePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50));
        tlpMessagePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
        tlpMessagePanel.Controls.Add(pIcon);
        tlpMessagePanel.Controls.Add(frmMessage);


        tlp.Controls.Add(frmTitle);
        tlp.Controls.Add(tlpMessagePanel);
        tlp.Controls.Add(flpButtons);
        newMessageBox.Controls.Add(tlp);




    }


    protected override bool ProcessDialogKey(Keys keyData)
    {
        switch (keyData)
        {
            case Keys.Escape:
                CYReturnButton = DialogResult.Cancel;
                this.Close();
                break;
        }
        return base.ProcessDialogKey(keyData);
    }






    /// <summary>
    /// Message: Text to display in the message box.
    /// </summary>
    static public DialogResult Show(string Message)
    {
        BuildMessageBox("");
        frmMessage.Text = Message;
        ReSizeWindow();
        ButtonStatements(MessageBoxButtons.OK);
        IconStatements(MessageBoxIcon.Warning);
        Image imageIcon = new Bitmap(frmIcon.ToBitmap(), 38, 38);
        pIcon.Image = imageIcon;
        newMessageBox.ShowDialog();
        return CYReturnButton;
    }


    private static void ReSizeWindow()
    {
        Graphics g = newMessageBox.CreateGraphics();
        SizeF sizeF = g.MeasureString(frmMessage.Text, new Font("宋体", 12));
        sizeF = new Size((int)(sizeF.Width * 1.8), (int)(sizeF.Height * 2));
        sizeF.Height = sizeF.Height > maxHeihgt ? maxHeihgt : sizeF.Height;
        sizeF.Width = sizeF.Width > maxWidth ? maxWidth : sizeF.Width;
        sizeF.Height = sizeF.Height < minHeight ? minHeight : sizeF.Height;
        sizeF.Width = sizeF.Width < minWidth ? minWidth : sizeF.Width;
        newMessageBox.Size = new System.Drawing.Size((int)sizeF.Width, (int)sizeF.Height);
        g.Dispose();
    }


    /// <summary>
    /// Title: Text to display in the title bar of the messagebox.
    /// </summary>
    static public DialogResult Show(string Message, string Title)
    {
        BuildMessageBox(Title);
        frmTitle.Text = Title;
        frmMessage.Text = Message;
        ReSizeWindow();
        ButtonStatements(MessageBoxButtons.OK);
        IconStatements(MessageBoxIcon.Warning);
        Image imageIcon = new Bitmap(frmIcon.ToBitmap(), 38, 38);
        pIcon.Image = imageIcon;
        newMessageBox.ShowDialog();
        return CYReturnButton;
    }


    /// <summary>
    /// MButtons: Display CYButtons on the message box.
    /// </summary>
    static public DialogResult Show(string Message, string Title, MessageBoxButtons MButtons)
    {
        BuildMessageBox(Title); // BuildMessageBox method, responsible for creating the MessageBox
        frmTitle.Text = Title; // Set the title of the MessageBox
        frmMessage.Text = Message; //Set the text of the MessageBox
        ReSizeWindow();
        ButtonStatements(MButtons); // ButtonStatements method is responsible for showing the appropreiate buttons
        newMessageBox.ShowDialog(); // Show the MessageBox as a Dialog.
        return CYReturnButton; // Return the button click as an Enumerator
    }


    /// <summary>
    /// MIcon: Display CYIcon on the message box.
    /// </summary>
    static public DialogResult Show(string Message, string Title, MessageBoxButtons MButtons, MessageBoxIcon MIcon)
    {
        BuildMessageBox(Title);
        frmTitle.Text = Title;
        frmMessage.Text = Message;
        ReSizeWindow();
        ButtonStatements(MButtons);
        IconStatements(MIcon);
        Image imageIcon = new Bitmap(frmIcon.ToBitmap(), 38, 38);
        pIcon.Image = imageIcon;
        newMessageBox.ShowDialog();
        return CYReturnButton;
    }


    /// <summary>
    /// MIcon: Display CYIcon on the message box.
    /// </summary>
    static public DialogResult Show(string Message, string Title, MessageBoxButtons MButtons, MessageBoxIcon MIcon, MessageBoxDefaultButton button)
    {
        BuildMessageBox(Title);
        frmTitle.Text = Title;
        frmMessage.Text = Message;
        ReSizeWindow();
        ButtonStatements(MButtons);
        IconStatements(MIcon);
        Image imageIcon = new Bitmap(frmIcon.ToBitmap(), 38, 38);
        pIcon.Image = imageIcon;
        defaultButton = button;
        newMessageBox.ShowDialog();
        return CYReturnButton;
    }


    static void btnOK_Click(object sender, EventArgs e)
    {
        CYReturnButton = DialogResult.OK;
        newMessageBox.Dispose();
    }


    static void btnAbort_Click(object sender, EventArgs e)
    {
        CYReturnButton = DialogResult.Abort;
        newMessageBox.Dispose();
    }


    static void btnRetry_Click(object sender, EventArgs e)
    {
        CYReturnButton = DialogResult.Retry;
        newMessageBox.Dispose();
    }


    static void btnIgnore_Click(object sender, EventArgs e)
    {
        CYReturnButton = DialogResult.Ignore;
        newMessageBox.Dispose();
    }


    static void btnCancel_Click(object sender, EventArgs e)
    {
        CYReturnButton = DialogResult.Cancel;
        newMessageBox.Dispose();
    }


    static void btnYes_Click(object sender, EventArgs e)
    {
        CYReturnButton = DialogResult.Yes;
        newMessageBox.Dispose();
    }


    static void btnNo_Click(object sender, EventArgs e)
    {
        CYReturnButton = DialogResult.No;
        newMessageBox.Dispose();
    }


    static private void ShowOKButton()
    {
        btnOK = new Button();
        btnOK.Text = "确认";
        btnOK.Size = new System.Drawing.Size(80, 25);
        btnOK.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
        btnOK.Font = new Font("宋体", 12, FontStyle.Regular);
        btnOK.Click += new EventHandler(btnOK_Click);
        flpButtons.Controls.Add(btnOK);
    }


    static private void ShowAbortButton()
    {
        btnAbort = new Button();
        btnAbort.Text = "退出";
        btnAbort.Size = new System.Drawing.Size(80, 25);
        btnAbort.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
        btnAbort.Font = new Font("宋体", 12, FontStyle.Regular);
        btnAbort.Click += new EventHandler(btnAbort_Click);
        flpButtons.Controls.Add(btnAbort);
    }


    static private void ShowRetryButton()
    {
        btnRetry = new Button();
        btnRetry.Text = "重试";
        btnRetry.Size = new System.Drawing.Size(80, 25);
        btnRetry.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
        btnRetry.Font = new Font("宋体", 12, FontStyle.Regular);
        btnRetry.Click += new EventHandler(btnRetry_Click);
        flpButtons.Controls.Add(btnRetry);
    }


    static private void ShowIgnoreButton()
    {
        btnIgnore = new Button();
        btnIgnore.Text = "忽略";
        btnIgnore.Size = new System.Drawing.Size(80, 25);
        btnIgnore.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
        btnIgnore.Font = new Font("宋体", 12, FontStyle.Regular);
        btnIgnore.Click += new EventHandler(btnIgnore_Click);
        flpButtons.Controls.Add(btnIgnore);
    }


    static private void ShowCancelButton()
    {
        btnCancel = new Button();
        btnCancel.Text = "取消";
        btnCancel.Size = new System.Drawing.Size(80, 25);
        btnCancel.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
        btnCancel.Font = new Font("宋体", 12, FontStyle.Regular);
        btnCancel.Click += new EventHandler(btnCancel_Click);
        flpButtons.Controls.Add(btnCancel);
    }


    static private void ShowYesButton()
    {
        btnYes = new Button();
        btnYes.Text = "是";
        btnYes.Size = new System.Drawing.Size(80, 25);
        btnYes.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
        btnYes.Font = new Font("宋体", 12, FontStyle.Regular);
        btnYes.Click += new EventHandler(btnYes_Click);
        flpButtons.Controls.Add(btnYes);
    }


    static private void ShowNoButton()
    {
        btnNo = new Button();
        btnNo.Text = "否";
        btnNo.Size = new System.Drawing.Size(80, 25);
        btnNo.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
        btnNo.Font = new Font("宋体", 12, FontStyle.Regular);
        btnNo.Click += new EventHandler(btnNo_Click);
        flpButtons.Controls.Add(btnNo);
    }


    static private void ButtonStatements(MessageBoxButtons MButtons)
    {
        MessageBoxNew.MButtons = MButtons;
        if (MButtons == MessageBoxButtons.AbortRetryIgnore)
        {
            ShowRetryButton();
            ShowAbortButton();
            ShowIgnoreButton();
        }


        if (MButtons == MessageBoxButtons.OK)
        {
            ShowOKButton();
        }


        if (MButtons == MessageBoxButtons.OKCancel)
        {
            ShowCancelButton();
            ShowOKButton();
        }


        if (MButtons == MessageBoxButtons.RetryCancel)
        {
            ShowCancelButton();
            ShowRetryButton();
        }


        if (MButtons == MessageBoxButtons.YesNo)
        {
            ShowNoButton();
            ShowYesButton();
        }


        if (MButtons == MessageBoxButtons.YesNoCancel)
        {
            ShowCancelButton();
            ShowNoButton();
            ShowYesButton();
        }
    }


    static private void IconStatements(MessageBoxIcon MIcon)
    {
        if (MIcon == MessageBoxIcon.Error)
        {
            MessageBeep(30);
            frmIcon = Icon.FromHandle(largeIcon[109]);
        }


        if (MIcon == MessageBoxIcon.Information)
        {
            MessageBeep(0);
            frmIcon = Icon.FromHandle(largeIcon[221]);
        }


        if (MIcon == MessageBoxIcon.Question)
        {
            MessageBeep(0);
            frmIcon = Icon.FromHandle(largeIcon[23]);
        }


        if (MIcon == MessageBoxIcon.Stop)
        {
            MessageBeep(0);
            frmIcon = Icon.FromHandle(largeIcon[27]);
        }


        if (MIcon == MessageBoxIcon.Warning)
        {
            MessageBeep(30);
            frmIcon = Icon.FromHandle(largeIcon[233]);
        }
    }


    static void newMessageBox_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        Rectangle frmTitleL = new Rectangle(0, 0, (newMessageBox.Width / 2), 22);
        Rectangle frmTitleR = new Rectangle((newMessageBox.Width / 2), 0, (newMessageBox.Width / 2), 22);
        Rectangle frmMessageBox = new Rectangle(0, 0, (newMessageBox.Width - 1), (newMessageBox.Height - 1));
        LinearGradientBrush frmLGBL = new LinearGradientBrush(frmTitleL, Color.FromArgb(87, 148, 160), Color.FromArgb(209, 230, 243), LinearGradientMode.Horizontal);
        LinearGradientBrush frmLGBR = new LinearGradientBrush(frmTitleR, Color.FromArgb(209, 230, 243), Color.FromArgb(87, 148, 160), LinearGradientMode.Horizontal);
        Pen frmPen = new Pen(Color.FromArgb(63, 119, 143), 1);
        g.FillRectangle(frmLGBL, frmTitleL);
        g.FillRectangle(frmLGBR, frmTitleR);
        g.DrawRectangle(frmPen, frmMessageBox);
    }


    public static void MessageBoxNew_KeyPress(object sender, KeyPressEventArgs e)
    {
        switch (e.KeyChar)
        {
            case '1':
                CYReturnButton = DialogResult.Cancel;
                break;
        }
    }


    private static void MessageBoxNew_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Escape:
                CYReturnButton = DialogResult.Cancel;
                break;
        }
    }


    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // MessageBoxNew
        // 
        this.ClientSize = new System.Drawing.Size(286, 221);
        this.Name = "MessageBoxNew";
        this.ResumeLayout(false);


    }


    static void MessageBoxNew_Activated(object sender, EventArgs e)
    {
        if (MButtons == MessageBoxButtons.OKCancel
            || MButtons == MessageBoxButtons.YesNo
            || MButtons == MessageBoxButtons.RetryCancel)
        {
            switch (defaultButton)
            {
                case MessageBoxDefaultButton.Button1:
                    flpButtons.Controls[1].Focus();
                    break;
                case MessageBoxDefaultButton.Button2:
                    flpButtons.Controls[0].Focus();
                    break;
            }
        }


        if (MButtons == MessageBoxButtons.AbortRetryIgnore
            || MButtons == MessageBoxButtons.YesNoCancel)
        {
            switch (defaultButton)
            {
                case MessageBoxDefaultButton.Button1:
                    flpButtons.Controls[2].Focus();
                    break;
                case MessageBoxDefaultButton.Button2:
                    flpButtons.Controls[1].Focus();
                    break;
                case MessageBoxDefaultButton.Button3:
                    flpButtons.Controls[0].Focus();
                    break;
            }
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值