winform里在电脑屏幕右下角弹出类似QQ消息框淡入淡出

本文介绍了一种仿照QQ弹窗效果的实现方案,通过C#和WinForms创建了一个可自定义显示与隐藏状态的弹窗应用。该应用利用了自定义窗口样式和动画效果,实现了平滑过渡的显示和隐藏功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个是我从同事那里拿来的,分享下:

新建一个Form窗体名为:AlertWindows

 

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TestDEMO
{
    public partial class AlertWindows : Form
    {
        #region 变量
        private InformStyle InfoStyle = InformStyle.Vanish;//定义变量为隐藏
        private System.Drawing.Rectangle Rect;//定义一个存储矩形框的数组
        private bool isMouseMove = false;//是否在窗体中移动
        static private AlertWindows alertwindows = new AlertWindows();//实例化当前窗体
        #endregion

        #region 内置属性
        /// <summary>
        /// 定义一个任务通知器的枚举值
        /// </summary>//InformStyle
        protected enum InformStyle
        {
            /// <summary>
            /// 隐藏
            /// </summary>
            Vanish = 0,
            /// <summary>
            /// 显视
            /// </summary>
            Display = 1,
            /// <summary>
            /// 显视中
            /// </summary>
            Displaying = 2,
            /// <summary>
            /// 隐藏中
            /// </summary>
            Vanishing = 3
        }        
        
        /// <summary>
        /// 获取或设置当前的操作状态
        /// </summary>
        protected InformStyle InfoState
        {
            get { return this.InfoStyle; }
            set { this.InfoStyle = value; }
        }
        #endregion

        public AlertWindows()
		{
			this.InitializeComponent();
			this.alerttimer.Stop();//停止计时器
			//初始化工作区大小
			System.Drawing.Rectangle rect = System.Windows.Forms.Screen.GetWorkingArea(this); 
			this.Rect = new System.Drawing.Rectangle( rect.Right - this.Width - 1, rect.Bottom - this.Height - 1, this.Width, this.Height );
		}

        #region 返回当前窗体的实例化
        /// <summary>
        /// 返回此对象的实例
        /// </summary>
        /// <returns></returns>
        static public AlertWindows Instance()
        {
            return  alertwindows;
        }
        #endregion

        #region 声明WinAPI
        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="nCmdShow"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);
        #endregion

        #region 方法
        /// <summary>
        /// 显示窗体
        /// </summary>
        public void Show(string value)
        {
            this.label2.Text = value;
            switch (this.InfoState)
            {
                case InformStyle.Vanish://窗体隐藏
                    this.InfoState = InformStyle.Displaying;//设置窗体的操作状态为显示中
                    this.SetBounds(Rect.X, Rect.Y + Rect.Height, Rect.Width, 0);//显示Popup窗体,并放置到屏幕的底部
                    ShowWindow(this.Handle, 4);//显示窗体
                    this.alerttimer.Interval = 100;//设置时间间隔为100
                    this.alerttimer.Start();//启动计时器
                    break;
                case InformStyle.Display://窗体显示
                    this.alerttimer.Stop();//停止计时器
                    this.alerttimer.Interval = 5000;//设置时间间隔为5000
                    this.alerttimer.Start();//启动记时器
                    break;
            }
        }
        #endregion

        #region 事件
        private void alerttimer_Tick(object sender, System.EventArgs e)
        {
            //switch (this.InfoState)
            //{
            //    case InformStyle.Display://显示当前窗体
            //        this.alerttimer.Stop();//停止计时器
            //        this.alerttimer.Interval = 100;//设置时间间隔为100
            //        if (!(this.isMouseMove))//如果鼠标不在窗体中移动
            //            this.InfoState = InformStyle.Vanishing;//设置当前窗体的操作状态为隐藏中
            //        this.alerttimer.Start();//启动计时器
            //        break;
            //    case InformStyle.Displaying://当前窗体显示中
            //        if (this.Height <= this.Rect.Height - 12)//当窗体没有完全显示时
            //            this.SetBounds(Rect.X, this.Top - 12, Rect.Width, this.Height + 12);//使窗体不断上移
            //        else
            //        {
            //            this.alerttimer.Stop();//停止计时器
            //            this.SetBounds(Rect.X, Rect.Y, Rect.Width, Rect.Height);//设置当前窗体的边界
            //            this.InfoState = InformStyle.Display;//设置当前窗体的操作状态为显示
            //            this.alerttimer.Interval = 5000;//设置时间间隔为5000
            //            this.alerttimer.Start();//启动计时器
            //        }
            //        break;
            //    case InformStyle.Vanishing://隐藏当前窗体
            //        if (this.isMouseMove)//如果鼠标在窗体中移动
            //            this.InfoState = InformStyle.Displaying;//设置窗体的操作状态为显示
            //        else
            //        {
            //            if (this.Top <= this.Rect.Bottom - 12)//如果窗体没有完全隐藏
            //                this.SetBounds(Rect.X, this.Top + 12, Rect.Width, this.Height - 12);//使窗体不断下移
            //            else
            //            {
            //                this.Hide();//隐藏当前窗体
            //                this.InfoState = InformStyle.Vanish;//设置窗体的操作状态为隐藏
            //            }
            //        }
            //        break;
            //}
        }

        private void Frm_Popup_MouseMove(object sender, MouseEventArgs e)
        {
            this.isMouseMove = true;//当鼠标移入时,设为true
        }

        private void Frm_Popup_MouseLeave(object sender, EventArgs e)
        {
            this.isMouseMove = false;//当鼠标移出时,设为false
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (this.InfoState != InformStyle.Vanish)//如果窗体的状态不是隐藏
            {
                this.alerttimer.Stop();//停止计时器
                this.InfoState = InformStyle.Vanish;//设置窗体的操作状态为隐藏
                base.Hide();//隐藏当前窗体
            }
        }

      
        #endregion

        private void alerttimer_Tick_1(object sender, EventArgs e)
        {
            switch (this.InfoState)
            {
                case InformStyle.Display://显示当前窗体
                    this.alerttimer.Stop();//停止计时器
                    this.alerttimer.Interval = 100;//设置时间间隔为100
                    if (!(this.isMouseMove))//如果鼠标不在窗体中移动
                        this.InfoState = InformStyle.Vanishing;//设置当前窗体的操作状态为隐藏中
                    this.alerttimer.Start();//启动计时器
                    break;
                case InformStyle.Displaying://当前窗体显示中
                    if (this.Height <= this.Rect.Height - 12)//当窗体没有完全显示时
                        this.SetBounds(Rect.X, this.Top - 12, Rect.Width, this.Height + 12);//使窗体不断上移
                    else
                    {
                        this.alerttimer.Stop();//停止计时器
                        this.SetBounds(Rect.X, Rect.Y, Rect.Width, Rect.Height);//设置当前窗体的边界
                        this.InfoState = InformStyle.Display;//设置当前窗体的操作状态为显示
                        this.alerttimer.Interval = 5000;//设置时间间隔为5000
                        this.alerttimer.Start();//启动计时器
                    }
                    break;
                case InformStyle.Vanishing://隐藏当前窗体
                    if (this.isMouseMove)//如果鼠标在窗体中移动
                        this.InfoState = InformStyle.Displaying;//设置窗体的操作状态为显示
                    else
                    {
                        if (this.Top <= this.Rect.Bottom - 12)//如果窗体没有完全隐藏
                            this.SetBounds(Rect.X, this.Top + 12, Rect.Width, this.Height - 12);//使窗体不断下移
                        else
                        {
                            this.Hide();//隐藏当前窗体
                            this.InfoState = InformStyle.Vanish;//设置窗体的操作状态为隐藏
                        }
                    }
                    break;
            }
        }

        private void pictureBox1_Click_1(object sender, EventArgs e)
        {
            if (this.InfoState != InformStyle.Vanish)//如果窗体的状态不是隐藏
            {
                this.alerttimer.Stop();//停止计时器
                this.InfoState = InformStyle.Vanish;//设置窗体的操作状态为隐藏
                this.Hide();
                //base.Hide();//隐藏当前窗体
               // this.Close();
            }
        }
    }
}


最后调用的时候,只需调用Show(string value),Value是你想弹出什么样的内容。

类似QQ弹出框DEMO

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值