Develop ActiveX controls using MFC

Source: Internet
Author: User
Tags ole
Abstract: This article introduces the MFC development method of ActiveX controls in COM components, and describes how to customize and create inventory attributes, methods, event adding methods, and property pages. This allows you to master the basic MFC ActiveX development methods.

Preface

ActiveX control is a COM component that implements a series of specific interfaces to make it more like a control in use and appearance. ActiveX controls involve almost all of the core technologies of COM and OLE, such as link objects, unified data transmission, Ole documents, property pages, permanent storage, and OLE automation.

As a basic interface unit, ActiveX controls must have their own properties and methods to suit different features.ProgramAnd provides functional services to the package container program. Its Attributes and methods are supported by the idispatch interface of the automated service. In addition to attributes and methods, ActiveX controls also have a feature different from that of Automated Services-events. An event refers to a notification sent from a control to its inclusion program. Similar to a window control that sends a message to its owner, the ActiveX control notifies its package container by triggering an event. Events are triggered by calling the methods of automated objects through the idispatch interface provided by the control package container. When designing ActiveX controls, consider the events that may occur in the controls and the events that the package container program will be interested in and include them. Unlike Automated Services, ActiveX controls have custom methods, attributes, and events (custom), and inventory (stock. Custom methods and attributes are common automated methods and attributes. Custom events are events with their own names and dispatch IDs. The so-called inventory methods, attributes, and events are the "standard" methods, attributes, and events that use ActiveX controls to specify names and dispatch IDs.

ActiveX controls can make COM components look and use the same as common window controls, and provide properties pages similar to setting Windows Standard control properties, this allows you to visually set the properties of ActiveX controls in the design phase of the package container program. ActiveX controls provide these functions to make it very convenient to use. The following section describes the development of ActiveX controls using MFC.

Establish Engineering Framework

The "MFC ActiveX controlwizard" Wizard allows you to easily create an MFC ActiveX Control Engineering Framework. Follow the default options to create the project structure shown in 1:


Figure 1 ActiveX control project structure created using the default options

The interfaces _ dsample68 and _ dsample68events provide the attributes, methods, and possible events of the control to the client program. The global functions dllregisterserver () and dllunregisterserver () are used for registration and logout of controls in the registry. They do not need to be modified.

The application class inherits from the colecontrolmodule. The colecontrolmodule is derived from cwinapp and provides the function of initializing the control module. The base class of csample68proppage is colepropertypage, a derived class of the cdialog class. It is mainly responsible for displaying the user control attributes on the property page on the graphic interface. The control class csample68ctrl is an important class among these classes. Most of the substantive work is completed in this class. Its base class is colecontrol, inherited from cwnd and c1_target, therefore, it can provide the same functions as the MFC window object and a series of event triggering functions and a distribution ing table for the control object, so that the ActiveX control can interact effectively with the container program in the same package. The derived classes of this class can send messages or trigger events to the control's package container when specific conditions are met to notify the package container program of some important events in the control. The distribution ing table is an important part of the table. It exposes the methods and Attributes provided by the control to the package container program. Figure 2 shows the role of the colecontrol class in the control-to-package container communication. It can be seen that all the communication process between ActiveX Control and Its package container is completed by colecontrol:


Figure 2 Role of colecontrol in communication between ActiveX Control and package container

The control class reloads the ondraw () function of the base class colecontrol. The Wizard generates the following default values:CodeIs used to draw an ellipse in the customer area of the control. In programming, we usually need to replace it:

Void csample68ctrl: ondraw (
CDC * PDC, const crect & rcbounds, const crect & rcinvalid)
{
// Todo: Replace the following code with your own drawing code.
PDC-> fillrect (rcbounds, cbrush: fromhandle (hbrush) getstockobject (white_brush )));
PDC-> ellipse (rcbounds );
}


Figure 3 insert ActiveX Control


Figure 4 inserted control to be tested

After compiling the code generated by the wizard, ActiveX control with the extension OCX will be generated. ActiveX controls cannot run independently. They can only run in the package container program. Generally, the ActiveX control test container tool attached with VC ++ is used to debug ActiveX controls for convenience. Right-click the client area of the test tool and select "Insert new control…" in the pop-up menu... "Menu item, the dialog box shown in Figure 3 is displayed. The list box on the left lists all registered ActiveX controls in the current system, select the control to be tested and insert it to the test program. You can test the control methods, attributes, and events by using the menu items in the control menu. In the split view below, the trace shows the debugging record (see figure 4 ).Add attributes, methods, and events


Figure 5 adding a property


Figure 6 Add a method

You can add inventory and custom properties, methods, and events of ActiveX controls. Add properties and methods to the automation page of the MFC classwizard dialog box and click "add property... "And" add method... "The add attribute and add method dialog box shown in Figure 5 and figure 6 is displayed. For inventory attributes and methods, you can directly select from the drop-down list of the external name combo box. The implementation item is automatically set to stock. Adding custom attributes and methods is the same as adding attributes and methods to interfaces in an Automation Object. the odl file and control class generate the corresponding code. The following shows some distribution ing Code implemented in the control class:

......
// Dispatch maps
// {Afx_dispatch (csample68ctrl)
Cstring m_message;
Afx_msg void onmessagechanged ();
Afx_msg short getxpos ();
Afx_msg void setxpos (short nnewvalue );
Afx_msg short getypos ();
Afx_msg void setypos (short nnewvalue );
Afx_msg short messagelen ();
//} Afx_dispatch
Declare_dispatch_map ()
// Dispatch and event IDS
Public:
Enum {
// {Afx_disp_id (csample68ctrl)
Dispidmessage = 1l,
Dispidxpos = 2L,
Dispidypos = 3l,
Dispidmessagelen = 4l,
//} Afx_disp_id
};
......
Begin_dispatch_map (csample68ctrl, colecontrol)
// {Afx_dispatch_map (csample68ctrl)
Disp_property_policy (csample68ctrl, "message", m_message, onmessagechanged, vt_bstr)
Disp_property_ex (csample68ctrl, "xpos", getxpos, setxpos, vt_i2)
Disp_property_ex (csample68ctrl, "ypos", getypos, setypos, vt_i2)
Disp_function (csample68ctrl, "messagelen", messagelen, vt_i2, vts_none)
Disp_stockprop_backcolor ()
Disp_stockprop_caption ()
Disp_stockprop_forecolor ()
//} Afx_dispatch_map
End_dispatch_map ()
......

A custom method messagelen () and three inventory attributes backcolor, caption, and forecolor are added to indicate the background color, title, and foreground color of the control, respectively), two custom properties xpos and ypos obtained in get/set mode, and a custom property message implemented in the form of member variables. These Custom Attributes indicate the X and Y coordinates of the string and the content to be displayed. For properties obtained using the get/set method, you should add corresponding member functions to them in the control class and modify the implementation process of their get and set member functions:

Short m_nypos;
Short m_nxpos;
......
Short csample68ctrl: getxpos ()
{
Return m_nxpos;
}
Void csample68ctrl: setxpos (short nnewvalue)
{
M_nxpos = nnewvalue;
Setmodifiedflag ();
}
Short csample68ctrl: getypos ()
{
Return m_nypos;
}
Void csample68ctrl: setypos (short nnewvalue)
{
M_nypos = nnewvalue;
Setmodifiedflag ();
}

For the attribute message created as a member variable, the wizard also generates a message response function:

Void csample68ctrl: onmessagechanged ()
{
Setmodifiedflag ();
}

The onmessagechanged () function is called as long as the value of this attribute is changed.

To enable the preceding property settings, such as the background color and foreground color, to be physically associated with the control, you need to replace the code generated by the wizard in the ondraw () function of the control class. For example, the following code sets the property added previously as the parameter value and displays a string of characters in the control:

// Set the paint brush with the background color
Cbrush brush (translatecolor (getbackcolor ()));
// Set the font color with the foreground color
PDC-> settextcolor (translatecolor (getforecolor ()));
// Draw the background
PDC-> fillrect (rcbounds, & brush );
// Set the font background to transparent
PDC-> setbkmode (transparent );
// Display characters
PDC-> textout (m_nxpos, m_nypos, m_message );

In order to make the effect immediately displayed on the control after the attribute settings are changed, invalidatecontrol () should be called in the function implementation related to the attribute settings to update the display of the control.

You can compile the program and test it in the ActiveX control test container tool. After the control is inserted, use "INVOKE methods... "The Dialog Box 7 is displayed. In the method name combo box, you can select the attributes and methods to be tested. The property tests are described as progget and progset to indicate that the property value is obtained and set. In the parameter editing box, enter the parameter to be set and its corresponding parameter type. Click the setvalue button to add the parameter value to the parameter list box, finally, click the invoke button to apply the Set Properties in the control and execute the specified method. For methods with return values, the execution results are displayed in the return edit box. If an exception occurs, the Exception error message is displayed in the exception edit box. Figure 8 shows the Property setting control interface.


Figure 7 test attributes and Methods


Figure 8 controls with properties set

To add a control event, go to the ActiveX Events page of the MFC classwizard dialog box and click "add event... The "add event" event add dialog box shown in "button" appears. Similar to adding methods and properties, you can enter a Custom Event name in the external name combo box or select an inventory event from the drop-down list. The implementation item automatically sets the stock or custom option based on the event type to be added. The ActiveX Control notifies the container program of a specific event by adding events. Inventory events are mostly Keyboard Events and mouse events, which are automatically handled by the colecontrol. For custom events. necessary Code such as the event ing table is added to the odl file and control class (included in the Code). Developers must compile their own code to determine the conditions under which the event should be triggered.


Figure 9 Add an event

dispinterface _ dsample68events
{< br> properties:
// event interface has no properties
methods:
// note-classwizard will maintain event information here.
// use extreme caution when editing this section.
// {afx_odl_event (csample68ctrl)
[ID (1)] void msgout ();
//}} afx_odl_event
};
......
// event maps
// {afx_event (csample68ctrl)
void firemsgout ()
{fireevent (eventidmsgout, event_param (vts_none ));}
//} afx_event
declare_event_map ()
// dispatch and event IDs
Public:
Enum {
// {afx_disp_id (csample68ctrl)
......
eventidmsgout = 1l,
//} afx_disp_id
};
......
begin_event_map (csample68ctrl, colecontrol)
// {afx_event_map (csample68ctrl)
event_custom ("msgout", firemsgout, vts_none)
//} afx_event_map
end_event_map ()

The above Code adds a custom msgout event, which can be triggered by calling firemsgout. Next, modify the onmessagechanged () Message response function of the message attribute. This function is called whenever the message attribute content is changed. In this function, call the previously added messagelen () method to determine the length of the modified message attribute. When the length is greater than 10, firemsgout () is called to trigger the msgout event:

Void csample68ctrl: onmessagechanged ()
{
Invalidatecontrol ();
If (messagelen ()> = 10)
Firemsgout ();
Setmodifiedflag ();
}


Figure 10 select the event to be logged

When you use ActiveX control test container to test the newly added event, you must first use the "logging... "Menu item 10 is displayed, and the events to be tracked are selected from the" events "property page. When the content of the message attribute is set to more than 10 characters in the invoke Methods dialog box, the split view at the bottom of the Program Framework records the msgout event triggered by the control (11 ).


Figure 11 test the event

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.