C# .NET做一个飘窗提示

先右键项目->添加->用户控件,命名为FloatingWindow.cs

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

namespace LzdGame
{
    public partial class FloatingWindow : UserControl
    {
        private Timer timer;
        private int startY;
        private int targetY;
        private float opacity = 1f; // 初始不透明度为1

        public FloatingWindow(string message)
        {
            Label label = new Label();
            label.Text = message;
            label.ForeColor = Color.White;
            label.Font = new Font("Arial", 12);
            label.AutoSize = true;
            label.BackColor = Color.Black;
            label.Padding = new Padding(10);
            this.Controls.Add(label);

            this.Height = 50; // 飘窗的高度
            this.Width = TextRenderer.MeasureText(label.Text, label.Font).Width + label.Padding.Horizontal;

            timer = new Timer();
            timer.Interval = 10;
            timer.Tick += Timer_Tick;

            this.Paint += FloatingWindow_Paint;
        }

        private void FloatingWindow_Paint(object sender, PaintEventArgs e)
        {
            // 创建圆角矩形路径
            GraphicsPath path = new GraphicsPath();
            int radius = 25; // 圆角半径
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
            path.AddArc(rect.Right - radius, rect.Y, radius, radius, 270, 90);
            path.AddArc(rect.Right - radius, rect.Bottom - radius, radius, radius, 0, 90);
            path.AddArc(rect.X, rect.Bottom - radius, radius, radius, 90, 90);
            path.CloseFigure();

            // 创建圆角矩形的 Region 对象
            Region region = new Region(path);

            // 应用 Region 对象到控件的外观
            this.Region = region;

            // 绘制圆角矩形的边框
            using (Pen pen = new Pen(Color.Black, 1))
            {
                pen.Alignment = PenAlignment.Inset;
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; // 设置抗锯齿模式
                e.Graphics.DrawPath(pen, path);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.Parent != null)
            {
                startY = (Parent.Height - this.Height) / 2;
                targetY = 100; // 调整为你希望的位置,离顶端的距离
                this.Top = startY;
                timer.Start();
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (this.Top > targetY)
            {
                this.Top -= 3; // 调整飘窗的速度
                this.Invalidate(); // 刷新控件,触发重绘
            }
            else
            {
                timer.Stop();

                // 创建一个定时器来控制渐变动画
                Timer fadeTimer = new Timer();
                fadeTimer.Interval = 10;
                fadeTimer.Tick += (fadeSender, fadeArgs) =>
                {
                    opacity -= 0.05f; // 每次减少0.05的不透明度
                    if (opacity <= 0)
                    {
                        fadeTimer.Stop();
                        this.Parent.Controls.Remove(this);
                    }
                    this.Invalidate(); // 刷新控件,触发重绘
                };
                fadeTimer.Start();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            // 设置控件的不透明度
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            e.Graphics.CompositingMode = CompositingMode.SourceOver;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            using (GraphicsPath path = new GraphicsPath())
            {
                path.AddRectangle(this.ClientRectangle);
                using (Brush brush = new SolidBrush(Color.FromArgb((int)(opacity * 255), this.BackColor)))
                {
                    e.Graphics.FillPath(brush, path);
                }
            }

        }
    }
}

在其他界面调用

 FloatingWindow floatingWindow = new FloatingWindow("这是一个飘窗");
                    floatingWindow.Left = (this.Width - floatingWindow.Width) / 2;
                    floatingWindow.Top = this.Height / 2;
                    panel.Controls.Add(floatingWindow);

啦啊啊啊啊啊啊啊啊啊啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值