(C++)窗口置前SetForegroundWindow(pThis->hwndWindow);

一段代码主要是创建一个Window,然后将其置顶显示。奇怪的是这个功能有时候无效。

                        pThis->bWindowDisplayed = SetForegroundWindow(pThis->hwndWindow);


查MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx 看来需要满足的条件不少。

网上解决方法:

HWND hForeWnd = NULL; 
HWND hWnd= FindWindow(NULL, ""); 
DWORD dwForeID; 
DWORD dwCurID; 
 
hForeWnd =  GetForegroundWindow(); 
dwCurID =  GetCurrentThreadId(); 
dwForeID =  GetWindowThreadProcessId( hForeWnd, NULL ); 
AttachThreadInput( dwCurID, dwForeID, TRUE); 
ShowWindow( hWnd, SW_SHOWNORMAL ); 
SetWindowPos( hWnd, HWND_TOPMOST, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE ); 
SetWindowPos( hWnd, HWND_NOTOPMOST, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE ); 
SetForegroundWindow( hWnd ); 
AttachThreadInput( dwCurID, dwForeID, FALSE);
原文地址:https://www.cnblogs.com/fdyang/p/5158675.html