Sun Xin VC Study Notes: ActiveX Controls

Source: Internet
Author: User

Sun Xin VC Study Notes: ActiveX Controls
Author: Hua Zi | input time: | CLICK: 313 times to print this article | Font: large, medium, and small
Basic concepts:

Containers and server programs

An application that can be embedded in or linked to a container application. Word is a container application. A server application is an application that can be started when an object is created and double-clicked. Excel is a server application. ActiveX controls cannot run independently. They must be embedded in container applications and run together with container applications.
 
---------------------------------------------------------------------------------
Compile an ActiveX Clock Control

1. Use MFC ActiveX contrlwizard to create a clock Project

2. There are three classes in the new project. The cclockapp derives from the colecontrolmodule,

It can be seen as an application class, and an instance of it represents the control program itself.
 
Colecontrol is derived from cwnd and is also a window class,
Cclockctrl is equivalent to the main window class of a single document program.
This class contains the ondraw function for repainting, and some message ing, including scheduling ing.
Dispatch maps scheduling ing is mainly provided by MFC to allow external applications to access the properties and methods of controls.
Event maps event ing. The control sends event notifications to the containers that contain the event maps.
 
The cclockproppage class is derived from the colepropertypage:
 
Colepropertypage
 
The colepropertypage class is used to display the properties of a custom control in a graphical interface, similar to a dialog box. is used to display the properties of a custom control, similar to a dialog box. It is a dialog box class, so Enum {IDD = idd_proppage_clock} associates it with a dialog box resource.
 
In the project, there are two global functions: stdapi dllregisterserver (void); write control information into the registry stdapi dllunregisterserver (void) to uninstall registration information.

3. There are also dclock and _ dclockevents icons similar to the small spoon on top of the three classes, they represent interfaces.

An interface is a protocol for communication between an external program and a control. It can be regarded as a set of functions. An external program accesses the properties and methods of a control through the methods provided by the interface. You can also regard the interface as an abstract base class. All the functions defined in the interface are pure virtual functions. These functions are implemented in the cclockctrl class, MFC uses the underlying encapsulation to make the cclockctrl class inherit the dclock interface. Therefore, the interface is actually called by a function in the cclockctrl class. Due to encapsulation, we cannot see the underlying communication details. If you are interested in this, you can read some COM programming materials.
 
4. If you compile the project later, a clock. ocx file will be generated, which is the ActiveX control file.

When using the control, you only need to pass the file to the other party. Note: ActiveX controls cannot be used independently and must be embedded in a window for continuous operation. When we added the control using VB, we found the control we just compiled. But how does VB know the position of the control we created? During compilation, the last line of "registering ActiveX Control..." in the output window indicates that the control will be registered during compilation. In fact, after compilation, VC will call regsvr32 to register the control and write the information to the Registry. When VB loads the ActiveX control, it will search for the relevant ActiveX control information from the registry. Note: ActiveX controls must be registered before use.

To uninstall the control, enter the following command in "run:
"The complete path name of the regsvr32/U control file" is actually called by the global function dllunregisterserver in the project to complete the uninstallation.
To register the control again, enter the following command in "run:
The full path name of the regsvr32 control file is called by calling the global function dllregisterserver in the project to complete registration.
 
5. the system time is output on the control below.

You can complete this function in the ondraw function.
 
In this way, a static time control can be created. to display the time in real time, we need to add two message response functions wm_create and wm_timer. to update the time per second, we first add wm_create message processing to the cclockctrl class, and set a timer in the Response Function oncreate (): settimer (, null ); next, add a wm_timer message response, and write invalidate () in ontimer to redraw the window. You can also call invalidatecontrol () to force the control to repaint itself.
 
6. In the VB test, we found that many other controls can modify the background color, foreground color, and font color of the control, but we still cannot.

ActiveX controls have four attributes:
Stock: the standard Attributes provided for each control, such as font or color.
Ambient: Environment attribute of the control-attributes of the placed container.
These attributes cannot be modified, but the control can use them to adjust its own attributes.
Extended: these are properties processed by the container, generally including the size and position on the screen.
Custom: The property added by the control developer.
 
7. in this case, in the VB test, our control can also modify the background color and foreground color of the control, but it does not work after the settings are complete, because we need to write the code in the ondraw function to complete these changes. To get standard properties of a control, you need to use some functions, such as colecontrol: getforecolor to get the foreground color, and colecontrol: getbackcolor to get the background color, however, note that they get the ole_color color and need to be converted to colorref through the translatecolor method.
 
8. the control usually has a property page. When you right-click the control, this property page is displayed to facilitate the setting of the control property. Our control already has a property page, implemented through the cclockproppage class, the displayed face is the content of the dialog box resource. The following is the modification control property page:
The property page can be seen in the window because it is in clockctl. proppageid (cclockppage: guid) is called between begin_proppageids (cclockctrl, 1) and end_proppageids (cclockctrl) in CPP. The GUID indicates a globally unique identifier, which is a 128-bit integer, uniquely identifies a component or interface. We can use the proppageid macro to add a property page.
 
Add color property page
Add the code proppageid (clsid_ccolorproppage) between begin_proppageids (cclockctrl, 1) and end_proppageids (cclockctrl) to add the property page.
 
The clsid_ccolorproppage attribute form is provided by the control itself. After adding the form, we can use it without any processing. The effect is as follows:
 
Note:
The number 2 in begin_proppageids (cclockctrl, 2) indicates how many property page codes are to be displayed,
If you add an attribute page number, you must add 1. Otherwise, unexpected errors may occur if no modification or modification is made.
 
9. Adding a custom attribute is also done through MFC classwizard, which is the same as adding a foreground color and a background color in step 1.

1) The attribute for setting the time interval is added below to control the time refresh frequency:
 
After the attribute is successfully added, the interval attribute is added to the _ dclock interface, and
Member variables m_interval and onintervalchanged () (this method is called after the external attribute is modified ).
And they are all in the scheduling ing:
 
2) Add the property handler code in cclockctrl: onintervalchanged ().
 
10. Set the added custom attributes in the attribute form.

Add an edit box to the resource in the dialog box and associate a variable with the edit box. Note that when we associate the variable m_updateinterval with the edit box, a property is also associated, in this way, we can associate controls with custom properties without adding code. The following code is generated in void cclockproppage: dodataexchange (cdataexchange * PDX:
 
In this way, we can set the time interval in the property page.
 
11. Add a method for the control:

Add a function to the control. For MFC classwizard --> Automation --> Add method class name, select cclockctrl and enter the function name. Then you can find the function in the cclockctrl class. After the method is successfully added, the hello method is added to the _ dclock interface, and the hello method is added to the cclockctrl class. Next, we can add the hello method to the cclockctrl class to add our own code.

12. Add a standard event for the control

Choose MFC classwizard --> ActiveX events ---> Add event. After an event is successfully added, an event click will be added to _ dclockevents. The dclockevents interface is the source interface, and the control will use this interface to send notification events. It is not the interface implemented by the control itself, this interface is implemented through containers.
 
13. Add a Custom Event: when the number of seconds is 0, a newminute event is triggered.

1) the process of adding a custom event is the same as that of adding a standard event. You can also right-click the event interface _ dclockevents and choose add event. The effect is the same, the add event dialog box is displayed. After the event is successfully added, a newminute event is added to _ dclockevents, and void firenewminute () is added to the cclockctrl class, that is, firenewminute () can be called inside the control () send event notifications to the container, and this function calls the void newminute () of the interface to send event notifications to the container.
 
2) Add the Code if (0 = time. getsecond () firenewminute () in the ondraw () method, so that a newminute event notification is sent to the container when the number of seconds is 0. Standard messages do not need to write code to send event notifications to the container. VC implements this process within the underlying code.

14. Save the custom control attributes permanently after modification. After you open the program, the attributes of the control are all saved with the original values. You need to add the following code px_short (ppx, "interval", m_interval, 1000) to void cclockctrl: dopropexchange (cpropexchange * ppx), and then oncreate () in the program () settimer (, null) in the method; change the code to settimer (1, m_interval, null );
 
15. When you modify the attributes of a custom control on the properties page, notify the container to make corresponding adjustments so that the container property list displays our modifications to the attributes in real time. Add the following code to void cclockctrl: onintervalchanged (): boundpropertychanged (0x1); // The attribute that notifies the container that the scheduling ID is 1 has changed
 
16. previously, we used to change the control properties when designing a container. If you want the clock control to stop running in the design mode, the time will beat in the user mode, you can use the ambientusermode () method inside the control to determine whether the current control is in the design or running state. Modify the code in void cclockctrl: ontimer (uint nidevent) as follows:
If (ambientusermode () invalidatecontrol ();

17. After writing the control, we can select the Win32 release method for compilation to generate the release ActiveX control. During development, it is usually compiled in Win32 debug mode. In this mode, development helps us to produce errors during development, such as illegal memory access errors. It can also help us Debug programs, trace the program to troubleshoot the error. However, the file generated in debug mode is large because it contains debugging information.

When we compile the program in the release mode after development, the VC compiler will optimize the code generation and execution speed, the executable programs or control files generated at the same time are relatively small.
-------------------------------------------------------------------

Compile a client in VC to call ActiveX Control:

1. Create a clocktest project based on the MFC dialog box.

2. Right-click and select "insert ActiveX Control". In the displayed dialog box, select the control we just created.

You can also add controls through the menu:
"Project"-> Add to project-> componets and controls
Select the registered control "registered ActiveX contrlos", find our own control, and then press insert.
 
When ActiveX control is inserted in this way, a class ccock is automatically generated in the project, and its base class is cwnd. This class is an encapsulation class that encapsulates some operations to Access ActiveX controls. At the same time, a clock control is added to the VC toolbox to directly drag a clock control to the form.

3. Use the second method to insert the control. In addition to manually inserting the control into the form, we can also dynamically generate a control through code.

1) Add a member variable cclocktestdlg: cclock m_clock;
2) the header file cclocktestdlg contains a clock. h
3) Next, you need to add the code for creating the control in the Click Event of a button:
4) during design, you can right-click to add event responses to the control.

 

 

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/yanghao58686763/archive/2008/02/06/2085196.aspx

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.