C# 创建跟随鼠标的图像

本文关键字:图像 鼠标 跟随 创建 | 更新日期: 2023-09-27 18:32:06

我正在尝试在 C# 的 winForm 中创建一个程序,其中图像将跟随应用程序外部的鼠标。

我不知道如何在表单之外绘制图像,更不用说让它跟随鼠标了。我的解决方案将是 - 创建一个无边框表单并让它跟随鼠标 - 但这个解决方案不起作用,因为我无法通过代码移动表单。

鼠标需要能够独立于此图像单击和运行。

我将如何做到这一点?

C# 创建跟随鼠标的图像

它必须在不改变鼠标使用方式的情况下执行此操作。

为扩展样式设置WS_EX_TRANSPARENT,使表单忽略鼠标单击。 将"最顶层"设置为"真",将"不透明度"设置为小于 100% 的值,使其半透明。 使用计时器移动表单。 像这样:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Opacity = .5;
        this.TopMost = true;
        this.BackColor = Color.Yellow;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        // Makes the form circular:
        System.Drawing.Drawing2D.GraphicsPath GP = new System.Drawing.Drawing2D.GraphicsPath();
        GP.AddEllipse(this.ClientRectangle);
        this.Region = new Region(GP);
    }
    const int WS_EX_TRANSPARENT = 0x20;
    protected override System.Windows.Forms.CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT;
            return cp;
        }
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        Point pt = Cursor.Position;
        pt.Offset(-1 * this.Width / 2, -1 * this.Height / 2);
        this.Location = pt;
    }
}

检查此线程:

如何使用鼠标拖动和移动通知

似乎你想做这样的事情。

希望这有帮助!