widows api setwindowpos with SWP_HIDEWINDOW not always works
-
int cpp_ptysetpg(unsigned long iProc) { HANDLE stdFds[3]; HWND hWnd; DWORD wndPId; int vRlt = 0; WINDOWINFO vInfo; hWnd = GetTopWindow(NULL); unsigned long vProc = GetCurrentProcessId(); iProc = vProc; assert(vProc == iProc); while (hWnd != NULL) { GetWindowThreadProcessId(hWnd, &wndPId); if (iProc == wndPId) break; hWnd = GetNextWindow(hWnd, GW_HWNDNEXT); } vRlt = SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 1, 1, SWP_NOACTIVATE| SWP_HIDEWINDOW); ShowWindow(hWnd, SW_HIDE); /* save the current standard handles */ stdFds[0] = GetStdHandle(STD_INPUT_HANDLE); stdFds[1] = GetStdHandle(STD_OUTPUT_HANDLE); stdFds[2] = GetStdHandle(STD_ERROR_HANDLE); FreeConsole(); if (AllocConsole()) SetConsoleCtrlHandler((PHANDLER_ROUTINE)myhdlr, TRUE); /* restore the standard handles */ SetStdHandle(STD_INPUT_HANDLE, stdFds[0]); SetStdHandle(STD_OUTPUT_HANDLE, stdFds[1]); SetStdHandle(STD_ERROR_HANDLE, stdFds[2]); hWnd = GetTopWindow(NULL); while (hWnd != NULL) { GetWindowThreadProcessId(hWnd, &wndPId); if (iProc == wndPId) break; hWnd = GetNextWindow(hWnd, GW_HWNDNEXT); } int test = 0; if (test) SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 1, 1, SWP_NOACTIVATE | SWP_HIDEWINDOW); else { vRlt = SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 1, 1, SWP_NOACTIVATE); assert(vRlt != 0); vRlt = SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 1, 1, SWP_HIDEWINDOW);//second call not always work assert(vRlt != 0); } vRlt = GetWindowInfo(hWnd, &vInfo); assert(vRlt != 0); if (vInfo.dwStyle&WS_VISIBLE) { assert(0); ShowWindow(hWnd, SW_HIDE); } vRlt = GetWindowInfo(hWnd, &vInfo); assert(vRlt != 0); if (vInfo.dwStyle&WS_VISIBLE) assert(0); return (0); } The second call (inside if(test) xx; else{ }) of SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 1, 1, SWP_NOACTIVATE) and SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 1, 1, SWP_HIDEWINDOW) does not always work. At least 1 of 10 times running of this function the window is still visible. However it always works with
SWP_NOACTIVATE|SWP_HIDEWINDOW
. Why?