如何在Windows中以编程方式全局隐藏/显示鼠标光标?

huangapple go评论44阅读模式
英文:

How to globally hide/show the mouse cursor in Windows, programmatically?

问题

在某些应用程序中,例如Notepad,在您开始输入时,鼠标光标会消失。然后,一旦您移动鼠标,光标就会重新出现。这很好,因为很多时候,鼠标光标会妨碍打字,特别是在使用大型反向光标时。

然而,这在大多数现代应用程序中不起作用;只有那些使用常见的Windows控件,比如文本区域控件。

我想创建一个在后台运行的应用程序,全局监听任何按键,无论前台是哪个应用程序,并在鼠标物理移动后隐藏鼠标指针。

我已经尝试在各处搜索,但每个解决方案要么根本不起作用,要么只在光标位于测试应用程序窗口上方时隐藏光标(而不是对所有窗口)。我尝试了AutoHotkey、C#、C++等各种“解决方案”,但都没有成功。

我不关心使用哪种编程语言,只要能实现这个目标。请不要发布只隐藏应用程序本身光标的解决方案。无论焦点在哪个应用程序,无论光标位于何处,只要按下任何键盘键,光标都必须消失。一旦移动鼠标,光标应该重新出现在上次存在的位置(不应该被传送到其他地方)。

windows.h的HideCursor()函数不起作用。CreateCursor/SetSystemCursor也不起作用。以管理员身份运行脚本/应用程序也不起作用。

我目前的解决方案只是光标从当前位置移动约100像素。这通常能将其挪开以避免打字,但有时它会悬停在其他弹出的东西上,所以这不是一个最佳解决方案。

非常感谢任何有效的建议。

英文:

In some applications, such as Notepad, when you start typing, the mouse cursor disappears. Then as soon as you move the mouse, the cursor reappears. This is great, because a lot of the time, the mouse cursor gets in the way of typing, especially when using large inverted cursors.

However, this doesn't work in most modern applications; only those that use common Windows controls like the textarea control.

I want to create an app that runs in the background, which globally listens for any keypress, no matter which app is in the foreground, and hides the mouse pointer until the mouse is physically moved again.

I have tried searching everywhere, but every solution either does nothing at all, or only hides the cursor when it is over the test application's window (not for all windows). I tried ChatGPT and it provides broken solutions that don't work. I have tried "solutions" in AutoHotkey, C#, C++ with no luck.

I don't care what programming language is used, as long as this can be achieved. Please do not post solutions that only hide the cursor for the app itself. The cursor must disappear, regardless of which app has focus, and regardless of where the cursor is located, when any keyboard key is pressed. Once the mouse is moved, the cursor should reappear where it last existed (should not be teleported somewhere else).

The HideCursor() function of windows.h does not work. CreateCursor/SetSystemCursor do not work. Running the script/app as Administrator does not work.

My current solution is to simply move the cursor about 100px away from its current spot. This usually gets it out of the way of typing, but sometimes it then hovers over something else that pops up in the way, so it's not an optimal solution.

MANY THANKS for any working suggestions.

答案1

得分: 1

如果您不介意失去点击能力,一个可能的解决方案是创建一个小的透明窗口,并使用 SetWindowPos 将其设置为最顶层模式。

然后,在窗口中隐藏光标(或将其形状更改为透明像素)。

使用 GetCursorPos 获取光标位置,然后创建一个循环,不断更新窗口位置以与光标匹配。

通过这种设置,光标不会跳出隐藏它的区域。

注意:不要忘记将窗口大小的一半添加到鼠标位置,以使其位于中心。

英文:

If you don't mind losing the ability to click, a possible solution would be to create a small transparent window and set it to be in top-most mode using SetWindowPos.

Then, Hide the cursor in it (or change the shape to a transparent pixel).

Get the cursor position using GetCursorPos, then make a loop that keeps updating the window position to match that of the cursor.

With this setup the cursor doesn't get out of the region that hides it.

Note: Don't forget to add half the size of the window to the mouse position so it is in the center.

huangapple
  • 本文由 发表于 2023年6月16日 13:21:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定