C#-自定义InformationBox

using System;
using System.Threading;
using System.Windows.Forms;

namespace InformationBoxE {
    public partial class InformationBox : Form {
        private Thread timeoutThread;       //倒计时线程
        private bool autoclose = false;     //是否是自动关闭,还是直接点击确定或关闭按钮关闭
        private bool pause = false;         //暂停

        public InformationBox() {
            InitializeComponent();
        }

        //关闭的操作
        private void InformationBox_FormClosing(object sender, FormClosingEventArgs e) {
            if (timeoutThread != null) {
                if (timeoutThread.IsAlive && !autoclose) {
                    try {
                        timeoutThread.Abort();
                    } catch { }
                }
            }
            this.Dispose();
        }

        //快捷键
        private void InformationBox_KeyPress(object sender, KeyPressEventArgs e) {
            e.Handled = true;
            if (e.KeyChar == 15) //确定:Ctrl + O
            {
                OKButton.PerformClick();
            }
        }

        //确定
        private void OKButton_Click(object sender, EventArgs e) {
            this.Close();
        }

        //双击暂停或开始
        private void pausePb_DoubleClick(object sender, EventArgs e) {
            //判断当前状态是否暂停
            if (pause) {
                pause = false;
                PausePb.Image = Properties.Resources.PauseIcon;
                PauseTip.RemoveAll();
                PauseTip.SetToolTip(PausePb, "双击暂停倒计时");
            } else {
                pause = true;
                PausePb.Image = Properties.Resources.StartIcon;
                PauseTip.RemoveAll();
                PauseTip.SetToolTip(PausePb, "双击开始倒计时");
            }
        }

        //显示
        public void Show(IWin32Window owner, string text = "", string caption = "", InformationBoxIcon icon = InformationBoxIcon.Information, int secondsTimeout = 10) {
            if (text == null)
                text = "";
            if (caption == null)
                caption = "";
            if (secondsTimeout < 2)     //最小倒计时为2
                secondsTimeout = 2;
            if (secondsTimeout > 99)     //最大倒计时为99
                secondsTimeout = 99;
            //提示框的宽和高
            testLab.Text = caption;     //标题长度比较
            int width = 206;
            width = (testLab.Width + 50 > width) ? testLab.Width + 50 : width;      //最小宽度为206
            testLab.Text = "";
            this.Text = caption;
            width = (width < 498) ? width : 498;        //最大宽度为498
            InformationTxt.Text = text;
            width = (498 - 380 + InformationTxt.Width > width) ? 498 - 380 + InformationTxt.Width : width;      //内容长度比较。这里不需要比较最大宽度,由于InformationTxt设置了最大宽度390
            int height = 188;
            if (InformationTxt.Height / 17 > 2)     //最小高度为188
            {
                height += 17 * (InformationTxt.Height / 17 - 2);    //内容高度比较
            }
            this.Size = new System.Drawing.Size(width, height);     //重绘提示框大小
            //倒计时
            TimeLab2.Text = secondsTimeout.ToString();
            //按钮提示
            OKTip.SetToolTip(OKButton, OKButton.Text + "  快捷键: Ctrl+O");    //Ctrl+O:15
            PauseTip.SetToolTip(PausePb, "双击暂停倒计时");
            //图标,提示音
            if (icon == InformationBoxIcon.Error) {
                IconPb.Image = Properties.Resources.ErrorIcon;
                System.Media.SystemSounds.Hand.Play();
            } else if (icon == InformationBoxIcon.Information) {
                IconPb.Image = Properties.Resources.InformationIcon;
                System.Media.SystemSounds.Asterisk.Play();
            } else if (icon == InformationBoxIcon.Question) {
                IconPb.Image = Properties.Resources.QuestionIcon;
                System.Media.SystemSounds.Beep.Play();
            } else if (icon == InformationBoxIcon.Warning) {
                IconPb.Image = Properties.Resources.WarningIcon;
                System.Media.SystemSounds.Exclamation.Play();
            }
            //显示在所有窗体的最上方
            this.TopMost = true;
            //弹出提示框
            base.Show(owner);
            //开启倒计时
            timeoutThread = new Thread(autoCloseThread);
            timeoutThread.IsBackground = true;
            timeoutThread.Start(secondsTimeout);
        }

        //自动关闭线程
        private void autoCloseThread(object timeObj) {
            try {
                int time = 10 * 5;       //默认时间
                try {
                    time = ( int ) timeObj * 5;
                } catch {
                    time = 10 * 5;
                }
                while (time > 0) {
                    if (!pause) {
                        Thread.Sleep(200);
                        time--;
                        //更新倒计时
                        if (time % 5 == 0) {
                            TimeLab2.Invoke(( EventHandler ) (delegate {
                                TimeLab2.Text = (time / 5).ToString();
                            }));
                        }
                    }
                }
                //Thread.Sleep(1000);
                TimeLab3.Invoke(( EventHandler ) (delegate {
                    TimeLab1.Visible = false;
                    TimeLab2.Visible = false;
                    TimeLab3.Visible = false;
                    TimeLab4.Visible = true;
                }));
                Thread.Sleep(500);
                this.Invoke(( EventHandler ) (delegate {
                    autoclose = true;   //自动关闭
                    this.Close();
                }));
            } catch { }
        }
    }

    //图标
    public enum InformationBoxIcon {
        Error = 0,          //错误
        Information = 1,    //消息
        Question = 2,       //询问
        Warning = 3         //警告
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InformationBoxE
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            iconTxt.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //标题
            string caption = captionTxt.Text;
            //内容
            string text = textTxt.Text;
            //图标
            InformationBoxIcon icon = InformationBoxIcon.Information;
            if (iconTxt.Text == "错误")
            {
                icon = InformationBoxIcon.Error;
            }
            else if (iconTxt.Text == "询问")
            {
                icon = InformationBoxIcon.Question;
            }
            else if (iconTxt.Text == "警告")
            {
                icon = InformationBoxIcon.Warning;
            }
            //时间
            int time = 10;
            try
            {
                time = int.Parse(timeTxt.Text);
            }
            catch
            {
                time = 10;
            }
            //提示框
            InformationBoxShow(text, caption, icon, time);
        }

        //弹出提示框
        public void InformationBoxShow(string text = "", string caption = "", InformationBoxIcon icon = InformationBoxIcon.Information, int secondsTimeout = 10)
        {
            InformationBox informationBox = new InformationBox();
            informationBox.Show(this, text, caption, icon, secondsTimeout);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://blog.csdn.net/softimite_zifeng");
        }
    }
}
原文地址:https://www.cnblogs.com/grj001/p/12223337.html