Click here to Skip to main content
15,897,371 members
Articles / Desktop Programming / MFC

A Doc/View Dialog, supports Dynamic Split

Rate me:
Please Sign up or sign in to vote.
3.69/5 (13 votes)
21 Aug 20031 min read 83K   3.1K   30  
You can arbitrarily add DOC/View, and all the view can be resized.
// MyDlgDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyDlg.h"
#include "MyDlgDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlgDlg dialog

CMyDlgDlg::CMyDlgDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlgDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlgDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyDlgDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlgDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlgDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlgDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlgDlg message handlers

BOOL CMyDlgDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	CRect rc;
	GetDlgItem(IDC_STA_SPLIT)->GetWindowRect(rc);
	ScreenToClient(rc);
	
	m_pSplit.Create(WS_CHILD | WS_VISIBLE, rc, this, IDC_STA_SPLIT);
	m_pSplit.SetRange(300, 300, -1);
	
	GetDlgItem(IDC_STA_VIEW)->GetWindowRect(rc);
	ScreenToClient(rc);
	
	
	/***************************************/
	//In here add the root View and Document 
	m_pSplit.AddRootView(0,1,rc);	
    m_pSplit.ShowWindow(SW_HIDE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlgDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMyDlgDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMyDlgDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}




/************************************************************************/
/*			made by Lava_sdb   
/* Action:
/*		  Split View using add new view
/* In:
/*	  docNum   ---  selected doc type 	
/*    viewNum  ---  selected view type
/*	  type     ---  the type of split 
/*					1  VERTICAL
/*					2  HORIZONTAL
/*	  splitter ---  the Point that will add view                                                        
/************************************************************************/

void CMyDlgDlg::AddView(UINT docNum, UINT viewNum,UINT type,
					   CSplitterControl *splitter,
					   CView *pCurView)
{
	CRect rect;
	CView *pAddView=NULL;
	pCurView->GetWindowRect(rect);
	ScreenToClient(rect);
	
	
//create the new Split	
	CSplitterControl *pNewSplit=new CSplitterControl;
	CRect rc;
	if(type==1)   //Ver
	{
		rc.left=rect.left+rect.Width()/2;
		rc.top=rect.top/2;
		rc.right=rect.left+5;
		rc.bottom=rect.top+rect.Height();
	
		pNewSplit->m_rect0.left=rect.left;
		pNewSplit->m_rect0.right=rect.left+rect.Width()/2;
		pNewSplit->m_rect0.top=rect.top;
		pNewSplit->m_rect0.bottom=rect.bottom;

		pNewSplit->m_rect1.left=rect.left+rect.Width()/2;
		pNewSplit->m_rect1.right=rect.left+rect.Width();
		pNewSplit->m_rect1.top=rect.top;
		pNewSplit->m_rect1.bottom=rect.bottom;	
	}
	else if(type==2)  //h
	{
		rc.left=rect.left;
		rc.top=rect.top+rect.Height()/2;
		rc.right=rc.left+rect.Width();
		rc.bottom=rc.top+5;

		pNewSplit->m_rect0.left=rect.left;
		pNewSplit->m_rect0.right=rect.right;
		pNewSplit->m_rect0.top=rect.top;
		pNewSplit->m_rect0.bottom=rect.top+rect.Height()/2;
		
		pNewSplit->m_rect1.left=rect.left;
		pNewSplit->m_rect1.right=rect.right;
		pNewSplit->m_rect1.top=rect.top+rect.Height()/2;
		pNewSplit->m_rect1.bottom=rect.top+rect.Height();
  }
		
	pNewSplit->Create(WS_CHILD | WS_VISIBLE, rc, this, IDC_STA_SPLIT);
	pNewSplit->SetRange(300, 300, -1);
	
	//judge the type of view on his parent,(Left<top> or Right<bottom>)
	if(*pCurView==*(splitter->m_pLView))
	{
		splitter->m_pSplit0=pNewSplit;
	}
	else
	{
		splitter->m_pSplit1=pNewSplit;
	}


	CWnd* pFrameWnd = this;	
//decide the type of Doc
	switch(docNum) {
	case 0:
		m_pContext.m_pCurrentDoc = new CMyDoc2;
		break;
	case 1:
		m_pContext.m_pCurrentDoc = new CMyDocument;
		break;
	}
//decide the type of view
	switch(viewNum) {
	case 0:
		{
			m_pContext.m_pNewViewClass = RUNTIME_CLASS(CMyView);		
			CMyView  *pView=(CMyView  *)((CFrameWnd*)pFrameWnd)->CreateView(&m_pContext);
			pAddView=pView;
		}
		break;
	case 1:
		{
			m_pContext.m_pNewViewClass = RUNTIME_CLASS(CDisView);
			CDisView *pView =(CDisView *) ((CFrameWnd*)pFrameWnd)->CreateView(&m_pContext);	
			pAddView=pView;
		}
		break;
	}
//adjust views point
	static_cast<CDisplayView *>(pAddView)->m_Split=pNewSplit;
	static_cast<CDisplayView *>(pCurView)->m_Split=pNewSplit;
	pNewSplit->m_pLView=pCurView;
	pNewSplit->m_pRView=pAddView;
//adjust the new view's pos
	CRect recttemp;
    	
	if(type==1)        //Ver
	{			
		recttemp.left=rect.left+rect.Width()/2;
		recttemp.top=rect.top;
		recttemp.right=recttemp.left+rect.Width()/2;
		recttemp.bottom=recttemp.top+rect.Height();
		
		rect.right=rect.left+rect.Width()/2;
	}
	else if(type==2)  //H
	{
		recttemp.top=rect.top+rect.Height()/2;	
		recttemp.left=rect.left;
		recttemp.right=recttemp.left+rect.Width();
		recttemp.bottom=recttemp.top+rect.Height()/2;
		
		rect.bottom=rect.top+rect.Height()/2;
	}	
	pCurView->MoveWindow(rect);	
	pAddView->MoveWindow(recttemp);
}

/************************************************************************/
/*			made by Lava_sdb   
/* Action:
/*		  Create the Split Root View
/* In:
/*	  docNum   ---  selected doc type 	
/*    viewNum  ---  selected view type
/*	  rect     ---  view's display region                                                           
/************************************************************************/

CDisplayView *CMyDlgDlg::AddRootView(UINT docNum, UINT viewNum, CRect rect,
						   CSplitterControl *splitter)
{
	ASSERT(splitter!=NULL);
	CWnd* pFrameWnd = this;	
	//Decide the type of doc
	switch(docNum) {
	case 0:
		{
			m_pContext.m_pCurrentDoc = new CMyDoc2;
		}
		break;
	case 1:
		m_pContext.m_pCurrentDoc = new CMyDocument;
		break;
	}
	//decide the type of view
	switch(viewNum) {
	case 0:
		{
			m_pContext.m_pNewViewClass = RUNTIME_CLASS(CMyView);		
			CMyView  *pView=(CMyView  *)((CFrameWnd*)pFrameWnd)->CreateView(&m_pContext);
			pView->m_Split=splitter;
			pView->MoveWindow(rect);
			return pView;
		}
		break;
	case 1:
		{
			m_pContext.m_pNewViewClass = RUNTIME_CLASS(CDisView);
			CDisView *pView =(CDisView *) ((CFrameWnd*)pFrameWnd)->CreateView(&m_pContext);		
			pView->m_Split=splitter;
			pView->MoveWindow(rect);
			return pView;
		}
		break;
	}
return NULL;
}




/************************************************************************/
/*   Name:
/*			UpdateRect()
/*	 Action:
/*       iterative compute the Rect AND redraw the rect 
/*   In:  
/*       pSplitter  --- the first split that will iterative                                                        
/************************************************************************/

static int DEBUG_COUNT=0;   //Debug
void CMyDlgDlg::UpdateRect(CSplitterControl *pSplitter)
{

if(DEBUG_COUNT==0)
{
//iterator the Left(Top) Tree
	if(pSplitter->m_pSplit0!=NULL)
	{
		CRect rect;
		pSplitter->m_pSplit0->GetWindowRect(rect);
		ScreenToClient(rect);
	
		//change the position of split
		if (pSplitter->m_pSplit0->GetStyle()==1)  //Ver Split
		{
			rect.left=pSplitter->m_rect0.left+pSplitter->m_rect0.Width()/2;		
			rect.top=pSplitter->m_rect0.top;
			rect.right=rect.left+5;	
			rect.bottom=pSplitter->m_rect0.bottom;
		
		}
		else
		{
			rect.top=pSplitter->m_rect0.top+pSplitter->m_rect0.Height()/2;
			rect.left=pSplitter->m_rect0.left;		
			rect.right=pSplitter->m_rect0.right;	
			rect.bottom=rect.top+5;
		}
		
		pSplitter->m_pSplit0->MoveWindow(rect);

		//Resizzing the m_rect of the split
		if (pSplitter->m_pSplit0->GetStyle()==2)  //Hor Split
		{
			pSplitter->m_pSplit0->m_rect0.left=pSplitter->m_rect0.left;
			pSplitter->m_pSplit0->m_rect0.right=pSplitter->m_rect0.right;
			pSplitter->m_pSplit0->m_rect0.top=pSplitter->m_rect0.top;			
			pSplitter->m_pSplit0->m_rect0.bottom=pSplitter->m_rect0.top+pSplitter->m_rect0.Height()/2;

			pSplitter->m_pSplit0->m_rect1.left=pSplitter->m_rect0.left;
			pSplitter->m_pSplit0->m_rect1.right=pSplitter->m_rect0.right;
			pSplitter->m_pSplit0->m_rect1.top=pSplitter->m_rect0.top+pSplitter->m_rect0.Height()/2;
			pSplitter->m_pSplit0->m_rect1.bottom=pSplitter->m_rect0.top+pSplitter->m_rect0.Height();
		}
		else
		{
			pSplitter->m_pSplit0->m_rect0.left=pSplitter->m_rect0.left;
			pSplitter->m_pSplit0->m_rect0.right=pSplitter->m_rect0.left+pSplitter->m_rect0.Width()/2;
			pSplitter->m_pSplit0->m_rect0.top=pSplitter->m_rect0.top;			
			pSplitter->m_pSplit0->m_rect0.bottom=pSplitter->m_rect0.bottom;
			
			pSplitter->m_pSplit0->m_rect1.left=pSplitter->m_rect0.left+pSplitter->m_rect0.Width()/2;
			pSplitter->m_pSplit0->m_rect1.right=pSplitter->m_rect0.left+pSplitter->m_rect0.Width();
			pSplitter->m_pSplit0->m_rect1.top=pSplitter->m_rect0.top;
			pSplitter->m_pSplit0->m_rect1.bottom=pSplitter->m_rect0.bottom;
		}
		
		UpdateRect(pSplitter->m_pSplit0);
	}
	if(pSplitter->m_pSplit0==NULL)
	{
	pSplitter->m_pLView->MoveWindow(pSplitter->m_rect0);
	pSplitter->m_pRView->MoveWindow(pSplitter->m_rect1);
	}
	//DEBUG_COUNT=1;

	
//iterator the Right(Bottom) Tree
	if(pSplitter->m_pSplit1!=NULL)
	{
		CRect rect;
		pSplitter->m_pSplit1->GetWindowRect(rect);
		ScreenToClient(rect);
		
		//change the position of split
		if (pSplitter->m_pSplit1->GetStyle()==1)  ///Ver Split
		{
			rect.left=pSplitter->m_rect1.left+pSplitter->m_rect1.Width()/2;		
			rect.top=pSplitter->m_rect1.top;
			rect.right=rect.left+5;	
			rect.bottom=pSplitter->m_rect1.bottom;
			
		}
		else
		{
			rect.top=pSplitter->m_rect1.top+pSplitter->m_rect1.Height()/2;
			rect.left=pSplitter->m_rect1.left;		
			rect.right=pSplitter->m_rect1.right;	
			rect.bottom=rect.top+5;
		}
		
		pSplitter->m_pSplit1->MoveWindow(rect);
		
		//Resizzing the m_rect of the split
		if (pSplitter->m_pSplit1->GetStyle()==2)  //Hor Split
		{
			pSplitter->m_pSplit1->m_rect0.left=pSplitter->m_rect1.left;
			pSplitter->m_pSplit1->m_rect0.right=pSplitter->m_rect1.right;
			pSplitter->m_pSplit1->m_rect0.top=pSplitter->m_rect1.top;			
			pSplitter->m_pSplit1->m_rect0.bottom=pSplitter->m_rect1.top+pSplitter->m_rect1.Height()/2;
			
			pSplitter->m_pSplit1->m_rect1.left=pSplitter->m_rect1.left;
			pSplitter->m_pSplit1->m_rect1.right=pSplitter->m_rect1.right;
			pSplitter->m_pSplit1->m_rect1.top=pSplitter->m_rect1.top+pSplitter->m_rect1.Height()/2;
			pSplitter->m_pSplit1->m_rect1.bottom=pSplitter->m_rect1.top+pSplitter->m_rect1.Height();
		}
		else
		{
			pSplitter->m_pSplit1->m_rect0.left=pSplitter->m_rect1.left;
			pSplitter->m_pSplit1->m_rect0.right=pSplitter->m_rect1.left+pSplitter->m_rect1.Width()/2;
			pSplitter->m_pSplit1->m_rect0.top=pSplitter->m_rect1.top;			
			pSplitter->m_pSplit1->m_rect0.bottom=pSplitter->m_rect1.bottom;
			
			pSplitter->m_pSplit1->m_rect1.left=pSplitter->m_rect1.left+pSplitter->m_rect1.Width()/2;
			pSplitter->m_pSplit1->m_rect1.right=pSplitter->m_rect1.left+pSplitter->m_rect1.Width();
			pSplitter->m_pSplit1->m_rect1.top=pSplitter->m_rect1.top;
			pSplitter->m_pSplit0->m_rect1.bottom=pSplitter->m_rect0.bottom;
		}
		
		UpdateRect(pSplitter->m_pSplit1);
	}
	if(pSplitter->m_pSplit1==NULL)
	{
		pSplitter->m_pRView->MoveWindow(pSplitter->m_rect1);
	}
}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Systems Engineer huawei
China China
I do I want to do,and you?

Comments and Discussions