基于CDialog的MFC应用程序仅在主监视器中启动

时间:2016-02-04 08:13:25

标签: c++ windows mfc

说,我通过双击其可执行文件从Windows资源管理器启动基于CDialog的MFC应用程序。它通常会在屏幕中央显示对话框窗口。

但是如果我将Windows资源管理器窗口移动到辅助监视器并在那里双击它,它的窗口仍然显示在主监视器中。

如何在启动应用程序的显示器中显示它?

PS。对话窗口从InitInstance显示为:

CTestMFCDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();

1 个答案:

答案 0 :(得分:2)

行。我知道了。没关系。

其他人都感兴趣,MFC没有多个监视器的概念。因此需要覆盖居中方法:

void CTestMFCDlg::CenterWindowSmart()
{
    //Try to get the monitor that the process was started in
    STARTUPINFO si = {0};
    ::GetStartupInfo(&si);
    MONITORINFO mi = {0};
    mi.cbSize = sizeof(mi);
    if(::GetMonitorInfo((HMONITOR)si.hStdOutput, &mi))
    {
        //Got monitor size & position where the process was started in
        CRect rcThis;
        this->GetWindowRect(rcThis);

        int x = ((mi.rcWork.right - mi.rcWork.left) - rcThis.Width()) / 2;
        int y = ((mi.rcWork.bottom - mi.rcWork.top) - rcThis.Height()) / 2;

        this->MoveWindow(mi.rcWork.left + x, mi.rcWork.top + y, rcThis.Width(), rcThis.Height());
    }
    else
        this->CenterWindow();
}

根据STARTUPINFO structure的评论(搜索HMONITOR。)