如何将窗口置于通过CreateProcess创建的流程之上 [英] How to bring window on top of the process created through CreateProcess

查看:52
本文介绍了如何将窗口置于通过CreateProcess创建的流程之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am launching a process from my application using CreateProcess API and I want to bring the window of the new process to top. Is there a way to do it? Do we have any flag or something like that with CreateProcess?

推荐答案

您可以尝试使用

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.

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

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

在调用EnumWindows()时,您需要像之前那样将您之前抓取的PID传递为lParam:

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

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

这篇关于如何将窗口置于通过CreateProcess创建的流程之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆