Windows下强制激活Qt窗口
#ifdef _WIN32
#include <QtPlatformHeaders/QWindowsWindowFunctions>
#endif
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
#ifdef _WIN32
QWindowsWindowFunctions::setWindowActivationBehavior(QWindowsWindowFunctions::AlwaysActivateWindow);
#endif
}
之后可以调用showNormal() activateWindow()
激活窗口
windows api方法激活窗口
// 激活窗口
#ifdef _WIN32
HWND hForeWnd = NULL;
DWORD dwForeID = 0;
DWORD dwCurID = 0;
hForeWnd = ::GetForegroundWindow();
dwCurID = ::GetCurrentThreadId();
dwForeID = ::GetWindowThreadProcessId(hForeWnd, NULL);
::AttachThreadInput(dwCurID, dwForeID, TRUE);
::ShowWindow(winID(), SW_SHOWNORMAL);
::SetWindowPos(winID(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
::SetWindowPos(winID(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
::SetForegroundWindow(winID());
::AttachThreadInput(dwCurID, dwForeID, FALSE);
#endif // _WIN32