[转载自:http://www.sudu.cn/info/html/edu/20080403/260953.html]
我们经常会在程序中提供一个全屏显示的功能,如何实现呢?
第一步:声明FullScreenHandler类。
#ifndef __FullScreenHandler_h__ #define __FullScreenHandler_h__ class CFullScreenHandler { public: CFullScreenHandler(); ~CFullScreenHandler(); void Maximize(CFrameWnd* pFrame, CWnd* pView); void Restore(CFrameWnd* pFrame); BOOL InFullScreenMode() { return !m_rcRestore.IsRectEmpty(); } CSize GetMaxSize(); protected: CRect m_rcRestore; }; #endif //__FullScrennHandler_h__ // Global instance extern CFullScreenHandler FullScreenHandler;第二步:实现FullScreenHandler类。
#include "FullScreenHandler.h" // Global object handles full-screen mode CFullScreenHandler FullScreenHandler; CFullScreenHandler::CFullScreenHandler() { m_rcRestore.SetRectEmpty(); } CFullScreenHandler::~CFullScreenHandler() { } ////////////////// // Resize frame so view’s client area fills the entire screen. Use // GetSystemMetrics to get the screen size -- the rest is pixel // arithmetic. // void CFullScreenHandler::Maximize(CFrameWnd* pFrame, CWnd* pView) { // get view rectangle if (pView) { CRect rcv; pView->GetWindowRect(&rcv); // get frame rectangle pFrame->GetWindowRect(m_rcRestore); // save for restore const CRect& rcf = m_rcRestore; // frame rect // now compute new rect CRect rc(0,0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)); rc.left += rcf.left - rcv.left; rc.top += rcf.top - rcv.top; rc.right += rcf.right - rcv.right; rc.bottom+= rcf.bottom- rcv.bottom; // move frame! pFrame->SetWindowPos(NULL, rc.left, rc.top, rc.Width(), rc.Height(), SWP_NOZORDER); } } void CFullScreenHandler::Restore(CFrameWnd* pFrame) { const CRect& rc = m_rcRestore; pFrame->SetWindowPos(NULL, rc.left, rc.top, rc.Width(), rc.Height(), SWP_NOZORDER); m_rcRestore.SetRectEmpty(); } CSize CFullScreenHandler::GetMaxSize() { CRect rc(0,0, GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN)); rc.InflateRect(10,50); return rc.Size(); }
第三步:定义ID_VIEW_FULL_SCREEN资源,响应视图的WM_GETMINMAXINFO消息
CRect m_rcRestore; afx_msg void OnGetMinMaxInfo(MINMAXINFO* lpmmi); BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) ON_WM_GETMINMAXINFO() END_MESSAGE_MAP() ////////////////// // Important to handle this in order to set size // of window larger than whole screen. // void CMainFrame::OnGetMinMaxInfo(MINMAXINFO* lpmmi) { CSize sz = FullScreenHandler.GetMaxSize(); lpmmi->ptMaxSize = CPoint(sz); lpmmi->ptMaxTrackSize = CPoint(sz); } ////////////////// // View full screen mode. Calls CFullScreenHandler to do the work. // void CMainFrame::OnViewFullScreen() { if (FullScreenHandler.InFullScreenMode()) FullScreenHandler.Restore(this); else FullScreenHandler.Maximize(this, GetActiveView()); //如果是拆分窗口则应为FullScreenHandler.Maximize(this, m_wndSplitter); } ////////////////// // Put checkmark next to command if in full screen mode. // void CMainFrame::OnUpdateViewFullScreen(CCmdUI* pCmdUI) { pCmdUI->SetCheck(FullScreenHandler.InFullScreenMode()); }