Programmatically change custom mouse cursor in windows?
Asked Answered
T

2

14

I am trying to change the windows cursors (the default is Windows Custom Scheme) to my custom cursors (It named Cut the rope):

enter image description here

Is there any idea to change all of cursors (Arrow, Busy, Help Select, Link select,...) to my Cut the rope?

Teucer answered 18/1, 2017 at 7:45 Comment(0)
S
10

If you want to change the default Mouse Cursor theme:

enter image description here

You can just change it in the registry:

There are three main registry keys that come into play.

  1. The registry key HKEY_CURRENT_USER\Control Panel\Cursors contains the active user cursors

1a) The values underneath this are the different types of cursors
1b) The Scheme Source specifies the type of cursor scheme that is currently being used.

The different values are:

"0" – Windows Default
"1" – User Scheme
"2" – System Scheme

  1. The registry key HKEY_CURRENT_USER\Control Panel\Cursors contains the user defined cursor schemes (i.e. Scheme Source = 1)

  2. The registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Control Panel\Schemes contains the system cursor schemes (i.e. Scheme Source = 2)

enter image description here

If you already changed the path to one of the cursor type in HKCU\Control Panel\Cursors and realized that it did not do anything. You are correct, just updating a key – HKCU\Control Panel\Cursors\Arrow, for instance – isn’t enough. You have to tell windows to load the new cursor.

This is where the SystemParametersInfo call comes in. To try this out let’s go ahead and change HKCU\Control Panel\Cursors\Arrow to C:\WINDOWS\Cursors\appstar3.ani (assuming you have this icon) and then make a call to SystemParametersInfo.

In AutoHotKey Script:

SPI_SETCURSORS := 0x57
result := DllCall("SystemParametersInfo", "UInt", SPI_SETCURSORS, "UInt", 0, "UInt", 0, "UInt", '0')
MsgBox Error Level: %ErrorLevel% `nLast error: %A_LastError%`nresult: %result%

Translated to C#:

[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
 
const int SPI_SETCURSORS = 0x0057;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDCHANGE = 0x02;

To call it:

SystemParametersInfo(SPI_SETCURSORS, 0, 0, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);

Changing to the Default Windows Cursor

Now the tricky part. If you look at HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Control Panel\Schemes you will notice that “Windows Default” is defined as “,,,,,,,,,,,,,” or in other words no pointers to actual cursors!

What to do now? Don’t worry. All you have to do is set the different cursor types to empty string and then make the SystemParametersInfo call as usual. In fact, you can set any of the cursor type to empty string in any scheme and Windows will default it to it’s equivalent in the “Windows Default” scheme.

REF:

https://thebitguru.com/articles/programmatically-changing-windows-mouse-cursors/3

https://social.msdn.microsoft.com/Forums/vstudio/en-US/977e2f40-3222-4e13-90ea-4e8d0cdf289c/faq-item-how-to-change-the-systems-cursor-using-visual-cnet?forum=csharpgeneral

Suttles answered 18/1, 2017 at 9:54 Comment(6)
I tried to change the value but nothing happens, it's still the default cursorTeucer
I got some errors here: i.imgur.com/Agns2vV.png. The pvParam didn't accept null value. I try to change pvParam value to "0" and it works fine. Thank you so much!Teucer
@HoangViet hey mate, can you tick this as the answer (it will let everyone know your problem is solved and marking answers as correct gives you a couple of rep points too). The tickbox/checkbox is below the Up/Down voting arrows to the left of this answer. Check it out, glad it worked for you.Suttles
I did it. I am very grateful for your help.Teucer
@Jeremy Thompson Isn't there another way to just switch between Windows' schemes? I want to make an AutoHotkey script to reflect the selected language in the cursors: black cursor, watch out, you're in the alternate keyboard layout! LeftAlt+Shift, white cursor, OK, you can type normally. The language bar is too far to notice on a large screen, but the mouse pointer is usually near where you're trying to type.Gassing
@JeremyThompson I managed to do it by setting each cursor individually. Here's the gist with AutoHotkey code and more details: Cursor Language Indicator - Reflect keyboard layout changes in mouse pointer look.Gassing
D
2

You can do like this. Get the Cursor.cur file to load custom cursor. On MouseLeave set the Default cursor for form.

public static Cursor ActuallyLoadCursor(String path)
    {
        return new Cursor(LoadCursorFromFile(path));
    }

    [DllImport("user32.dll")]
    private static extern IntPtr LoadCursorFromFile(string fileName);

Button btn = new Button();
btn.MouseLeave += Btn_MouseLeave;
btn.Cursor = ActuallyLoadCursor("Cursor.cur");

private static void Btn_MouseLeave(object sender, EventArgs e)
    {
        this.Cursor = Cursors.Default;
    }
Disinfest answered 18/1, 2017 at 8:3 Comment(1)
I want to change all of windows cursors not only for the form. Thanks for your help!Teucer

© 2022 - 2024 — McMap. All rights reserved.