Using MFC to write ActiveX controls

Source: Internet
Author: User
Introduced:
It is convenient to use the wizards in VC to create ActiveX controls. This article is not going to tell you the principle of the ActiveX control, it will tell you step-by-step how to create a simple ActiveX control, which mainly introduces the event and property page programming, if you want to know the truth, I think you need to read some related books. This article is about MFC creating ActiveX controls, and if you want to learn ATL to create ActiveX controls, do not read this article.

Create control <?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

Using wizards to build ActiveX control Engineering Select New in file, and then select MFC ActiveX control Wizard Input project name Select all default settings click Next if you want to subclass, you can select the appropriate class from the ComboBox Click the End button to complete.  This produces the following code C**app-derived from COleControlModule. This class provides initialization initialization (InitInstance) and Destruction Code (EXITINSTANCE) for the control class. C**ctrl-derived from COleControl provides most of the functionality of the control, which is where you need to write most of the code c**proppage-c**proppage derived from COlePropertyPage. This class manipulates the control's property page primarily

The control is compiled and the. ocx file is generated. Then the VC comes with the test control container that will help us to test the control. Click Tool->activex Control Test Container. Click New Control in the container toolbar we've chosen the project we just built, like Smaple, where we'll find an ellipse. The following figure.



Events

When we use MFC's controls, we find that the controls provide some events, the simplest example being a button that provides a click event, which is a user-defined process when a user clicks on a button, which is the simplest control event. Since ActiveX is a control, you want to make it rich and natural to support events.

There are two types of events in an ActiveX control, one is the stock, which is a system-defined event, and custom is the user-defined event. Let's first look at how the stock event is handled.


The stock of events:

1. Click the ActiveX Events property page in Classwiard

2. Click Add Event ... Button

3. If we choose an existing stock event from Extanal name (we need to select the stock below, we cannot choose Custom), which is the system definition. We're here to choose Dblstock.

4. End

Then we create a stock event, which is the double click event. Let's test it out. Select Tool->activex Control Test Container and then double-click on the controls to see that a double-click message is printed below, which means that the control responds to our double click



The custom of the event:


Here we describe how to define user-defined events

We would like to complete the following functions, if the user mouse in the circle or ellipse within the click operation will trigger an event.

1. Click the ActiveX Events property page in Classwiard

2. Click Add Event ... Button

3. Fill in the exteranl name inside ClickIn

4. End


Now we have defined this event. The point is that we have to consider how to trigger this event. This is when we click the left mouse button in a circle or ellipse to trigger this event. Here we can think of adding a WM_LBUTTONDOWN message to the CSampleCtrl.

1. Select CSampleCtrl class in Classwizrd

2. Add Message Wm_lbuttondown

3. End


Add a member function to class CSampleCtrl bool Csamplectrl::incircle (cpoint& point)

The contents of the function are as follows:
CRect RC;
GetClientRect (RC);

Determine radii
Double A = (Rc.right-rc.left)/2;
Double b = (rc.bottom-rc.top)/2;

Determine X, y
Double x = point.x-(rc.left + rc.right)/2;
Double y = point.y-(rc.top + rc.bottom)/2;

Apply Ellipse formula
return ((x * x)/(A * a) + (Y * y)/(b * b) <= 1);

Then edit the Lbuttondown response function

void Ccirc3ctrl::onlbuttondblclk (UINT nflags, CPoint Point)
{
if (incircle (point))
{
FireClickIn (); Triggering events
}
COLECONTROL::ONLBUTTONDBLCLK (nflags, point);
}

Here we complete the ClickIn event writing and response process, let's test it.



We can add some paint operations to make the event more obvious, and change the code as follows:
static int i = 0;
if (incircle (point))
{
FireClickIn ();
i++;
CString num;
CBrush Brush;
cdc* PDC = GetDC ();
CRect RT;
GetClientRect (RT);
CRgn Rgn;
Rgn. CreateEllipticRgnIndirect (RT);
Num. Format ("%d", I);
Brush. CreateSolidBrush (RGB (0, 0, 255));
Pdc->fillrgn (&rgn, &brush);
Pdc->setbkmode (Transparent);
Pdc->textout ((Rt.left + rt.right)/2, (Rt.top + rt.bottom)/2, num);
}



So each click will be in the circle or the middle of the ellipse output hits the cumulative number of times. Here we have simply introduced the handling of events that are writing ActiveX controls using MFC, and let's look at the property page programming for ActiveX controls.

Property Pages:

We can use the controls provided by MFC to set some properties to change the control's representation, or to take a button for example, the button properties have visible this option, we can set this property to change whether the button is visible. Now let's take a look at the ActiveX control's property page programming.

Property pages are divided into two types, one is the system built property pages, such as background color, font, etc.; one is a user-defined property page. Let's first look at the system-built property page, where we'll set the background color.

Property Page's stock

1. Click Add Property in the automation in ClassWizard, then select BackColor and ForeColor from external name

2. Change the code in a CSampleCtrl. cpp file

Begin_proppageids (CSampleCtrl, 1)
Proppageid (CLSID_CColorPropPage)
End_proppageids (CSampleCtrl)
Change the OnDraw function in CSampleCtrl add code
CBrush Bkbrush (TranslateColor (GetBackColor ()));
Pdc->fillrect (Rcbounds, &bkbrush);
After you have placed it in the drawing ellipse, test the following



Property Page Custom:

1. Create a dialog resource (size 250x62 or 250x110 dialog units) or select Insert in the Create dialog box and select Idd_ole_proppage_small in the dialog box.

2. Then double-click the dialog box to create a new class named Cmyproperty, and the base class selects Colepropertypate.

3. Then place a CheckBox control on the dialog box, named Erase.

4. In the automaition in Classwiard, select Add Property (CSampleCtrl) external name fill erase, type select bool other default, implement select member variable.

5. Select the class Cmyproperty in ClassWizard, and then add the member variables for the checkbox. Variable named M_berase, type Bool,optinal property name to fill in the newly added user-defined attribute name erase.

6. Add two strings to the resource's string table one is the title of the new property page, the name of the new property page, where we set the value Ids_ppg_myproperty (options) and Ids_ppg_myproperty_ CAPTION (CAPTION) Of course this user can modify.

7. Change the Cmyproperty CPP file as follows:
BOOL Cmyproperty::cmypropertyfactory::updateregistry (bool bregister)
{
if (bregister)
Return AfxOleRegisterPropertyPageClass (AfxGetInstanceHandle (), M_clsid, Ids_ppg_myproperty);
Else
Return Afxoleunregisterclass (M_clsid, NULL);
}

Cmyproperty::cmyproperty (): COlePropertyPage (IDD, ids_ppg_myproperty_caption)
{
{{Afx_data_init (Cmyproperty)
M_berase = FALSE;
}}afx_data_init
}

8. Finally, modify the following code in the CSampleCtrl CPP file:

Begin_proppageids (CSampleCtrl, 2)
Proppageid (CLSID_CColorPropPage)
Proppageid (Cmyproperty::guid)
End_proppageids (CSampleCtrl)

Remember to change the count from 1 to 2 and add #include "myproperty.h"

9. Modify the following code in CSampleCtrl


Here we have basically completed our controls, as well as the programming of ActiveX control events and property pages. Enjoy it.












Author:simahao time:05.1.10



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.