在 C# 中自动化鼠标点击

发布于 2024-11-19 16:07:57 字数 254 浏览 1 评论 0原文

我对 winforms 和自动化非常陌生,想知道是否有一种简单的方法可以强制光标在 C# 中单击。我已经将光标移动到正确的位置,并且我想让它执行一次单击。是否有代码可以做到这一点,而不变得超级复杂。 (为了清楚起见,我在另一个应用程序中的按钮上自动单击鼠标,此时该应用程序已打开)

以下是我设置光标位置的方法:

Cursor.Position = new Point(x, y);Cursor.Position = new Point(x, y);代码>

I am very new to winforms and automation in general, and was wondering if there is a simple way to force the cursor to click in C#. I am already moving the cursor to the right spot, and I want to make it perform a click. Is there code that will do this, without getting super complicated. (for clarity, I am automating a mouseclick on a button in another application, which has been opened at this point)

Here is how I set the cursor position:

Cursor.Position = new Point(x, y);

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

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

发布评论

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

评论(2

花伊自在美 2024-11-26 16:07:58

我认为你应该尝试 AutoIt v3 ,除非你特别绑定到 C#。它是为这种自动化而设计的。

AutoIt v3 是一种类似 BASIC 的免费软件脚本语言,设计用于自动化 Windows GUI 和一般脚本编写。它使用模拟击键、鼠标移动和窗口/控件操作的组合,以便以其他语言(例如 VBScript 和 SendKeys)不可能或可靠的方式自动执行任务。 AutoIt 也非常小、独立,可以开箱即用地在所有版本的 Windows 上运行,不需要烦人的“运行时”!

例如:

; Double click at the current mouse pos MouseClick("left") 
MouseClick("left")

; Double click at 0,500 
MouseClick("left", 0, 500, 2)   

; SAFER VERSION of Double click at 0,500 - takes into account user's control panel settings 
MouseClick("primary", 0, 500, 2)

I think you should try AutoIt v3 instead unless you specifically are bound to C#. It's designed for this kind of automation.

AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required!

For example:

; Double click at the current mouse pos MouseClick("left") 
MouseClick("left")

; Double click at 0,500 
MouseClick("left", 0, 500, 2)   

; SAFER VERSION of Double click at 0,500 - takes into account user's control panel settings 
MouseClick("primary", 0, 500, 2)
失去的东西太少 2024-11-26 16:07:57

您需要通过 MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUPmouse_event 函数。

You will want to pass MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP to the mouse_event function.

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