• 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();
            }

    效果:

  • 相关阅读:
    Tomcat架构解析(五)-----Tomcat的类加载机制
    session与cookie
    freemarker常用标签解释遍历
    freemarker常用标签解释三
    freemarker常用标签解释二
    freemarker常用标签解释
    禁止浏览器自动填充
    使用cookie实现自动登录
    长连接和短连接
    filter防止xxs攻击
  • 原文地址:https://www.cnblogs.com/xubao/p/14047592.html
Copyright © 2020-2023  润新知