tags:

views:

88

answers:

2

The application is of MFC. Sometimes I need to activate the window and display it at the topmost of the screen when it's deactivated, or hidden, or minimized. Here's what I did:

AfxGetMainWnd()->BringWindowToTop();
AfxGetMainWnd()->SetActiveWindow();
AfxGetMainWnd()->SetForegroundWindow();

if(AfxGetMainWnd()->IsIconic())
AfxGetMainWnd()->ShowWindow(SW_SHOWNORMAL);
else
     AfxGetMainWnd()->ShowWindow(SW_SHOW);
AfxGetMainWnd()->UpdateWindow();

But I found sometimes the window was not activated and was still convered by window of other appliactions. Is there anything wrong with my approach? How should I fix this?

Thank you very much!

A: 

Try also calling SetFocus on the window you want to show.

If that still doesn't work, or doesn't work 100% you could use a hacky workaround whereby you would launch a thread or window timer (timer's easier) which would periodically check to see whether or not the window that you want to be top most does indeed make it to the top of the order. Once this happens, possibly on the first iteration, you kill the thread or timer.

quantity, i see from your profile that you've asked 12 questions and accepted none. i find it hard to believe that none of the answers worked for you. Please consider going through the responses and marking ones that work as answers. 0% acceptance might cause folks to not care to answer your questions soon.

Cheers.

Paul Sasik
+1  A: 

try SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_SHOWWINDOW);

it should work on all windows, since all windows have the same handle type.

Andrew Keith