如何使用C#移动鼠标光标?

发布于 2024-12-14 15:48:41 字数 85 浏览 1 评论 0原文

我想每 x 秒模拟一次鼠标移动。为此,我将使用计时器(x 秒),当计时器滴答作响时,我将移动鼠标。

但是,如何使用 C# 使鼠标光标移动呢?

I want to simulate mouse movement every x seconds. For that, I'll use a timer (x seconds) and when the timer ticks I'll make the mouse movement.

But, how can I make the mouse cursor move using C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴骨ら 2024-12-21 15:48:41

看一下 Cursor.Position 属性。它应该让你开始。

private void MoveCursor()
{
   // Set the Current cursor, move the cursor's Position,
   // and set its clipping rectangle to the form. 

   this.Cursor = new Cursor(Cursor.Current.Handle);
   Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
   Cursor.Clip = new Rectangle(this.Location, this.Size);
}

Take a look at the Cursor.Position Property. It should get you started.

private void MoveCursor()
{
   // Set the Current cursor, move the cursor's Position,
   // and set its clipping rectangle to the form. 

   this.Cursor = new Cursor(Cursor.Current.Handle);
   Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
   Cursor.Clip = new Rectangle(this.Location, this.Size);
}
混浊又暗下来 2024-12-21 15:48:41

首先添加一个名为 Win32.cs 的类,

public class Win32
{ 
    [DllImport("User32.Dll")]
    public static extern long SetCursorPos(int x, int y);

    [DllImport("User32.Dll")]
    public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;

        public POINT(int X, int Y)
        {
            x = X;
            y = Y;
        }
    }
}

然后您可以像这样使用它:

Win32.POINT p = new Win32.POINT(xPos, yPos);

Win32.ClientToScreen(this.Handle, ref p);
Win32.SetCursorPos(p.x, p.y);

First Add a Class called Win32.cs

public class Win32
{ 
    [DllImport("User32.Dll")]
    public static extern long SetCursorPos(int x, int y);

    [DllImport("User32.Dll")]
    public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;

        public POINT(int X, int Y)
        {
            x = X;
            y = Y;
        }
    }
}

You can use it then like this:

Win32.POINT p = new Win32.POINT(xPos, yPos);

Win32.ClientToScreen(this.Handle, ref p);
Win32.SetCursorPos(p.x, p.y);
走走停停 2024-12-21 15:48:41

如果有人想知道 - 它对屏幕保护程序没有帮助,但如果您仍然需要它(我工作的公司出于某种原因计算“活动计算机时间”,所以我决定向他们展示)这里是如何做到这一点.net 8 控制台应用程序(不需要 winforms 不需要 wpf,支持 dpi 缩放)。
程序.cs:

using System.Drawing;
using System.Runtime.InteropServices;
using MouseMover;

var screenSize = DisplayTools.GetCurrentDisplaySize();//DisplayTools.GetVirtualDisplaySize();
Console.WriteLine($"ScreenWidth = {screenSize.Width} ScreenHeight = {screenSize.Height}"); //return;
Console.WriteLine("Starting movement...");

var timer = new System.Timers.Timer(2000);
var change = new Point(1, 1);

timer.Elapsed += (o, e) =>
{
    var point = new Point();
    GetCursorPos(ref point);
    Console.Write($"X = {point.X} Y = {point.Y}");
    var newX = point.X + change.X;
    var newY = point.Y + change.Y;
    if(newX >= screenSize.Width || newX < 0)
        change.X *= -1;
    if (newY >= screenSize.Height|| newY < 0)
        change.Y *= -1;
    SetCursorPos(point.X + change.X, point.Y + change.Y);
    Console.WriteLine($" moved to X = {point.X + change.X} Y = {point.Y + change.Y}");
};

timer.AutoReset = true;
timer.Enabled = true;
Console.WriteLine("(press any key to stop)");
Console.ReadKey();
timer.Stop();
timer.Dispose();
Console.WriteLine("Stopped");


[DllImport("user32.dll")]
static extern bool GetCursorPos(ref Point point);

[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);

显示工具.cs:

using System.Drawing;
using System.Runtime.InteropServices;

namespace MouseMover;

static class DisplayTools
{

    [DllImport("gdi32.dll")]
    static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
    private enum DeviceCap
    {
        VERTRES = 10,
        HORZRES = 8,
        DESKTOPVERTRES = 117,
        DESKTOPHORIZRES = 118,

        // http://pinvoke.net/default.aspx/gdi32/GetDeviceCaps.html
    }

    public static Size GetCurrentDisplaySize()
    {
        using Graphics g = Graphics.FromHwnd(IntPtr.Zero);
        IntPtr desktop = g.GetHdc();
        int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
        int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
        int LogicalScreenWidth = GetDeviceCaps(desktop, (int)DeviceCap.HORZRES);
        int PhysicalScreenWidth = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPHORIZRES);

        g.ReleaseHdc(desktop);

        return new Size(LogicalScreenWidth, LogicalScreenHeight);

    }

    private enum SystemMetric
    {
        VirtualScreenWidth = 78,
        VirtualScreenHeight = 79
    }

    [DllImport("user32.dll")]
    static extern int GetSystemMetrics(SystemMetric systemMetric);

    public static Size GetVirtualDisplaySize()
    {
        var width = GetSystemMetrics(SystemMetric.VirtualScreenWidth);
        var height = GetSystemMetrics(SystemMetric.VirtualScreenWidth);

        return new Size(width, height);
    }

}

In case anyone wonders - it's not helping with a screen saver, but if you still need it (the company I'm working for counts "active computer time" for some reason, so I decided to show them) here's how to do it in .net 8 console app (no winforms no wpf needed, supports dpi scaling).
Program.cs:

using System.Drawing;
using System.Runtime.InteropServices;
using MouseMover;

var screenSize = DisplayTools.GetCurrentDisplaySize();//DisplayTools.GetVirtualDisplaySize();
Console.WriteLine(
quot;ScreenWidth = {screenSize.Width} ScreenHeight = {screenSize.Height}"); //return;
Console.WriteLine("Starting movement...");

var timer = new System.Timers.Timer(2000);
var change = new Point(1, 1);

timer.Elapsed += (o, e) =>
{
    var point = new Point();
    GetCursorPos(ref point);
    Console.Write(
quot;X = {point.X} Y = {point.Y}");
    var newX = point.X + change.X;
    var newY = point.Y + change.Y;
    if(newX >= screenSize.Width || newX < 0)
        change.X *= -1;
    if (newY >= screenSize.Height|| newY < 0)
        change.Y *= -1;
    SetCursorPos(point.X + change.X, point.Y + change.Y);
    Console.WriteLine(
quot; moved to X = {point.X + change.X} Y = {point.Y + change.Y}");
};

timer.AutoReset = true;
timer.Enabled = true;
Console.WriteLine("(press any key to stop)");
Console.ReadKey();
timer.Stop();
timer.Dispose();
Console.WriteLine("Stopped");


[DllImport("user32.dll")]
static extern bool GetCursorPos(ref Point point);

[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);

DisplayTools.cs:

using System.Drawing;
using System.Runtime.InteropServices;

namespace MouseMover;

static class DisplayTools
{

    [DllImport("gdi32.dll")]
    static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
    private enum DeviceCap
    {
        VERTRES = 10,
        HORZRES = 8,
        DESKTOPVERTRES = 117,
        DESKTOPHORIZRES = 118,

        // http://pinvoke.net/default.aspx/gdi32/GetDeviceCaps.html
    }

    public static Size GetCurrentDisplaySize()
    {
        using Graphics g = Graphics.FromHwnd(IntPtr.Zero);
        IntPtr desktop = g.GetHdc();
        int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
        int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
        int LogicalScreenWidth = GetDeviceCaps(desktop, (int)DeviceCap.HORZRES);
        int PhysicalScreenWidth = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPHORIZRES);

        g.ReleaseHdc(desktop);

        return new Size(LogicalScreenWidth, LogicalScreenHeight);

    }

    private enum SystemMetric
    {
        VirtualScreenWidth = 78,
        VirtualScreenHeight = 79
    }

    [DllImport("user32.dll")]
    static extern int GetSystemMetrics(SystemMetric systemMetric);

    public static Size GetVirtualDisplaySize()
    {
        var width = GetSystemMetrics(SystemMetric.VirtualScreenWidth);
        var height = GetSystemMetrics(SystemMetric.VirtualScreenWidth);

        return new Size(width, height);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文