DLL encapsulation framework view Experience summary 2

Source: Internet
Author: User

Author: Zhu Jincan

Source: http://www.cnblogs.com/clever101

Last time I talked about the first way to dynamically create the DLL encapsulation framework View window. Next I will talk about the second way to create a document template. In fact, this method is simpler. (ALLCodeDevelopment Environment: vs C ++ 2005 + SP1, Win XP + SP2)

 

Method 2 create a document template

 

 First, create a rule DLL project custom1 using the shared MFC. My basic idea is to define five classes. The descriptions are as follows:

class name

description

remarks

ccustommanage1

external interface class, this class is responsible for creating and destroying dynamic windows.

cmysingledoctemplate

derived from cmysingledoctemplate class, used to create a template class for a new document.

ccustomdoc

derived from cdocument , which is the document class corresponding to the new window.

ccustomframewnd2

derived from cframewnd , to create a framework window class.

Ccustomview

Derived fromCviewTo create a View class.

 

 

Ccustommanage1Class definition and implementation, the specific code is as follows:

// Custommanage2.h

# Pragma Once

Class Afx_ext_class ccustommanage2
{
Public :
Ccustommanage2 ( Void );
~ Ccustommanage2 ( Void );

Public :

Void Createdoctemple ();

Ccustomframewnd2 * Getframewnd ();

Private :

Cmysingledoctemplate * M_pmysingledoctemplate;

Ccustomframewnd2 * M_pframewnd;
} ;


// Custommanage2.cpp


# Include " Stdafx. h "
# Include " Custommanage2.h "
# Include " Resource. h "
# Include " Customdoc. h "
# Include " Customview. h "
# Include " Custom2.h "

Ccustommanage2: ccustommanage2 ( Void )
{
M_pmysingledoctemplate=NULL;
M_pframewnd=NULL;
}

Ccustommanage2 :: ~ Ccustommanage2 ( Void )
{
Delete m_pmysingledoctemplate;
}

Extern Ccustom2app theapp;
Void Ccustommanage2: createdoctemple ()
{
// Make sure the resource handle is valid
Afxsetresourcehandle (theapp. m_hinstance );

M_pmysingledoctemplate = New Cmysingledoctemplate (
Idr_menu1, // Menu Bar ID
Runtime_class (ccustomdoc ),
Runtime_class (ccustomframewnd2 ), // Custom MDI child frame
Runtime_class (ccustomview ));

Cstring cstile = _ T ( " Try to encapsulate the framework by creating a document template " );
Cdocument * Pdoc = M_pmysingledoctemplate -> Createnewdocument ();
M_pframewnd = Static_cast < Ccustomframewnd2 *> (M_pmysingledoctemplate -> Createnewframe (pdoc, null ));
Pdoc -> Settitle (cstile );
M_pmysingledoctemplate -> Initialupdateframe (m_pframewnd, pdoc );

}


Ccustomframewnd2 * Ccustommanage2: getframewnd ()
{
ReturnM_pframewnd;
}

 

Now let's talk about how to make external calls. Create a single-document project ower that calls the DLL. Now the ower project is in the framework classCmainframeClass defines a private variable of the ccustommanage2 class:

Private :

Ccustommanage2 m_custommanage2;

Create a menu item and a new window is displayed in the command response function of the menu item. The Code is as follows:

Void Cmainframe: ontest2 ()
{
//Todo: add the command handler code here
M_custommanage2.createdoctemple ();
}

 

Next we will talk about how to destroy the window to prevent memory leakage. In this case, we need to consider how the user closes the new window.ProgramOr click the close button of the new window in the DLL pop-up. The user first closes the new window and then closes the calling program. There is no memory leakage. However, if you close the application (that is, close two windows at the same time), memory leakage will occur. Therefore, we need to rewriteCmainframeWm_close message of the class:

Void Cmainframe: onclose ()
{
// Todo: add the message processing program code and/or call the default value here

// Get frame window pointer
Ccustomframewnd2 * Pframewnd = M_custommanage2.getframewnd ();
// If the newly created DLL window still exists, close the window handle when the window handle is valid.
If (Null ! = Pframewnd)
{
Hwnd = NULL;

Hwnd = Pframewnd -> Getsafehwnd ();
If (: Iswindow (hwnd ))
Pframewnd -> Sendmessage (wm_close, null, null );
}

Cframewnd: onclose ();
}

 

This ensures that the memory is not leaked. As follows:

 

It can be seen that a single-document program is encapsulated in DLL. From this perspective, this method looks more natural than creating a window dynamically. In addition, the MDI program can be encapsulated, and the above custom document template class must be derived from cmultidoctemplate. If you are interested, you can do it.

What is the significance of Using DLL to encapsulate the framework view? In fact, it can deal with more diverse needs. In many cases, only the dialog box does not meet the requirements, such as the processing software is often multi-view, of course, you can also draw in the dialog box, however, it is not as convenient as drawing in the View class. The dialog box does not have a button to resize the window, which is also a disadvantage. In a large system, using the DLL encapsulation framework view can easily divide multiple business logic into multiple modules, making development easier.

 

 

 

 

 

 

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.