当新窗口在前面时,GetForegroundWindow()不会更新

时间:2017-09-19 06:40:18

标签: winapi

我这样做

HWND lParentWinHandle = GetForegroundWindow();

if (!CreateProcessA(NULL, lArgs, NULL, NULL, FALSE,
    CREATE_NEW_CONSOLE, NULL, NULL,
    &StartupInfo, &ProcessInfo))
{ ... }

// at this point a new window appears in the foreground.

HWND lChildWinHandle = GetForegroundWindow();
SetParent(lChildWinHandle , lParentWinHandle );

但是lParentWinHandle和lChildWinHandle具有相同的值!

1 个答案:

答案 0 :(得分:0)

如果您想获得新流程的句柄。您应该使用PROCESS_INFO结构的进程和线程句柄。

根据你应该用WaitForInputIdle编写的文档,直到你开始搜索。

代码与此类似(它只是一个脏样本,没有错误检查):

BOOL CALLBACK EnumThreadWndProc(HWND hWnd, LPARAM lParam)
{
   hWndMain = hWnd; // got the main window...
   return FALSE; // ...stop enum
}

// ...
   STARTUPINFO siStartInfo = {0};
   PROCESS_INFORMATION piProcessInfo = {0};

   CreateProcessA(NULL, pCmd, 
                  NULL, NULL, FALSE, 0, NULL, NULL,
                  &siStartInfo,&piProcessInfo);

   if(WaitForInputIdle(piProcessInfo.hProcess, 5000)==0)
   {
      EnumThreadWindows(piProcessInfo.dwThreadId, 
                            EnumThreadWndProc, NULL);
   }
   else
   {
      // Could not start program
      ...
   }
   // ...

但请阅读Is it legal to have a cross-process parent/child or owner/owned window relationship?