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);
        }
    }
相关推荐
Crossoads7 分钟前
【汇编语言】call 和 ret 指令(一) —— 探讨汇编中的ret和retf指令以及call指令及其多种转移方式
android·开发语言·javascript·汇编·人工智能·数据挖掘·c#
陌小呆^O^13 分钟前
关于C/C++Windows下连接MYSQL操作
c语言·c++·windows
菜鸟挣扎史7 小时前
grafana+prometheus+windows_exporter实现windows进程资源占用的监控
windows·grafana·prometheus·进程·process
Zԅ(¯ㅂ¯ԅ)7 小时前
C#桌面应用制作计算器
c#
shepherd枸杞泡茶8 小时前
WPF动画
c#·.net·wpf
code_shenbing8 小时前
跨平台WPF框架Avalonia教程 三
前端·microsoft·ui·c#·wpf·跨平台·界面设计
三天不学习8 小时前
C# AutoMapper 10个常用方法总结
c#·automapper·对象映射
oioihoii9 小时前
单例模式详解
c++·单例模式·c#·大学必学
大今野9 小时前
windows系统中实现对于appium的依赖搭建
windows·appium
suzh1qian11 小时前
Unity类银河战士恶魔城学习总结(P133 Merge skill tree with Parry skill把技能树和格挡技能相组合)
学习·unity·c#·游戏引擎