In a single document Program Multiple forms are dynamically switched in: yueshang reading:
885 Secondary Keyword: Font: [large, medium, small]
Create a cformview-based Single-document application, add a form and the corresponding cformview-based New View class, and add control in the main framework class Code And menu control to achieve the dynamic switching of these two forms, the following is the specific implementation process: (1) Use "MFC Appwizard (exe)" to create a new project "switchform ", in step 2, select single document mode as the creation type. in step 3, step 4, step 5, and step 6, the single document mode is 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 switchtoform (): 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 function and update function 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 yes, 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 a reference to the two view classes at the beginning of the file: # include "switchformdoc. H "# include" switchformview. H "# include" nextformview. H "NOTE: the reference to the document class should be added before the reference of the two depending classes; otherwise, compilation errors may occur. In addition, because the class constructor does save the Declaration as a protected type during the Declaration, it cannot be referenced in the framework class, so we need to change the class declaration of the two classes as follows: class cnextformview: Public cformview { Declare_dyncreate (cnextformview) // Protected: Change protected to publicpublic: Cnextformview (); Virtual ~ Cnextformview (); ...... }; Class cswitchformview: Public cformview {// protected: Change protected to publicpublic: Cswitchformview (); Declare_dyncreate (cswitchformview) ...... };