Dynamically switch multiple forms in a single document Program
Abstract: This article uses a program example to describe how to dynamically control the switching of multiple forms through menus in a single document program under VC ++ 6.0.
I. Introduction
In programming, we will choose multi-document, single-document or dialog mode in the program style based on different needs. For Single-document mode, we may use a lot, however, sometimes we want to display multiple forms in the form of a single document. For example, as a database front-end application, this type of problem occurs. The database is composed of a large number of forms, in a common form, only one form is displayed. To display other forms, you must use the form switching technology. The following describes how to implement this technology through a program.
II. Implementation Technology
Create a CFormView-based Single-document application, and add a form and the corresponding CFormView-based new video class, then, you can add the control code and Menu Control in the main framework class to implement dynamic switching between the two forms. The following describes the specific implementation process:
(1) Use "MFC AppWizard (exe)" to create a new project "SwitchForm", and select Single document mode as the creation type in step 2, steps 3, 4, 5, and 6 are saved. In the last step, select "CFormView" as the base class of the video class. Click "finish" to generate the initial project "SwitchForm ".
(2) Click "Insert" and "Resource... ", In the displayed" Insert Resource "Dialog box," IDD_FORMVIEW "in the" Dialog "tree, click" New "to generate a New form, change its ID to "IDD_NEXTFORM ". Add a static box "This is the first form" to the original form; add a static box "this is the second Form" to the new form ".
(3) Add a level-1 menu "form switching" on "idr_mainframe" of the menu resource, and its level-2 menus "first form" and "second form ", the ID numbers are "id_firstform" and "id_secondform ". The attribute of the "first form" is "checked", indicating that the first form is displayed at the beginning of the program.
(4) Right-click "switchform classes" on the "classview" property page and select "New Class... ", The" New Class "dialog box is displayed. Select" dialog ID: "as the new form we just added" idd_nextform ", and select" base class: "as" cformview ", the class name is "cnextformview", so that the view class corresponding to the second form is added to the project.
(5) Add the function switchtoform () to the framework class ():
Void cmainframe: switchtoform (INT nform)
{
// Obtain the view handle of the original active form
Cview * poldactiveview = getactiveview ();
// Obtain the view handle corresponding to the form identified by "nform"
Cview * pnewactiveview = (cview *) getdlgitem (nform );
// If the view handle is empty, a new view is created.
If (pnewactiveview = NULL ){
If (nform = idd_switchform_form)
Pnewactiveview = (cview *) New cswitchformview;
If (nform = idd_nextform)
Pnewactiveview = (cview *) New cnextformview;
Ccreatecontext context;
Context. m_pcurrentdoc = poldactiveview-> getdocument ();
Pnewactiveview-> Create (null,
Null,
0l,
Cframewnd: rectdefault,
This,
Nform,
& Context );
PNewActiveView-> OnInitialUpdate ();
}
// Select pNewActiveView as the activity form
SetActiveView (pNewActiveView );
// Display the activity form to hide the activity form
PNewActiveView-> ShowWindow (SW_SHOW );
POldActiveView-> ShowWindow (SW_HIDE );
Int ID;
If (pOldActiveView-> GetRuntimeClass () = RUNTIME_CLASS (CSwitchFormView ))
ID = IDD_SWITCHFORM_FORM;
If (pOldActiveView-> GetRuntimeClass () = RUNTIME_CLASS (CNextFormView ))
ID = IDD_NEXTFORM;
// Set the ID of the form
POldActiveView-> SetDlgCtrlID (ID );
PNewActiveView-> SetDlgCtrlID (AFX_IDW_PANE_FIRST );
RecalcLayout ();
}
(6) Add the command response functions and update functions corresponding to the two menus as follows:
Void CMainFrame: OnFirstform ()
{
// Use the IsKindOf function to determine whether the current active window is the first window. If yes, no switchover is required,
// Otherwise, the current active window will be switched to "IDD_SWITCHFORM_FORM" through the SwitchToForm Function"
// The second form of the identifier.
If (GetActiveView ()-> IsKindOf (RUNTIME_CLASS (CSwitchFormView )))
Return;
SwitchToForm (IDD_SWITCHFORM_FORM );
}
Void CMainFrame: OnUpdateFirstform (CCmdUI * pCmdUI)
{
// Use the IsKindOf function to determine whether the current active window is the first form. If so, select it.
PCmdUI-> SetCheck (GetActiveView ()-> IsKindOf (RUNTIME_CLASS (CSwitchFormView )));
}
Void CMainFrame: OnSecondform ()
{
If (GetActiveView ()-> IsKindOf (RUNTIME_CLASS (CNextFormView )))
Return;
SwitchToForm (IDD_NEXTFORM );
}
Void CMainFrame: OnUpdateSecondform (CCmdUI * pCmdUI)
{
PCmdUI-> SetCheck (GetActiveView ()-> IsKindOf (RUNTIME_CLASS (CNextFormView )));
}
Then add reference to two view classes at the beginning of the file:
# Include "SwitchFormDoc. h"
# Include "SwitchFormView. h"
# Include "NextFormView. h"
Note that reference to the document class should be added before the reference of the two depending classes; otherwise, compilation errors may occur. In addition
The constructor of the class is declared as protected and cannot be referenced in the framework class.
The Declaration changes are as follows:
Class CNextFormView: public CFormView
{
DECLARE_DYNCREATE (CNextFormView)
// Protected: Change protected to public
Public:
CNextFormView ();
Virtual ~ CNextFormView ();
......
};
Class CSwitchFormView: public CFormView
{
// Protected: Change protected to public
Public:
CSwitchFormView ();
DECLARE_DYNCREATE (CSwitchFormView)
......
};
Iii. Compile and run
Compile and run the program. The "This is the first form" on the form at the beginning, and only the "first form" on the menu is selected, the current active form is the first form. Click "second form" in the menu, and the words on the form in the view are changed to "this is the second form ", at the same time, the selected menu is changed from "First Form" to "second form", enabling dynamic switching of the form through the menu.
Summary: The key in this program is the SwitchToView function. In this function, the program searches all the display windows of the current document to find the View class that matches the CruntimeClass variable. If found, the window is activated. Similar methods can also be used to implement single-file (document) multi-view (View) in Multi-document mode ), different views display data from the same document in different ways to better meet the needs of the program.
From: http://topic.csdn.net/t/20050216/17/3785968.html