如何将窗口置于通过CreateProcess创建的进程之上

How to bring window on top of the process created through CreateProcess

本文关键字:创建 进程 CreateProcess 于通过 窗口      更新时间:2023-10-16

我正在使用CreateProcess API从我的应用程序启动一个进程,我想把新进程的窗口放在顶部。有办法吗?我们在CreateProcess中有任何标志或类似的东西吗?

您可以尝试使用CreateProcess传入的STARTUPINFO结构并设置SW_SHOW。不过,我不确定这是否有助于将注意力转移到高层。如果这不起作用,请尝试以下操作:

首先,不要使用FindWindow(),它是不必要的不可靠的,因为它只通过窗口名和类名工作。相反,应该从CreateProcess()调用中读取lpProcessInformation并获取dwProcessId。然后调用EnumWindows(),让你的回调看起来像这样:

BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam ) {
  DWORD dwPID;
  GetWindowThreadProcessId( hwnd, &dwPID );
  if( dwPID == lParam ) {
    SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
    // Or just SetFocus( hwnd );
    return FALSE;
  }
  return TRUE;
}

当调用EnumWindows()时,您将需要传入之前捕获的PID作为lParam,如下所示:

EnumWindows( EnumWindowsProc, ( LPARAM )( PI -> dwProcessId ) );

您需要启动的应用程序的窗口句柄。如果你没有,你可以使用FindWindowA API调用。

然后使用SetFocus API调用,并将窗口句柄作为参数。

相关链接:

http://www.andreavb.com/tip020001.html
http://msdn.microsoft.com/en-us/library/aa697422%28v=vs.71%29.aspx