MFC-based ActiveX Control development (RPM)

Source: Internet
Author: User

ActiveX controls are reusable software components based on Component Object Model (COM) and are widely used in desktop and Web applications. In the VC under the development of the ActiveX control can be divided into three kinds, one is directly using the COM API to develop, this is obviously very troublesome, the programmer requirements are very high, so generally not considered; one is based on the traditional MFC, The basic functionality of COM is encapsulated in several MFC C + + classes in an object-oriented way, and developers get COM support by inheriting these classes. MFC for the vast number of VC programmers familiar, easy to learn, but the disadvantage is that the MFC package is more, so the control developed with MFC relatively large, so it is more suitable for the development of desktop Activexx control, especially with GUI interface control. The third is based on ATL, ATL can be said to be specifically oriented to COM development of a framework, using C + + template technology, at runtime do not need to rely on similar MFC programs required by the large code module, more suitable for web application development.

This article introduces the second approach, that is, the application of MFC for desktop visual control development method steps, the development environment is based on VC2005.

1. Creating a control project

After opening VC2005, we will create a project, select Visual C++-mfc on the left side of the new project page, select the MFC ActiveX control on the right, fill in the solution and project name, for example, here my project name is Activexdemo1, The solution name is Activexdemo.

Then go to the Control Wizard page, there is a run-time license on the second page of the wizard, selecting this will generate a license file at the same time as the control is generated, and other users must attach this license at the same time when using this control, which we remain in the default state, not selected.

The next page is about the naming of each part of the project, which can be customized as needed, and is not modified by default.

The next page is the extension of the selection control based on which control and some of the basic properties of the control. If the new control is based on a specific control, select the name of the control you want to inherit under the control you created, or leave none. The additional features below are selected according to the actual needs, and you can place the mouse over the options, and the description of the feature will automatically appear in the Dynamic Small prompt information window. When you're finished clicking Finish, the wizard builds the new project based on your selection.

into the development environment, we can look at the Class View first.

The Cactivexdemo1app is the main program module of our control, which defines the functions of registration (DllRegisterServer), deletion (DllUnregisterServer) of the control, and generally does not move, If necessary, we can define our own initialization and termination operation code in the InitInstance and ExitInstance, which is usually the initialization and destruction of some resources.

Caxtivexdemo1ctrl is a control class, and the control function we want to do is basically implemented in this class.

It is necessary to mention that the OnDraw function of the parent class is overridden in this class, as in the following two lines of code:

Pdc->fillrect (RcBounds, Cbrush::fromhandle (hbrush) getstockobject (White_brush));

Pdc->ellipse (RcBounds);

That is, an ellipse is drawn on the control, and the actual control development can be modified to override this function to draw the control interface.

Caxtivexdemo1proppage is a property page class that implements a dialog box that sets the properties of a control at development time.

Activexdemo1lib is a library node that provides the client program with properties, methods, and interfaces for events that might respond, which are used when adding controls to these functions.

The Cactivexdemo1app is the main program module of our control, which defines the functions of registration (DllRegisterServer), deletion (DllUnregisterServer) of the control, and generally does not move, If necessary, we can define our own initialization and termination operation code in the InitInstance and ExitInstance, which is usually the initialization and destruction of some resources.

Caxtivexdemo1ctrl is a control class, and the control function we want to do is basically implemented in this class.

It is necessary to mention that the OnDraw function of the parent class is overridden in this class, as in the following two lines of code:

Pdc->fillrect (RcBounds, Cbrush::fromhandle (hbrush) getstockobject (White_brush));

Pdc->ellipse (RcBounds);

That is, an ellipse is drawn on the control, and the actual control development can be modified to override this function to draw the control interface.

Caxtivexdemo1proppage is a property page class that implements a dialog box that sets the properties of a control at development time.

Activexdemo1lib is a library node that provides the client program with properties, methods, and interfaces for events that might respond, which are used when adding controls to these functions.

Set project properties set configuration type to static library (. Lib)

2. Build and test the control

OK, now we can build this project first, of course, until now we just use the system automatically generated a control project, what function is not, just an empty frame.

After a few seconds, the project should be built smoothly. The control is also automatically registered to the system. So how do you test this control? Of course, you can build a new project, such as a dialog box program, in the dialog Resource Editing window, right side of the toolbox in the right-click menu, click the option.

From the COM component in the Pop-up window, locate the control that we just generated, tick check.

This control is then present in the Toolbox.

You can then use this control by dragging the mouse over the dialog box.

In addition to this approach, VS also provides a convenient control testing tool. There is an ActiveX Control Test container under the tools in the VisualStudio menu.

Click the New Control button in the toolbar.

In the Insert Control dialog box, locate and select our control.

You can then test the various functions of the control in this container.

3. Events

The ActiveX control uses events to notify the container control that something has happened. Common examples of events include clicking a control, using the keyboard to enter data, and control state changes. When these actions occur, the control raises an event to alert the container.

MFC supports two types of events: Common and Custom. Common events are events that are handled automatically by the COleControl class. A custom event enables a control to notify a container when a specific action for that control occurs. A control's internal state changes or a window message is received that belongs to such an event.

Common events

Common events are automatically raised by the COleControl class. COleControl contains predefined member functions that raise events caused by common operations. Some common actions implemented by COleControl include clicking and double-clicking controls, keyboard events, and mouse button state changes.

The action to add a common event is to right-click the ActiveX control class in Class View, such as Caxtivexdemo1ctrl in this case. In the menu, choose Add Event to open the Add Event Wizard.

In the Add Event Wizard, in the event name, select Click, which is the mouse click event to add to the control. Then select the third node under the Library node in Class View, which is dactivexdemo1events. You can see the event we just added below.

And then generate a new control program, let's take a look at testing this new event.

Open the above mentioned ActiveX Control Test Container, add this control, with the mouse click on the control, will be in the message bar below the program to see Activexdemo1 control:click such a message, this is the mouse corresponding event we added in.

What does it look like when using a control in a development environment? Okay, as mentioned above. Create a dialog box item and place the control on the dialog box. With this control selected, the Click event is in the control event in the Properties window, and if you need a corresponding mouse click event in the control, add the CLICKACTIVEXDEMO1CTRL1, and the event response function adds the functionality you need.

For example, like this:

void Ctestmfcdlg::clickactivexdemo1ctrl1 ()

{

TODO: Add Message Handler code here

MessageBox (_t ("Hi."));

}

Compile and run the test program, click on the control location will pop up with the hi. Words of the MessageBox.

Custom events

The difference between custom events and common events is that custom events are not automatically raised by the COleControl class. A custom event identifies an action identified by the control developer as an event.

The action to add a common event is to right-click the ActiveX control class in Class View, such as Caxtivexdemo1ctrl in this case. In the menu, choose Add Event to open the Add Event Wizard. Define an event called MyEvent, which can take parameters, such as the parameter msg we add a BSTR to.

Then, go back to Class View, this time select the third node under the library node, that is, _dactivexdemo1events, and you will see the newly added event below.

The control class in Class View is checked again, Cactivexdemo1ctrl, and a MyEvent function appears below.

Double-click this myevent to see the definition code as follows:

void MyEvent (BSTR msg)

{

FireEvent (Eventidmyevent, Event_param (VTS_PI1), msg);

}

The MyEvent (BSTR msg) function here is used to trigger the MyEvent event, which means that the MyEvent (BSTR msg) is called when a custom myevent in the control needs to be set. Here's an example to see what's going on.

For example, we want the user to double-click the mouse button to trigger this event, so to do. Select the control class in Class View, which is Cactivexdemo1ctrl, and then find WM_LBUTTONDBCLK in the message window and add the handler for the message.

In the message handler function, modify the following:

void Cactivexdemo1ctrl::onlbuttondblclk (UINT nflags, CPoint Point)

{

TODO: Add Message Handler code and/or call default values here

MyEvent (_t ("HI, MyEvent."));

COLECONTROL::ONLBUTTONDBLCLK (nflags, point);

}

This will trigger our custom MyEvent event every time the user double-clicks the control.

Then look at the test results.

Open the ActiveX Control Test Container, add the control, and then double-click the control to see what appears in the message box?

Two events occur at the same time because the double-click nature is two clicks.

Activexdemo1 control:myevent {msg=72}

Activexdemo1 Control:click

When you open a test project, there is one more MyEvent event in the control's control event.

Add a handler for this event MYEVENTACTIVEXDEMO1CTRL1 (LPCTSTR msg), MSG is the MSG parameter when we define the event, in the definition above we pass a "HI, MyEvent." A string message. Now let's see if it looks like this. In the MYEVENTACTIVEXDEMO1CTRL1 function we display a messagebox and print out the MSG parameters.

void Ctestmfcdlg::myeventactivexdemo1ctrl1 (LPCTSTR msg)

{

TODO: Add Message Handler code here

MessageBox (msg);

}

Also note that you want to comment out the handler function for the Click event defined in the Clickactivexdemo1ctrl1 event, and then double-click the control after compiling the run program to pop up the Hi, myevent. dialog box. That is, because the double-click action triggers the MyEvent event that we define, we call MyEvent's handler function.

4. Methods

A method is a function that the control opens to the user, similar to a C + + class function. There are two types of control methods, one is a common method, and the implementation is provided by the parent class COleControl. A custom method is defined by the developer, which provides the user with a customized feature implementation.

Common methods

COleControl supports two common methods: DoClick and Refresh. Refresh is called by the user of the control to immediately update the appearance of the control, while calling DoClick is the Click event that is used to raise the control.

The action of adding a common method is to open the Library node in Class View, in this case the Activexdemo1lib node. Select the second node, the _dactivexdemo1 in this example, and select Add method in the right-click menu to open the Add Method Wizard.

In the method name, select the common method that you want to add. Like DoClick.

Then select _dactivexdemo1 in Class View and you'll see the common method we just added below. At the same time, you can see here there is a AboutBox method, which is automatically added to our system, the function is to display a about window, this window can be found in the project's Resource View and dialog under the editor.

Also, verify the newly added method. Or, open the ActiveX Control Test Container, add the control, and then click the Red box on the toolbar, which is called the method.

In the call method, select the DoClick method that we just added to the window's method name and click Invoke. Remember what this doclick was for? , yes, it is to raise the click event, we have added the click of this common event when the event is described above, so now call DoClick is to throw the Click event we added above, The Activexdemo1 Control:click appears in the message box in the main window of the Test container.

So what's the effect in the development environment?

Well, go back to that test project. We first add a button to the form, called DoClick, and so on, it will be clicked to call the DoClick method.

Then for the convenience of operation, we first add a bound object variable to the control you just added to the dialog box. Right-click the control to select Add variable.

Add a control variable bound to this control in the Add Member Variable Wizard, for example, called M_activexdemo.

Then, double-click the button that you just added to the form, the mouse click event function that adds the button, and add a line of code, called the DoClick method of the control.

void Ctestmfcdlg::onbnclickedbutton1 ()

{

TODO: Add control notification Handler code here

M_activexdemo. DoClick ();

}

Finally, compile and run the test program, click the DoClick button, the result pops up the Hi. dialog box. This means that we raised the mouse click event of the control by executing the DoClick method of the control object, so the event handler function Clickactivexdemo1ctrl1 we added in the section on common events above was executed.

Custom Methods

The difference between custom methods and common methods is that custom methods are not implemented by COleControl. You must provide an implementation for each custom method that you add to the control.

The action to add a custom method is to open the Library node in Class View, in this case the Activexdemo1lib node. Select the second node, the _dactivexdemo1 in this example, and select Add method in the right-click menu to open the Add Method Wizard.

In the Add Method Wizard, add the name, return value, and parameters of the method that you want to customize.

Note that there is an internal name, the default is the same as the above method name, but can also be modified to a different name, the internal name is the function name of the method within the control class, for example, here our method named Mythod, the second node under the Library interface in Class View is selected, The MyMethod method that you just defined appears below.

With the control class Cactivexdemo1ctrl selected, we see that the method name defined in this class is the internal name Mymethodinner specified above.

The specific function of the method is implemented in this intrinsic function. For example, we define Mymethodinner as follows:

void Cactivexdemo1ctrl::mymethodinner (LPCTSTR msg)

{

Afx_manage_state (AfxGetStaticModuleState ());

TODO: Add the Dispatch handler code here

MessageBox (msg);

}

This means that a message dialog box pops up, and the message content is the string passed in the argument.

Then to test the effect, first or the ActiveX Control Test Container, open the Call Method window, in the method name will see we set a MyMethod, select, but regret will see in the parameter area we define the MSG parameter, select, and then fill in the parameter values below the value you want to pass to the parameter, such as Hello, and then click on the set value, the last click on the call, Mymethodinner will be executed, that is, pop up the Hello message window.

Then look at the effects of using control development.

Back to the test project, remember that DoClick button, or use this, but a little bit of a problem, because there is no custom method to bind the control variable for this control, so the control class definition generated when binding does not have this method, it cannot be tested. For the sake of convenience, we simply create a new project, add the control as mentioned earlier, bind the control variable, and write the following code in the DoClick click event:

void Ct1dlg::onbnclickedbutton1 ()

{

TODO: Add control notification Handler code here

M_activexdemo. MyMethod (_t ("This is MyMethod."));

}

Compile the execution, click the DoClick button, you will see the that is MyMethod message box.

5. Properties

property is a data member that is exposed to all containers in an ActiveX control. Similar to events and methods, it is also divided into common properties and custom attributes.

Common Properties

Common properties have been implemented by the COleControl class. The COleControl class contains predefined member functions that support the common properties of a control. Some common properties include the caption of the control, as well as the foreground and background colors.

The action to add a common property is to open the Library node in Class View, in this case the Activexdemo1lib node. Select the second node, the _dactivexdemo1 in this example, and select Add properties in the right-click menu to open the Add Property Wizard. Check common under the implementation type, and select the attributes you want to add under the property name, for example, here we select Caption.

You can then see the newly added property in Class View.

This caption is the Caption property in the control's Properties window when we are developing with the control.

In the development environment, we can use the getcaption and setcaption of the control variables to get and set this property of the control.

For example, in the test project, the DoClick button's Click event function is modified as follows:

void Ct1dlg::onbnclickedbutton1 ()

{

TODO: Add control notification Handler code here

MessageBox (M_activexdemo. Getcaption ());

M_activexdemo. Setcaption (_t ("Change"));

MessageBox (M_activexdemo. Getcaption ());

}

This will display the control's caption Name dialog when the DoClick button is clicked, then modify the name and then display the modified name in the popup dialog box.

If it is in the ActiveX Control Test Container, we still open the call Method window, in the method name will see Caption (proput) and Caption (Proget) method, that is, corresponding to the above setcaption and Getcaption, Also here you can try to modify the content for caption and get the modified value.

Custom properties

The difference between custom properties and common properties is that custom properties are not implemented by the COleControl class. Custom properties are used to expose a state or appearance of an ActiveX control to a programmer who uses the control.

The action to add a custom property is to open the Library node in Class View, in this case the Activexdemo1lib node. Select the second node, the _dactivexdemo1 in this example, and select Add properties in the right-click menu to open the Add Property Wizard. Here the custom attribute has the member variable and the Get/set method two kinds.

Member Variable Properties

Let's look at the properties of the member variable first. Select the type of attribute as needed, and fill in the property name, and the variable name and notification function are automatically filled in, and you can manually modify both names if you are not satisfied with the default name.

Where the variable name is a member variable of the control class that stores the control's properties, such as the control class in the Class View, which is Cactivexdemo1ctrl, the M_MYPROP1 member variable is seen below.

The use of this custom property is similar to common properties, and is also called in the ActiveX Control Test container with methods such as MYPROP1 (Proput) and MYPROP1 (Proget) for testing, as well as properties that appear in the control's Properties window at development time. You can also use GETMYPROP1 and SETMYPROP1 to get and set property values in your program.

The notification function is a function that is triggered when the property is modified. is defined as a member function in a control class.

For example, if we write the following code in this function:

void cactivexdemo1ctrl::onmyprop1changed (void)

{

Afx_manage_state (AfxGetStaticModuleState ());

TODO: Add Property handler code here

MessageBox (_t ("MyProp1 Changed."));

SetModifiedFlag ();

}

This dialog box pops up when you call MYPROP1 (propput) in the ActiveX Control Test Container to modify the property value. Similarly, if you modify this property value in the Properties window or modify this property value in your code during development, the notification function will be triggered, and the dialog box in our code pops up.

Get/set Method-Type properties

Adding the Get/set method property is to select the Get/set method in the implementation type in the Add Property Wizard window, specify the property type and the property name, the wizard will automatically fill in the Get and set functions, and you can specify the custom parameters.

The GetMyProp2 (void) and SETMYPROP2 (LPCTSTR newval) Two functions are then present in the definition of the control class to get and set the property value. However, it is important to note that if you look at the code of these two functions, these two automatically generated functions do not actually have the ability to get and set property values, this is just two frames, basically nothing, if you put in the ActiveX Control Test Container Call these two functions will not see any reaction, So we need to define the functions of the function manually.

For example, we can add a member variable for the control class to store our property values, since our property defines a BSTR type, we can declare the added member variable as CString M_MYPROP2, and then modify the GetMyProp2 (void) separately. and SetMyProp2 (LPCTSTR newval) are as follows:

BSTR cactivexdemo1ctrl::getmyprop2 (void)

{

Afx_manage_state (AfxGetStaticModuleState ());

CString strresult (M_MYPROP2);

TODO: Add the Dispatch handler code here

return strresult.allocsysstring ();

}

void Cactivexdemo1ctrl::setmyprop2 (LPCTSTR newval)

{

Afx_manage_state (AfxGetStaticModuleState ());

TODO: Add Property handler code here

M_myprop2=newval;

SetModifiedFlag ();

}

You then call SETMYPROP2 in the ActiveX Control Test Container to assign a value to the property, and then use GETMYPROP2 to return the newly assigned property value.

Also note that when defining this property, the parameter list is left blank, the default is the above SETMYPROP2 and GETMYPROP2 two functions, if you add other parameters in the parameter list, then the new parameters will be added to the parameters of the two functions of the parameter lists. For example, if we add a BSTR-type attribute MyProp3, add a long arg parameter to the parameter list, then the resulting two functions will be GETMYPROP3 (long Arg) and SETMYPROP3 (long arg, BSTR newval).

6. Property Pages

Property pages enable ActiveX control users to view and change ActiveX control properties. You can access these properties by calling the Control Properties dialog box. The dialog box contains one or more property pages that provide a custom graphical interface for viewing and editing control properties.

Use the default property page

After you create an ActiveX control project, the system automatically adds a property page class for us, which is cactivexdemo1proppage. There will also be a property Page dialog resource in the dialog resource, this is idd_proppage_activexdemo1.

You will see this property Page dialog box when you click the Properties button on the toolbar in the ActiveX Control Test Container.

Like this, for example. Of course there's nothing on it.

If you are using control development, you can open this property page by clicking the Property Page button in the Properties window after the control is selected, and the developer can set the properties of the control in this pop-up property page window.

If you want to modify a control property through a property page, you modify the property value by placing some controls on the property page to the user. For example, we have added a caption attribute above, and the ability to modify this property on the property page is usually a text input box. This allows us to place an EDIT control on the dialog box of the property page with the ID idc_edit_caption. The edit control is then bound to a variable. Right click this edit control, learn to add variables, open

Add a new variable to the Add Member Variable Wizard, set to M_caption, and note that to set the type to value, because the property is a string, this local variable type also uses CString.

Then let's take a look at a DoDataExchange function in the property page class. This function is automatically called by the system and is used to exchange data between the control and the member variable. Since we just bound the EDIT control to the m_caption variable, the DoDataExchange has already written to us such a sentence ddx_text (PDX, idc_edit_caption, m_caption); The data exchange between the caption and the edit control. However, the control CAPTION property is not automatically obtained from the value entered in the EDIT control, and an operation is required, namely DDP_Text (PDX, Idc_edit_caption, M_caption, _t ("CAPTION")); This binds the Caption property to the M_caption variable in the property page so that the control's caption can be modified whenever we modify the contents of the edit control in the Property page window.

Then the complete DoDataExchange function should look like this.

void Cactivexdemo1proppage::D odataexchange (cdataexchange* PDX)

{

DDX_Text (PDX, idc_edit_caption, m_caption);

DDP_Text (PDX, Idc_edit_caption, M_caption, _t ("CAPTION"));

Ddp_postprocessing (PDX);

}

Also note that this place ddp_postprocessing is the system automatically generated code, this sentence must be placed in the back of DDX_Text and ddp_text, otherwise you cannot implement control properties and controls on the property page binding. At this point, we can use this property page at design time to modify the Caption property without having to modify it through program code.

Add additional custom property pages

By default there is only one property page, and if you need to set too many properties, you need to add a custom property page.

A new way to add a property page is to open a resource view of VS and add a new dialog resource.

Here we choose Idd_ole_proppage_small, of course also can choose and large. Specify the new resource ID as Idd_ole_proppage_small.

Then you need to create a new association class for this resource. This association class should be the MFC class, the class name is CPropPage1, the base class should be colepropertypage, and the dialog ID select the new dialog resource ID we added.

Then, open the CPP file for the control class Cactivexdemo1ctrl, and locate the Macro Declarations section of the property page.

Begin_proppageids (Cactivexdemo1ctrl, 1)

Proppageid (Cactivexdemo1proppage::guid)

End_proppageids (Cactivexdemo1ctrl)

Add the new property page we created, adding a property page count.

TODO: Add more Property pages as needed. Please remember to increase the count!

Begin_proppageids (Cactivexdemo1ctrl, 2)

Proppageid (Cactivexdemo1proppage::guid)

Proppageid (Cproppage1::guid)

End_proppageids (Cactivexdemo1ctrl)

This appends the newly added property page to the control, and then opens the property page for the control to see one more property page.

Common Property Pages

In addition to customizing property pages as needed, the system provides developers with a number of pre-fabricated property pages, such as color, font, and other property pages. MFC provides three common property pages for use with ActiveX controls: CLSID_CColorPropPage, CLSID_CFontPropPage, and Clsid_cpictureproppage. These pages display the user interface for common color, font, and picture properties, respectively.

To merge these property pages into the control, simply add their IDs to the code that initializes the control's property page ID array. That is, the Proppageids declaration in the CPP file of the class control Cactivexdemo1ctrl is modified to the following form. As with the custom property page, you also need to increase the property page count.

TODO: Add more Property pages as needed. Please remember to increase the count!

Begin_proppageids (Cactivexdemo1ctrl, 3)

Proppageid (Cactivexdemo1proppage::guid)

Proppageid (Cproppage1::guid)

Proppageid (CLSID_CColorPropPage)

End_proppageids (Cactivexdemo1ctrl)

This adds a color property page to the property page, but you need to add a few more common properties to use this property page, for example, we want to be able to set the BackColor and ForeColor properties through this property page. Add a BackColor and a ForeColor property to the methods in the section on common properties, compile the build control, and then click Properties in the ActiveX Control Test Container in the toolbar.

You can see the settings for these two properties in the pop-up Properties window.

To use a color property, you need to call the Colecontrol::translatecolor member function. The parameters for this function are the Color property value and the optional palette handle. The return value is a COLORREF value that can be passed to GDI functions, such as SetTextColor and CreateSolidBrush. The following example shows how to use these two color properties when drawing a control.

CBrush Bkbrush (TranslateColor (GetBackColor ()));

COLORREF Clrfore = TranslateColor (GetForeColor ());

Pdc->fillrect (RcBounds, &bkbrush);

Pdc->settextcolor (Clrfore);

Pdc->drawtext (Internalgettext (),-1, rcBounds, Dt_singleline | Dt_center | Dt_vcenter);

Transferred from: http://www.cnblogs.com/lidabo/archive/2012/12/04/2800921.html

MFC-based ActiveX Control development (RPM)

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.