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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了更改'当我按下Ctrl键时显示指针的位置'来自代码的Windows设置相关的知识,希望对你有一定的参考价值。

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

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

以上是关于更改'当我按下Ctrl键时显示指针的位置'来自代码的Windows设置的主要内容,如果未能解决你的问题,请参考以下文章

当视图在iOS中加载时如何在缩放时显示用户当前位置?

无法强制选择器在按钮按下时显示

禁用alt键临时c#

单击按钮时显示网络图像

当我按下运行时,Intellij Idea 没有构建

使 VSCode 的注释从第 0 列开始