背景:
- 我正在尝试创建一个“鼠标隐藏”应用程序,在设定的时间后将用户的鼠标从屏幕上隐藏起来。
- 我已经尝试了很多事情,使用SetCursor只会从当前应用程序中隐藏鼠标,我的必须能够坐在托盘中(例如)并且仍然可以工作。
- 我想我已经找到了 SetSystemCursor 的解决方案,除了一个问题。
我的问题:
- 我需要能够捕获任何类型的鼠标光标,并替换完全相同类型的鼠标光标。
- 更换鼠标时,我需要提供我想用句柄引用的鼠标替换的鼠标类型的 id,但我使用的任何函数都没有为我提供复制的鼠标 id(或类型) .
我的问题:
- 以这种方式继续这样做是否足够,但首先将鼠标移动到 0,0,将其隐藏,然后在取消隐藏时将其移回原来的位置?(只需移动鼠标即可完成取消隐藏)
- 0,0 处的鼠标是否总是 OCR_NORMAL 鼠标?(标准箭头。)
- 如果没有,如何找到鼠标类型/ID 以使我能够用正确的手柄替换正确的鼠标?
资源:
[DllImport("user32.dll")]
public static extern IntPtr LoadCursorFromFile(string lpFileName);
[DllImport("user32.dll")]
public static extern bool SetSystemCursor(IntPtr hcur, uint id);
[DllImport("user32.dll")]
static extern bool GetCursorInfo(out CURSORINFO pci);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public Int32 x;
public Int32 y;
}
[StructLayout(LayoutKind.Sequential)]
struct CURSORINFO
{
public Int32 cbSize; // Specifies the size, in bytes, of the structure.
// The caller must set this to Marshal.SizeOf(typeof(CURSORINFO)).
public Int32 flags; // Specifies the cursor state. This parameter can be one of the following values:
// 0 The cursor is hidden.
// CURSOR_SHOWING The cursor is showing.
public IntPtr hCursor; // Handle to the cursor.
public POINT ptScreenPos; // A POINT structure that receives the screen coordinates of the cursor.
}
private POINT cursorPosition;
private IntPtr cursorHandle;
private bool mouseVisible = false;
private const uint OCR_NORMAL = 32512;
//Get the current mouse, so we can replace it once we want to show the mouse again.
CURSORINFO pci;
pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
GetCursorInfo(out pci);
cursorPosition = pci.ptScreenPos;
cursorHandle = CopyIcon(pci.hCursor);
//Overwrite the current normal cursor with a blank cursor to "hide" it.
IntPtr cursor = LoadCursorFromFile(@"./Resources/Cursors/blank.cur");
SetSystemCursor(cursor, OCR_NORMAL);
mouseVisible = false;
//PROCESSING...
//Show the mouse with the mouse handle we copied earlier.
bool retval = SetSystemCursor(cursorHandle, OCR_NORMAL);
mouseVisible = true;