MFC's ActiveX Control learning

Source: Internet
Author: User
Tags textout

This article describes how ActiveX controls are applied and how they work, and readers can think of ActiveX controls as a minimal server application that cannot run independently and must be embedded in a container program to run with a container, like a video card in a computer host. Its own in the computer hardware system is unable to play a role must and embedded in the motherboard, working with the motherboard.

There are also differences between ActiveX controls and common Windows controls. First, the ActiveX control has its own properties and methods. An operation on an ActiveX control can be done by invoking the control's external method. Windows controls are objects inside windows, although they also have their own properties and methods but are not exposed to the caller. In the author's opinion, the ActiveX control is more open source, the caller is more flexible, you can design the type and function of the control according to your own needs. Control's properties, how the control triggers the container program, and so on, can be set by itself. Here, it seems easier to understand the ActiveX control as a dynamic link library.

The following is an introduction to the ActiveX plugin from the insertion of existing controls and the application of custom plug-ins:

First insert an already existing plug-in, here the author on the vc6.0 with a display date of the calendar plugin as an example:

(1) Create a dialog-based application named SHOWTIME1 that uses the default settings. (2) Select Project->add to Project->components and controls into the following interface:

(3) Select registered ActiveX Controls double click and enter the following interface to select:

(4) Select Insert to enter the next interface after the default selection can be

At this point we can see that we have added a more calendar control in the original control bar. Simply drag and drop to manipulate the controls we've added.

Switch to Class View we can see that there are two new classes on the basis of the three classes of the original dialog box:

These two new classes are classes that have a class with the ActiveX control we've added, and there are related functions and variables in the class that are associated with the control. Let's take a simple example to manipulate the newly added calendar control.

First we associate the previous variable with the newly added control: Right-button resource->class wizard->member variables-> select->add Variable under base class Cshowtime1dlg. Here we name the variable m_ccalendar1 the type of the variable is Ccalendar. Then add an edit box and a button on the original dialog resource base. The function is to display the current time in the edit box. First, add a response function to the button: Right button resource->class wizard->message maps-> Select Click (Base class Selection Cshowtime1dlg)->add Function->edit Code then add the codes to the button response function:

void Cshowtime1dlg::onbutton1 () {    //  Todo:add your control notification handler codehere 
   m_strtime1.format ("%d-%d-%d", M_ccalendar1.getyear (), M_ccalendar1.getmonth (), M _ccalendar1.getday ());    UpdateData (FALSE);}

Run the result diagram.

*************************************************************************************************************** *

(Creation of controls)

Learning from the Calendar control we probably learned how to insert a control, how to manipulate it after inserting it, the introduction of a new control, the class that is associated with the control, the variables in the class, and the properties and methods of the function or control.

Sometimes we need to create the controls ourselves, create the controls ourselves, and then reference them to other container programs. A typical ActiveX control has three characteristics: methods, properties, events. The following example describes the creation of an ActiveX control and the mechanism by which the container program invokes the control.

First open the vc6.0 Select File\new menu item on the Open dialog box, select the Project tab, select MFC ActiveX ControlWizard in the list project named Clock1 Click OK to enter MFC ActiveX ControlWizard Wizard First Step

There are four options on the interface: The first option is to ask the user about the number of controls that will be available in the project. The second is to ask the user if they want to run the license (we spend a lot of time developing the control and certainly don't want others to use it for free). Here you choose to generate a run-time license so that no one else will be able to use the control without permission. The third is to ask if a source file comment is generated. The last one is asking if you want to generate a help document. Here we have the second choice yes the other three items are the default system settings. Then directly finish into the engineering interface:

Through the engineering interface we can find that the project automatically generates three classes, where the Cclock1ctrl class is derived from the CWnd class is a window class equivalent to the main window class in a single document application provides a OnDraw function in this class, if the control needs to output graphics in the function to write code. In addition to the three classes think there are _dclock1 and _dclockevents their front are like a flat spoon of the same icon, the icon corresponding to the interface item. An interface is a protocol for a control to communicate with an external program. the external program accesses the properties and methods of the control through the interface.

This opens the OnDraw function we can find:

void Cclock1ctrl::ondraw (            CDCconstconst crect& rcinvalid) {    //  Todo:replace The following code with your own drawing code.    Pdc->FillRect (rcBounds, Cbrush::fromhandle (hbrush) getstockobject (White_brush));    PDC--Ellipse (rcBounds);}

There are already two functions in the function, namely, the rectangle and the ellipse are drawn. Because ActiveX controls cannot be run separately, they must be embedded in the container, and vc6.0 provides an ActiveX Control Test Container. We run the control program will pop up a shortcut menu, select the button with a right arrow on the menu, and then choose the ActiveX Control Test Container in the popup menu, that is, we choose the ActiveX Control test Container as a container for Clock1 controls, open the ActiveX Control Test container Application

Import a control into a container program by Edit\insert New control to select any control in the list box quickly and continuously by pressing the C,L,O key (uppercase), you can quickly lock to the Clock1 control we created.

Click OK to see the CLOCK1 controls we created are displayed:

In order for the control to display the system time we completed in the OnDraw function:

voidCclock1ctrl::ondraw (CDC* PDC,Constcrect& RcBounds,Constcrect&rcinvalid) {    //Todo:replace The following code with your own drawing code.Pdc->FillRect (RcBounds, Cbrush::fromhandle (hbrush) getstockobject (White_brush)); PDC-Ellipse (rcBounds); CTime Time=Ctime::getcurrenttime (); CString Str=time. Format ("%h:%m:%s"); PDC->textout (Ten,Ten, str);}

To get the clock going, we need the timer function to refresh the system time every second. Based on the window knowledge we should set the timer after the window creation is complete here Add the WM_CREATE message response function:

intcclock1ctrl::oncreate (lpcreatestruct lpcreatestruct) {if(Colecontrol::oncreate (lpcreatestruct) = =-1)        return-1; //Todo:add your specialized creation code hereSetTimer (1, +, NULL); return 0;}voidCclock1ctrl::ontimer (UINT nidevent) {//todo:add your message handler code here and/or call defaultInvalidate (); Colecontrol::ontimer (nidevent);}

The Invalidate () function can cause the window to redraw, and the window redraw calls the OnDraw () function to display the system time in real time. The embryonic form of this basic control comes out. But sometimes we want the controls we write to be powerful, giving the controls the properties that the user can arbitrarily change to set the properties of the control.

In VC + + development environment can be done through the class Wizard.

The specific process is to open the Class Wizard->automation->add property;

Click the external Name drop-down list box to see many properties of the list box these are the standard properties that MFC provides for ActiveX, where BackColor and ForeColor are background and foreground color properties, where we select BackColor:

The same way we add the ForeColor property back to the Class view of the control development environment and see the two properties you just added under the _dclock1 interface: BackColor and Foreclor.

In order to allow the added properties to be applied directly to the user, it is also necessary to add code in OnDraw ():

voidCclock1ctrl::ondraw (CDC* PDC,Constcrect& RcBounds,Constcrect&rcinvalid) {    //Todo:replace The following code with your own drawing code.CBrush Brush (TranslateColor (GetBackColor ())); PDC->fillrect (rcbounds,&brush); PDC-SetBkMode (TRANSPARENT); PDC-SetTextColor (TranslateColor (GetForeColor ()));//Pdc->fillrect (RcBounds, Cbrush::fromhandle (hbrush) getstockobject (White_brush));//Pdc->ellipse (rcBounds);CTime Time=Ctime::getcurrenttime (); CString Str=time. Format ("%h:%m:%s"); PDC->textout (Ten,Ten, str);}

At this point we insert the newly created control into a dialog program, drag the control to the window, and then right-click Properties to see:

In all we see the two properties that are set when the control is created: BackColor and Foreclor so that the user can set the two properties of the newly created control directly:

Control Clock1 background and foreground two properties are replaced by the user's own needs.

MFC's ActiveX Control learning

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.