码迷,mamicode.com
首页 > 其他好文 > 详细

SDI实现多视图并切换视图

时间:2019-01-23 13:02:35      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:control   share   odi   features   ida   tin   using   public   hang   

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

classCMultiViewApp : publicCWinApp

{

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:

       virtualBOOLInitInstance();

       //}}AFX_VIRTUAL

 

// Implementation

       //{{AFX_MSG(CMultiViewApp)

       afx_msgvoidOnAppAbout();

       afx_msg void OnViewOtherview();

       afx_msg void OnViewFirstview();

       //}}AFX_MSG

       afx_msgvoidOnViewChange(UINTnCmdID);

       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创建一个关联的视图/文档实例,切显示出来.具体实现如下:

BOOLCMultiViewApp::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 = newCSingleDocTemplate(

              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

       CCommandLineInfocmdInfo;

       ParseCommandLine(cmdInfo);

 

       // Dispatch commands specified on the command line

       if (!ProcessShellCommand(cmdInfo))

              returnFALSE;

 

       CView* pActiveView = ((CFrameWnd*) m_pMainWnd)->GetActiveView();

       m_pFirstView = pActiveView;

       m_pOtherView = (CView*) newCOtherView;

 

       CDocument* pDoc = ((CFrameWnd*)m_pMainWnd)->GetActiveDocument();

       //通过CCreateContext实现第二视图和文档的关联

       CCreateContextcontext;

       context.m_pCurrentDoc = pDoc;

 

       UINTm_ID = AFX_IDW_PANE_FIRST + 1;

       CRectrect;

       //为了演示第一种多视图是实现方法,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;

       returnTRUE;

}

1.     SDI单文档多视图实现方法1

voidCMultiViewApp::OnViewOtherview()

{

       // TODO: Add your command handler code here

       UINTtemp = ::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();

      

}

 

voidCMultiViewApp::OnViewFirstview()

{

      // TODO: Add your command handler code here

   

    UINTtemp = ::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

voidCMultiViewApp::OnViewChange(UINTnCmdID)

{

       //另外一种方法实现SDI的多视图切换

       CView* pViewAdd;

       CView* pViewRemove;

       CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();

       CDocument* pDoc = pMainFrame->GetActiveDocument();

      

       if((nCmdID == ID_VIEW_VIEW1) && (m_currentView == 1))

              return;

       if再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

SDI实现多视图并切换视图

标签:control   share   odi   features   ida   tin   using   public   hang   

原文地址:https://www.cnblogs.com/wicnwicnwh/p/10308241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!