C#/WinForm如何制作Windows桌面特效(宠物等)

首先看特效

  1. 开启双缓冲
cs 复制代码
SetStyle(
          ControlStyles.ResizeRedraw
          | ControlStyles.DoubleBuffer
          | ControlStyles.UserPaint
          | ControlStyles.AllPaintingInWmPaint
          | ControlStyles.SupportsTransparentBackColor,
          true
          );

2.设置窗体透明、鼠标穿透、窗体置顶

cs 复制代码
this.TransparencyKey = this.BackColor = System.Drawing.SystemColors.Control;
this.TopMost = true;

3.设置定时器

cs 复制代码
_timer = new System.Timers.Timer();
  _timer.Elapsed += _timer_Elapsed;
  _timer.Interval = 140;
  _timer.Start();

4.设置图像

5.添加窗体Paint事件

cs 复制代码
private void FrmEffects_Paint(object? sender, PaintEventArgs e)
{
    e.Graphics.Smooth();
    _effects?.Draw(e.Graphics);
    e.Graphics.Smooth(false);
}

窗体全部源码:

cs 复制代码
public partial class FrmEffects : Form
    {
        private readonly System.Timers.Timer _timer;
        private readonly IEffects _effects;
        public FrmEffects()
        {
            InitializeComponent();
            SetStyle(
          ControlStyles.ResizeRedraw
          | ControlStyles.DoubleBuffer
          | ControlStyles.UserPaint
          | ControlStyles.AllPaintingInWmPaint
          | ControlStyles.SupportsTransparentBackColor,
          true
          );
            this.TransparencyKey = this.BackColor = System.Drawing.SystemColors.Control;
            this.TopMost = true;
            _effects = new DragonHorseEffects();

            _timer = new System.Timers.Timer();
            _timer.Elapsed += _timer_Elapsed;
            _timer.Interval = 140;
            _timer.Start();


            this.Paint += FrmEffects_Paint;
            this.SizeChanged += FrmEffects_SizeChanged;
            this.MouseMove += FrmEffects_MouseMove;
            this.MouseUp += FrmEffects_MouseUp;
            this.MouseDown += FrmEffects_MouseDown;
            this.SizeChanged += FrmEffects_SizeChanged;
            this.Shown += FrmEffects_Shown;
            this.FormClosing += FrmEffects_FormClosing;
        }

        private void FrmEffects_FormClosing(object? sender, FormClosingEventArgs e)
        {
            _effects?.Dispose();
        }

        private void FrmEffects_Shown(object? sender, EventArgs e)
        {
            Screen screen = Screen.PrimaryScreen;
            this.Location = new System.Drawing.Point(screen.Bounds.Width - this.Width, screen.Bounds.Height - this.Height - 50);
        }

        private void _timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            _effects?.Business();
            this.Invalidate();
        }

        private void FrmEffects_SizeChanged(object? sender, EventArgs e)
        {

        }
        private void FrmEffects_MouseDown(object? sender, MouseEventArgs e)
        {

        }

        private void FrmEffects_MouseUp(object? sender, MouseEventArgs e)
        {

        }

        private void FrmEffects_MouseMove(object? sender, MouseEventArgs e)
        {

        }
        private void FrmEffects_Load(object sender, EventArgs e)
        {
          
        }
        private void FrmEffects_Paint(object? sender, PaintEventArgs e)
        {
            e.Graphics.Smooth();
            _effects?.Draw(e.Graphics);
            e.Graphics.Smooth(false);
        }
    }
相关推荐
CYX_cheng39 分钟前
C#```
c#
程序猿多布7 小时前
预定义委托(C# and Unity)
unity·c#
CoderIsArt8 小时前
C#中的虚函数定义,原理与用法
c#
labview_自动化8 小时前
C#功能测试
windows·microsoft·c#
来一杯龙舌兰9 小时前
【Postgresql】Windows 部署 Postgresql 数据库 (图文教程)
数据库·windows·postgresql
blog_wanghao9 小时前
C#: 创建Excel文件并在Excel中写入数据库中的数据
数据库·c#·excel
中游鱼10 小时前
探讨如何加快 C# 双循环的速度效率
c#·for双循环·hypack数据处理
席子哥哥的代码库10 小时前
自制资源管理器(python)
开发语言·windows·python
ぃ扶摇ぅ10 小时前
Windows系统编程(六)内存操作与InlineHook
windows