GetWindowRect(LPRECT lprect) issue...

Dec 1, 2011 at 5:23am
Hi! Just a general question. I have a HWND that I want to pass into GetWindowRect(HWND hwnd, LPRECT lprect) but the compiler is comming up with errors saying i can only use this: GetWindowRect(LPRECT lprect)

I found this in my afxwin2.inl so i certainly should be able to use it:
_AFXWIN_INLINE void CWnd::GetWindowRect(LPRECT lpRect) const
{ ASSERT(::IsWindow(m_hWnd)); ::GetWindowRect(m_hWnd, lpRect); }


1
2
3
4
HWND hWindow = *pWnd; //*pWnd is a pointer to a CWnd object
			
RECT rRect = { 0 };
GetWindowRect(&rRect); //but i want: GetWindowRect(hWindow, &rRect) 


I know i'm doing something wrong. Please help !
(i'm a noobie :p)
Dec 1, 2011 at 5:36am
You seem to be programming with MFC, and the class already has a wrapper to this WinAPI, and is called the same. So if you are trying to get the rectangle of the window controlled by a CWnd class, you just use this one. In your code I see you have a pointer to a CWnd. That means you can:

1
2
RECT r;
pWnd->GetWindowRect(&r);
Dec 1, 2011 at 7:08am
Hi! Yes I am using MFC. Thanks for the suggestion. The build went fine but the program crashed...msdn suggests fixing the order of operations but I don't see where I can do that...


in the debug (among many other errors) i see:
m_hWnd CXX0030: expression cannot be evaluated


and the program breaks exactly at:
{ ASSERT(::IsWindow(m_hWnd)); ::GetWindowRect(m_hWnd, lpRect); }


PS: Thanks for the super fast reply last time !
Dec 1, 2011 at 1:36pm
I guess the process stops in the assertion. That means the window hasn't been constructed by the time you call GetWindowRect(). Suggestion: Make sure you have created the window by calling Create() or CreateEx().

FYI, I don't know MFC. I have learned little bits here and there due to my need to maintain an ATL/MFC BHO, but that's about it.
Dec 2, 2011 at 9:24am
Hmmm maybe i'm doing something wrong then. I'm trying to extract information from a window in another program that I don't control. I have the HWND to that window. Maybe there is a struc i can use to get an equivalent RECT for that window ?

ps: Ur replies make perfect sense so far so don't worry about how much MFC u know :)

thanks !
Dec 2, 2011 at 1:56pm
Ah, I see. So you have the HWND of an external window. Then call the Windows API directly:

1
2
RECT r;
::GetWindowRect(myExternalHWND, &r);


See the two colons at the beginning of the second line? Global scope operator.
Dec 4, 2011 at 9:00am
Ahhhh ok ! I think that'll work. U'r a genius thanks !!! :)
Topic archived. No new replies allowed.