1)在CMainFrame类中声明几个变量,用来保存和记录信息。注意在构造函数中给m_bFullScreen赋值FALSE。
private:
CRect m_FullScreenRect;//全屏显示时的窗口位置
BOOL m_bFullScreen;//全屏显示标志
2)全屏显示的实现部分:
void CMainFrame::onFullScreen()
{
GetWindowPlacement(&m_OldWndpl); //保存原窗口位置
CRect WindowRect,ClientRect;
GetWindowRect(&WindowRect); //获取普通窗口位置
//获取各个控制条之外的客户区位置
RepositionBars(0,0xffff,AFX_IDW_PANE_FIRST,reposQuery,&ClientRect);
ClientToScreen(&ClientRect);
//获取屏幕分辩率
int nFullWidth=GetSystemMetrics(SM_CXSCREEN);
int nFullHeight=GetSystemMetrics(SM_CYSCREEN);
//计算全屏显示的窗口位置
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; //全屏标志
//进入全屏显示
WINDOWPLACEMENT wndpl;
wndpl.length=sizeof(WINDOWPLACEMENT);
wndpl.flags=0;
wndpl.showCmd=SW_SHOWNORMAL;
wndpl.rcNormalPosition=m_FullScreenRect;
SetWindowPlacement(&wndpl);
}
3)必须重载CMainFrame类的OnGetMinMaxInfo函数
void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
if(m_bFullScreen)
{
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);
}
4)设置退出全屏
全屏时,只有view来接收消息,所以,相关代码要以View为着手点:
void C**View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
::SendMessage(AfxGetApp()->m_pMainWnd->m_hWnd,WM_CHAR,KeyCode,NULL);
}
或者
void C**View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(nChar==VK_ESCAPE)
{
//获取主框架窗口的指针
CMainFrame *pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
pFrame->/*加入主框架窗口中有关退出全屏的自定义函数(一般情况下为非系统消息函数)*/;
}
}
void CMainFrame::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(nChar==VK_ESCAPE)
{
if(m_bFullScreen)
{
m_bFullScreen=FALSE;
ShowWindow(SW_MAXIMIZE);
//SetWindowPlacement(&m_OldWndpl);
}
}
CFrameWnd::OnChar(nChar, nRepCnt, nFlags);
}
调试一下效果,应该差强人意。