现在的位置: 首页 > 综合 > 正文

SDI实现多视图并切换视图

2013年09月06日 ⁄ 综合 ⁄ 共 3563字 ⁄ 字号 评论关闭

首先在H文件有如下声明:

class CMultiViewApp : public CWinApp

{

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

抱歉!评论已关闭.