Using VS2010 to create an MFC ActiveX Project _c language

Source: Internet
Author: User
Tags ole uuid

The basic concept of 1.ActiveX

An ActiveX control can be viewed as a minimal server application that cannot run independently, and must be embedded in a container program to run with that container. This container includes Web pages, application forms, etc. ...

The name of the ActiveX control's suffix is OCX or DLL. Generally, OCX and dynamic library coexist in the form of packaging into a cab or EXE file on the server, the client download after the installation of the CAB or EXE unpacked into OCX and dynamic library coexistence of files, and then register OCX files.

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 all aspects of Web servers and clients. At the same time, ActiveX technology is also used to easily create common desktop applications, and ActiveX generally has an interface.

2. Three concepts: ActiveX, ole, and COM

In terms of time, OLE is the oldest, then COM and ActiveX; From an architectural standpoint, OLE and ActiveX are built on COM, so COM is the base; OLE, ActiveX is the two brand name from the name point of view, And COM is a pure technical term, which is why we hear more about ActiveX and OLE. COM was born at the request of OLE, so although COM is the base of OLE, OLE is generated before COM. The basic starting point for COM is to have a software service to another software through a common organization. The most central technology of ActiveX is COM. The biggest difference between ActiveX and OLE is that OLE is for integration between applications 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 with different processes. Using a COM object's browser, you do not need to care about what language the object is written in, or whether it is executed in a DLL or in a different process. From the browser side, there is no difference. Such a generic processing technique is useful.

Creation of 3.ActiveX Control engineering

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

First: Create the MFC ActiveX control project;

Second: Create an "ATL project". Because using ATL to develop ActiveX controls requires knowledge of COM technology, high requirements for programmers, and long development times, if ActiveX runs only under Windows operating systems, use the MFC ActiveX control project to quickly establish ActiveX controls. But note here: Use the MFC ActiveX control project to quickly build ActiveX controls, not only to build on Windows operating systems, but also to install C + + dependent library installation packages under Windows operating systems because MFC is built on Microsoft's C + + Dynamic library based on the. So there are pros and cons to each of these two ways, depending on the project needs to choose the appropriate way.

4. Use VS2010 to create MFC ActiveX Project steps

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

First: New Project-"MFC ActiveX Control" project, to name the project Testmfcatldebug, click OK, Pop-up Control wizard dialog box

Second: In the overview, application settings, control name and control settings can choose the default, and then click Finish, so that the MFC ActiveX control project Creation completed

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

By 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 and CPP files for the three classes above and find them to be derived classes.

DllRegisterServer and DllUnregisterServer are defined in the Testmfcatldebug:cpp file, and you can find that both the registration and the anti-group of ActiveX are related to the class.

Testmfcatldebugctrl: You can see that the header file declares a message map (so that ActiveX control programs can accept event notifications sent by the system, such as form creation and shutdown events), Scheduling mappings (allowing an external caller (a container containing ActiveX) to easily access the properties and methods of an ActiveX control), Event mappings (lets ActiveX controls send event notifications to external callers, which contain ActiveX containers). That is, window operations on ActiveX controls are done in this class, including the creation of ActiveX controls, redrawing, and creating visual MFC forms in this class.

Testmfcatldebugproppage: Displays the properties page of the ActiveX control

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

 #include <olectl.h> #include <idispids.h> [UUID (69ee37f4-8b36-495f-9f60- 
 
  5e3aaf2fb494), Version (1.0), control] library Testmfcatldebuglib {importlib (stdole_tlb); Ctestmfcatldebugctrl Master Dispatch interface [UUID (6B60346D-5CCD-4907-83F4-51938558A9B7)] dispinterface _dtestmfcatlde 
  Bug {properties:methods: [ID (Dispid_aboutbox)] void AboutBox (); 
 
  }; Ctestmfcatldebugctrl event Dispatch interface [UUID (e26ecc46-9ba2-4c25-a4dd-a690560a5113)] dispinterface _dtestmfcat 
 
  Ldebugevents {properties://Event interface does not have any attributes methods:}; 
    Ctestmfcatldebugctrl 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 ActiveX methods, attributes, and ClassID registered in the Registry (Web page calls need to use), you must understand this file, this file can be divided into four parts to see:

The first is TESTMFCATLDEBUG.LIB this library information

This is not a detailed explanation.

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

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

The third part is the interface information of event mapping

The Forth part is the information of the class, where the UUID is the classid of the ActiveX control registered to the registry, which is the unique identifier of the system after the ActiveX is registered, which is the Web page that loads the ActiveX control with this ID.

6. Define scheduling mappings and event mapping methods that are provided to external callers using the

So how do you define new scheduling mappings and event mapping methods, and 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-click in the _dtestmfcatldebug key--Add methods (or properties), This action is the addition of methods and properties that complete the dispatch map, and the right key in the _dtestmfcatldebugevents key-to add a method (or property) that is the addition of the method and property that completes the event map.

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

You can right-click in the _dtestmfcatldebug item-Add method, set box

After filling in the information and clicking Finish, you can call method Fuck2 outside, while the internal ActiveX control project code will add code to the three files:

1. In

2. In

3. In

7. Registering ActiveX controls

Following the steps above, you have completed the writing of a simple ActiveX control (a control without a form interface), which generates a Testmfcatldebug.ocx file under Debug, and you can register this ActiveX control using Windows DOS Windows

Steps:

First win+r the run, then enter the registration command: regsvr32 c:\.......\testmfcatldebug.ocx (anti-registration command: regsvr32 c:\.......\testmfcatldebug.ocx-u)

There are two scenarios in which the control registration fails:

First: Using a non-administrator user login system will not be able to register the COM component due to insufficient permissions, this time you must use the administrator user to log into the operating system

Second: the DLL library that the ActiveX control relies on is consumed by the program, which causes the registration to fail, by shutting down the running program to

8. Methods to test ActiveX controls

By following the steps above, the ActiveX control has been written, and the control has been registered successfully, then how to test the control, there are three ways:

First: Use HTML Web pages to test

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

<HTML> 
<HEAD> 
<title>test activex</title> 
</HEAD> 
<object id= " Testmfcatl control "width=528 height=545 classid=" Clsid:dd0cf7ef-a181-428c-b5fc-c44a1c13ca43 "> 
  <PARAM Name= "_version" value= "65536" > 
  <param name= "_extentx" value= "12806" > 
  <param name= "_extenty" value= "1747" > 
  <param name= "_stockprops" value= "0" > 
</OBJECT> 
</HTML>

Note that the above classid is the UUID for the class in the IDL file, and the ActiveX control is displayed when the page is opened. The above ClassID after the successful registration of the control can also be found through the registry, the specific method is Win+r key, enter the regedit command, will pop up "Registry Editor", location in the "Hket_classes_root", according to the name of your control, quickly press the first three letters, Then you can navigate to a more easily accessible location, as shown below

Second: Create MFC applications, right-click MFC window-Insert ActiveX control, and then display ActiveX controls on MFC forms

The third: and one of the most convenient ways to test ActiveX controls with the VS-Self ActiveX Control Test Container, but VS2010 does not have this item in the tool, we can first manually add this tool to 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 solution TstCon.sln, compile the project Tcprops and TstCon, and after the compilation is done in C:\Program Files\Microsoft Visual Studio 10.0\ Samples\2052\c++\mfc\ole\tstcon\debug\ generates the TstCon.exe execution program, which is the ActiveX control Test Container, Next we add this TstCon.exe to the tools in VS2010, select External Tools in the Tools menu item in VS2010, and add a new tool to the pop-up form, titled ActiveX control Test Container, with the command C : \program Files\Microsoft Visual Studio 10.0\samples\2052\c++\mfc\ole\tstcon\debug\tstcon.exe, then click OK to complete the addition of the tool.

In this way, there is an ActiveX control Test Container in the tool, and clicking on it pops up the test ActiveX container, as shown below

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

The registered Acitvex control is then displayed, and if you want to test the method of the control's dispatch mapping Fuck2, select the control first, then click control-Invoke Methods and select Methods in the Fuck2 name dropdown box. Click on the Invoke button to test this method, as shown in the following figure:

We can see that the ActiveX control above is a blank background and a circle, and there is 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 box resource in the Resource view

Remove the OK and Cancel buttons above, and then modify the dialog box properties: Border change to None,control to Ture,id to Idd_main_dialog,style instead to Child,system to false, Visible to True, and then double-click in the dialog box to add a class to the dialog box, as shown in the following figure:

Click "Finish". A new ViewDialog.h and ViewDialog.cpp 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 drag a button to the dialog box, at which point the dialog effect is:

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

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

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

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

Click "Add handler", so that there are oncreate and OnSize functions in "Existing handlers", click OK to complete the addition of the message mapping function, as shown in figure:

Automatically add the following code in TestMfcAtlDebugCtrl.cpp, as shown in figure:

Write the following code in the OnCreate function (Create is creating a Form dialog box in an ActiveX control, Idd_view_dialog is just the New dialog box):

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

Third: Recompile, register OCX, use ActiveX Control Test Container to test it again

You can see that there's already an interface in this ActiveX control.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.