Creating an lParam value for windows API function calls (C#)
Submitted by aaron on Mon, 02/09/2009 - 18:47
I recently found a really neat way to create lParam parameters for the Window’s API SendMessage and PostMessage functions. This specifically applies to messages like WM_MOUSEMOVE that require both an x and a y coordinate to be encoded into that single parameter. I had been using code like this :
DllImport("user32.dll", EntryPoint = "PostMessage", CharSet = CharSet.Auto)]
static extern bool PostMessage1x(IntPtr hWnd, uint Msg, int wParam, uint lParam);
public static void PostMouseMove(IntPtr hControl, int x, int y) {
uint lParam = makeDWord(Convert.ToUInt16(x), Convert.ToUInt16(y));
PostMessage1x(hControl, WM_MOUSEMOVE, 0, lParam);
}
public static uint makeDWord(ushort LoWord, ushort HiWord) {
return (uint)(LoWord + (HiWord << 16));
},but now I am using IntPtr’s instead and encoding the coordinates with some simple math.
static uint WM_MOUSEMOVE = 0x0200;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
public static void PostMouseMove(IntPtr hControl, int x, int y) {
PostMessage(hControl, WM_MOUSEMOVE, 0, new IntPtr(y * 0x10000 + x));
}


