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);
        }
    }
相关推荐
一心赚狗粮的宇叔1 小时前
oracle使用CTE递归分解字符串
mysql·oracle·c#·database
丶Darling.1 小时前
vscode在windows和linux如何使用cmake构建项目并make生成可执行文件,两者有什么区别
linux·windows·vscode
爱技术的小伙子3 小时前
【Windows】让你的磁盘更健康!Windows `chkdsk`命令使用全指南
windows
宠你同行3 小时前
猫用宠物空气净化器推荐,希喂,美的哪款除毛好、噪音小?
宠物
乐茵安全4 小时前
windows基础
windows·安全·网络安全
小码编匠4 小时前
WPF 自定义按钮样式(添加依赖属性、圆角)
后端·c#·.net
dangoxiba4 小时前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第十八集补充:制作空洞骑士独有的EventSystem和InputModule
游戏·unity·c#·游戏引擎·playmaker
无敌最俊朗@5 小时前
unity3d————屏幕坐标,GUI坐标,世界坐标的基础注意点
开发语言·学习·unity·c#·游戏引擎
.net开发5 小时前
WPF使用Prism框架首页界面
前端·c#·.net·wpf
来一杯龙舌兰6 小时前
【MongoDB】Windows/Docker 下载安装,MongoDB Compass的基本使用、NoSQL、MongoDB的基础概念及基础用法(超详细)
windows·mongodb·docker·mongodb compass