How to activate a window of an application and dis

2019-09-04 08:05发布

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!

标签: mfc
2条回答
我想做一个坏孩纸
2楼-- · 2019-09-04 08:37

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.

查看更多
迷人小祖宗
3楼-- · 2019-09-04 08:40

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.

查看更多
登录 后发表回答