【问题标题】:Change and update the size of the cursor in Windows 10 via PowerShell通过 PowerShell 在 Windows 10 中更改和更新光标的大小
【发布时间】:2020-02-06 22:55:23
【问题描述】:

我编写了下面的代码来影响(我认为)是负责 Windows 10 中光标和指针大小的唯一 reg 键。

这是我目前拥有的代码(里面还有一些额外的 cmets):

$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://docs.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 代码,则易于访问设置中的鼠标大小处于正确的值。

但是光标没有更新。

如何在不注销并重新登录或重新启动机器的情况下强制更新。


以下是一些相关的 MS 链接:

WM_SETTINGCHANGE message

SystemParametersInfoA function


编辑 - 一些附加信息

如果我运行 Process Monitor from Sysinternals 并深入挖掘,我可以在堆栈摘要下看到这一点。

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

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

这也适用于可访问性部分。 Windows.UI.Accessibility.dll

这是我在 Process Monitors 过滤器中使用的设置,用于缩小项目范围。

【问题讨论】:

    标签: powershell winapi


    【解决方案1】:

    因此,在使用作弊引擎对 SystemSettings.exe 进行了一些修改之后,我发现了 MS 如何设置光标大小。它最终仍然使用 SystemParametersInfo 但带有一些未记录的参数。试试下面的:)

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

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

    【讨论】:

    • 非常感谢这项工作。你知道为什么这张图片中的 UI 没有更新吗?它仅在重新启动后更新。 i.imgur.com/0wJI8Ww.png
    • 该工具设置多个注册表项并从使用 1-15 大小格式的 Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Accessibility\CursorSize 读取。而 SystemParametersInfo 与 0x2029 只是 Computer\HKEY_CURRENT_USER\Control Panel\Cursors\CursorBaseSize
    • 谢谢,有没有办法检查滑块移动时更改了哪些?
    【解决方案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。

    请参阅Use PowerShell to Change the Mouse Pointer SchemeProgrammatically change custom mouse cursor in windows

    【讨论】:

    • 谢谢。这就是为什么我试图更改相互关联的两个值的注册表项。 CursorSizeCusorBaseSize。不幸的是,这种解决方法不是我所追求的。我想像访问设置一样控制它,只是为了保持现有的风格。但这部分超出了问题的范围。如果我可以设置大小,那么另一个就不会那么困难了。
    • @Ste 如果您可以从系统设置中编辑尺寸和颜色,您可以考虑UI Automation
    • 嗨,我不确定。如果您不知道何时将某种样式设置为光标并更改了大小,则该样式将丢失。我可以更改样式,但我想在保留现有方案的同时更改大小。我的最终目标是导出现有方案的.reg 文件,更改光标大小(不重新启动时),然后再次重新导入.reg 文件。这是我能看到的唯一解决方法。但是这个线程主要是关于设置大小。你有一个关于这个 UI 自动化如何解决我的问题的例子吗?
    • 您找到解决此问题的方法了吗?这个 AHK 脚本也使用 SPI_SETCURSORS (0x57) 并且它可以工作,但我不知道如何使用它来改变光标的大小 - autohotkey.com/board/topic/32608-changing-the-system-cursor
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多