通过PowerShell在Windows 10中更改和更新光标大小

13
我已经编写了下面的代码,可以影响(我认为)是 Windows 10 中光标和指针大小唯一负责的注册表键。
以下是到目前为止我编写的代码(其中有一些注释):
$RegConnect             = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"CurrentUser", "$env:COMPUTERNAME")
$RegCursorsAccess       = $RegConnect.OpenSubKey("Software\Microsoft\Accessibility", $true)
$RegCursorsControlPanel = $RegConnect.OpenSubKey("Control Panel\Cursors", $true)

# In the code below I'm trying to change the size of the cursor.

$RegCursorsControlPanel.SetValue("CursorBaseSize", 48)
$RegCursorsAccess.SetValue("CursorSize", 3)

$RegCursorsAccess.Close()
$RegConnect.Close()

# This section is where I thought it would update the cursor size.

# Here is where it lists stuff relating to setting and updating any settings changed.
# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfoa
    # SPI_SETCURSORS
    # 0x0057
    # Reloads the system cursors. Set the uiParam parameter to zero and the pvParam parameter to NULL.

$CSharpSig = @'
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(
                  uint uiAction,
                  uint uiParam,
                  uint pvParam,
                  uint fWinIni);
'@
$CursorRefresh = Add-Type -MemberDefinition $CSharpSig -Name WinAPICall -Namespace SystemParamInfo -PassThru
$CursorRefresh::SystemParametersInfo(0x0057,0,$null,0)

它将更改注册表中的正确值。

因此,如果我运行此 PowerShell 代码,则辅助功能设置中的鼠标大小为正确值。

cursor and pointer size setting

但光标没有更新。

如何在不注销、重新登录或重启计算机的情况下强制更新。


以下是相关的微软链接:

WM_SETTINGCHANGE消息

SystemParametersInfoA函数


编辑 - 一些额外信息

如果我运行Sysinternals的进程监视器并深入挖掘,我可以在堆栈摘要下看到这个。

这可能会帮助比我更有知识的人找到如何更新鼠标大小。

HKCU\Control Panel\Cursors\(Default)部分SettingsHandlers_nt.dll

Sysinternals Process Monitor

这也与无障碍性部分相关。 Windows.UI.Accessibility.dll

Windows.UI.Accessibility.dll

以下是我在Process Monitor的筛选器中使用的设置,以缩小项目范围。

Settings used as the filter

2个回答

8

在使用 Cheat Engine 对 SystemSettings.exe 进行一些破解后,我找到了微软设置光标大小的方法。它最终仍然使用 SystemParametersInfo,但使用了一些未记录的参数。请尝试以下操作:

SystemParametersInfo(0x2029, 0, 16, 0x01);

设置光标大小为16。是的,您可以将光标大小降至最低32以下,一直到1 :)


非常感谢,这个有效。你知道为什么UI在这张图片中不会更新吗?只有在重新启动后才会更新。https://i.imgur.com/0wJI8Ww.png - Ste
该工具设置多个注册表项并从 Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Accessibility\CursorSize 读取,该项使用1-15大小格式。而SystemParametersInfo与0x2029只是 Computer\HKEY_CURRENT_USER\Control Panel\Cursors\CursorBaseSize。 - HaPpY
谢谢,有没有办法在滑块移动时检查哪些内容已更改? - Ste
@Ste只需在您的应用程序中处理WM_SETTINGCHANGE消息,然后重新读取注册表值。 - DJm00n
我明白了,随时可以添加答案。干杯。 - Ste
显示剩余2条评论

2

但光标没有更新。

更改注册表值后,需要触发器来应用这些更新

可以使用SystemParametersInfo函数,并使用SPIF_UPDATEINIFILESPIF_SENDCHANGE将新的系统范围参数设置写入用户配置文件并在更新用户配置文件后广播WM_SETTINGCHANGE消息。

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

以下是PowerShell命令示例:
$RegConnect = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"CurrentUser","$env:COMPUTERNAME")


$RegCursors = $RegConnect.OpenSubKey("Control Panel\Cursors",$true)


$RegCursors.SetValue("","Windows Black")
$RegCursors.SetValue("CursorBaseSize",0x40)

$RegCursors.SetValue("AppStarting","%SystemRoot%\cursors\wait_r.cur")

$RegCursors.SetValue("Arrow","%SystemRoot%\cursors\arrow_rl.cur")

$RegCursors.SetValue("Crosshair","%SystemRoot%\cursors\cross_r.cur")

$RegCursors.SetValue("Hand","")

$RegCursors.SetValue("Help","%SystemRoot%\cursors\help_r.cur")

$RegCursors.SetValue("IBeam","%SystemRoot%\cursors\beam_r.cur")

$RegCursors.SetValue("No","%SystemRoot%\cursors\no_r.cur")

$RegCursors.SetValue("NWPen","%SystemRoot%\cursors\pen_r.cur")

$RegCursors.SetValue("SizeAll","%SystemRoot%\cursors\move_r.cur")

$RegCursors.SetValue("SizeNESW","%SystemRoot%\cursors\size1_r.cur")

$RegCursors.SetValue("SizeNS","%SystemRoot%\cursors\size4_r.cur")

$RegCursors.SetValue("SizeNWSE","%SystemRoot%\cursors\size2_r.cur")

$RegCursors.SetValue("SizeWE","%SystemRoot%\cursors\size3_r.cur")

$RegCursors.SetValue("UpArrow","%SystemRoot%\cursors\up_r.cur")

$RegCursors.SetValue("Wait","%SystemRoot%\cursors\busy_r.cur")

$RegCursors.Close()

$RegConnect.Close()


function Update-UserPreferencesMask {
$Signature = @"
[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;

public static void UpdateUserPreferencesMask() {
    SystemParametersInfo(SPI_SETCURSORS, 0, 0, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
}
"@
    Add-Type -MemberDefinition $Signature -Name UserPreferencesMaskSPI -Namespace User32
    [User32.UserPreferencesMaskSPI]::UpdateUserPreferencesMask()
}
Update-UserPreferencesMask

但不幸的是,光标大小更新不是这样工作的。

一个解决方法是使用arrow_rl.cur(大图像)代替arrow_r.cur。

请参考使用PowerShell更改鼠标指针方案在Windows中以编程方式更改自定义鼠标光标


谢谢。这就是我试图更改两个相互关联的值的注册表项的原因,即“CursorSize”和“CusorBaseSize”。不幸的是,这种解决方法并不是我想要的。我希望像访问设置一样控制它,只需保留现有的样式。但这部分超出了问题的范围。如果我可以设置大小,那么另一个问题就不会那么困难了。 - Ste
@Ste 如果您可以接受从系统设置中编辑大小和颜色,那么您可以考虑使用UI自动化 - Rita Han
嗨,我不确定。如果您在将某个样式设置为光标并更改大小时没有意识到,那么该样式将会丢失。我可以更改样式,但我想在保持现有方案的同时更改大小。我的最终目标是导出现有方案的.reg文件,更改光标大小(而无需重新启动),然后再次重新导入.reg文件。这是我能想到的唯一解决方法。但是,此线程主要是关于设置大小的。您是否有关于如何解决我的问题的UI自动化示例? - Ste
你是否找到了解决这个问题的方法?这个 AHK 脚本也使用了 SPI_SETCURSORS (0x57),它可以工作,但我无法弄清如何使用它来改变光标的大小 - https://autohotkey.com/board/topic/32608-changing-the-system-cursor/ - shadowz1337

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接