Try something like:
Code:void SwitchTo(HWND hWnd) { HWND hPopup = GetWindow(hWnd, GW_ENABLEDPOPUP); if (hPopup) hWnd = hPopup; if (!IsWindow(hWnd) || IsHungAppWindow(hWnd)) return; if (IsIconic(hWnd)) { PostMessage(hWnd, WM_SYSCOMMAND, SC_RESTORE, 0); return; } HWND hForeground = GetForegroundWindow(); if (hWnd != hForeground) { BringWindowToTop(hWnd); SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); // Try SetForegroundWindow if (!SetForegroundWindow(hWnd)) { // SetForegroundWindow failed, try with attaching input threads if (!hForeground) hForeground = FindWindow(_T("Shell_TrayWnd"), NULL); DWORD idForeground = GetWindowThreadProcessId(hForeground, NULL); DWORD idTarget = GetWindowThreadProcessId(hWnd, NULL); AttachThreadInput(idForeground, idTarget, TRUE); // Try SetForegroundWindow again if (!SetForegroundWindow(hWnd)) { // Still not working... // Simulate two single ALT keystrokes, See LockSetForegroundWindow INPUT inp[4]; ZeroMemory(&inp, sizeof(inp)); inp[0].type = inp[1].type = inp[2].type = inp[3].type = INPUT_KEYBOARD; inp[0].ki.wVk = inp[1].ki.wVk = inp[2].ki.wVk = inp[3].ki.wVk = VK_MENU; inp[0].ki.dwFlags = inp[2].ki.dwFlags = KEYEVENTF_EXTENDEDKEY; inp[1].ki.dwFlags = inp[3].ki.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP; SendInput(4, inp, sizeof(INPUT)); SetForegroundWindow(hWnd); } AttachThreadInput(idForeground, idTarget, FALSE); } } Sleep(50); }




Reply With Quote