如何在 Windows 中更改鼠标指针大小?

Dav*_*son 5 c# windows powershell mouse-cursor

在我的 4k 屏幕上,我喜欢在窗口中调大鼠标指针的大小。
将 Windows 鼠标指针设置为 4

这对于 95% 的使用情况来说非常有用,但有时我又需要一个小鼠标指针来进行更精细的工作。
不幸的是我每次都必须打开设置,我想要一个键盘快捷键,可以用来在大鼠标和小鼠标之间切换。

所以我写了以下内容,希望能起到作用:

  1. 使用新的鼠标大小设置更新注册表
  2. 使用SystemParametersInfo()参数调用SPI_SETCURSORS来点动系统以重新读取鼠标设置。

唉,没有喜悦。

谁能指出我的做法是愚蠢的吗?干杯,戴夫

代码如下:

param([int]$MouseSize=0)

###########################################
#
# DISAPPOINTINGLY  THIS DOES NOT YET WORK
# Need to figure out why
#
###########################################
 
##################
# Constants
##################
 
[int]$small_mouse = 48
[int]$big_mouse   = 80
[int]$min_mouse   = 1
[int]$max_mouse   = 256
 
[string]$path = 'HKCU:\Control Panel\Cursors'
[string]$name = 'CursorBaseSize'
 
##################
# API Import
##################
 
Try{
    [void][SysParams]
} Catch {
Add-Type @'
    using System;
    using System.Runtime.InteropServices;
    public class SysParams {
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
 
        public static bool RefreshCursor()
        {
            return SystemParametersInfo(0x0057, 0, 0, 0); // SPI_SETCURSORS = 0x0057;
        }
    }
'@
}
 
##################
# Main
##################
 
# Is $MouseSize passed in valid?
if ($MouseSize -ge $min_mouse -and $MouseSize -le $max_mouse) {
    $new_size = $MouseSize
} else {
    # If invalid $MouseSize passed in, toggle current mouse setting
    # First determine if current setting closer to $small_mouse or $big_mouse
 
    $current_size = (Get-ItemProperty $path).$name
    $diff_small = [math]::Abs($current_size - $small_mouse)
    $diff_big = [math]::Abs($current_size - $big_mouse)
 
    if ($diff_small -lt $diff_big) {
        $new_size = $big_mouse # Mouse is small, so toggle big
    } else {
        $new_size = $small_mouse # Mouse is big, so toggle small
    }
}

Set-ItemProperty -Path $path -Name $name -Value $new_size
$resp = [SysParams]::RefreshCursor()
Run Code Online (Sandbox Code Playgroud)

Baj*_*jan 0

获取当前系统参数

$systemParameters = [System.Windows.Forms.SystemInformation]::Mouse
Run Code Online (Sandbox Code Playgroud)

设置鼠标指针大小

$systemParameters.MouseSize = 20
Run Code Online (Sandbox Code Playgroud)

更新系统参数

[System.Windows.Forms.SystemInformation]::Mouse = $systemParameters
Run Code Online (Sandbox Code Playgroud)