I wrote some code to determinate whether a certain window is inside the desktop bounds.
Somehow, it doesn't work. For every window, regardless whether it is inside or outside the desktop, false
is returned. Something is terribly wrong here, but after staring at this piece of code for 3 hours now, I still don't know where the problem is. If I try to read a RECT struct from the pointer sent in the PMSG's WPARAM, I get a AccessViolationException. Why is this happening?
My code look like this, and always returns false:
static bool IsInBounds(HWND window)
{
DEVMODE d;
d.dmSize = sizeof(DEVMODE);
BOOL b = EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &d);
if(b == FALSE)
{
PostMessage(FindWindow(NULL, L"Window #1"), RegisterWindowMessage(L"FMSG"), (WPARAM)window, NULL);
}
RECT R;
GetWindowRect(window, &R);
POINT p = POINT();
p.x = (LONG)d.dmPelsWidth;
p.y = (LONG)d.dmPelsHeight;
PostMessage(FindWindow(NULL, L"Window #1"), RegisterWindowMessage(L"PMSG"), (WPARAM)&R, d.dmPelsWidth);
if(R.right < 0 || R.bottom < 0 || R.left > (LONG)d.dmPelsWidth || R.top > (LONG)d.dmPelsHeight)
{
return false;
}
return true;
}
EDIT:
after calling IsInBounds, which returned an error number 1400 (Invalid window handle), but before returning from the hook callback, I called IsWindow(window)
, to see, if my handle is still valid. The truth is: it is in deed a valid handle! How can it be that GetWindowRect says its an invalid handle?
EDIT: I tried MonitorFromWindow as sujested, but it returned NULL, and calling GetLastError resulted in error no. 1400, which is already familiar to me. Looks like MonitorFromWindow implicitly calls GetWindowRect. I don't care about the size, but is there another way to get window coordinates from a handle?