全屏模式下的视图






4.76/5 (32投票s)
2005年2月19日
2分钟阅读

101040

2504
在全屏模式下查看窗口。
引言
在许多应用程序中,需要以全屏模式查看我们的视图。这尤其适用于从 CView
派生的类。该项目展示了一个简单的应用程序(SDI),它使用 CScrollView
类在屏幕上显示数据。最初,视图处于正常模式(放置在主框架中,带有工具栏、状态栏和菜单)。将我们的视图转换为全屏模式,需要隐藏所有其他控件。这真的有必要吗?这涉及到隐藏带有工具栏和菜单的主框架,并且需要在放置新控件时更改代码。该项目解放了程序员,避免了上述必要性,并提供了一种技巧,用于预览在您的视图中运行的进度。
第一步
第一步是在 CView
(CSrollView
,....)中添加变量
boolean isFull; /* in constructor of CView: isFull=false; */ CWnd *saveParent; /* Besides other 3 var.: */ COLORREF background, text; CMenu *ContextMenu;
下一步是在资源中添加一个新的按钮(新的子菜单)并附加一个新的 ID:ID_FULL_SCREEN_MODE
- 以启用/禁用全屏模式。将此 ID(在 ClassWizard 中)与函数 CFullScreenView::OnFullScreenMode()
、CFullScreenView::OnUpdateFullScreenMode(CCmdUI* pCmdUI)
关联。
最重要的行(技巧)
(注释中的描述)
void CFullScreenView::OnFullScreenMode() { /* if normal mode - start (enable) full screen mode */ if(!isFull){ isFull=true; /* Variable saveParent is used to save address of parent window (address is necessary in disabling this mode -to place view in former position) */ saveParent=this->GetParent(); /* this (our view) is placed on desktop window - CWnd::GetDesktopWindow() - Returns the Windows desktop window */ this->SetParent(GetDesktopWindow()); CRect rect; /* temporary variable */ /* Next function copies the dimensions of the bounding rectangle of the desktop */ GetDesktopWindow()->GetWindowRect(&rect); /* view is placed on desktop */ this->SetWindowPos(&wndTopMost,rect.left,rect.top, rect.right,rect.bottom,SWP_SHOWWINDOW); } else{ /* disable full screen mode */ isFull=false; /* our view is placed in old window - using address saveParent (pointer that has been used in former if-section) */ this->SetParent( saveParent); /* pointer to the main frame and function RecalcLayout() (run on behalf of CmainFrame object) - that repositions all control bars in the frame (#include <MainFrame.h>) */ ((CMainFrame *)AfxGetMainWnd())->RecalcLayout(); } }
本质上,上述函数(其主体)是实现我们目标的方式 - 在初始化前一章中提到的变量之后。
关闭全屏模式
重要的是能够禁用全屏模式。因此,添加了一个上下文菜单(通过右键单击运行)。仅上下文菜单,因为假设要隐藏所有控件、工具栏和菜单。
如何禁用全屏模式?最好的方法是发送消息 ID_FULL_SCREEN_MODE
。在资源中,插入一个新的菜单 IDR_MENU_VIEW
,并在子菜单中定义新的项目(第一个是 ID_FULL_SCREEN_MODE
,我的项目中的其他项目用于启动 CColorDialog
)。
在 CView::OnInitialUpdate()
中,
ContextMenu= new CMenu(); if(!ContextMenu->LoadMenu(IDR_MENU_VIEW)){ AfxMessageBox("Fail to create context menu"); }
上下文菜单应该在单击鼠标(右键)后出现。
void CFullScreenView::OnRButtonDown(UINT nFlags, CPoint point) { ClientToScreen(&point); if(ContextMenu->GetSubMenu(0)){ CMenu *pSubMenu= ContextMenu->GetSubMenu(0); if(isFull){ /* check menu item if full screen mode */ pSubMenu->CheckMenuItem(ID_FULL_SCREEN_MODE,MF_CHECKED ); /* disable next positions - to avoid sending message, that start dialogs, etc. */ pSubMenu->EnableMenuItem(ID_CHANGE_TEXT_COLOR,MF_GRAYED); pSubMenu->EnableMenuItem(ID_CHANGE_BACKGROUND_COLOR,MF_GRAYED); } else{ /* uncheck menu item */ pSubMenu->CheckMenuItem(ID_FULL_SCREEN_MODE,MF_UNCHECKED ); /* enable menu item - in normal mode. */ pSubMenu->EnableMenuItem(ID_CHANGE_TEXT_COLOR,MF_ENABLED); pSubMenu->EnableMenuItem(ID_CHANGE_BACKGROUND_COLOR,MF_ENABLED); } pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x,point.y,this); } CScrollView::OnRButtonDown(nFlags, point); }
警告!!
在全屏模式下无法显示其他窗口(对话框等)。此模式仅用于查看视图的内容。