以编程方式更改 windows 中的自定义鼠标光标?

Programmatically change custom mouse cursor in windows?

我正在尝试将 windows 游标(默认为 Windows 自定义方案)更改为我的自定义游标(名为 Cut the rope):

是否可以将所有光标(箭头、忙碌、帮助 Select、Link select、...)更改为我的 Cut the rope?

你可以这样做。获取 Cursor.cur 文件以加载自定义光标。在 MouseLeave 上设置表单的默认光标。

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

如果您想更改默认的鼠标光标主题:

您可以在注册表中更改它:

主要有三个注册表项在起作用。

  1. 注册表项 HKEY_CURRENT_USER\Control Panel\Cursors 包含活动用户光标

1a) 下面的值是不同类型的游标
1b) Scheme Source 指定当前正在使用的游标方案类型。

不同的值是:

"0" – Windows 默认值
“1”——用户方案
"2" – 系统方案

  1. 注册表项 HKEY_CURRENT_USER\Control Panel\Cursors 包含用户定义的游标方案(即 Scheme Source = 1)

  2. 注册表项 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Control Panel\Schemes 包含系统游标方案(即 Scheme Source = 2)

如果您已经将路径更改为 HKCU\Control Panel\Cursors 中的一种游标类型,并且意识到它没有执行任何操作。你是对的,仅仅更新一个密钥——例如 HKCU\Control Panel\Cursors\Arrow——是不够的。您必须告诉 windows 加载新光标。

这是调用 SystemParametersInfo 的地方。要尝试这个,让我们继续将 HKCU\Control Panel\Cursors\Arrow 更改为 C:\WINDOWS\Cursors\appstar3.ani (假设您有此图标),然后调用 SystemParametersInfo。

在 AutoHotKey 脚本中:

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%

翻译成 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;

调用它:

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

更改为默认 Windows 光标

现在是棘手的部分。如果您查看 HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Control Panel\Schemes,您会注意到“Windows Default”被定义为“,”,换句话说,没有指针实际游标!

现在怎么办?不用担心。您所要做的就是将不同的游标类型设置为空字符串,然后照常调用 SystemParametersInfo。事实上,您可以在任何方案中将任何游标类型设置为空字符串,并且 Windows 会将其默认为“Windows 默认”方案中的等效项。

参考文献:

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