the last sentence in it is more important, that is, OCX control developed by VC can be called in other languages, which well realizes the good recycling of functional components and can also be called across languages (for example, you can call OCX control developed by C++ with C#).
here's how to use VC++ to generate the "*.ocx" file you want 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" all the way until you click "finish". Finally, VC++ will automatically generate some files, which constitute the basic template of ActiveX. The main structure of the files is as follows:
Compile directly, and then a control registration file named "ocxDemo.ocx" will be generated under the Debug directory. Then, this control can be registered locally by using the "regsvr32" command, and then this control can be referenced to realize the corresponding functions when writing programs in this language or across languages.
1.2 ocx debugging method:
VC++ comes with an ActiveX control test container, which can be opened in three ways:
1. Click the "Debug" button. The following dialog box will appear:
then browse "c: program files Microsoft Visual StudioCommonToolsTSTCON32.EXE"
2. Start-Programs-Microsoft Visual C++6.-Microsoft Visual C++of the system. 6. tools-active control test container
3. Tools-ActiveX control test container in VC++development environment
You can call up the following programs by any of the above methods:
Right-click a blank area and insert a control. Then the following dialog box will pop up:
Select the specified control, and then click OK, the control will be loaded into this tool, and then you can see the related event response of this control through this tool, and so on.
2. Customize functions based on templates generated by VC++
All customized functions basically come from the "MFC ClassWizard" class wizard dialog box.
(shortcut key Ctrl+W or view-build class wizard)
add methods and properties to the control in the Automation tab.
add an event to the control in the ActiveX Events tab.
2.1 add control properties
switch to the "Automation" tab, and click "Add Property" on the right to open the dialog box:
External name: external name. Refers to the property name seen by external programs when this control is used, and it is only used when it is externally referenced.
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. This reminder function is triggered when this property is changed.
Implementation: implementation mode. Refers to three types of attributes: intrinsic type, member variable type and Get/Set method type. Intrinsic type refers to the inherent attributes given by the system, such as background color and title; Member variables are user-defined attributes; Get/Set method type may refer to variables that can only be obtained and changed through Get/Set method (this has not been studied).
2.2 add control method
in the Automation tab, click "Add Method" on the right to open the dialog box:
External name: external name of the method.
Internal name: method internal name.
Return type: return value type. In addition to basic data types such as shaping, there are many complex advanced data types.
Implementation: implementation mode. 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 a control event
switch to the "ActiveX Events" tab and click "Add Event" on the right to pop up a dialog box:
External name: external name of the event.
Internal name: the internal name of the event. There is a prefix "Fire" more than the external name.
Implementation: implementation mode. There are two kinds: intrinsic events and custom events. Inherent events are generally mouse movement, double click and other events, which are triggered by system messages; A user-defined event is a function that is completely defined by the user, but this function needs to be called by the user in the source file (to the user of the control, it is equivalent to the event being triggered where it is called, and the parameters passed in internally are the attached 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 attributes, methods and events to the control through the Class Wizard tool, VC++ will automatically generate code in the corresponding file, such as the mapping between internal method attributes and external method attributes, the establishment of messages, the declaration of messages, and so on. If users want to do in-depth research on citation, they need to be familiar with the structure of the program, know the function 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 VS25 and the following Visual Studio series. However, because VC6. is a classic development environment, and all the online C++ programming is basically based on VC6., it is necessary to learn it, so that you can read the online code and digest it.
2.4 generate ocx file and Debug
directly compile the project with user-defined code, and then generate an ocx file under the debug directory of the project, which is the registration file of this control.
the debugging tool of the control 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 "WM_MOUSEMOVE" message, and returns the x coordinate of the current mouse position).
run "ActiveX Control Test Container" and insert the current control. When the mouse moves over it, you can see that the MouseMove has generated an event.
at the same time, you can test the method of the control by "control"-"invoke method". The test method is that you enter parameters, and it returns the calculation result (take the user-defined method funHello as an example).
3. How to use the control
3.1 Register the control
There are many ways to install the p>ocx control. 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 the 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 a reminder to register successfully.
3.2 Call of ActiveX control
As a general COM component, ActiveX can be called by different languages.
3.2.1 Call through VC++
Use VC6. to establish a MFC basic dialog application
After completing the program wizard. Perform the following steps:
1. Perform Project-Add to Project-Components and Controls.
2. in the pop-up file browsing dialog box, find the control you just registered under the Registered ActiveX Controls file directory, such as "OcxDemo Control", and then click the "Insert" button to add this control to the control toolbar collection.
3. drag the newly added OCX control on the control toolbar into the application main window.
after completing the above steps, you can use this control in VC just like a normal control (right-click this control to view the "events" and "properties" of this control, which are 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 Windows window program in C#.NET with Visual Studio 25, and then on the toolbox panel, right-click "Select Item", select the COM component, and find your registered ActiveX control:
After confirmation, the OcxDemo Control will be loaded into the toolbox. You can drag this control directly to the main window of the C#.NET application, and then use this control just like a normal control.
for example, in the custom event of ActiveX control in this paper, it is triggered by mouse movement, so in the application, as long as the mouse moves over the control, this custom event will be triggered and the abscissa of the current mouse position will be obtained.
4. Some last 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 the style of a white box inscribed with an ellipse by default).
2) When using ActiveX Events in C#, the data generated by the events are all contained in the event variables, and can be taken out by using a dot operator.
5. Prospect
There are so many and complicated return value types of control functions. If you want to use them well, you need to learn those OLE data types well. Let's learn them slowly when we need them in the future.