从代码更改 'Show location of pointer when I press the Ctrl key' Windows 设置

Change 'Show location of pointer when I press the Ctrl key' Windows setting from code

如何从代码 (.net) 更改 'Windows mouse checkBox' 配置 'Show location of pointer when I press the Ctrl key'?如本网页手动所述?

https://mcmw.abilitynet.org.uk/windows-7-and-8-finding-your-mouse-pointer/

使用 SystemParametersInfo WinAPI 函数和 SPI_SETMOUSESONAR 命令来启用或禁用鼠标声纳(因为此功能在 WinAPI 术语中被称为)

private void buttonEnableMouseSonar_Click(object sender, EventArgs e)
{
    SetMouseSonarEnabled(true);
}

private void buttonDisableMouseSonar_Click(object sender, EventArgs e)
{
    SetMouseSonarEnabled(false);
}


[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);

private void SetMouseSonarEnabled(bool enable)
{
    const uint SPI_SETMOUSESONAR = 0x101D;
    const uint SPIF_UPDATEINIFILE = 0x01;
    const uint SPIF_SENDCHANGE = 0x02;

    if(!SystemParametersInfo(SPI_SETMOUSESONAR, 0, (uint)(enable ? 1 : 0), SPIF_UPDATEINIFILE | SPIF_SENDCHANGE))
    {
        throw new Win32Exception();
    }
}

要从托管 .net 代码使用 WinAPI 函数,您需要使用一个名为 "p/invoke" 的功能。


VB.net版本:

Private Sub buttonEnableMouseSonar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    SetMouseSonarEnabled(True)
End Sub

Private Sub buttonDisableMouseSonar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
    SetMouseSonarEnabled(False)
End Sub

<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function SystemParametersInfo(ByVal uiAction As UInteger, ByVal uiParam As UInteger, ByVal pvParam As UInteger, ByVal fWinIni As UInteger) As Boolean
End Function


Private Sub SetMouseSonarEnabled(ByVal enable As Boolean)
    Const SPI_SETMOUSESONAR As UInteger = 4125
    Const SPIF_UPDATEINIFILE As UInteger = 1
    Const SPIF_SENDCHANGE As UInteger = 2
    If Not SystemParametersInfo(SPI_SETMOUSESONAR, 0, CUInt((If(enable, 1, 0))), SPIF_UPDATEINIFILE Or SPIF_SENDCHANGE) Then
        Throw New Win32Exception()
    End If
End Sub