VC multi-document user interface design and switching between documents

Source: Internet
Author: User

Using Delphi, VB, and Windows form (Visual C #) is called rad (Rapid Application Development) development tools for project development will meet the needs of these tools and quick interface development provided by the platform: You can easily implement a management interface of the MIS system and provide a main framework, click a menu item on the main framework to open a window for processing transactions. These windows can overlap and be maximized/minimized. Everything looks like that. However, when you develop in VC, you find that the whole world has changed. Although VC provides a framework that supports multiple document views using MDI, but every time you click the "open"/"new" menu item, you will find that the new window is in the same style and cannot meet the development requirements of the project. This requires you to provide different display/Operation interfaces for different business logic. Here is a template for your reference:

Step 1: Use VC 6.0 to create a project named "mis. In addition to selecting single-document attributes, the "default" method is used for all items. Therefore, you can obtain five classes:Cmainframe,Cmisapp,Cmisdoc,Cmisview, AndCaboutdlg;

Step 2: Create a new set of DOC/View/frame: add the new class cnewdoc, the base class is cdocument (method: insert --> New Class (class type: MFC class ), select cdocument for the base class ). Add a new framework class cnewchildframe. The base class is javasichildwnd. The method for adding a framework class is the same as that for adding a framework class. Add a new view class cnewview. The method is insert --> new form. In the dialog box, select cnewdoc as the document class (which is also used by default ).

Step 3: Add the menu item "function" to the menu resource idr_mainframe and add the menu item "function 1" and "function 2", which correspond to different business logics in project development. To ensure the consistency of menu items throughout the process, CTRL + C on idr_mainframe, and then Ctrl + V are two menus that are the same as idr_mainframe, then, change the two menus to idr_mistype and idr_view2_tmpl respectively. (Note: copy the names of the last two menus before deleting them, and then rename them. The menu names can be changed here, however, you can simply use the default menu name generated by the system. If you change the name, you must modify the corresponding item at the time of new cmultidoctemplate ).

Step 4: InCmisappAdd variable records in the two framework classes, declared as public for the sake of simplicity (to facilitate subsequent access, it is too lazy to manage the object-oriented design principles, because it is just an example ):

 
Public: Cmultidoctemplate*M_pdoctemplate1; cmultidoctemplate* M_pdoctemplate2;

And setBool cmisapp: initinstance ()FunctionVCAutomatically GeneratedCodeMake the following changes:

 {M_pdoctemplate2 =New  Cmultidoctemplate (idr_view2_tmpl,  //  Here is the menu item Runtime_class (cnewdoc ), //  Document class Runtime_class (cmdichildwnd ), //  Frame class Runtime_class (cnewview )); //  View class  Adddoctemplate (m_pdoctemplate2); m_pdoctemplate1 = New Cmultidoctemplate (idr_mistype, runtime_class (cmisdoc), runtime_class (cchildframe ),  //  Custom MDI child frame  Runtime_class (cmisview); adddoctemplate (m_pdoctemplate1 );} 

Step 5: InMainfrm. hAdd two file class variable records here two differentDocumentFor the sake of simplicity, it is declaredPublic:

 
Public: Cmisdoc*M_pdoc1; cnewdoc* M_pdoc2

Note: InCmainframeConstructorSet the above two variablesNull(Otherwise :)).And respondIdr_mainframeTwo menu items1And functions2"Message, add the Response Function in the respective response menu:

  If (M_pdoc1! = NULL) //  Activated if enabled  {Position Pos; POS = M_pdoc1-> Getfirstviewposition (); cview * Pview = m_pdoc1-> Getnextview (POS); pview -> Getparentframe ()->Activateframe ();}  Else  {Cmisapp * PAPP = (cmisapp * ) Afxgetapp (); m_pdoc1 = (Cmisdoc *) (PAPP-> m_pdoctemplate1-> Opendocumentfile (null);} and:  If (M_pdoc2! = Null) {position Pos; POS = M_pdoc2-> Getfirstviewposition (); cview * Pview = m_pdoc2-> Getnextview (POS); pview -> Getparentframe ()->Activateframe ();}  Else  {Cmisapp * PAPP = (cmisapp * ) Afxgetapp (); m_pdoc2 = (Cnewdoc *) (PAPP-> m_pdoctemplate2-> Opendocumentfile (null ));} 

Now the entire work has been completed. It should be noted that:

1) Because related classes are used in the implementation, You need to include relevant header files as necessary. This is omitted here (when compilation is wrong, add it :));

2) Here is a sample code. In actual development, you can obtain the specific application you want to implement through reference implementation (for example, different view classes and different quantities, more importantly, different implementations of business logic );

But so far,ProgramA bug occurs. When function 1 is clicked, It is disabled and then turned on. A memory error occurs. This is because m_pdoc1 is not set to null after function 1 window is closed. The solution is to add the ondestroy message response to the view class:

VoidCmisview: ondestroy () {cview: ondestroy ();//Todo: add your message handler code here(Cmainframe *) afxgetmainwnd ()-> m_pdoc1 =NULL ;}

 

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.