提高指针精度
本文关键字:精度 指针 高指针 | 更新日期: 2023-09-27 18:12:53
我们基本上是在创建一个控制面板小程序。我们需要在鼠标属性中切换"增强指针精度"。
要做到这一点,我们需要用SPI_GETMOUSE
来称呼SystemParametersInfo
。它的第三个参数是一个包含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
:试图读写保护内存。这通常表明其他内存已损坏。
而搜索我想出了这是在VB。
解决方案:感谢GWLlosa的答案和这个,我想出了解决方案:
[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);
}
这个文档也被证明很有帮助。
你试过了吗:
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, IntPtr pvParam, SPIF fWinIni);
无耻地从:
http://www.pinvoke.net/default.aspx/user32/SystemParametersInfo.html