How to split windows in MFC multi document

This paper records how to segment windows in MFC multi files and realize data transfer between windows

1. Define a partition in ChildFrame and embed a CSplitterWnd member variable.

CSplitterWnd m_splitterWnd;

2. Create two dialog boxes and set the properties of the dialog box: set 'style' to 'lower level'; The use of title blocks is prohibited; Select 'resize' for the border; Other formats are not set temporarily;

 

 

 

 

 

  Add classes for the dialog box, with class names CLeftView and CRightView; Select CFormView as the parent class;

We want to input two addends in the dialog box on the left, and click the button to pass the two addends to the text. The text calculates the sum of the two addends and passes it to the dialog box on the right, and outputs the results in the edit box on the right. Then click the button in the dialog box on the right to transfer the value to the dialog box on the left and display the transferred data in the edit box on the left. Therefore, add CString type value variables for each edit box in the figure and set each control ID. The ID of the edit box on the right is IDC_EDIT_RESULT, the IDs of the left edit box from top to bottom are IDC respectively_ EDIT_ NUM1,IDC_EDIT_NUM2,IDC_EDIT_SUM.

Add the following variables in CLeftView

public:
	// First addend
	CString num1;
	// Second addend
	CString num2;
	// Sum of two addends
	CString sum;

Add the following variables in CRightView

public:
	// The result of two addends
	CString result;

Due to the interaction through text, the following variables need to be added to the text CSplitDoc

public:
	CString m_num1;
	CString m_num2;
	CString m_sum;
	CString m_result;

3. Next, start to split the window. Because it is a multi document structure, split it in the sub frame and rewrite the OnCreateClient function in the ChildFrame.

BOOL CChildFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
	// TODO: add special code and / or call base class here
	CRect rect;
	GetClientRect(&rect);
	m_splitterWnd.CreateStatic(this, 1, 2);//One row and two columns
	m_splitterWnd.CreateView(0, 0, RUNTIME_CLASS(CLeftView), CSize(rect.Width() / 4, rect.Height()), pContext);//The first dialog box is placed in the first row and the first column
	m_splitterWnd.CreateView(0, 1, RUNTIME_CLASS(CRightView), CSize(rect.Width() - rect.Width() / 5, rect.Height()), pContext);//The second dialog box is placed in the first row and the second column
	return TRUE;

	//return CMDIChildWnd::OnCreateClient(lpcs, pContext);
}

At this time, you should get the results as shown in the figure:

4. Next, consider the problem of data interaction

We hope that by clicking Button1 in the left dialog box, we can pass the values of addend 1 and addend 2 to the text, and then add an event handler to Button1. A pointer to a document class is defined in the handler, and the value is passed to the document through the pointer.

void CLeftView::OnBnClickedButton1()
{
	// TODO: add control notification handler code here
	UpdateData();
	CSplitDoc* pDoc = (CSplitDoc*)GetDocument();
	pDoc->m_num1 = num1;
	pDoc->m_num2 = num2;
	pDoc->UpdateAllViews(NULL);
}

  For data reception, to receive in the dialog box on the right, override the ondraw function in CRightView

void CRightView::OnDraw(CDC* pDC)
{
	// TODO: add special code and / or call base class here
	CSplitDoc* pDoc = (CSplitDoc*)GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_sum = pDoc->m_num1 + pDoc->m_num2;
	//pDC->TextOutW(500, 200, pDoc->m_sum);
	SetDlgItemText(IDC_EDIT_RESULT, pDoc->m_sum);
}

  Similarly, it is the same to transfer data from the right dialog box to the left dialog box. The specific code is as follows:

void CRightView::OnBnClickedButton1()
{
	// TODO: add control notification handler code here
	UpdateData();
	CSplitDoc* pDoc = (CSplitDoc*)GetDocument();
	pDoc->m_result = result;
	pDoc->UpdateAllViews(NULL);
}
void CLeftView::OnDraw(CDC* /*pDC*/)
{
	// TODO: add special code and / or call base class here
	CSplitDoc* pDoc = (CSplitDoc*)GetDocument();
	ASSERT_VALID(pDoc);
	SetDlgItemText(IDC_EDIT_SUM, pDoc->m_result);
}

So far, the data interaction between the two split views has been basically completed. Here, I don't think the received code must rewrite the ondraw function, but it's still in the trial stage. First receive it in ondraw. Here are the results of the program:

 

 

 

Tags: C++ MFC microsoft

Posted on Thu, 21 Oct 2021 02:55:58 -0400 by canobi