这个“简单”的问题似乎充满了次要问题。
例如。新进程是否打开多个窗口;它有启动画面吗?
有简单的方法吗? (我正在启动一个新的 Notepad++ 实例)
...
std::tstring tstrNotepad_exe = tstrProgramFiles + _T("\\Notepad++\\notepad++.exe");
SHELLEXECUTEINFO SEI={0};
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.hwnd = hWndMe; // This app's window handle
sei.lpVerb = _T("open");
sei.lpFile = tstrNotepad_exe.c_str();
sei.lpParameters = _T(" -multiInst -noPlugins -nosession -notabbar ";
sei.lpDirectory = NULL;
sei.nShow = SW_SHOW;
sei.hInstApp = NULL;
if( ShellExecuteEx(&sei) )
{ // I have sei.hProcess, but how best to utilize it from here?
}
...
最佳答案
首先使用 WaitForInputIdle
暂停您的程序,直到应用程序启动并等待用户输入(此时主窗口应该已经创建),然后使用 EnumWindows
和GetWindowThreadProcessId
判断系统中哪些窗口属于创建的进程。
例如:
struct ProcessWindowsInfo
{
DWORD ProcessID;
std::vector<HWND> Windows;
ProcessWindowsInfo( DWORD const AProcessID )
: ProcessID( AProcessID )
{
}
};
BOOL __stdcall EnumProcessWindowsProc( HWND hwnd, LPARAM lParam )
{
ProcessWindowsInfo *Info = reinterpret_cast<ProcessWindowsInfo*>( lParam );
DWORD WindowProcessID;
GetWindowThreadProcessId( hwnd, &WindowProcessID );
if( WindowProcessID == Info->ProcessID )
Info->Windows.push_back( hwnd );
return true;
}
....
if( ShellExecuteEx(&sei) )
{
WaitForInputIdle( sei.hProcess, INFINITE );
ProcessWindowsInfo Info( GetProcessId( sei.hProcess ) );
EnumWindows( (WNDENUMPROC)EnumProcessWindowsProc,
reinterpret_cast<LPARAM>( &Info ) );
// Use Info.Windows.....
}
关于c++ - 如何获取 ShellExecuteEx.. hProcess 打开的窗口的 hWnd?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3269390/