C# 自定义弹窗提醒

    class ShaderBox
    {
        private Form _shader;
        private Form _parent;
        private Form _child;

        public ShaderBox(Form parent, Form child)
        {
            _parent = parent;
            _child = child;

            _shader = new Form()
            {
                Owner = _parent,
                Location = _parent.Location,
                Size = _parent.Size,
                BackColor = System.Drawing.Color.DimGray,
                //DoubleBuffered = true,
                FormBorderStyle = FormBorderStyle.None,
                Opacity = 0.8D,
                ShowInTaskbar = false,
                StartPosition = FormStartPosition.CenterParent
            };
            _child.Owner = _shader;
            _child.MaximumSize = new System.Drawing.Size(_shader.Width * 80 / 100, _shader.Height * 80 / 100);
            _child.Font = _parent.Font;

            _shader.Shown += Shader_Shown;
        }

        public void Show()
        {
            _shader.Show();
            _shader.Location = _parent.Location;
            _shader.BringToFront();
        }

        public void Hide()
        {
            _shader.Hide();
        }

        public void Close()
        {
            _shader.Close();
        }

        private void Shader_Shown(object sender, EventArgs e)
        {
            _child.Show();

            int x = _shader.Left + (_shader.Width - _child.Width) / 2;
            int y = _shader.Top + (_shader.Height - _child.Height) / 2;
            _child.Location = new System.Drawing.Point(x, y);

            _child.BringToFront();
        }
    }

    class WaitBox : Form
    {
        private Label _lblMsg = null;
        private Timer _timer = null;
        private Label _lblElapsed = null;
        private long _elapsed = 0;

        /// <summary>
        /// 消息
        /// </summary>
        public string Msg
        {
            get => _lblMsg.Text;
            set => _lblMsg.Text = value;
        }

        public WaitBox()
        {
            Init();

            FontChanged += WaitBox_FontChanged;
        }

        private void WaitBox_FontChanged(object sender, EventArgs e)
        {
            _lblMsg.Font = Font;
        }

        private void Init()
        {
            Width = 400;
            Height = 70;
            FormBorderStyle = FormBorderStyle.None;
            AutoScaleMode = AutoScaleMode.None;
            ShowInTaskbar = false;
            Shown += WaitBox_Shown;

            _lblElapsed = new Label
            {
                Dock = DockStyle.Top,
                TextAlign = ContentAlignment.MiddleRight,
                Text = "",
                ForeColor = Color.Blue,
                Font = new Font(SystemFonts.DefaultFont, FontStyle.Regular),
                Parent = this
            };
            Controls.Add(_lblElapsed);

            _lblMsg = new Label
            {
                Dock = DockStyle.Fill,
                TextAlign = ContentAlignment.MiddleCenter,
                Text = "请等待...",
                Parent = this
            };
            Controls.Add(_lblMsg);

            _timer = new Timer();
            _timer.Interval = 1000;
            _timer.Tick += _timer_Tick;
        }

        private void WaitBox_Shown(object sender, EventArgs e)
        {
            _timer.Start();
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            _elapsed++;
            _lblElapsed.Text = string.Format("{0}s", _elapsed);
        }
    }

    class WaitHelper
    {
        private static ShaderBox _shaderBox = null;
        private static WaitBox _childBox = null;

        /// <summary>
        /// 等待开始
        /// </summary>
        /// <param name="parent">父窗体</param>
        /// <param name="msg">提示消息</param>
        public static void WaitBegin(Form parent, string msg)
        {
            _childBox = new WaitBox
            {
                Msg = msg
            };
            _shaderBox = new ShaderBox(parent, _childBox);
            _shaderBox.Show();
            Application.DoEvents();
        }
        /// <summary>
        /// 实时更新消息
        /// </summary>
        /// <param name="msg"></param>
        public static void WaitUpdate(string msg)
        {
            _childBox.Msg = msg;
        }
        /// <summary>
        /// 等待结束
        /// </summary>
        public static void WaitEnd()
        {
            _shaderBox.Close();
            _shaderBox = null;
            _childBox = null;
        }
    }

调用:

        private void button1_Click(object sender, EventArgs e)
        {
            WaitHelper.WaitBegin(this, "你好,我的朋友!");
            for (int q = 0; q < 100000; q++)
            {
                textBox1.Text = q.ToString();
                Application.DoEvents();//实时响应文本框中的值

                //WaitHelper.WaitUpdate(q.ToString());
            }
            WaitHelper.WaitEnd();
        }

效果:

 

posted @ 2020-11-27 14:00  许宝  阅读(1731)  评论(0编辑  收藏  举报