在MFC程序中添加全屏显示功能

时间:2022-08-05 10:06:52

以SDI工程为例,在工具条上添加一个ID,ID_FULL_SCREEN, 同时添加处理函数,代码如下:

void CMainFrame::OnFullScreen()
{
 // TODO: Add your command handler code here
 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_bFullScreen=TRUE; //设置全屏显示标志为 TRUE
 //进入全屏显示状态
 WINDOWPLACEMENT wndpl;
 wndpl.length=sizeof(WINDOWPLACEMENT);
 wndpl.flags=0;
 wndpl.showCmd=SW_SHOWNORMAL;
 wndpl.rcNormalPosition=m_FullScreenRect;
 SetWindowPlacement(&wndpl);
}

 

void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
 // TODO: Add your message handler code here and/or call default
 if(m_bFullScreen)
 {
  lpMMI->ptMaxSize.y = m_FullScreenRect.Height();
  lpMMI->ptMaxTrackSize.y = lpMMI->ptMaxSize.y;
  lpMMI->ptMaxSize.x = m_FullScreenRect.Width();
  lpMMI->ptMaxTrackSize.x = lpMMI->ptMaxSize.x;

 } 
 CFrameWnd::OnGetMinMaxInfo(lpMMI);
}

 

另外添加一个终止全屏显示函数:

void CMainFrame::EndFullScreen()
{
 if(m_bFullScreen)
 {
  m_bFullScreen=FALSE; //设置全屏显示标志为 TRUE
  SetWindowPlacement(&m_OldWndPlacement);
 }
}

 

在视图中添加对escape键的处理,终止全屏显示:

void CDragSDIView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
 // TODO: Add your message handler code here and/or call default
 if( nChar == VK_ESCAPE)
 {
  CMainFrame *pFrame=(CMainFrame*)AfxGetMainWnd();
  pFrame->EndFullScreen();
 }
 CView::OnKeyDown(nChar, nRepCnt, nFlags);
}