GetActiveWindow()
-
Hi all, I faced with an intresting event about GetActiveWindow() method. When I call the code blok below by clicking to a button, I can see the messagebox as "asdasd". But when I call the code blok in a worker thread I cannot see the messagebox.
CWnd *wnd;
wnd = GetActiveWindow();
if(pDlgb == wnd)
MessageBox(_T("asdasd"));What is the reason for that? Is there any body who can give me an advice about getting the active window in a worker thread? Thanks, ibrahim
-
Hi all, I faced with an intresting event about GetActiveWindow() method. When I call the code blok below by clicking to a button, I can see the messagebox as "asdasd". But when I call the code blok in a worker thread I cannot see the messagebox.
CWnd *wnd;
wnd = GetActiveWindow();
if(pDlgb == wnd)
MessageBox(_T("asdasd"));What is the reason for that? Is there any body who can give me an advice about getting the active window in a worker thread? Thanks, ibrahim
Because MFC keeps internal table(s) of (CWnd) windows on a per-thread-basis. Therefore , the CWnd returned by GetActiveWindow() on a different thread can't possibly be the same pointer as your pDlgb. It will be a temporary CWnd object also attached to your dialog's HWND (assuming that window is actually the active window). Compare the HWNDs instead
if(pDlgb->GetSafeHwnd() == wnd->GetSafeHwnd())
Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Because MFC keeps internal table(s) of (CWnd) windows on a per-thread-basis. Therefore , the CWnd returned by GetActiveWindow() on a different thread can't possibly be the same pointer as your pDlgb. It will be a temporary CWnd object also attached to your dialog's HWND (assuming that window is actually the active window). Compare the HWNDs instead
if(pDlgb->GetSafeHwnd() == wnd->GetSafeHwnd())
Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
It's a little more complicated than that, although it’s true that MFC's handle maps are thread specific. Here’s how MFC implements
CWnd::GetActiveWindow
:_AFXWIN_INLINE CWnd* PASCAL CWnd::GetActiveWindow()
{ return CWnd::FromHandle(::GetActiveWindow()); }Now the following is from MSDN on the ::GetActiveWindow[^] function:
The GetActiveWindow function retrieves the window handle to the active window attached to the calling thread's message queue.
So::GetActiveWindow
is also thread specific, although you could use the AttachThreadInput[^] to get around this.Steve