Splitting Child Window in MFC MDI Program

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-11 12:20:32

问题


I am trying to split the Child Window of an MFC MDI progarm that I am working on but am having some problems. I know I have to use the CSplitterWnd class and have been trying to follow the instructions on the post here:

Create multi views in a CChildFrame using CSplitterWnd

but can't seem to get it to work, would anyone be able to offer me some advice with regard to these instructions, I have some specific questions:

  1. Is CRightView also a CView derived class and what code should go in there if any?

  2. Are m_pLeftView, m_pRightView, m_pPhongView and m_pPhongInfo all variables of the appropriate classes and do they have any particular type?

  3. Where does CTreeView come from, does not seem to be a standard base class?

  4. rc.Width in CChildFrame::OnCreateClient gives an undeclared identifier error, is there something I should be declaring somewhere here?

I would appreciate any advice about this, really struggling to get the splitter to work.

Thanks


回答1:


After working on it for a couple of days I've managed to solve my own problem, I'm adding the solution here for anyone else who might have the same problem.

  1. Declare the two view classes, in my case CElement View which is a CWnd derived class and CSampleViewer3dView which is a CView derived class.

  2. In CChildFrame add a variable with access private, type CSplitterWnd and name m_wndSplitter.

  3. Add an override for the OnCreateClient function in CChildFrame, this should add the code:

     virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
    

to ChildFrm.h, you should also add a boolean flag m_bInitSplitter to ChildFrm.h:

    BOOL m_bInitSplitter;

you also have to add:

m_bInitSplitter = false;

to the constructor of ChildFrm.cpp, the following code is added to ChildFrm.cpp when you add the variable using the wizard:

    BOOL CChildFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext)
    {
    }
  1. Put the following code into CChildFrame::OnCreateClient:

     BOOL CChildFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext)  
     {  
         CRect cr;  
         GetWindowRect( &cr );  
    
         if (!m_wndSplitter.CreateStatic(this, 1, 2))   
         {   
             MessageBox("Error setting up splitter frames!", "Init Error!", MB_OK | MB_ICONERROR);   
             return FALSE;   
         }  
    
         if (!m_wndSplitter.CreateView( 0, 0, RUNTIME_CLASS(CElementView), 
     CSize(cr.Width()/2, cr.Height()), pContext ) )   
         {   
             MessageBox("Error setting up splitter frames!", "Init Error!", MB_OK | MB_ICONERROR);  
             return FALSE;   
         }  
    
         if (!m_wndSplitter.CreateView( 0, 1, RUNTIME_CLASS(CSampleViewer3dView), 
     CSize(cr.Width()/2, cr.Height()), pContext))   
         {    
             MessageBox("Error setting up splitter frames!", "Init Error!", MB_OK | MB_ICONERROR);  
         return FALSE;   
         }  
         m_bInitSplitter = TRUE;  
    
         return TRUE;  
     }  
    
  2. Add an override for the WM_SIZE message to CChildFrame and insert the following code:

     void CChildFrame::OnSize(UINT nType, int cx, int cy)  
     {  
         CMDIChildWnd::OnSize(nType, cx, cy);  
         CRect cr;  
         GetWindowRect(&cr);  
    
         if (m_bInitSplitter && nType != SIZE_MINIMIZED)  
         {  
             m_wndSplitter.SetRowInfo( 0, cy, 0 );  
             m_wndSplitter.SetColumnInfo(0, cr.Width()*0.25 / 2, 50);  
             m_wndSplitter.SetColumnInfo(1, cr.Width()*0.75 / 2, 50);  
    
             m_wndSplitter.RecalcLayout();  
         }  
     } 
    

You can edit the size of each window by changing the values of 0.25 and 0.75 to the required percentage of the screen that you want each view to take up.

  1. Add header files for the two views to ChildFrm.cpp, e.g. ElementView.h and SampleViewer3dView.h.

You should then have two independent views in the child window of an MDI program.



来源:https://stackoverflow.com/questions/5471536/splitting-child-window-in-mfc-mdi-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!