4

I've been trying to do so and couldn't make it, this is what I got to :

[DllImport("user32.dll")]
static extern bool SetSystemCursor(IntPtr hcur, uint id);

[DllImport("user32.dll", EntryPoint = "GetCursorInfo")]
public static extern bool GetCursorInfo(out CURSORINFO pci);

[DllImport("user32.dll", EntryPoint = "CopyIcon")]
public static extern IntPtr CopyIcon(IntPtr hIcon);

[StructLayout(LayoutKind.Sequential)]
public struct CURSORINFO
{
    public Int32 cbSize;        // Specifies the size, in bytes, of the structure. 
    public Int32 flags;         // Specifies the cursor state. This parameter can be one of the following values:
    public IntPtr hCursor;          // Handle to the cursor. 
    public POINT ptScreenPos;       // A POINT structure that receives the screen coordinates of the cursor. 
}

public void etc()
{
    IntPtr hwndic = new IntPtr();
    CURSORINFO curin = new CURSORINFO();
    curin.cbSize = Marshal.SizeOf(curin);
    if (GetCursorInfo(out curin))
    {
        if (curin.flags == CURSOR_SHOWING)
        {
            hwndic = CopyIcon(curin.hCursor);
            SetSystemCursor(hwndic, OCR_NORMAL);
        }
    }
}

The problem is that the copied icon sometimes is different than the default mouse icon, if it catches it on waiting position for example then it might give me the mouse waiting icon.

I wish to get the default cursor icon (the icon at idle) of the running system, how can I do so?

Thanks in advance.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • Look at LoadCursor and IDC_ARROW constant – Pascalz Jul 21 '17 at 21:35
  • @Pascalz that doesn't help me, its not detecting what the system's cursor is. – Matan Yashar Jul 21 '17 at 21:37
  • Can you set Cursor.Current = Cursors.Default before copying? – obl Jul 21 '17 at 22:07
  • @obl i tried doing it before, it said member 'cursor.current' cannot be accessed with an instance reference qualify it with a type name instead, and as far as i know the cursor commands can only be used in the form designer file, and not outside of it. so even if i manage to get the icon of the cursor at the application launch time the user might change his cursor icon afterwards, and I won't be able to return to the last user changed cursor icon. – Matan Yashar Jul 21 '17 at 22:17
  • @obl also the thing that you're saying won't work properly even if it would, because the setsystemcursor function pastes the cursor with more shadow than it should. anyway i've solved this issue and i will answer to this thread. – Matan Yashar Jul 21 '17 at 23:04
  • Does this answer your question? [C# - Capturing the Mouse cursor image](https://stackoverflow.com/questions/918990/c-sharp-capturing-the-mouse-cursor-image) – Robin Dinse Jun 10 '20 at 22:54
  • @RobinDinse i dont remember what i did, but i solved it however, i see that i've answered my own question so, just view my answer. – Matan Yashar Jun 13 '20 at 20:33

1 Answers1

1

I've solved my issues, after some research I got to something interesting that would probably work all the time.

Instead of trying to save a cursor's icon and loading it I thought of a different idea.

I've noticed that whenever I change a cursor icon (lets say using a code, to some random icon) then whenever I go to the windows cursor settings the icon is not changed there, but there's no apply button to apply the settings I had before, because windows thinks I'm using those settings.

So I changed the settings to some random other settings without applying and returned to the settings I had before and applied, doing so resetted my cursor to its original cursor, before any of the changes.

Therefore what windows was just doing is "refreshing" the mouse, so I went that way, maybe if I can force a refresh everything would be perfect, and so I found a way to do it, using this function :

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SystemParametersInfo(UInt32 uiAction,UInt32 uiParam, String pvParam, UInt32 fWinIni);

As I've watched msdn I've found something interesting in its parameters, the parameter "SPI_SETCURSORS (0x57)", and I quote :

"Reloads the system cursors. Set the uiParam parameter to zero and the pvParam parameter to NULL."

So I've tried it, and it worked, example of the process :

[DllImport("User32.dll", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
private static extern IntPtr LoadCursorFromFile(String str);

uint SPI_SETCURSORS = 0x57;
var NewArrow = LoadCursorFromFile("C:\\Users\\mtnju\\Downloads\\invisible.cur"); // loads some new cursor icon using the LoadCursorFromFile function
SetSystemCursor(NewArrow, OCR_NORMAL); // sets the new cursor icon using the SetSystemCursor function
SystemParametersInfo(SPI_SETCURSORS, 0, null, 0);// reloads all of the system cursors

I thought it'd take me 5 minutes to do something like this... I hope it'll help you guys, and I really appreciate the comments trying to help me.