Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Writing ActiveX control with VC++
Writing ActiveX control with VC++
ActiveX technology is realized by generating "*". Ocx "file. Let's take a look at the ocx file first. In Baidu Encyclopedia, ocx explains it like this: ". OCX is an extension of OCX control, and OCX is an object class extension component. If you have used visual programming tools such as Visual Basic or Delphi, you will be familiar with the concept of controls, that is, small buttons on the toolbar, such as EditBox, Grid, ImageBox, Timer and so on. Each control has its own events, methods and properties. Programming with controls is very easy. First of all, you can set some properties, such as size, location, title, etc., at the design stage of the program. In the running stage of the program, you can change these properties and call different methods to control the control for different events. Controls are like building blocks, and all a program has to do is put these building blocks together. The biggest advantage of control is that it can be reused and even used between different programming languages. For example, you can embed controls developed with VC in VB. "

The last sentence is more important, that is, OCX control developed with VC can be called in other languages, which well realizes the good recovery of functional components and can also be called across languages (for example, OCX control developed with C++ can be called with C#).

Let's start with how to use VC++ to generate *. Ocx file, you have to do it step by step.

1. Create the simplest ocx file and debug it.

1. 1 Create the simplest ocx file.

VC- new project -MFC ActiveX WinZard

Click OK until you click Finish. Finally, VC++ will automatically generate some files, which constitute the basic template of ActiveX. The main structure of the document is as follows:

Compile directly, then a control registration file named "ocxDemo.ocx" will be generated in the Debug directory, and then the control can be registered locally by using the "regsvr32" command, and then the control can be referenced to realize the corresponding functions when writing programs in this language or across languages (mentioned later).

Debugging method of 1.2 ocx:

VC++ comes with a tool for debugging controls, ActiveX control test container, which can be opened in three ways:

1. Click the "Debug" button and the following dialog box will appear:

Then browse the c: program file Microsoft visual studio common tools tstcon 32. EXE”。

2. Startup, programming, Microsoft Visual C++6.0, Microsoft Visual C++6.0 tools and active control test containers of the system.

3. Tool in VC++development environment -ActiveX control test container

By any of the above methods, you can call up the following program:

Right-click the blank area, insert the control, and the following dialog box will pop up:

Select the specified control, and then click OK. The control will be loaded into the tool, and then you can see the related event response of the control through the tool.

2. Template customization function generated based on VC++

All customization functions basically come from the "MFC ClassWizard" class wizard dialog box.

("Ctrl+W" or "View"-"Create Class Wizard")

Add methods and properties to controls in the Automation tab.

Add an event to a control in the ActiveX event tab.

2. 1 Add control properties

Switch to the Automation tab, and click Add Attribute on the right to pop up the dialog box:

External name: external name. Reference the name of the property that the external program sees when using this control, and only use it when referencing externally.

Type: Attribute type. In addition to basic data types such as shaping, there are many complex advanced data types.

Variable name: variable name. The variable name of this attribute in the control source file, which is used when writing the control source code.

Notification function: reminder function. When the attribute changes, the reminder function is triggered.

Implementation: implementation method. Refers to three types of attributes: intrinsic type, member variable type and Get/Set method type. Inherent type refers to the inherent attributes given by the system, such as background color, title, etc. Member variables are user-defined attributes; The type of Get/Set method may refer to variables that can only be obtained and changed through the Get/Set method (this has not been studied).

2.2 Add control methods

In the Automation tab, click Add Method on the right to open the dialog box:

External Name: The external name of the method.

Internal Name: the internal name of the method.

Return Type: the return value type. In addition to basic data types such as shaping, there are many complex advanced data types.

Implementation: implementation method. There are two kinds: intrinsic method and user-defined method.

Parameter list: parameter list. Parameter name and parameter type: Parameter types contain many advanced data types.

2.3 Add control events

Switch to the "ActiveX Events" tab and click "Add Events" on the right to pop up the dialog box:

External Name: The external name of the event.

Internal Name: The internal name of the event. There is one more prefix "fire" than the external name.

Implementation: implementation method. There are two kinds: intrinsic events and custom events. Inherent events are generally events such as mouse movement and double click, which are triggered by system messages; A user-defined event is a function completely defined by the user, but this function needs to be called by the user in the source file (for the user of the control, it is equivalent to the event being triggered where it is called, and the internal passed-in parameters are the incidental information of the message generated by this event).

Parameter list: parameter list. Parameter name and parameter type: Parameter types contain many advanced data types.

Summary: After adding properties, methods and events to the control through the class wizard tool, VC++ will automatically generate code in the corresponding file, such as the mapping of internal method properties and external method properties, the establishment of messages, the declaration of messages and so on. If users want to do in-depth research on citations, they need to be familiar with the structure of the program, know the functions of each part of the code, and know which codes are automatically generated by the system and which codes need to be added manually by users. Although Visual C++ development environment has many advantages, one obvious disadvantage is that the code structure is chaotic, which is not as good as VS2005 and subsequent Visual Studio series. But because VC6.0 is a classic development environment, and all C++ programming on the Internet is basically based on VC6.0, it is necessary to learn it, so that you can read the online code and digest it.

2.4 Generate ocx file and debug it.

Compile the project directly with custom code, and then generate an ocx file in the debugging directory of the project, which is the registration file of this control.

Control debugging tool is still "ActiveX control test container".

Suppose we add an event to the control: an inherent event-"mousemove" mouse movement event; User-defined event -ocxclick event (this event is triggered by the message "WM_MOUSEMOVE" and returns the x coordinate of the current mouse position).

Run the ActiveX Control Test Container and insert the current control. When the mouse moves over it, you can see that MouseMove has generated an event.

At the same time, you can test the method of the control through "Control"-"Call Method". The test method is that you input parameters, and it returns the calculation result (take the user-defined method funHello as an example).

3. How to use the control

3. 1 Registration control

There are many ways to install ocx controls. Here is the simplest one.

Steps:

1. Copy the OCX control file to a directory, such as the root directory of drive C.

2. Enter start and click Run.

3. Type regsvr32 C:/xxxx.ocx in the box that appears. (XXXX is the control name and C:/ is the directory)

4. Click OK and wait for the prompt of successful registration.

3.2 ActiveX control call

As a universal COM component, ActiveX can be called by different languages.

3.2. 1 called by VC++

Create MFC basic dialog application with VC6.0.

After completing the program wizard. Perform the following steps:

1. Execute Project-Add to Project-Components and Controls.

2. In the pop-up file browsing dialog box, find the newly registered control under the registered ActiveX control file directory, such as "OcxDemo control", and then click the "Insert" button to add the control to the control toolbar collection.

3. Drag the newly added OCX control on the control toolbar to the main window of the application.

After completing the above steps, you can use this control in VC like a normal control (right-click this control to view the "event" and "property" of this control, which is the "external name" when you write the control source code).

3.2.2 Call through C#

In fact, this is the key point, because the author is most optimistic about calling ActiveX technology across languages.

Create a new C#. NET Windows window program, then right-click Options on the toolbox panel, select COM components, and find your registered ActiveX control:

After confirmation, the OcxDemo control control is loaded into the toolbox. You can drag the control directly into the main window of C#. NET application, and then use the control like a normal control.

For example, the custom event of ActiveX control in this paper is triggered by mouse movement, so in application, as long as the mouse moves to the control, this custom event will be triggered and the abscissa of the current mouse position will be obtained.

4. Some final tips

1) When customizing the control, you can set the appearance of the control in the OnDraw () function of the control source code (that is, the appearance of the control when it is dragged into the application, which is generally a white box style with an ellipse by default).

2) When using ActiveX events in C#, all the data generated by the events are contained in the event variables, which can only be extracted by using a dot operator.

look into the future

There are many types of return values of control functions, which are very complicated. If you want to use them well, you need to learn those OLE data types well. Learn slowly when you need it later.