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

随声附和 提交于 2019-12-01 05:26:41

You can try to use the STARTUPINFO structure which is passed in with CreateProcess and set SW_SHOW. I'm not sure this will help bring the focus to the top though. If that doesn't work then try the following.

First off, do not use FindWindow(), it is unnecessarily unreliable since it only works via the window name and class name. Instead, from your CreateProcess() call you should read lpProcessInformation and grab the dwProcessId. Then call EnumWindows() and have your callback look something like this:

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;
}

When calling EnumWindows() you will need to pass in the PID you grabbed earlier as the lParam like so:

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

You need the window handle of the application you launched. If you don't have it, you could use the FindWindowA API call.

Then use the SetFocus API call with the window handle as parameter.

Related links:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!