运行 进程与父进程在同一个监视器上
Run process on the same monitor as the parent process
我有一个用 C++ 编写的 MFC 应用程序,它通过 ShellExecuteEx()
启动记事本。假设这两个应用程序都在双显示器系统上 运行,我如何确保记事本在与主应用程序相同的显示器上打开?
您可以在 SHELLEXECTUTEINFO
structure and specify the handle of the required monitor in the hMonitor
member. You can get the monitor handle of your application's main window using the MonitorFromWindow
API 调用的 fMask
成员中设置 SEE_MASK_HMONITOR
位。
以下代码(或非常类似的代码)应该可以解决问题:
void RunNotepadOnMyMonitor() {
SHELLEXECUTEINFO sei;
memset(&sei, 0, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_HMONITOR;
sei.lpVerb = _T("open"); // Optional in this case: it's the default
sei.lpFile = _T("notepad.exe");
sei.lpParameters = nullptr; // Add name of file to open - if you want!
sei.nShow = SW_SHOW;
sei.hMonitor = ::MonitorFromWindow(AfxGetMainWnd()->GetSafeHwnd(), MONITOR_DEFAULTTONEAREST);
ShellExecuteEx(&sei);
}
我有一个用 C++ 编写的 MFC 应用程序,它通过 ShellExecuteEx()
启动记事本。假设这两个应用程序都在双显示器系统上 运行,我如何确保记事本在与主应用程序相同的显示器上打开?
您可以在 SHELLEXECTUTEINFO
structure and specify the handle of the required monitor in the hMonitor
member. You can get the monitor handle of your application's main window using the MonitorFromWindow
API 调用的 fMask
成员中设置 SEE_MASK_HMONITOR
位。
以下代码(或非常类似的代码)应该可以解决问题:
void RunNotepadOnMyMonitor() {
SHELLEXECUTEINFO sei;
memset(&sei, 0, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_HMONITOR;
sei.lpVerb = _T("open"); // Optional in this case: it's the default
sei.lpFile = _T("notepad.exe");
sei.lpParameters = nullptr; // Add name of file to open - if you want!
sei.nShow = SW_SHOW;
sei.hMonitor = ::MonitorFromWindow(AfxGetMainWnd()->GetSafeHwnd(), MONITOR_DEFAULTTONEAREST);
ShellExecuteEx(&sei);
}