“Hiding” System Cursor

好久不见. 提交于 2019-12-18 05:13:21

问题


BACKGROUND:

  • I'm trying to create a "mouse hiding" application that hides the user's mouse from the screen after a set amount of time.
  • I've tried many things, and using SetCursor only hides the mouse from the current application, mine must be able to sit in the tray (for example) and still function.
  • I think I've found a solution with SetSystemCursor except for one problem.

MY PROBLEM:

  • I need to be able to capture any kind of mouse cursor, and replace the exact same kind of mouse cursor.
  • When replacing the mouse, I need to provide the id of the type of mouse I'd like to replace with the mouse referenced by the handle, but none of the functions I'm using provide me with the copied mouse's id (or type).

MY QUESTION:

  • Would it be sufficient to continue doing it this way, but move the mouse to 0,0 first, hiding it, and moving it back to it's original location upon un-hiding? (Unhiding is accomplished by simply moving the mouse)
  • Would a mouse at 0,0 always be an OCR_NORMAL mouse? (The standard arrow.)
  • If not, how could the mouse type/id be found to enable me to replace the proper mouse with the proper handle?

SOURCE:

[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;

回答1:


One application can't affect another applications cursor. You'd have to write a mouse driver of some sort in order to do this.




回答2:


I found a good workaround for hiding system cursor temporarily that doesn't involve screwing around with setsystemcursor().

SetSystemCursor() is dangerous, because if the app crashes or otherwise throws a bug, the cursor will be changed permanently until the next reboot.

Instead, I implemented a transparent window over the whole desktop, and that window hides the cursor when needed. The method to use is ShowCursor from Win32.

The transparent window can be something like this: http://www.codeproject.com/Articles/12597/OSD-window-with-animation-effect-in-C

[DllImport("user32.dll")]
static extern int ShowCursor(bool bShow);

ShowCursor(false);


来源:https://stackoverflow.com/questions/10541014/hiding-system-cursor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!