Window switch segmentation

Source: Internet
Author: User

Here we will write down the window switch to split. This section describes the separation and switching between single-document interfaces and multi-document interfaces. There is no difference between the multi-document approach and single-document approach. Here we take single-document as an example. At the end of this article, I will list an example of the split dialog box. This part of the content is not very small. I will not detail what I found in the book.

The commonly used MFC window structure is document/view architecture ). Many people say that this structure wastes a lot of resources and is not enough to save money. However, I think there is no big problem in wasting resources at the interface level. As long as the memory is not leaked, the efficiency is enough. What's more, this is Microsoft's Most Admired standard interface.
The document/view architecture structure consists of four classes. Document, view, framework, and app. The app class is the engine of the program, and is the least concerned class in MFC. Framwork is a window framework. When the program starts running, it becomes a framework and then a document class. It is used to store data. The view class is used to display data for data exchange at the same time. The single document interface has only one document class, but there can be more view classes. At least one view class is active. You can use GetActiveView () to get its pointer. No view class associated with document class has a control ID, which is an integer. If only one view class is displayed in total, the control ID of this class is AFX_IDW_PANE_FIRST. If several view classes are displayed at the same time, you need to split them by splitter. The class name is CSplitterWnd. CSplitterWnd has two different cutting frameworks. It is a kind of dynamic and implemented using Create (), which is not ideal. I have never seen many classes use this method. Static cutting is widely used and implemented using CReateStatic. Of course, we can see from the name that the disadvantage of static cutting is that it cannot be dynamically re-split. In this article, I will introduce a program that can achieve static cutting. The Control ID of the window separated by the splitter can be obtained through the IdFromRowCol (row, col) function. row and col are the number of rows and columns of the window. The value is also AFX_IDW_PANE_FIRST. It is also a relatively large number. Therefore, when hiding a view that you do not want to display, change its control ID to a small number such as 1, 2, and 3.

This is not detailed enough. You can refer to the Visual C ++ tutorials for details. The following describes some specific problems. Start from a single window.
1. Display a View in the Framework. Switch to different views through menus or buttons. Suppose there are three types of views: CViewA, CViewB, and CViewC. Three constants are used to indicate the control ID when they are not displayed.

Enum eView {ViewA, ViewB, ViewC };
Add the following function to the CMainFrame to implement switching between different windows. It is easy to understand. The only thing that is not mentioned is CCreateContext context, which must be set every time you Create a view. In fact, the pointer m_pCurrentDoc pointing to the current document class needs to be set. You can use the default values for others.

Void CMainFrame: SwitchToView (eView nView)
{
CView * pOldActiveView = GetActiveView ();
CView * pNewActiveView = (CView *) GetDlgItem (nView );
If (pNewActiveView = NULL)
{
Switch (nView)
{
Case ViewA:
PNewActiveView = (CView *) new CViewA;
Break;
Case viewb:
Pnewactiveview = (cview *) New cviewb;
Break;
Case viewc:
Pnewactiveview = (cview *) New cviewc;
Break;
}
Ccreatecontext context;
Context. m_pcurrentdoc = poldactiveview-> getdocument ();
Pnewactiveview-> Create (null, null, ws_border | ws_child,
Cframewnd: rectdefault, this, nview, & context );
Pnewactiveview-> oninitialupdate ();
}

Setactiveview (pnewactiveview );
Pnewactiveview-> showwindow (sw_show );
Poldactiveview-> showwindow (sw_hide );
Poldactiveview-> setdlgctrlid (m_ncurrentview );
Pnewactiveview-> setdlgctrlid (afx_idw_pane_first );
M_ncurrentview = nview;
Recalclayout ();
}

2. One, two, or four windows are displayed.
Splitter class is required. I will not detail it here. It is available in any Visual C ++ book. Either Dynamic or Static.

3. One, two, or four windows are displayed. You can also switch the window.
Here, we only talk about switching static windows. The dynamic results are not very good and will be automatically switched if you don't want to switch them.
The effect of switching a static Window is Window Explorer. the right side of the Directory column on the left is changed. Here we need to write a small enhancement based on the existing CSplitterWnd. A switchover function is required. Inherit a class from CSplitterWnd, for example, CDynViewSplitter.

BOOL CDynViewSplitter: ReplaceView (int row, int col, CRuntimeClass * pViewClass, SIZE size)
{
CCreateContext context;
BOOL bSetActive;


// Get pointer to CDocument object so that it can be used in the creation
// Process of the new view
CDocument * pDoc = (CView *) GetPane (row, col)-> GetDocument ();
CView * pActiveView = GetParentFrame ()-> GetActiveView ();
If (pactiveview = NULL | pactiveview = getpane (row, col ))
Bsetactive = true;
Else
Bsetactive = false;

// Set flag so that document will not be deleted when view is destroyed
Pdoc-> m_bautodelete = false;
// Delete existing View
(Cview *) getpane (row, col)-> destroywindow ();
// Set flag back to default
Pdoc-> m_bautodelete = true;

// Create new view

Context. m_pNewViewClass = pViewClass;
Context. m_pCurrentDoc = pDoc;
Context. m_pNewDocTemplate = NULL;
Context. m_pLastView = NULL;
Context. m_pCurrentFrame = NULL;

CreateView (row, col, pViewClass, size, & context );

CView * pNewView = (CView *) GetPane (row, col );

If (bSetActive = TRUE)
GetParentFrame ()-> SetActiveView (pNewView );

RecalcLayout ();
GetPane (row, col)-> SendMessage (WM_PAINT );

Return TRUE;
}
Here, destroy is used up, and the processing is not the same as the first one. Nothing else is worth mentioning.

4. This is a problem that I have never thought about before. The static window is split again and the time is merged. With the above two examples, you can combine them. You need to know that CSplitterWnd cannot be split into one row or one column at the beginning of the CreateStatic split window. CreateStatic must be used for real cutting. This brings a lot of trouble to the entire problem. Fortunately, all member programs of CSplitterWnd can be read, with only over two thousand lines. There are a lot of things to do after taking a look at construct, but desctructor is very simple, so you can delete your CSplitterWnd before merging. The following example shows how to switch between CViewA, CViewB in a single window, and CViewMenu/CViewA in two windows. In the window, you can switch between CViewA and CViewB in the right window.

5. The division of multiple windows, not only 1X2 2. It can be divided into very complex, more than the windows on vc ide. Multiple Splitter is required.
6. There is no standard MFC class for dialog box splitting. You need to write one by yourself.
Examples of 5 and 6 will be added later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.