Multi-Document Template application development in the VC ++ environment (with source code)

Source: Internet
Author: User

We have previously seen that the MDI application automatically generated by APP Wizard in the VC ++ environment only supports opening multiple files of the same document type, however, you cannot open different types of documents at the same time. This kind of public information on the internet is relatively small. I have consulted relevant information on Zhizhi. I will summarize the following information (one by one ).

The following is an application that can process two types of documents: TXT and Bub.

The first type of document TXT is automatically generated using the APP Wizard, except that the document type is set to the TXT type when set, and the generated view derived class inherits from the CEditView class.

The second document type Bub, which is the custom document type added to the generated MDI Program (which will be described below ).

The application creation process (the corresponding source code will be uploaded for download). Note that the following describes VC6.0 as a development tool:

1. first, use APP Wizard to create an MDI program. Note that you must set the file extension to txt in the Advanced Options dialog box in step 1; in step 2, set the base class of the View class to CEditView. after compilation, the application can edit the text file.

2. The following uses the Insert/New Class menu option to add three MFC Derived classes to the project: CBubDoc, CBubView, and CBubFrame, which are derived from CDocument, CView, and javasichildwnd.

Add two data members to the CBubDoc class:

CRect m_rectBubble [MAX_BUBLLE];
Int m_nBubbleCount;

MAX_BUBLLE is defined as 200;

Use Class Wizard to add the DeleteContents () function for the CBubDoc Class and add code for it:

M_nBubbleCount = 0;

Modify the Serialize () function of the CBubDoc class so that it can access the bubble image data.

If (ar. IsStoring ())
{
Ar < For (int I = 0; I {
Ar < }
}
Else
{
Ar> m_nBubbleCount;
For (int I = 0; I {
Ar> m_rectBubble [I];
}
}

Add # include "BubDoc. h" in the header file of the CBubView class and add a member function for the CBubView class.

Public:

CBubDoc * GetDocument ();

Add the following code at the end of the class declaration:

# Ifndef _ DEBUG
Inline CBubDoc * CBubView: GetDocument ()
{
Return (CBubDoc *) m_pDocument;
}

# Endif

Add the following code to the class implementation file:

CBubDoc * CBubView: GetDocument ()
{
ASSERT (m_pDocument-> IsKindOf (RUNTIME_CLASS (CBubDoc )));
Return (CBubDoc *) m_pDocument;
}

Modify the OnDraw function of the CBubView class to display the bubble chart.

The Code is as follows:

Void CBubView: OnDraw (CDC * pDC)
{
CBubDoc * pDoc = GetDocument ();
PDC-> SelectStockObject (LTGRAY_BRUSH );
For (int I = 0; I M_nBubbleCount; I ++)
{
PDC-> Ellipse (pDoc-> m_rectBubble [I]);
}
}

Use Class Wizard to add the message processing function with the left mouse button pressed for the CBubView Class, and edit the Code as follows:

Void CBubView: OnLButtonDown (UINT nFlags, CPoint point)
{
CBubDoc * pDoc = GetDocument ();
ASSERT_VALID (pDoc );
If (pDoc-> m_nBubbleCount {
Int r = rand () % 50 + 10;
PDoc-> m_rectBubble [pDoc-> m_nBubbleCount] =
CRect (point. x-r, point. y-r, point. x + r, point. y + r );
InvalidateRect (pDoc-> m_rectBubble [pDoc-> m_nBubbleCount]);
PDoc-> m_nBubbleCount ++;
}
CView: OnLButtonDown (nFlags, point );
}

3. Define the Resource Identifier IDR_BUBTYPE for the new document type. This identifier is used for all resources of the new document type, including string resources, sub-Framework Window menus, and icons.

4. Add a new string resource (ID: IDR_BUBTYPE) for the new document type to the string resource)

As follows:

Bub program interface \ nBub file (*. bub) \ n. bub \ nBub. Document \ nBub Document

The corresponding explanations are as follows:

\ N is the separator:

The following characters are described in sequence. You can view them one by one:
Bub Program Interface: application window title (only valid for the single-Document Interface)
\ NBub: Document Type
\ NBub: Document Name (displayed in the Select File Dialog Box)
\ NBub file (*. bub): file name filter (displayed in the Select File Dialog Box)
\ N. bub: File Extension
\ NBub. Document: The Document type identifier stored in the system registry.
\ NBub Document: Document Type stored in the system registry

5. Edit the menu resources of the main framework window.

Add the new and open options for new types of documents in the file sub-menu. The identifiers of these two menu options are set to ID_FILE_NEW_BUB and ID_FILE_OPEN_BUB, And the identifiers of the original new menu options are changed to ID_FILE_NEW_TXT.

6. Edit the toolbar resource of the main framework window. You can set it by yourself, but the identifier is the same.

7. Use Insert/Resource to add menu resources (ID: IDR_BUBTYPE) for the new sub-Framework Window of the document type ).

The options in the file sub-menu include creating a graphic document (ID_FILE_NEW_BUB), opening a graphic document (ID_FILE_OPEN_BUB) and creating a text document (ID_FILE_NEW_TXT), opening a text document (ID_FILE_OPEN), closing the document, save the document, save it as, and exit.

Other unlisted identifiers are set according to the menu of the main framework, and the remaining menu items are set according to the menu of the main framework.

8. Add the icon Resource (IDR_BUBTYPE) for the newly created document type)

9. Find the corresponding InitInstance () function in the application derived class and add the following code to set a new document template;

CMultiDocTemplate * pBubTemplate;
PBubTemplate = new CMultiDocTemplate (
IDR_BUBTYPE,
RUNTIME_CLASS (CBubDoc ),
RUNTIME_CLASS (CBubFrame ),
RUNTIME_CLASS (CBubView ));
AddDocTemplate (pBubTemplate );

Add the following code before calling ProcessShellCommand (cmdInfo:

// Do not create any types of documents when an application is created.

Using info. m_nShellCommand = CCommandLineInfo. FileNothing;

Add the following code to the header of the derived class of the application:

# Include "BubDoc. h"
# Include "BubFrame. h"
# Include "BubView. h"

Use Class Wizard to create the message response functions of ID_FILE_NEW_BUB, ID_FILE_OPEN_BUB, and ID_FILE_NEW_TXT.

The Response Function Code is as follows:

Void CMultiDoctempApp: OnFileNewTxt ()
{
POSITION curTemplatePos = GetFirstDocTemplatePosition ();
While (curTemplatePos! = NULL)
{
CDocTemplate * curTemplate = GetNextDocTemplate (curTemplatePos );
CString str;
CurTemplate-> GetDocString (str, CDocTemplate: docName );
If (str = _ T ("Txt "))
{
CurTemplate-> OpenDocumentFile (NULL );
Return;
}
}
AfxMessageBox ("no txttempalte! ");
}

Void CMultiDoctempApp: OnFileNewBub ()
{
POSITION curTemplatePos = GetFirstDocTemplatePosition ();
While (curTemplatePos! = NULL)
{
CDocTemplate * curTemplate = GetNextDocTemplate (curTemplatePos );
CString str;
CurTemplate-> GetDocString (str, CDocTemplate: docName );
If (str = _ T ("Bub "))
{
CurTemplate-> OpenDocumentFile (NULL );
Return;
}
}
AfxMessageBox ("no bubtempalte! ");
}

Void CMultiDoctempApp: OnFileOpenBub ()
{
POSITION curTemplatePos = GetFirstDocTemplatePosition ();
While (curTemplatePos! = NULL)
{
CDocTemplate * curTemplate = GetNextDocTemplate (curTemplatePos );
CString str;
CurTemplate-> GetDocString (str, CDocTemplate: docName );
If (str = _ T ("Bub "))
{
CFileDialog dlg (true );
If (dlg. DoModal () = IDOK)
{
CString path = dlg. GetPathName ();
CurTemplate-> OpenDocumentFile (path );
}
Return;
}
}
AfxMessageBox ("no txttempalte! ");
}

The above is a small example of multi-Document Template application development in the VC ++ environment. Hope to help you.

This article is summarized by myself based on my documents on Zhizhi online.
Source code: http://download.csdn.net/my




Related Article

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.