C#窗体右下角弹框

C#窗体右下角弹框

1. 弹框窗体

1.1 属性

在这里插入图片描述

  • 窗体属性
    • AtuoScaleMode: None
    • FormBorderStyle: None 无边框
  • pictureBox 属性
    • BackColor: Highlight
    • SizeMode: Zoom
  • Button 属性
    • FlatStyle: Flat
    • ForeColor: White
    • Image:图片
    • ImageAlign: MiddleCenter

1.2 代码

private Form_Alert.enmAction action;    //当前窗体当前动作
        private int x, y;                       //显示窗体的坐标变量

        //枚举窗体动作
        public enum enmAction
        {
            Wait,
            Start,
            Close
        }

        //枚举窗体类型
        public enum enmType
        {
            Success,
            Warning,
            Error,
            Info
        }
        
        // 外部访问该函数实现窗体的显示(显示信息,弹窗类型)
        public void showAlert(string msg, enmType type)
        {
            this.Opacity = 0.0;
            this.StartPosition = FormStartPosition.Manual;
            string fname;

            for (int i = 1; i < 10; i++)
            {
                fname = "alert" + i.ToString();
                Form_Alert frm = (Form_Alert)Application.OpenForms[fname];


                if (frm == null)
                {
                    this.Name = fname;
                    this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width + 15;
                    this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height * i - 5 * i;
                    this.Location = new Point(this.x, this.y);
                    break;
                }
            }
            this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width - 5;

            switch (type)
            {
                // 根据类型设置弹框的颜色和图标
                case enmType.Success:
                    this.pictureBox1.Image = Resources.success;
                    this.BackColor = Color.SeaGreen;
                    break;
                case enmType.Error:
                    this.pictureBox1.Image = Resources.error;
                    this.BackColor = Color.DarkRed;
                    break;
                case enmType.Info:
                    this.pictureBox1.Image = Resources.info;
                    this.BackColor = Color.RoyalBlue;
                    break;
                case enmType.Warning:
                    this.pictureBox1.Image = Resources.warning;
                    this.BackColor = Color.DarkOrange;
                    break;
            }


            this.LblMsg.Text = msg; //显示弹窗信息
            this.Show();            //显示
            this.action = enmAction.Start;
            this.timer1.Interval = 1;
            timer1.Start();

        }
        //关闭窗体
        private void Button1_Click(object sender, EventArgs e)
        {
            timer1.Interval = 1;
            action = enmAction.Close;
        }


        // 利用定时器来处理窗体的显示过度和行动事件
        // 监听每1毫秒当前弹窗的动作,设置Opacity透明度,用来过度显示和关闭。
        private void Timer1_Tick(object sender, EventArgs e)
        {
            switch (this.action)
            {
                case enmAction.Wait:
                    timer1.Interval = 5000;
                    action = enmAction.Close;
                    break;
                case enmAction.Start:
                    timer1.Interval = 1;
                    this.Opacity += 0.1;
                    if (this.x < this.Location.X)
                    {
                        this.Left--;
                    }
                    else
                    {
                        if (this.Opacity == 1.0)
                        {
                            action = enmAction.Wait;
                        }
                    }
                    break;
                case enmAction.Close:
                    this.timer1.Interval = 1;
                    this.Opacity -= 0.1;
                    this.Left -= 3;

                    if (base.Opacity == 0.0)
                    {
                        base.Close();
                    }
                    break;

            }
        }

2. 父窗体

在这里插入图片描述

属性

序号名称颜色
1SuccessSeaGreen
2ErrorDarRed
3WarningDarkOrange
4InfoRoyalBlue

代码

  1. 弹窗显示部分代码
       //弹窗显示程序
        public void Alert(string msg,Form_Alert.enmType type)
        {
            Form_Alert frm = new Form_Alert();
            frm.showAlert(msg,type);
        }
  1. 按钮调用部分
        //在调用的地方使用.例如:Button事件
        //弹出成功对话框
        private void Button1_Click(object sender, EventArgs e)
        {
            this.Alert("Success Alert", Form_Alert.enmType.Success);
        }
        //警告
        private void Button2_Click(object sender, EventArgs e)
        {
            this.Alert("Warning Alert", Form_Alert.enmType.Warning);
        }
        //错误
        private void Button3_Click(object sender, EventArgs e)
        {
            this.Alert("Error Alert", Form_Alert.enmType.Error);
        }
        //提示
        private void Button4_Click(object sender, EventArgs e)
        {
            this.Alert("Info Alert", Form_Alert.enmType.Info);
        }

弹框效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值