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);
        }
    }
相关推荐
Mr-Apple1 小时前
windows编译googletest框架搭配vscode调试
ide·windows·vscode
慕羽★11 小时前
详细介绍如何使用rapidjson读取json文件
linux·c++·windows·json·file·param·rapidjson
向宇it11 小时前
【从零开始入门unity游戏开发之——C#篇23】C#面向对象继承——`as`类型转化和`is`类型检查、向上转型和向下转型、里氏替换原则(LSP)
java·开发语言·unity·c#·游戏引擎·里氏替换原则
sukalot12 小时前
windows C#-命名实参和可选实参(下)
windows·c#
小码编匠12 小时前
.NET 下 RabbitMQ 队列、死信队列、延时队列及小应用
后端·c#·.net
只抄16 小时前
Windows “高性能”模式既不高效,也不节能?
windows
我不是程序猿儿21 小时前
【C#】Debug和Release的区别和使用
开发语言·c#
字母数字或汉字1 天前
了解与配置 Git autocrlf 文本换行符处理
windows·git
lzhdim1 天前
2、C#基于.net framework的应用开发实战编程 - 设计(二、二) - 编程手把手系列文章...
开发语言·c#·.net
misty youth1 天前
学生信息管理系统
c语言·数据结构·算法·c#