Windows Programming _ sun Xin C ++ lesson18 ActiveX Control

Source: Internet
Author: User

Windows Programming _ sun Xin C ++ lesson18 ActiveX Control

Solution to environment problems in vc6.0 win7:
Note that the environment in this course is very important. errors may occur in the Windows 6.0 system. The error and solution are as follows:
(1) You cannot register a control and try to register it as an administrator. For example, run cmd.exe with the administrator ID, and then type regsvr *. ocx or
The administrator ID to run a few registration of auxiliary tools, this example I use regctrls.exe tool, for the http://wj.codefans.net/x/6/201011/RegCtrls.rar
(2) The Control Generated in VC 6.0 cannot be added to the project, so the corresponding encapsulation class cannot be generated. In the solution, right-click the control and add it to the dialog box,
Then, use the Appwizard Wizard to add member variables, and the system will generate a cclock file for you.

 

Highlights of this section:
1. Differences between container programs and server programs
2. Create a Clock Control
3. Use of the clock control
//************************************** ************************
1. containers and server programs
A container application is an application that can be embedded or linked to objects, and a 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.
When you insert an Excel table in Word, Excel is the server program of the Excel table, and word is the container application that holds the Excel table.
When word is embedded in Excel, the roles of the two are changed.
ActiveX control can be considered as a very small server program and cannot run independently. It must be embedded in a container and run together with the container application.
A control file can contain multiple controls.
2. Create a clock control clock
(1) classes automatically generated by the MFC framework: the cclockctrl class generated by the clock control generated by the MFC is derived from the colecontrol. The colecontrol class is derived from the cwnd class and supports the control events, attributes, and methods.
The colepropertypage class generated by the clock control generated by MFC displays the properties of the custom control, similar to a dialog box.
(2) interface concept: an interface is a protocol for communication between an external program and a control. An interface is a set of functions. An external program accesses the properties and methods of a control through methods exposed by the interface.
The interface can be seen as an abstract base class. All these functions defined by the interface are pure virtual functions. MFC allows cclockctrl to inherit from dclock. Therefore, the function called by the interface actually calls the function actually implemented inside cclockctrl.
(3) control test program: ActiveX control test container of vc6.0 control test program. Other programs such as VB can also be used for testing.
Registration control: Write the registration information and path information of the control to the Registry.
You can run the regsvr32 command in the registration and anti-registration controls. Regsvr32 *. ocx (write complete path information) registration control; regsvr32/u *. ocx (write complete path information) uninstall control.
3. Create a clock control
(1) Add predefined system attributes
On the automation properties page of Appwizard, select Add property to add attributes. The attributes include the following, as shown in:

(2) Add foreground and background color attributes
This is the property prepared by MFC. After the settings are completed, the control is not changed because the control is not implemented by encoding.
First, obtain the color set by the Control. Use the colecontrol: getbackcolor and colecontrol: getforecolor functions,
Then, use colecontrol: translatecolor to convert ole_color, the return value type of the preceding function, to colorref.
Then, add the color property page by modifying the following code segment in the implementation file of the cclockctrl class:
// Property pages
// Todo: add more property pages as needed. Remember to increase the Count!
Begin_proppageids (cclockctrl, 2)
Proppageid (cclockproppage: guid)
Proppageid (clsid_ccolorproppage) // Add color property page note change count
End_proppageids (cclockctrl)
(3) add custom attribute time display interval attribute
On the automation properties page of Appwizard, select Add property to add a property. The following types are described:
Stock: Standard attributes prepared by MFC
Select member variable: Add a member variable and the onintervalchanged notification function.
Select get/set methods. Two setinterval and getinterval functions are added.
The generated member variable is: Short m_interval;
The generated function is afx_msg void onintervalchanged ();
You can set the attribute page to idd_proppage_clock and add member variables to the edit box. The variable name is m_updateinterval and the type is short;
Optional attribute name: interval. this is a feature of MFC. When you associate a member variable in the edit box, you can select an attribute for association. It can be either a standard attribute or a custom attribute, and the associated attribute name is an external name.
The code associated with the edit box and attributes and variables is automatically generated by MFC as follows:
Void cclockproppage: dodataexchange (cdataexchange * PDX)
{
// {Afx_data_map (cclockproppage)
Ddp_text (PDX, idc_edit_interval, m_updateinterval, _ T ("interval "));
Ddx_text (PDX, idc_edit_interval, m_updateinterval );
//} Afx_data_map
Ddp_postprocessing (PDX );
}
(4) add methods
On the automation properties page of Appwizard, select Add method to add the property.
Internal name program internal implementation, external is in the external method, can be different.
Add Hello method:
Void cclockctrl: Hello ()
{
// Todo: add your dispatch handler code here
MessageBox ("Hello world! ");
}
(5) add events
On the ActiveX events attribute page of Appwizard, select Add event to add an event. :
When the click standard event (Alert CK) is added, MFC automatically implements Event Notifications at the underlying layer.
Coclass clock
{
[Default] dispinterface _ dclock ;//
[Default, source] dispinterface _ dclockevents; // The source interface control uses this interface to send notification events instead of the Interface Control Implemented by the control.
The _ dclockevents interface is implemented by the container. The container Implementation defined by the control does not matter if the control implementation Definition. The interface definition does not matter, as long as both parties follow the unified interface for communication.
};
To add a Custom Event (custom) newminute, you must explicitly call the internal firenewminute to send a notification.
4. control development issues
(1) Control Data Persistence: the property value set by the user should be saved and the previous value can be reproduced at the next loading.
Functions starting with PX _ enable data persistence. They are called by the Framework to read or access control attributes. For more information, see ActiveX Controls: serializing topic in msdn .)
The data persistence code in this section (you can also manually add it by Appwizard) is as follows:
Void cclockctrl: dopropexchange (cpropexchange * ppx)
{
Exchangeversion (ppx, makelong (_ wverminor, _ wvermajor ));
Colecontrol: dopropexchange (ppx );

// Todo: Call PX _ functions for each persistent custom property.
Px_short (ppx, "interval", m_interval, 1000); // persistence of the event interval value
}
(2) synchronization of Custom Attributes and attribute panels in containers (for example, panels when controls are added during VB development)
When the custom attributes are changed, the container is not notified to make corresponding adjustments. Notify the container when changing the attribute value,
Use the colecontrol: boundpropertychanged function. When this function is called, The dispid parameter is the scheduling ID and typedef long dispid;
See the scheduling Id set in the definition of the first attribute in the program. The scheduling ID is as follows in this program:
Dispidinterval = 1l,
Dispidhello = 2L,
Code Used in the program: boundpropertychanged (0x1); // The container property is changed.
(3) The design mode and running mode allow the control to display changes
In this example, the time of the clock control is moving.
The container knows whether it is in the design or runtime, and uses the environment attribute to obtain whether the control is currently in the design or runtime,
This function is completed by the colecontrol: ambientusermode function. More environment attributes can be operated through the ambient attribute function of the colecontrol class.
5. Use of controls
(1) After the control is designed, it can be applied in VB or VC programs. Create a dialog box program in VC. When you right-click to add a control, the VC system does not add a class encapsulated by the control, when you need to add components and controls from the menu, or add member variables after adding controls, the system prompts to generate corresponding classes.
(2) Test the classes generated by clock. You can test the properties, methods, and events of the control by creating the clocktest dialog box project.
(3) Respond to dynamically generated clock control events
// Note that the event ing function is prototype:
Event ing is completed by the on_event (theclass, ID, dispid, pfnhandler, vtsparams) function. Note that dispid indicates the dispatch ID of the event triggered by the control,
If the parameter of the dynamically created control is incorrect, it does not correctly respond to the event.
The code for event ing in the clocktest program is:
Begin_eventsink_map (cclocktestdlg, cdialog)
// {Afx_eventsink_map (cclocktestdlg)
On_event (cclocktestdlg, idc_clockctrl,-600/* click */, onclickclockctrl, vts_none)
On_event (cclocktestdlg, idc_clockctrl, 1/* newminute */, onnewminuteclockctrl, vts_none)
//} Afx_eventsink_map
On_event (cclocktestdlg, idc_clockdynamic, 1/* newminute */, onnewminutedynamicctr, vts_none)
End_eventsink_map ()
// Here, the click event is a standard system event. The system predefines the assigned ID, while the newminute event is the ID generated when the clock control is generated,
You can view the clock source file. The source file is in the form of [ID (1)] void newminute (), indicating that the ID of the newminute event is 1 above.
To dynamically respond to control events, first add the resource ID idc_clockdynamic, then add event ing to eventsink_map (), and write the Event Response Function Code. For more information about the code, see section 6th of this article.
6. Main Code and demo results
Only the main code is listed here, and some code generated by the framework is not listed here.
To view the complete code, the clocktest file of the entire Clock Control and Its Test Program is:

(1) write the main code and test results of the clock control
//************************************** **************************************** ******

// Clockctl. cppvoid cclockctrl: ondraw (CDC * PDC, const crect & rcbounds, const crect & rcinvalid) {// todo: Replace the following code with your own drawing code. cbrush brush (translatecolor (getbackcolor (); PDC-> fillrect (rcinvalid, & brush); PDC-> settextcolor (translatecolor (getforecolor ())); PDC-> setbkmode (transparent); ctime time = ctime: getcurrenttime (); If (0 = time. getsecond () firenewminute (); // initiate Custom Event display call newminute () function implemented by the container cstring strtime = time. format ("% H: % m: % s"); PDC-> textout (0, strtime );} //////////////////////////////////////// /// // cclockctrl:: dopropexchange-persistence supportvoid cclockctrl: dopropexchange (cpropexchange * ppx) {exchangeversion (ppx, makelong (_ wverminor, _ blank); colecontrol: dopropexchange (ppx ); // todo: Call PX _ Functions for E Ach persistent custom property. px_short (ppx, "interval", m_interval, 1000 ); // persistent storage of the event interval value }/////////////////////////////// //////////////////////////////////////// /////// cclockctrl message handlersvoid cclockctrl:: ontimer (uint nidevent) {// todo: add your message handler code here and/or call default // determine whether the current control is in the running mode or the design mode if (ambientusermode ()) invalidate (); // method 1 // invalidatecontrol (); // colecontrol: inv Alidatecontrol () method 2 colecontrol: ontimer (nidevent);} int cclockctrl: oncreate (maid) {If (colecontrol: oncreate (lpcreatestruct) =-1) return-1; // todo: add your specialized creation code heresettimer (1, m_interval, 0); // change to the time interval variable return 0 ;} // set the time display interval. In fact, it is the interval at which a time change is displayed. Void cclockctrl: onintervalchanged () {// todo: add notification handler code if (m_interval <0 | m_interval> 60 00) m_interval = 1000; elsem_interval = m_interval/1000*1000; // adjust the user input to take the integer number of seconds killtimer (1); settimer (1, m_interval, 0 ); boundpropertychanged (0x1); // notification that the container property has been changed setmodifiedflag (); // set the modification tag} void cclockctrl: Hello () {// todo: add your dispatch handler code here MessageBox ("Hello world! ");}

//************************************** **************************************** ******
The test results in tstcon32.exe are as follows:

 

 

(2) The main code of clocktest is as follows. Note that the clock control contains encapsulation files.
//************************************** **************************************** ******

Events (events, cdialog) // {events (cclocktestdlg) on_event (events, idc_clockctrl,-600/* click */, onclickclockctrl, vts_none) on_event (cclocktestdlg, idc_clockctrl, 1/* newminute */, onnewminuteclockctrl, vts_none) //} commit (cclocktestdlg, idc_clockdynamic, 1/* newminute */, commit, vts_none) end_eventsink_map () // dynamically create the control void CC Locktestdlg: onbtncreate () {// todo: add your control notification handler code here // the create function of the control allows you to view the definition of the class m_ctlclock.create ("Clock", ws_child | ws_visible, crect (255,), this, idc_clockdynamic); m_ctlclock.setbackcolor (RGB (,); m_ctlclock.setforecolor (RGB (, 0 )); // set the background color and foreground color m_ctlclock.hello (); // call its method} // event response void cclocktestdlg: onclickclockctrl () {// todo: add your control notification handl Er code heremessagebox ("control clicked! ") ;}// Dynamically create the Event Response of the control // note that the event ing function prototype here is: // on_event (theclass, ID, dispid, pfnhandler, vtsparams) // dispid indicates the dispatch ID of the control trigger event. If the parameter of the dynamically created control is incorrect, it does not correctly respond to the event. // to dynamically respond to the control event, add the resource ID idc_clockdynamic // and then eventsink_map () add the event ing Code in the following line and add the Event Response Function onnewminutedynamicctr. // on_event (cclocktestdlg, idc_clockdynamic, 10/* newminute */, plaintext, vts_none) void cclocktestdlg: onnewminuteclockctrl () {// todo: add your Control notification handler code heremessagebox ("new minute on static control! ");} Void cclocktestdlg: onnewminutedynamicctr () {MessageBox (" new minute on dynamic control! ");}

//************************************** **************************************** ******
Shows the program running test results:


//************************************** **************************************** ******
Summary:
This section describes how to write a clock control. First, we introduce the concepts of container programs and server programs. This is a basic concept. We understand this concept to facilitate the design of better controls; secondly, it introduces some basic concepts in the control, such as interfaces, and introduces how to use Appwizard to add attributes, methods, and events to the control. These three are the three key elements of a control. Finally, the problems and solutions of some controls are introduced and the clock control is tested.
This section describes the complete process of compiling a control under MFC, which is helpful for further learning ActiveX control programming.

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.