我们基本上是在创建一个控制面板小程序。我们需要在鼠标属性中切换“增强指针精度”。
为此,我们需要调用SystemParametersInfo
with SPI_GETMOUSE
。它有一个包含 3 个元素的数组作为其第三个参数。我是 PInvoke 的新手,我尝试过很多签名,但到目前为止还没有成功。这是我为签名所做的尝试:
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, [MarshalAs(UnmanagedType.LPArray)] ref long[] vparam, SPIF fWinIni);
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref long[] vparam, SPIF fWinIni);
以上都不适合我,这是我得到的例外:
System.AccessViolationException
: 尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
在搜索时我想出了this http://forums.codeguru.com/showthread.php?3302-Get-Set-Mouse-speed这是VB中的。
解决方案:谢谢GWLosa 的回答 https://stackoverflow.com/a/24738152/2550529 and this one https://stackoverflow.com/a/9052357/2550529我想出了解决方案:
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo", SetLastError = true)]
public static extern bool SystemParametersInfoGet(uint action, uint param, IntPtr vparam, SPIF fWinIni);
public const UInt32 SPI_GETMOUSE = 0x0003;
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo", SetLastError = true)]
public static extern bool SystemParametersInfoSet(uint action, uint param, IntPtr vparam, SPIF fWinIni);
public const UInt32 SPI_SETMOUSE = 0x0004;
public static bool ToggleEnhancePointerPrecision(bool b)
{
int[] mouseParams = new int[3];
// Get the current values.
SystemParametersInfoGet(SPI_GETMOUSE, 0, GCHandle.Alloc(mouseParams, GCHandleType.Pinned).AddrOfPinnedObject(), 0);
// Modify the acceleration value as directed.
mouseParams[2] = b ? 1 : 0;
// Update the system setting.
return SystemParametersInfoSet(SPI_SETMOUSE, 0, GCHandle.Alloc(mouseParams, GCHandleType.Pinned).AddrOfPinnedObject(), SPIF.SPIF_SENDCHANGE);
}
Also 本文档 http://technet.microsoft.com/en-us/library/cc978664.aspx事实证明是有帮助的。