This makes it damn near impossible to figure out where I even need to look for the problem here:
I am using the message WM_MOVE to monitor when a window is moved. It works really well and everything is fine... until I move the window to a negative X position.
instead of -100,600 I get 65436,600.
That's obviously because the negative wraps around to 2^16 + x.
Now, I could check of the number is closer to 0 or closer to 65536 and if it is the latter, subtract 65536, but obviously that would be an ugly hack (And what if you had a reeeally high desktop resolution? )
I was getting the values from the DWORD by simply using LoWord and HiWord functions:
Code: Select all
LoWord(byref dword)
{
return, dword & 0xFFFF
}
HiWord(ByRef dword)
{
return, dword >> 16
}
Microsoft docs even lay out the conversion: https://msdn.microsoft.com/en-us/librar ... s.85).aspx
And other sources point that out as well. Sometimes you'll see macros called GET_X_LPARAM and GET_Y_LPARAM referenced. That one seems to be exactly what we have above: http://www.cplusplus.com/forum/windows/143936/[/color]C++Code: Select all
xPos = (int)(short) LOWORD(lParam); // horizontal position yPos = (int)(short) HIWORD(lParam); // vertical position
So, naturally, I tried to do that.The funny part about that: from the 'windowsx.h':
(Again, C++)Code: Select all
#define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
Google lead me to NumPut or NumGet for converting types in AHK.
As I type this, I have actually made it work, but I still would like some feedback (I can't imagine this is the best way to do it)
Here are my functions:
Code: Select all
LoWord(byref dword)
{
return, dword & 0xFFFF
}
HiWord(ByRef dword)
{
return, dword >> 16
}
UIntToShort(ByRef num)
{
numPut(num,num,"UInt")
return numGet(num, "Short")
}
GET_X_LPARAM(lp)
{
return, UIntToShort(LoWord(lp))
}
GET_Y_LPARAM(lp)
{
return, UIntToShort(HiWord(lp))
}
But do I really have to use both NumPut and NumGet like this?
Oh, btw: Script illustrating the process:
Creates a small window that you can drag around, showing you its location in GUI Text.
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
#SingleInstance force
GUI, new, +LastFound, Tester
GUI, -caption
GUI, font, s16 Bold
GUI, add, text, x30 y20 w200 h35 vXText, X:
GUI, add, text, x30 y70 w200 h35 vYText, Y:
GUI, show
OnMessage(0x201, "drag") ; WM_LBUTTONDOWN
OnMessage(0x3, "moving") ; WM_MOVE
drag(w,l,m,h)
{
PostMessage, 0xA1, 2,,, % "Ahk_id " h ; Left Mouse Down on GUI sends Left Mouse Down on Non-Client Area (Like title bar), enabling you to drag the window even without title bar
}
moving(w, l, m, h)
{
global
GuiControl, Text, XText, % "X: " GET_X_LPARAM(l)
GuiControl, Text, YText, % "Y: " GET_Y_LPARAM(l)
}
LoWord(byref dword)
{
return, dword & 0xFFFF
}
HiWord(ByRef dword)
{
return, dword >> 16
}
UIntToShort(ByRef num)
{
numPut(num,num,"UInt")
return numGet(num, "Short")
}
GET_X_LPARAM(lp)
{
return, UIntToShort(LoWord(lp))
}
GET_Y_LPARAM(lp)
{
return, UIntToShort(HiWord(lp))
}