[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;
答案 0 :(得分:1)
一个应用程序不能影响另一个应用程序游标。为了做到这一点,你必须编写某种类型的鼠标驱动程序。
答案 1 :(得分:0)
我找到了一个暂时隐藏系统光标的好方法,不涉及setsystemcursor()
。
SetSystemCursor()
很危险,因为如果应用程序崩溃或以其他方式抛出错误,光标将永久更改,直到下次重新启动。
相反,我在整个桌面上实现了一个透明窗口,该窗口在需要时隐藏了光标。使用的方法是Win32的ShowCursor。
透明窗口可以是这样的: http://www.codeproject.com/Articles/12597/OSD-window-with-animation-effect-in-C
[DllImport("user32.dll")]
static extern int ShowCursor(bool bShow);
ShowCursor(false);