全屏显示控制
// 全屏时的窗口size
CRect m_rectFull ;
// 全屏标志
BOOL m_bFullScreen;
修改 CMainFrame::CMainFrame 如下:
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
// Calculates window position and span in full screen mode
// Calculates span of display area
HDC hDC = ::GetDC(NULL);
int XSpan = GetDeviceCaps(hDC, HORZRES);
int YSpan = GetDeviceCaps(hDC, VERTRES);
::ReleaseDC(NULL, hDC);
// Calculates size of window elements
int XBorder = GetSystemMetrics(SM_CXFRAME);
int YCaption = GetSystemMetrics(SM_CYCAPTION);
int YMenu = GetSystemMetrics(SM_CYMENU);
int YBorder = GetSystemMetrics(SM_CYFRAME);
// Calculates window origin and span for full screen mode
CPoint Origin = CPoint(-XBorder, -YBorder - YCaption - YMenu);
XSpan += 2 * XBorder, YSpan += 2 * YBorder + YMenu + YCaption;
// Calculates full screen window rectangle
rectFull = CRect(Origin, CSize(XSpan, YSpan));
}In the function 'ToggleView()', toggling of the view is carried out. Toolbar and status bar are hidden after storing their visibility status and the window is expanded such that only the client area will be visible. Size and position of the window is also stored before expanding. Calling 'ToggleView()' a second time, the window is displayed in the original size and position again.Here is the code for 'ToggleView()':
void CMainFrame::ToggleView()
{
// Previous window status parameters
static CRect rectNormal;
static boolFull = FALSE, boolStatus, boolTool;
// Checks SDI / MDI application
if (IsKindOf(RUNTIME_CLASS(CMDIFrameWnd)))
{
// MDI Application
static nCmdShow; // Previous child-window status
CMDIChildWnd* pChild = ((CMDIFrameWnd*) this)->MDIGetActive();
if (boolFull)
{
// Resets child window show status
pChild->ShowWindow(nCmdShow);
}
else
{
// Gets child window show status
WINDOWPLACEMENT WP;
pChild->GetWindowPlacement(&WP);
nCmdShow = WP.showCmd;
// Maximizes child window
pChild->ShowWindow(SW_SHOWMAXIMIZED);
}
}
if (boolFull)
{
// Toggles from full screen to normal mode
// Shows status bar and tool bar if they were visible
if (boolStatus)
m_wndStatusBar.ShowWindow(SW_SHOW);
if (boolTool)
m_wndToolBar.ShowWindow(SW_SHOW);
RecalcLayout();
}
else
{
// Toggles from normal to full screen mode
// Saves status bar and toolbar status and hides them
if (boolStatus = m_wndStatusBar.IsWindowVisible())
m_wndStatusBar.ShowWindow(SW_HIDE);
if (boolTool = m_wndToolBar.IsWindowVisible())
m_wndToolBar.ShowWindow(SW_HIDE);
RecalcLayout();
// Saves current window position and size
GetWindowRect(rectNormal);
}
// Shows window in full screen / normal mode
MoveWindow(boolFull ? rectNormal : rectFull);
// Toggles window status
boolFull = !boolFull;
}This works for both for SDI and MDI applications.We have to set the maximum size and position of the window by overriding the message handler function 'CMainFrame::OnGetMinMaxInfo()', handling the 'WM_GETMINMAXINFO' message. Here is the message handler function:
void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
// TODO: Add your message handler code here and/or call default
// Resets position and maximum window size for full screen mode
lpMMI->ptMaxPosition = rectFull.TopLeft();
lpMMI->ptMaxTrackSize = lpMMI->ptMaxSize = CPoint(rectFull.Size());
CMDIFrameWnd::OnGetMinMaxInfo(lpMMI);
}With this, a SDI application will expand to full screen mode on calling 'ToggleView()'. One can call this function from anywhere in the application by((CMainFrame*) AfxGetMainWnd())->ToggleView();
One has to include the file 'MainFrm.h' if the 'CMainFrame' class is not defined where you make the function call.
For an MDI application, you have to handle the 'WM_GETMINMAXINFO' message for the 'CChildFrame' class also to effect the expansion properly. 'CChildFrame::OnGetMinMaxInfo()' can be implemented as
#include "MainFrm.h"
void CChildFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
// TODO: Add your message handler code here and/or call default
// Resets position and maximum child window size for full screen mode
CSize Span = ((CMainFrame*) AfxGetMainWnd())->rectFull.Size();
lpMMI->ptMaxPosition = ((CMainFrame*) AfxGetMainWnd())->rectFull.TopLeft();
lpMMI->ptMaxTrackSize = lpMMI->ptMaxSize = CPoint(Span.cx, Span.cy);
CMDIChildWnd::OnGetMinMaxInfo(lpMMI);
}
在SDI视图中按下esc键退出全屏模式
OnKeyDown()捕获不到 esc 消息
重写 BOOL CSMFCView::PreTranslateMessage(MSG* pMsg)
BOOL CSMFCView::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if( (pMsg->message == WM_KEYDOWN)//消息类型
&& (pMsg->wParam == VK_ESCAPE) ) //如果按的键为Esc键
{
//获取主框架窗口的指针
CMainFrame *pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
// 在全屏模式下才返回全屏
if (pFrame->IsFullScreen() == TRUE)
{
// 调用主窗口类的自定义函数 EndFullScreen ,便可退出全屏显示状态
pFrame->ToggleView();
}
}
return CRichEditView::PreTranslateMessage(pMsg);
}
转自 http://www.codeguru.com/forum/showthread.php?t=363365
浙公网安备 33010602011771号