首先在H文件有如下声明:
class CMultiViewApp : public CWinApp<?XML:NAMESPACE PREFIX = O />
{
public:
CView* m_pFirstView;
CView* m_pOtherView;
int m_currentView;
CView* m_pView2;
CView* m_pView1;
CMultiViewApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMultiViewApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CMultiViewApp)
afx_msg void OnAppAbout();
afx_msg void OnViewOtherview();
afx_msg void OnViewFirstview();
//}}AFX_MSG
afx_msg void OnViewChange(UINT nCmdID);
DECLARE_MESSAGE_MAP()
};
其次,在CPP文件有如下消息MAP:
/////////////////////////////////////////////////////////////////////////////
// CMultiViewApp
BEGIN_MESSAGE_MAP(CMultiViewApp, CWinApp)
//{{AFX_MSG_MAP(CMultiViewApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
ON_COMMAND(ID_VIEW_OTHERVIEW, OnViewOtherview)
ON_COMMAND(ID_VIEW_FIRSTVIEW, OnViewFirstview)
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
ON_COMMAND_RANGE( ID_VIEW_VIEW1, ID_VIEW_VIEW2, OnViewChange)
END_MESSAGE_MAP()
说明:SDI程序在CMyApp::InitInstance()已经通过DocTemplate创建一个关联的视图/文档实例,切显示出来.具体实现如下:
BOOL CMultiViewApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMultiViewDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CMultiViewView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
CView* pActiveView = ((CFrameWnd*) m_pMainWnd)->GetActiveView();
m_pFirstView = pActiveView;
m_pOtherView = (CView*) new COtherView;
CDocument* pDoc = ((CFrameWnd*)m_pMainWnd)->GetActiveDocument();
//通过CCreateContext实现第二视图和文档的关联
CCreateContext context;
context.m_pCurrentDoc = pDoc;
UINT m_ID = AFX_IDW_PANE_FIRST + 1;
CRect rect;
//为了演示第一种多视图是实现方法,把Vew的实例创建放在了这里
m_pOtherView->Create(NULL, NULL, WS_CHILD, rect, m_pMainWnd, m_ID, &context);
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
m_pMainWnd->UpdateWindow();
m_currentView=1;
return TRUE;
}
1. SDI单文档多视图实现方法1
void CMultiViewApp::OnViewOtherview()
{
// TODO: Add your command handler code here
UINT temp = ::GetWindowLong(m_pOtherView->m_hWnd, GWL_ID);
::SetWindowLong(m_pOtherView->m_hWnd, GWL_ID, ::GetWindowLong(m_pFirstView->m_hWnd, GWL_ID));
::SetWindowLong(m_pFirstView->m_hWnd, GWL_ID, temp);
m_pFirstView->ShowWindow(SW_HIDE);
m_pOtherView->ShowWindow(SW_SHOW);
((CFrameWnd*)m_pMainWnd)->SetActiveView(m_pOtherView);
((CFrameWnd*) m_pMainWnd)->RecalcLayout();
m_pOtherView->Invalidate();
}
void CMultiViewApp::OnViewFirstview()
{
// TODO: Add your command handler code here
UINT temp = ::GetWindowLong(m_pOtherView->m_hWnd, GWL_ID); //GetWindowWord()
::SetWindowLong(m_pOtherView->m_hWnd, GWL_ID, ::GetWindowLong(m_pFirstView->m_hWnd, GWL_ID));//SetWindowWord()
::SetWindowLong(m_pFirstView->m_hWnd, GWL_ID, temp);//SetWindowWord()
m_pOtherView->ShowWindow(SW_HIDE);
m_pFirstView->ShowWindow(SW_SHOW);
((CFrameWnd*)m_pMainWnd)->SetActiveView(m_pFirstView);
((CFrameWnd*)m_pMainWnd)->RecalcLayout();
m_pFirstView->Invalidate();
}
2. SDI单文档多视图实现方法2
void CMultiViewApp::OnViewChange(UINT nCmdID)
{
//另外一种方法实现SDI的多视图切换
CView* pViewAdd;
CView* pViewRemove;
CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
CDocument* pDoc = pMainFrame->GetActiveDocument();
if((nCmdID == ID_VIEW_VIEW1) &&
分享到:
相关推荐
MFC SDI多视图切换,视图窗体静态分割,树控制视图切换
如何在SDI中切换多视图,创建多个视图,并可以进行自由切换。
SDI只支持单文档,单VIEW,可以使用拆分窗口实现多VIEW.本类实现多个VIEW,同一时间只显示单个VIEW,并可实现VIEW之间的切换.详细用法请看博客相关文章
在一个SDI应用程序中实现了单文本与多视图的关联,通过菜单切换可实现各视图自由切换~
SDI程序,可以实现多个视图之间的切换。切换到新的视图后,原有视图销毁。
在MS Visual Studio 2008 MFC中Ribbon界面风格出现了,这种界面风格给人带来耳目一新的感觉。Ribbon界面风格出现,可以说改革了传统的菜单式界面风格,常见的应用就是MS Office 2007中的Word、Excel、PowerPoint等...
单文档双视图以及单文档多视图的程序。可以实现任意多的视图之间的切换,但是用户始终只看到一个视图。根据csdn上的一个源码改编,希望对大家有用。
实现SDI中不同VIEW的切换,内容很简单,供初学者学习参考。
实现了View区域的分割,可以用作视频监控系统中的视频窗口,即实现视频窗口的分屏功能,可动态切换分屏。
views单文档程序的多视图切换,所有可切换的视图都有共同的父窗口,是一个单文档多视图的示例程序
实现单文档多视图,非切割方式,可以双击listview某一项,实现切换
这篇文章解释了如何在WTL SDI应用程序中切换视图。
功能强大的CSplitterCtrl示例程序,自己根据教程创作。集合了MFC SDI SplitterCtrl 分割窗体 多视图切换等多种功能。是SDI多视图切换及分割不错的首选示例程序
通过VC6。0实现SDI视图的TAB的切换
MFC单文档,DockingPane,点击切换多视图的基本框架
MFC SDI中使用TabControl控制多视图切换
在SDI中进行多视图切换的小程序
5.15 实例84:在SDI中切换多视图 5.16 实例85:在SDI中动态切分窗口 5.17 实例86:在SDI中静态切分窗口 5.18 实例87:滚动视图的使用 5.19 实例88:列表视图的使用 5.20 实例89:树视图的使用 5.21 实例90:HTMI视图...
20.4.6 实现对话框切换 20.4.7 创建订单ID 20.4.8 存储订单数据 20.4.9 为订单选择产品 20.4.10 添加新订单 20.5 小结 20.6 练习 第21章 使用Windows Forms的应用程序 21.1 理解Wqndows Forms 21.2 理解Windows ...