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

时间:2024-11-01 07:22:45

首先看特效

  1. 开启双缓冲

SetStyle(
          ControlStyles.ResizeRedraw
          | ControlStyles.DoubleBuffer
          | ControlStyles.UserPaint
          | ControlStyles.AllPaintingInWmPaint
          | ControlStyles.SupportsTransparentBackColor,
          true
          );

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


this.TransparencyKey = this.BackColor = System.Drawing.SystemColors.Control;
this.TopMost = true;

3.设置定时器

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

4.设置图像

5.添加窗体Paint事件


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

 窗体全部源码:


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);
        }
    }