小记MFC下全屏实现方法之一

时间:2022-02-23 10:08:41

在CMainFrame类下添加变量:

public:
    BOOL m_bFullScreenMode;        //全屏显示标志

private:
    WINDOWPLACEMENT m_OldWndPlacement;         //用来保存原窗口位置
    CRect m_FullScreenRect;        //表示全屏显示时的窗口位置

 

添加函数:

public:
    void FullScreenModeOn();
    void FullScreenModeOff();

 

重载函数(自己也不算太明白):
    void OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI);

 

内容为:

void CMainFrame::FullScreenModeOn()
{
    GetWindowPlacement( &m_OldWndPlacement);
    CRect WindowRect;
    GetWindowRect(&WindowRect);
    CRect ClientRect;
    RepositionBars(0, 0xffff, AFX_IDW_PANE_FIRST, reposQuery, &ClientRect);
    ClientToScreen(&ClientRect);

    // 获取屏幕的分辨率

    int nFullWidth=GetSystemMetrics(SM_CXSCREEN);
    int nFullHeight=GetSystemMetrics(SM_CYSCREEN);
   
    //将除控制条外的客户区全屏显示到从(0,0)到(nFullWidth, nFullHeight)区域,
    //将(0,0)和(nFullWidth, nFullHeight)两个点外扩充原窗口和除控制条之外的 客户区位置间的差值, 就得到全屏显示的窗口位置
    m_FullScreenRect.left = WindowRect.left - ClientRect.left;
    m_FullScreenRect.top = WindowRect.top - ClientRect.top;
    m_FullScreenRect.right = WindowRect.right - ClientRect.right + nFullWidth;
    m_FullScreenRect.bottom = WindowRect.bottom - ClientRect.bottom + nFullHeight;
    m_bFullScreenMode = TRUE;
   
    //设置全屏显示标志为 TRUE
    //进入全屏显示状态
    WINDOWPLACEMENT wndpl;
    wndpl.length=sizeof(WINDOWPLACEMENT);
    wndpl.flags=0;
    wndpl.showCmd=SW_SHOWNORMAL;
    wndpl.rcNormalPosition=m_FullScreenRect;
    SetWindowPlacement(&wndpl);

    m_bFullScreenMode=true;
}

void CMainFrame::FullScreenModeOff()
{
    ShowWindow(SW_HIDE);
    SetWindowPlacement(&m_OldWndPlacement);
    m_bFullScreenMode=false;
}

void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
    if(m_bFullScreenMode)
    {
        lpMMI -> ptMaxSize.x = m_FullScreenRect.Width();
        lpMMI -> ptMaxSize.y = m_FullScreenRect.Height();
        lpMMI -> ptMaxPosition.x=m_FullScreenRect.Width();
        lpMMI -> ptMaxPosition.y=m_FullScreenRect.Height();
        // 最大的Track尺寸也要改变
        lpMMI -> ptMaxTrackSize.x=m_FullScreenRect.Width();
        lpMMI -> ptMaxTrackSize.y=m_FullScreenRect.Height();
    }
    CFrameWnd::OnGetMinMaxInfo(lpMMI) ;
}

 

对CMainFrame类改动到此为止。

只需要在view中添加消息,获取CMainFrame的指针pWnd,调用FullScreenModeOn/Off()即可。