Sun Xin teaching video note (18) ActiveX Controls

Source: Internet
Author: User

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.

Dispatch maps scheduling ing. It mainly provides 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.

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 an excuse. All functions defined in the interface are pure virtual functions.

Regsvr32... registration control regsvr32/u... uninstall Control

Stdapi dllregisterserver (void) writes control information to the Registry
Stdapi dllunregisterserver (void) uninstall registration information.

Create a time control in
Void cclockctrl: ondraw (CDC * PDC, const crect & rcbounds,
Add the following code in const crect & rcinvalid:
Cbrush brush (translatecolor (getbackcolor ()));
PDC-> fillrect (rcbounds, & brush );
PDC-> setbkmode (transparent );
PDC-> settextcolor (translatecolor (getforecolor ()));
// Set properties for the control. You must add properties for the control in MFC classwizared. The preceding several // lines of code are useful.

Ctime time = ctime: getcurrenttime ();
Cstring STR = time. Format ("% H: % m: % s ");
PDC-> textout (0, 0, STR );
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.
Code:
Int cclockctrl: oncreate (maid)
{
If (colecontrol: oncreate (lpcreatestruct) =-1)
Return-1;

// Todo: add your dedicated creation code here

Settimer (1,1000, null );
Return 0;
}

Void cclockctrl: ontimer (uint nidevent)
{
// Todo: add the message processing program code and/or call the default value here
Invalidate (); // immediately causes the window to be repainted
// You can also use invalidatecontrol (); // force the window to be repainted, with the same effect
Colecontrol: ontimer (nidevent );
}
The background color, foreground color, and font color of the control to be modified.
Add in ondraw
Cbrush brush (translatecolor (getbackcolor ()));
PDC-> fillrect (rcbounds, & brush );
PDC-> setbkmode (transparent );
PDC-> settextcolor (translatecolor (getforecolor ()));

ActiveX Control's 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.

Make the control have more than one property page
First, find property pages in ** CTL. cpp. The Code is as follows:
Begin_proppageids (cclockctrl, 2)
Proppageid (cclockproppage: guid)
Proppageid (clsid_ccolorproppage)
End_proppageids (cclockctrl)
Note that the code in the begin_proppageids (cclockctrl, 2) attribute pages is displayed. If no modification or modification error occurs, unexpected errors may occur.

To add a standard attribute, right-click _ dclock, select Add attribute, and select stock. For example, select the background color and foreground color.
The following code is generated under clock. odl:
Dispinterface _ dclock
{
Properties:
[ID (dispid_backcolor), helpstring ("attribute backcolor")] ole_color backcolor;
[ID (dispid_forecolor), helpstring ("foreforecolor")] ole_color forecolor;
Methods:
[ID (dispid_aboutbox)] void aboutbox ();
};

Next, add custom attributes. The method is the same as above. You only need to select "member variable" or "Get/put"
The member variables m_interval and onintervalchanged are automatically generated.
Next, add the Code:
Void cclockctrl: onintervalchanged (void)
{
Afx_manage_state (afxgetstaticmodulestate ());

// Todo: add the property handler code here
If (m_interval <0 | m_interval> 6000)
{
M_interval = 2000;
}
Killtimer (1 );
Settimer (1, m_interval/1000*1000, null );

Setmodifiedflag ();
}
Test: run the ActiveX tester and select control ---> invoke methods to modify m_interval.

Add the member variable MFC classwizard --> member variables --> Add member variable --> to the edit box.
Optional property name:
Select the external name of the custom property, so that you can associate the control with the custom property without adding code.
The following code is generated in void cclockproppage: dodataexchange (cdataexchange * PDX:
Ddp_text (PDX, id_edit_interval, m_updateinterval, _ T ("interval "));
Ddx_text (PDX, id_edit_interval, m_updateinterval );

Under. net2003, I can't find the "Optional property name:", so the edit box on the my properties page is invalid. I can only select control ---> invoke methods to modify it.

Add a function for the control. For example, MFC classwizard --> member variables --> Add Method
Select cclockctrl for Class Name
Enter the function name and you can find it in the cclockctrl class.

Choose MFC classwizard --> ActiveX events ---> Add event
An event 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 the container.

To save custom control properties, you must
Void cclockctrl: dopropexchange (cpropexchange * ppx) Add the following code:
Px_short (ppx, "interval", m_interval, 1000 );
Then modify the code in the program:
Px_short (ppx, "interval", m_interval, 1000 );
If you want to display custom control properties in the container property list in real time,
Add the following code to void cclockctrl: onintervalchanged:
Boundpropertychanged (0x1); // The scheduling code is 1.
If you want the clock control to stop running in design mode and run in user mode, you can
Modify the code in void cclockctrl: ontimer (uint nidevent) as follows:
If (ambientusermode () // query environment attributes
Invalidatecontrol ();

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.