我正在尝试使用WndProc挂钩从Win32 API获取消息后获取鼠标线。
下面是我的代码..它不长,应该很容易理解.. 我正在学习这一切,因为我去了,只是无法弄清楚如何将lParam更改为点x和y ..
任何帮助都会很好,谢谢:)
private const int WM_LEFTBUTTONDOWN = 0x0201;
private const int WM_LEFTBUTTONUP = 0x0202;
private const int WM_MOUSEMOVE = 0x0200;
private const int WM_MOUSEWHEEL = 0x020A;
private const int WM_RIGHTBUTTONDOWN = 0x0204;
private const int WM_RIGHTBUTTONUP = 0x0205;
public MainWindow()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_MOUSEMOVE)
{
label1.Content = "Msg: " + msg + " wParam: " + wParam + " lParam: " + lParam;
}
return IntPtr.Zero;
}
答案 0 :(得分:5)
您可以使用Point(int dw)
构造函数:
Point point = new Point(lParam.ToInt32());
...
从MSDN了解int dw
参数:
dw参数的低16位指定水平x坐标,高16位指定新Point的垂直y坐标。
答案 1 :(得分:4)
x坐标为低16位,y为接下来的16位。像这样破解:
int x = (short)lParam.ToInt32();
int y = lParam.ToInt32() >> 16;