A brief introduction to OCX __ocx

Source: Internet
Author: User

the difference between the 1.exe,ocx,dll

Analysis: EXE is an executable program, and OCX as a window control (the control is OCX, such as button), DLL is a function, variables, classes packaged in the library

OCX form is as follows:


Each of the diagrams is an OCX control (I think so), but MFC built some controls, but if it is the OCX control itself, the program must run the generated OCX control to the executable directory


2.ocx features (based on the normal button)

(1) OCX is a visual window, an OCX has an ID, you can use the ID to establish a corresponding class, call the function (if you do not see, then certainly hidden)

(2) Can respond to click, double-click and other mouse events

(3) containing the corresponding property


3. Create an OCX program (how to let OCX contain the above attributes?)

(1) Use VS2010 to create an MFC ActiveX control project called Myocx


Where the myocx.h InitInstance is the entrance,. CPP contains OCX registration, anti-registration functions

MyocxCtrl.h Cmyocxctrl is OCX Visual window class, the parent class is COleControl, because it is a window, so you can respond to oncreate,onsize messages, including message mapping, scheduling mapping, event mapping

The myocxPropPage.h is an OCX property, just as the button buttons class contains attributes

Myocx.idl is an external interface, the Cmyocxctrl class is said to inherit it, all external interfaces are declared here in Myocx.idl, implemented in the Cmyocxctrl class

Myocx.def,ocx is a special kind of DLL, so there is also a DEF file, which reads

; Myocx.def:Declares the module parameters.

Exports
	DllCanUnloadNow     private
	dllgetclassobject   private
	DllRegisterServer   private
	DllUnregisterServer PRIVATE
The private meaning of which is the OCX is proprietary, that is, these function names will not be added to the generated library file (compiler OCX found in addition to generate OCX, there is a Lib file)

(2) Message mapping, dispatch mapping, event mapping

>1 meaning

Message mapping: A message that responds to the operating system

Dispatch mapping: An interface function that can be called externally, and the popular one is that a control can call its internal functions, such as CButton btn;btn. function name ();

Event Mapping: Click, double-click, and so on events by OCX to its container response, such as some of the button's event, triggered can be in its parent window to respond to processing

>2 Add Scheduling mappings

Add under the corresponding dispatch map in Myocx IDL:

[ID (1)] void Hello ();


Add in Myocx.h:

afx_msg void Hello ();
Add schedule enum type
enum {
        //dispatch ID, starting from 1
	Dispidhello = 1L,
		
        //Event ID, starting at 1
	};


Add in Myocx.cpp:

void Cmyocxctrl::hello ()
{
	AfxMessageBox (_t ("Hello World");
}

Compile and generate OCX, the default compilation generated OCX also registered well


Create a Dialog based MFC project named Useocx, right-click the Insert ActiveX Control in the dialog box window, and quickly enter the first few letters of the generated OCX to find OCX, add it in

Right key OCX, select Add variable, variable name set to M_myocx, set up, will generate the corresponding OCX class

You can see that the OCX has been automatically included:

void Hello ()
{
	invokehelper (0x1, Dispatch_method, vt_empty, NULL, NULL);


This time, you can use the M_myocx to call the Hello function inside.

Attachment: You can also choose Add function to be implemented in the following position


>3 Add Event Mappings

Right-click on the image below


Select Add Event, select Click to confirm that the effect is to have the following line in the IDL file event:

[ID (Dispid_click)] void CLICK (void);


More in the middle of the myocx.cpp:

Begin_event_map (Cmyocxctrl, COleControl)
	Event_stock_click ()
End_event_map ()


The effect is in the Useocx Project Resource dialog file, right-click Add OCX control, view properties, found in the event

More out of the Click Property, you can add a function, run Useocx project when the mouse clicks on the control will trigger this function

If you add a custom event map, add it under the event map in Myocxi.idl:

[ID (1)] void Mydrag (void);

Add in MyocxCtrl.h

enum {
	//dispatch map id
        dispidhelloclick = 1L,

	//Event map ID
	eventidmydrag = 1L,
     };

Protected:
void Mydrag (void)
{
      FireEvent (Eventidmydrag, Event_param (Vts_none));


Also view the Useocx Project Resource dialog box file control properties:

More than Mydrag properties, at this time can be written outside the response function, specific triggers need to be called in OCX internal call can trigger

Notice here that OCX event triggers in OCX internal trigger, external response processing, such as button class, click or double-click are triggered in the buttons themselves, and finally by its parent window response processing, custom events are also to be triggered inside


Note: InvokeHelper and fireevent These two functions represent arguments that have multiple parameters, separated by spaces (one function parameter can have many, but the return value is only one)


Summarize:

1.ocx has three mappings, but only dispatch mappings and event mappings are provided externally in IDL

2. (1) OCX of the class and OCX is through the IDL file in the last class ID established contact

In IDL:

  Class information for Cmyocxctrl
	[
		uuid (EF2CFD6B-E121-47E3-BD63-5F5DD51DCCFF)
	]


In the OCX class (class Cmyocxctrl_use:public CWnd)

CLSID const& Getclsid ()
	{
		static clsid const CLSID
			= {0xef2cfd6b, 0xe121, 0x47e3, {0xBD, 0x63, 0x5f, 0x5 D, 0xd5, 0x1d, 0xCC, 0xFF}};
		return CLSID;
	}


(2) The dispatch map is established through the ID of the dispatch function in IDL.

In IDL:

[ID (1)] void Hello ();

In the OCX class (class Cmyocxctrl_use:public CWnd)

void Hello ()
{
	invokehelper (0x1, Dispatch_method, vt_empty, NULL, NULL);



(3) The event map is triggered internally, that is, the function containing fireevent is called within the OCX as the triggering method, the response is implemented on the outside

3.ocx is used as a control in MFC, the control is associated with its container as a parent window and a child window, as well as a function call between a normal control and a parent window.


4ocx Properties

Related to the Cmyocxproppage class in MyocxPropPage.h, the corresponding effect:



5.ocx Debugging

(1) Debugging in EXE

According to the above method in the Useocx project to add a good ocx, in the Myocx project set the following properties:


You can debug it.

(2) Debugging in IE

First write an HTML file with the suffix named htm and add the following:

html>  
<HEAD>  
<title>test activex</title>  
</HEAD>  
<object id= " Testmfcatl control "width=528 height=545 classid=" CLSID:EF2CFD6B-E121-47E3-BD63-5F5DD51DCCFF ">  
    <PARAM Name= "_version" value= "65536" >  
    <param name= "_extentx" value= "12806" >  
    <param name= "_extenty" value= "1747" >  
    <param name= "_stockprops" value= "0" >  
</OBJECT>  


Note that the ID is the ID of the class in IDL, and the following properties are set in Myocx:



The first is the path of IE, the second is the data in the IE address bar, according to the need to fill out, some IP address, some htm, so you can


Note: Some IE is not a single process, can be attached to the process for debugging, you can also set IE as a single process of

I am here through IE debugging point of interruption, debugging and running the mouse is not available, also do not know what the reason, the temporary unresolved


6.ocx Program Release

OCX can not run alone, the release must be copied to the corresponding executable program, published together

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.