更改'当我按下Ctrl键时显示指针的位置'代码中的Windows设置

时间:2018-03-06 14:15:11

标签: c# winapi mouse

如何更改Windows鼠标复选框'配置'当我按下Ctrl键时显示指针的位置'从代码(.net)?如在此网页上手动描述的那样?

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

1 个答案:

答案 0 :(得分:1)

使用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