Integration of classic and modern: Integrating the RAD. NET Framework in MFC

Source: Internet
Author: User

By Sun Hui

MFC has been around for more than a decade. However, it is still a key component of Visual C ++. Almost eight years since Visual C ++ 1996 in 4.2, the main characteristics of MFC have not changed significantly and are still "old" faces, therefore, the comments about this class library are naturally reasonable. From my personal point of view, MFC is still robust and powerful, and it is a rare, stable development framework in the industry that has been tested for a long time. If you study this class library in depth, you will find the more mellow the wine is. One of the success factors of MFC is the provision of a complete Document/View framework. However, this is also the point of criticism against MFC. Perhaps because this framework is too classic, it makes it look that MFC is no longer "Tingting Yu Li", but "Old and yellow", so that when we open today's Visual Studio IDE, we may feel a little uncoordinated: compared with other. the development language of the NET Framework. The development with MFC will look very "Rustic" and "lonely"-there is no RAD mechanism, and there is a clear lack of "interoperability" with other fashionable objects, and strictly abide by your own territory. Every time a document-based MFC program is generated, we always see a vicissitudes of life, as if Liu Jie entered the Grand View Park, with the fashionable C # and VB around. NET and other obvious "generation gap" and "incompatible ". Many people once asked me if there is a future for MFC? Are you already running? The discussion on MFC or. NET is everywhere. CLR is a charming world, which makes C #, VB. NET and so on eye-catching. However, MFC is not aging. If you have a deep understanding of MFC, you will find that MFC can compete with C # and VB. NET ...... Use managed extension in the MFC project

Support for managed Extension

. The managed extension support provided by NET FrameWork ensures that the managed extension (CLR) is supported in the MFC project, and developers can make the MFC project (In this article we will use Test as the project name) you can enable the managed extended properties switch of the project to increase the compiler hosting support (1 ).

(Figure 1: Enable the managed compilation support switch)

Dual. NETObject

After the hosting extension compilation switch is enabled, you can use managed objects in the MFC project. The common practice is: match a hosted object for each important MFC object to form a dual pair. The matched objects contain pointers to the other object... NET object, while other MFC objects can be operated through the MFC object in the dual pair. NET object (2 ).

(Figure 2: Dual hosted object)

In Visual Studio. NET, the wizard for adding hosted C ++ class objects is not provided. Therefore, you can add a Component Object Based on hosted C ++ (3) first ).

(Figure 3. Add Class Wizard: Add a hosted C ++ Component Object)

After the testDocObject managed component Object is added, change the base class of the Object to the Object, and delete some code to get a minimum Managed class:

Namespace test {_ gc public class testDocObject: public Object {public: testDocObject (void ){}};}

After the above steps, Visual Studio. NET generated code is loaded into the MFC program, of course, can be manually created. h file and. cpp file, enter the corresponding code, and then add them to the current project. Because the above steps are often encountered in managed extension programming, it is necessary to automate the above process. In view of this, we provide a complete Wizard for adding. NET objects in the attached CD.

Define managed member variables in the MFC unmanaged class

Using managed objects in the MFC class provides object declarations and initialization methods, which are slightly different from traditional methods. Take adding a managed member variable to the CtestDoc document class as an example. The code for declaring a managed object is as follows:

Public: gcroot M_ptestDocObj;

Gcroot type secure packaging templates can embed hosted reference type pointers into unmanaged classes as member variables, which can be used like other types of variables. Create this object in the member function InitialDocument of CtestDoc. The Code is as follows:

BOOL CtestDoc: InitialDocument () {# pragma push_macro ("new") # undef new m_ptestDocObj = new test: testDocObject (); # pragma pop_macro ("new ")}

Because testDocObject is a managed reference type, it is always allocated on the CLR stack, so it cannot be used in afx. h. Declare the MFC object in the managed object, which is consistent with the conventional method.

After understanding the dual usage of the above basic hosting classes and objects in traditional MFC projects, you can make full use of them.. NET Framework provides rich class library support (it is necessary to reference the relevant dynamic link library and declare the namespace ). If you want to learn more about the technology of mixed hosting and unmanaged C ++ code, you can refer to Extending MFC Applications with. NET Framework. Host. NET Control

Host. NETTheoretical Basis of controls

In the world of the. NET Framework, the rich. NET controls are undoubtedly dazzling. The MFC program naturally wants to marry these gorgeous things. Because the MFC framework does not provide. the direct support of the. NET control leads to the loss of MFC (lack of C #, VB. NET-specific visual design mechanism and free Control Organization function. NET World is a reasonable objective reason. However, we noticed that :. NET controls are essentially ActiveX controls. The important difference between them is that the registration method is different-ActiveX controls are global and registered in the system registry; and. NET controls can be registered in the global AssemblyCache or stored in a local directory. The corresponding methods for obtaining their related information in the program are different. However, once the basic information of the. NET control is "captured", we can use the same method as creating the ActiveX control to create the. NET control to the MFC project.

(Figure 4: Creating ActiveX controls in the MFC Framework)

We know that MFC creates ActiveX controls through the COleControlSite class, because the COleControlSite class for ActiveX controls is not applicable. NET control, so you must re-derive a new class CWFControlSite to provide the necessary support, through a CWFControlWrapper class will be. NET controls are encapsulated in a CWnd form, and the packaged form is "placed" in CWFControlSite. The CWFControlWrapper class code is as follows:

Class CWFControlWrapper: public CWnd {public: CWFControlWrapper (); virtual ~ CWFControlWrapper (void); IUnknown * pUnkControl; IUnknown * GetManagedControl () {return pUnkControl;} void SetControlSite (COleControlSite * pSite) {m_pCtrlSite = pSite ;}};

Next, design a common CUserCtrlView class (derived from the CView class) so that the. NET control specified in CWFControlSite can be displayed to the user like the ActiveX control specified in COleControlSite. Just as each ActiveX control must be created with a CWnd object, one is supported. the CView class of the NET control requires a corresponding CWnd object. The CWFControlWrapper is designed for this purpose. Through the CWFControlWrapper object, the MFC program can get the result. the IUnknow and IDispatch corresponding to the. NET object. Later, we will introduce the specific design and usage of the CUserCtrlView class.

(Figure 5: Create a. NET Control in the MFC Framework)

NETMessage Processing of controls

Generally, message processing in the control dialog box is a critical issue. The solution of the Host Control in the MFC that can be found on the Internet is not implemented.. NET Control dialog box message processing, an obvious feature is that the "Tab" key message cannot be processed. Therefore, we have reloaded the PreTranslateMessage function of CUserCtrlView:

BOOL CUserCtrlView: PreTranslateMessage (MSG * pMsg) {BOOL bRet = FALSE; if (m_Control.pUnkControl! = NULL) {CComQIPtr SpInPlace (m_Control.pUnkControl); if (spInPlace) bRet = (S_ OK = spInPlace-> TranslateAccelerator (pMsg ))? TRUE: FALSE;} if (CView: PreTranslateMessage (pMsg) return TRUE; CFrameWnd * pFrameWnd = GetTopLevelFrame (

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.