简体   繁体   English

如何在启动时居中MFC MDI应用程序?

[英]How can I center an MFC MDI application at startup?

I am working with a legacy MFC (VC 6) application that needs to be modified to be centered vertically and horizontally on the screen at startup. 我正在使用旧的MFC(VC 6)应用程序,该应用程序在启动时需要进行修改以在屏幕上垂直和水平居中。 I have tried using the call to CenterWindow in the main frame OnCreate call, but that doesn't appear to do anything. 我已经尝试在主框架OnCreate调用中使用对CenterWindow的调用,但这似乎无济于事。 Any help would be greatly appreciated. 任何帮助将不胜感激。

在InitInstance()上是m_pMainWnd-> CenterWindow()?

左=(ScreenWidth-WindowWidth)/ 2顶部=(ScreenHeight-WIndowHeight)/ 2

I have an MDI application which does the positioning at startup in InitInstance of the Application class. 我有一个MDI应用程序,它在启动时在Application类的InitInstance中进行定位。 (I remember having read that OnCreate of the main frame is indeed the wrong place, but I don't know anymore where I've read that ... long time ago) I try to strip out the relevant parts here: (我记得曾经读过主机的OnCreate确实是错误的地方,但是很久以前我不知道该在哪里阅读了),我尝试在此处删除相关部分:

BOOL CMyApp::InitInstance()
{
    // stuff...

    CMyMainFrame* pMyMainFrame=CreateMainFrame();
    if (!pMyMainFrame)
        return FALSE;
    m_pMainWnd = pMyMainFrame;

    // stuff...

    if (!ProcessShellCommand(cmdInfo))
        return FALSE;

    int nCmdShow=SW_NORMAL;
    UINT flags=WPF_SETMINPOSITION;
    WINDOWPLACEMENT aWndPlacement;
    CRect rect;

    // determine the desired rect of the application window

    aWndPlacement.length=sizeof(WINDOWPLACEMENT);
    aWndPlacement.showCmd=nCmdShow;
    aWndPlacement.flags=flags;
    aWndPlacement.ptMinPosition=CPoint(0,0);
    aWndPlacement.ptMaxPosition=CPoint(-::GetSystemMetrics(SM_CXBORDER),
                                       -::GetSystemMetrics(SM_CYBORDER));
    aWndPlacement.rcNormalPosition=rect;
    m_pMainWnd->SetWindowPlacement(&aWndPlacement);
    m_nCmdShow=nCmdShow;

    pMyMainFrame->ShowWindow(m_nCmdShow);
    pMyMainFrame->UpdateWindow();

    return TRUE;
}

I hope that works for you. 希望对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM