Write an ActiveX control with MFC

xiaoxiao2021-03-06  42

Introduction:

use

VC

Wizard to create

ActiveX

Control is very convenient. This article is not ready to tell you

ActiveX control

The principle, it will tell you about how to build a simple

ActiveX

Control, which mainly introduces the program and the property page programming. If you want to know the truth, I think you need to read some related books. This article is telling

MFC

create

ActiveX

Control, if you want to learn

ATL

create

ActiveX control

Please don't look at this article.

Create control

Use the Wizard to establish the ActiveX Control Engineering Selecting New NEW, then select the name of the MFC ActiveX Control Wizard Enterprise Select all the default settings Click Next If you want a child, you can select the appropriate class Click End button from ComboBox. The following code is generated at this time.

C ** App - Delicate from ColecontrolModule. This class provides the initialization of the control class Initialization (InitInstance) and destroy code (EXITINSTANCE). C ** Ctrl - most of which is derived from the ColeControl providing controls, this class is what you need to write Part of the local c ** proppage - c ** Proppage is derived from ColePropertyPage. This class mainly manipulates the property page compile control of the control, and will generate .oCX files. At this time, the container of the test controls that come with the VC will help us test the control. Click Tool-> ActiveX Control Test Container. Click New Control in the toolbar in Container We choose the project we have established, such as Smake, we will find an ellipse in which it is painted. As shown below.

event

We will find that the control will provide some events when using the MFC control, the simplest example is the button to provide a Click event, that is, some user-defined processing processes are performed when the user clicks on the button. This is the simplest. Control event. Since ActiveX is a control, then if you want to enrich its functionality and you can't support your support.

There are two events in the ActiveX control, one is an event that Stock is a system-defined event, and a Custom is also a user-defined event. Let's take a look at how the STOCK event is handled.

At Events:

1. Click on the activex events property page in Classwiard

2. Click Add Event ... button

3. If we choose existing from EXTANAL NAME (of course we need to select STOCK below, can't choose Custom) is also a system definition. We are here choosing dblstock

4. End

At this time we created a stock event, that is, double-click the event. Let's test it. Select Tool-> ActiveX Control Test Container, and then double-click on the control, you will find that the double-click message will be printed, that is, the control responds to our double click.

Custom of incident:

Below we describe how to define user-defined events

We want to complete the following features, trigger an event if the user's mouse is done in the circle or ellipse.

1. Classwiaard in the ActiveX Events Properties page

2. Click Add Event ... button

3. Fill in Clickin in EXTERANL NAME

4. end

Now we define this event. The key is what we have to consider how to trigger this event. That is to trigger this event when we click the left mouse button in the circle or ellipse. Here we can think of adding a WM_LBUTTONDOWN message in CSampleCtrl.

1. OK in ClassWizRD Select CSAMPLECTRL class

2. Add message WM_LButtondown

3. End Add a member function BOOL CSample Csample Csample (CPoint & Point) to end the class csamplectrl

The function content is 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 elipse formula return ((x * x) / (a ​​* a) (y * y) / (b * b) <= 1); then edit the response function of lbuttondown

Void ccirc3ctrl :: ONLBUTTONDBLCLK (uint nflags, cpoint point) {if (incircole (point)) {FireClick (); // Trigger Event} ColeControl :: ONLBUTTONDBLCLK (NFLAGS, POINT);} Here we complete Click Incidents And the response process, let us test it.

We can add some drawing operations to make the event more obvious, change the code as follows: static int = 0; if (Incircle (Point) {FireClick (); 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);}

This time you have output a cumulative number of clicks in the middle or ellipse. Here we have simply introduced the processing of events that use the MFC to write ActiveX controls, let's take a look at the property page programming of the ActiveX control.

Properties page:

In the control provided by the MFC we can set some properties to change the performance of the control, or take the button for example, the button properties have a Visible option, we can change the button by setting this property. Let's take a look at the property page programming of the ActiveX control.

The attribute page is divided into two, one is the system built-in property page, such as background color, font, etc .; one is a user-defined attribute page. Let's first look at the realizable property page in the system, here let's set the background color.

Attribute page STOCK 1. Click Add Property in the Automation in ClassWizard and select BackColor and ForeColor2 from External Name. Change the code in the.cpp file of csamplectrl.

BEGIN_PROPPAGEIDS (CSampleCtrl, 1) PROPPAGEID (CLSID_CColorPropPage) END_PROPPAGEIDS (CSampleCtrl) Change CSampleCtrl in OnDraw function add code CBrush bkBrush (TranslateColor (GetBackColor ())); after the place to draw an ellipse; pdc-> FillRect (rcBounds, & bkBrush) Test is as follows

Attribute page Custom:

1. Create a dialog resource (Size 250x62 or 250x110 Dialog Units) or select Insert in the Confused Dialog and select IDD_OLE_PROPPAGE_SMALL in the dialog box.

2. Then double-click the dialog to create a new class, name CMYPROPERTY, and the base class selects ColePropertyPate.

3. Then place a CheckBox control on the dialog and name the ERASE.

4. Select Add Property (CSampleCtrl) External Name in Classwiard to fill out the ERASE, the type selects Bool other default, Implement Select MEMBER VARIABLE.

5. Select Class CMyProperty in ClassWizard and add a member variable for Checkbox. The variable name is m_berase, type bool, Optinal property name fills in the user-defined attribute name ERASE that has just been added.

6. Adding two string Table in the resource String Table is the title of the new attribute page, one is the name of the new attribute page, where we set the value is IDS_PPG_MYPROPERTY (OPTIONS) and IDS_PPG_MYPROPERTY_CAPTION (CAPTION) Of course, this user can modify itself.

7. The following documents cpp change CMyProperty: 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) Always hold count from 1 to 2 while adding #include "myproperty.h" 9. CSampleCtrl modify the following code void CSampleCtrl :: DoPropExchange (CPropExchange * pPX) {ExchangeVersion (pPX, MAKELONG (_wVerMinor, _wVerMajor)); COleControl :: DoPropExchange (pPX); PX_Bool (pPX, _T ( "Erase"), FALSE) ;} void CSampleCtrl :: OnEraseChanged () {InvalidateControl (); SetModifiedFlag ();} void CSampleCtrl :: OnDraw (CDC * pdc, const CRect & rcBounds, const CRect & rcInvalid) {pdc-> FillRect (rcBounds, CBrush :: FromHandle ( (HBRUSH) GetStockObject (WHITE_BRUSH))); CBrush bkBrush (TranslateColor (GetBackColor ())); pdc-> FillRect (rcBounds, & bkBrush); if (m_erase) {pdc-> MoveTo (rcBounds.left, (rcBounds.top RcBounds.Bottom / 2); PDC-> Lineto (RcBounds.right, (Rcboun) DS.TOP RCBOUNDS.BOTTOM) / 2);} PDC-> Ellipse (rcbounds);

It is basically completed here, and also shows the programming of the ActiveX control event and property page. Enjoy it. Author: Simahao Time: 05.1.10

转载请注明原文地址:https://www.9cbs.com/read-70763.html

New Post(0)