使用
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();
}
}
使用托管中的WinAPI函数。net代码中,您使用了一个名为“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