应用程序窗口置前
应用程序窗口置前
描述:二次打开exe程序时,直接激活并显示原有程序界面。
QString appName = a.applicationName(); /* 注意编码 */
std::wstring wAppName; = appName.toStdWString();
LPCWSTR lpwAppName = wAppName.c_str();
HWND hWnd = ::FindWindow(NULL, lpwAppName);
if (hWnd) {
::ShowWindow(hWnd, SW_SHOWDEFAULT);
::SetForegroundWindow(hWnd);
::FlashWindow(hWnd, TRUE); /* 任务栏闪烁 */
}
由于Windows系统对SetForegroundWindow的执行方式有所调整;有些情况单独设置SetForegroundWindow函数,则无法实现置前。
则,考虑如下方式:
HWND hWnd = ::FindWindow(NULL, lpwAppName);
if (hWnd) {
::ShowWindow(hWnd, SW_SHOWDEFAULT);
::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);
::FlashWindow(hWnd, TRUE); /* 任务栏闪烁 */
}
或
/* 置前的一种方式 */
AttachThreadInput(GetWindowThreadProcessId(::GetForegroundWindow(),NULL), GetCurrentThreadId(),TRUE);
SetForegroundWindow();
SetFocus();
AttachThreadInput(GetWindowThreadProcessId(::GetForegroundWindow(),NULL), GetCurrentThreadId(),FALSE);

浙公网安备 33010602011771号