You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需重启Windows,PowerShell修改注册表改光标方案失败求助

解决PowerShell修改光标方案无需重启生效的问题

修正后的完整代码

function Change-MouseScheme {
    function Update-CursorRegistry {
        # 注意:Windows仅支持.cur(静态)或.ani(动态)格式的光标文件
        $cursorPath = "C:\Windows\Cursors\your-custom-cursor.cur" # 替换为你的合法光标路径

        $regKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Control Panel\Cursors", $true)
        if (-not $regKey) {
            Write-Error "无法打开注册表项"
            return
        }

        # 设置自定义光标路径(按需修改单个光标)
        $regKey.SetValue("", "Custom Cursor Scheme") # 自定义方案名称
        $regKey.SetValue("AppStarting", $cursorPath)
        $regKey.SetValue("Arrow", $cursorPath)
        $regKey.SetValue("Crosshair", $cursorPath)
        $regKey.SetValue("Hand", $cursorPath) # 禁止设为空,可替换为系统默认路径如"c:\windows\cursors\aero_link.cur"
        $regKey.SetValue("Help", $cursorPath)
        $regKey.SetValue("IBeam", $cursorPath)
        $regKey.SetValue("No", $cursorPath)
        $regKey.SetValue("NWPen", $cursorPath)
        $regKey.SetValue("SizeAll", $cursorPath)
        $regKey.SetValue("SizeNESW", $cursorPath)
        $regKey.SetValue("SizeNS", $cursorPath)
        $regKey.SetValue("SizeNWSE", $cursorPath)
        $regKey.SetValue("SizeWE", $cursorPath)
        $regKey.SetValue("UpArrow", $cursorPath)
        $regKey.SetValue("Wait", $cursorPath)

        $regKey.Close()
        Write-Host "[+] 光标注册表已更新"
    }

    function Apply-CursorChanges {
        $code = @'
            [DllImport("user32.dll", SetLastError = true)]
            public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool SendMessageTimeout(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam, uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);

            public static void ApplyCursorSettings() {
                // SPI_SETCURSORS = 0x0057,通知系统更新光标配置
                bool result = SystemParametersInfo(0x0057, 0, IntPtr.Zero, 0x01 | 0x02); // SPIF_UPDATEINIFILE | SPIF_SENDCHANGE
                if (!result) {
                    throw new System.ComponentModel.Win32Exception();
                }

                // 发送WM_SETTINGCHANGE消息到所有顶级窗口,强制刷新UI
                UIntPtr unused;
                SendMessageTimeout((IntPtr)0xffff, 0x001A, UIntPtr.Zero, IntPtr.Zero, 0x0002, 1000, out unused);
            }
'@
        Add-Type -MemberDefinition $code -Name CursorSettings -Namespace User32
        try {
            [User32.CursorSettings]::ApplyCursorSettings()
            Write-Host "[+] 光标设置已生效"
        }
        catch {
            Write-Error "应用设置失败: $_"
        }
    }

    Update-CursorRegistry
    Apply-CursorChanges
    Write-Host "[+] 光标方案已修改"
}

# 执行修改
Change-MouseScheme

原代码的核心问题解析

  1. 光标格式不兼容:Windows仅识别.cur或.ani格式的光标文件,你使用的.jpg完全无法被系统解析,必须替换为合法光标文件。
  2. API调用参数错误:
    • SystemParametersInfo的pvParam参数应传入IntPtr.Zero,而非空字符串
    • 参数类型定义错误,原代码中pvParam被定义为uint,实际应为IntPtr
  3. 注册表配置错误:Hand字段设为空会导致系统找不到对应光标,需设置为有效路径(自定义光标或系统默认光标路径)。
  4. 缺少全局刷新通知:仅调用SystemParametersInfo可能无法让所有窗口立即更新光标,需额外发送WM_SETTINGCHANGE消息强制刷新整个系统的UI配置。

内容的提问来源于stack exchange,提问作者jefrey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 04:52:03