Reputation: 169
I have an MFC application written in C++ that launches Notepad via ShellExecuteEx()
. Assuming both applications are running on a dual monitor system, how do I make sure that Notepad is opened on the same monitor as the main application?
Upvotes: 3
Views: 319
Reputation: 51915
You can set the SEE_MASK_HMONITOR
bit in the fMask
member of the 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 call.
The following code (or something very like it) should do the trick:
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);
}
Upvotes: 3