C + + ActiveX Foundation 1: Creating an MFC ActiveX Project using VS2010

Source: Internet
Author: User
Tags ole

Basic concepts of 1.ActiveX

An ActiveX control can be thought of as a very small server application that cannot run on its own and must be embedded in a container program and run with that container. This container includes Web pages, application forms, etc...

The suffix name of the ActiveX control is OCX or DLL. Usually in the form of OCX and Dynamic Library co-packaged into a cab or EXE file placed on the server, the client after downloading the installation cab or EXE to extract into the OCX and the dynamic library coexistence files, and then register the OCX file.

ActiveX controls are a set of technologies that enable software components to interact in a networked environment based on COM standards. It has nothing to do with the specific programming language. As a technology developed for Internet applications, ActiveX is widely used in various aspects of Web servers and clients. At the same time, ActiveX technology is also used to easily create common desktop applications, in addition to the interface that ActiveX typically has.

2. Three concepts: ActiveX, ole, and COM

In terms of time, OLE is the first to appear, then COM and ActiveX, and from an architectural standpoint, OLE and ActiveX are built on top of COM, so COM is the basis; From a name perspective, OLE, ActiveX is two trademark names, COM is a purely technical term, which is why we hear more about ActiveX and OLE. COM was born to the needs of OLE, so although COM is the basis of OLE, OLE is produced before COM. COM's basic starting point is to have a software service for another software through a common organization. The most core technology of ActiveX is COM. The biggest difference between ActiveX and OLE is that OLE is for integration between application software and files on the desktop, while ActiveX is primarily for user interaction with further network applications. COM objects can be written in any language, such as C + +, Java, and VB, and can be implemented in the form of DLLs or executable files that work in different processes. A browser that uses a COM object does not need to worry about what language the object is written in, or whether it is executed in a DLL or another process. From the browser side, there is no difference. Such a common processing technique is very useful.

Creation of 3.ActiveX Control engineering

There are two ways to create an ActiveX project using VS2010,

The first: Create an "MFC ActiveX control" project;

The second type: Create an ATL project. because the use of ATL to develop ActiveX controls requires knowledge of COM technologies, the requirements for programmers are high and development time is long, so if ActiveX is only running under the Windows operating system, use the MFC ActiveX control Engineering to quickly build ActiveX controls. But here's a note: Using the MFC ActiveX controls project to quickly build an ActiveX control, he's not only built under the Windows operating system, but also has to install the C + + dependency library installation package under the Windows operating system, because MFC is built on Microsoft's C + + On the basis of a dynamic library. So each of these two approaches has advantages and disadvantages, according to the project needs to choose the appropriate way.

4. Steps to create an MFC ActiveX project using VS2010

Because it is difficult to create an ActiveX project using ATL, you first use the MFC ActiveX control project to create a simple ActiveX control.

First: New Project-"Choose MFC ActiveX Control" project, give the project name Testmfcatldebug, click OK, pop up the "Control Wizard" dialog box

Second: In the overview, application settings, control name and control settings can be selected by default, and then click "Finish" so that "MFC ActiveX control" project is created

5. Analysis of three important classes of "MFC ActiveX Control" project and external interface definition file IDL

Using the wizard to create a project you can see that three classes are automatically generated, Testmfcatldebug,testmfcatldebugctrl and Testmfcatldebugproppage

You can open the header files and CPP files for the above three classes and find that they are all derived classes.

testmfcatldebug: The DllRegisterServer and DllUnregisterServer are defined in the CPP file, and you can see that both the registration and the anti-Book of ActiveX are related to that class.

Testmfcatldebugctrl: You can see that the message map is declared in the header file (so that ActiveX control programs can accept event notifications sent by the system, such as form creation and shutdown events), and the dispatch map (Let the external invoker (the container that contains the ActiveX) have easy access to the properties and methods of the ActiveX control), the event map (which allows the ActiveX control to send an event notification to the external calling program, which contains the ActiveX container). That is, the window operation of the ActiveX control will be done in this class, including the creation of ActiveX controls, redrawing, and creating visual MFC forms in this class.

testmfcatldebugproppage: Displays the ActiveX control's property page

Let's take a look at the most important part: external interface definition file testmfcatldebug.idl, the code is as follows:

#include  <olectl.h> #include  <idispids.h>[ uuid (69ee37f4-8b36-495f-9f60-5e3aaf2fb494 ),  version (1.0),    control ]  library testmfcatldebuglib  {      importlib (stdole_tlb);     //   ctestmfcatldebugctrl  's main dispatch interface    [         uuid ( 6B60346D-5CCD-4907-83F4-51938558A9B7)        ]      dispinterface _dtestmfcatldebug     {          properties:         methods:              [id (Dispid_aboutbox)] void aboutbox ();      };     //  CTestMfcAtlDebugCtrl  Event Dispatch Interface     [          uuid (e26ecc46-9ba2-4c25-a4dd-a690560a5113)         ]     dispinterface _DTestMfcAtlDebugEvents      {         properties:              //   event interface does not have any properties         methods:     };     //  ctestmfcatldebugctrl  The class information    [         uuid ( DD0CF7EF-A181-428C-B5FC-C44A1C13CA43)      ]     coclass  testmfcatldebug     {         [ default] dispinterface _dtestmfcatldebug;         [ Default, source] dispinterface _dtestmfcatldebugevents;     };  }; 

This is the external interface definition file, if the external program wants to invoke the ActiveX method, the property and the registry registration in the ClassID (Web page calls need to use), it is necessary to understand the file, this file can be divided into four parts:

The first is TESTMFCATLDEBUG.LIB this library information

That's not an explanation.

The second part is the interface information of the dispatch map, which contains the properties (such as the control background color) and the external method

Inside defines a method AboutBox (), the method can be called by an external program, the functions defined in the interface are pure virtual functions, the implementation of these functions are done in Testmfcatldebugctrl, MFC through the underlying encapsulation, Let the Testmfcatldebugctrl class inherit this interface and implement the function.

The third part is the interface information of the event map

The Forth part is the information of the class, where the UUID is the classid that the ActiveX control registers with the registry, which is the unique identity within the system after the ActiveX registration, and the Web page is using this ID to load the ActiveX control's

6. Define the dispatch map and event mapping methods, which are provided to external callers using
So how to define a new scheduling map and event mapping method, if the manual definition is inconvenient, of course, using the compiler to define, the step is to open Class View:

You can see that there are _dtestmfcatldebug and _dtestmfcatldebugevents in the Testmfcatldebuglib, and you can right-add a method (or property) in the _dtestmfcatldebug item. This operation is the addition of methods and properties to complete the dispatch map, and in the _dtestmfcatldebugevents item you can right-click to add a method (or property) that is the addition of the methods and properties that complete the event map.

For example, if you want to add a dispatch map Method Fuck2 (), so that the external can be called:

In the _dtestmfcatldebug key can right-"Add method, set box

After filling out the information, click Finish, external can call method Fuck2, and ActiveX control project code inside will be in three files new code:

1. In

2. In

3. In

7. Registering ActiveX controls

Follow the steps above to finish writing a simple ActiveX control (a control without a form interface), regenerate a testmfcatldebug.ocx file under Debug, and Use Windows DOS window to register the ActiveX control

Steps:

First Win+r key to open run, and then enter the registration command: regsvr32 c:\.......\testmfcatldebug.ocx (anti-registration command: regsvr32 c:\.......\testmfcatldebug.ocx-u)

There are two situations that can cause a control registration to fail:

The first: Using a non-administrator user login system will not be able to register COM components due to insufficient permissions, it is necessary to use the Administrator user login operating system

The second type: the DLL library that the ActiveX control relies on is occupied by the program, which causes the registration to fail, and the workaround is to close the running program to

8. Methods for testing ActiveX controls

Following the steps above to complete the ActiveX control, and also register the control successfully, then how to test the control, there are three ways:

The first : Use HTML pages to test

Write the code in the Testmfcactivex.htm file as follows:

 

Note that the classid above is the UUID of the class in the IDL file, and then opening this page will display the ActiveX control. The above ClassID after the control successfully registered can also be found through the registry, the method is the Win+r key, enter the regedit command, will pop up "Registry Editor", the location in "Hket_classes_root", according to the name of your control, quickly press the first three letters, You can then navigate to a more accessible location, such as

The second is to create an MFC application that inserts an ActiveX control in the MFC window right-click, and then displays the ActiveX control on the MFC form

The third , and most convenient, method is to test the ActiveX control using the VS bring-your-own container, but VS2010 does not have this item in the tool. Then we can first manually add this tool to the VS2010, first find C:\Program Files\Microsoft Visual Studio 10.0\samples\2052\c++\mfc\ole\tstcon\ TstCon.sln, and then use VS2010 to open the solution TstCon.sln, compile the project Tcprops and TstCon, and after the compilation is complete, the C:\Program Files\Microsoft Visual Studio 10.0\ The TstCon.exe execution program is generated in samples\2052\c++\mfc\ole\tstcon\debug\, which is the ActiveX Control Test Container, Next we add this TstCon.exe to the tools in the VS2010, select External Tools in the Tools menu item in VS2010, add a new tool to the popup form, titled ActiveX Control Test Container, command C : \program Files\Microsoft Visual Studio 10.0\samples\2052\c++\mfc\ole\tstcon\debug\tstcon.exe, then click OK to finish adding the tool.

In this way, in the "tools" there is an ActiveX Control test Container, click it will pop up the test ActiveX container, such as

Click Edit->insert New control-> Select Testmfcatldebug Control, click OK

Then it will show this registered Acitvex control, if you want to test the control of the scheduling mapping method Fuck2, first select the control, and then click control-"Invoke Methods, in the Methods Name drop-down box select Fuck2 This method, Click the Invoke button to test this method, such as:

We can see that the above ActiveX control is a blank background and a circle consisting of no form interface, then how to add a form?

9. Add an MFC form to an ActiveX control, which is an ActiveX control with an interface

Steps:

First: Create a new dialog resource in Resource View

Remove the "OK" and "Cancel" buttons from the top, and then modify the dialog box properties: Border to None,control instead of Ture,id to Idd_main_dialog,style instead of Child,system to false, Visible to True, and then double-click in the dialog box to add a class for the dialog box, such as:

Click "Finish". A new ViewDialog.h and ViewDialog.cpp is added in Solution Explorer this Viewdialog class is just the dialog class we created

Then drag an EDIT control to the dialog box, modify its ID to idc_edit_output, and then drag a button to the dialog box, where the dialog effect is:

The dialog is completed and the next step is to add it to the ActiveX control.

Second: Define the dialog box instance in TestMfcAtlDebugCtrl.h M_videodlg

Then define two message mappings in the Testmfcatldebugctrl class: Form Create completion message map and form change size message map

In the VS2010 menu item--Class wizard, select the project and class you want to add to, select the Messages tab, select Wm_create, and then click the Add Handler button and select Wm_size

Then click "Add Handler", so in "existing handlers" there are oncreate and onsize These two functions, click OK, complete the addition of the message map function,

The following code is automatically added to the TestMfcAtlDebugCtrl.cpp

Write the following code in the OnCreate function (Create is a form dialog box created in the ActiveX control, and Idd_view_dialog is just the New dialog box):

Write the code in the OnSize function as follows (MoveWindow is determining the size of the Form dialog box in ActiveX):

Third: Recompile, register OCX, test the control again using ActiveX Control Test Container

You can see that the interface is already in the ActiveX control.

C + + ActiveX Foundation 1: Creating an MFC ActiveX Project using VS2010

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.