ActiveX component development and use

Source: Internet
Author: User
Tags textout

ActiveX Control Concept (Application of COM components)

1. What is ActiveX Control?
Based on the COM component technology, it is as convenient to use as a general control. The difference from a general control is that it can be used by any computer program that supports the com standard.
2. What is a COM component?
Components implement some small binary files with certain functions and provide services externally through interfaces.
A project generates an EXE file
A project consists of one or more EXE files plus one or more components. The component interacts with the component directly through the interface. It has better reusability and maintainability.
3. What is an interface?
C language-the interface is a function
C ++ language-the interface is a public member function.
COM component-interface is a set of pure virtual functions (pure virtual class ). Because pure virtual functions are not implemented only by declarations, the interfaces are stable.
Void sort (parameter) = 0;


Develop, register, test and use ActiveX Controls
1. Understand the ActiveX control testing tools provided by Microsoft.
ActiveX control test container
The development and testing of ActiveX controls include events, properties, and methods of controls.
2. Use the MFC Wizard to develop ActiveX Controls
2.1 Introduction to the interfaces and classes generated by the wizard
First interface-add attributes and Methods
Second Interface-add an event.
App-application class
CTRL-control class, including the implementation of various attributes and methods. In addition, the widget's appearance is completed in the ondraw () function.
Proppage-property page class, add control property settings

Here we will develop a component for finding the maximum common divisor of two numbers.

Run the following command:


Create a project and select "MFC ActiveX controlwizard". After the project is created
Right-click the first interface of the project and select "addd method" to add a method (function)
Function prototype: void setnumber (INT m_num1, int m_num2). It is mainly used to accept two calculation data transmitted by the user (by calling the function interface we provide ).
Specific operations:

After the function is added, the function is displayed in the first interface and in our CTRL class. The number of functions in the interface provides external services. The function in ctrl needs to be implemented.

Add two member variables for the CTRL class to accept the number of computations passed in by the user:

int  m_a;int  m_b;

Right-click the first interface and select Add property. Here, a bool variable named direction is added to control the display position of the result ball (above/below)

After adding the direction property, you will find an ondirectionchange In the CTRL class. When the value of the direction variable is set externally through the proction (propput), this function is called.

Void cactivexmcdctrl: ondirectionchanged () {// todo: Add notification handler code // re-calculate the invalidate (); setmodifiedflag ();}


Then initialize them and the control size in the CTRL class constructor:

Cactivexmymcctrl: cactivexmymcctrl () {initializeiids (& iid_dactivexmymcd, & iid_dactivexmymcdevents); // todo: initialize your control's instance data here. // set the initial size of the control setinitialsize (230,230); M_a = 16; m_ B = 12; m_direction = false; // control the display position of the result ball}
Next, return to the CTRL class to implement the setnumber method.

Void cactivexmymcctrl: setnumber (long m_num1, long m_num2) {// todo: add your dispatch handler code herem_a = m_num1; m_ B = m_num2; // trigger control re-painting call our MCD function settlement result and display invalidate (true );}
The ondraw function of CTRL begins to draw our controls:
First, remove the default draw code, and then add our draw code:

Void cactivexmcdctrl: 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 ); // set the color of the control background cbrush brush (RGB (255,255, 0); PDC-> fillrect (& rcbounds, & brush ); // draw 3 circles // set the circle background color cbrush brush2 (RGB (, 0); // set the center position point P1 = {40,120}; point P2 = {190, p1.y}; point P3 = {(p1.x + p2.x)/2, m_direction? P1.y-50: p1.y + 50}; // set the radius of the circle to int radious = 15; // draw a circular cbrush * poldbrush = PDC-> SelectObject (& brush2 ); PDC-> ellipse (p1.x-radious, p1.y-radious, p1.x + radious, p1.y + radious); PDC-> ellipse (p2.x-radious, p2.y-radious, p2.x + radious, p2.y + radious); PDC-> ellipse (p3.x-radious, p3.y-radious, p3.x + radious, p3.y + radious); // connection line PDC-> moveTo (P1 ); PDC-> lineto (P3); PDC-> lineto (P2); // set the font color, alignment, and background transparency PDC-> settextcolor (RGB (255,255,255 )); PDC-> settextalign (ta_center); PDC-> setbkmode (transparent); // write the value cstring strmsg in the circle; strmsg. format ("% d", M_a); PDC-> textout (p1.x, p1.y-9, strmsg); strmsg. format ("% d", m_ B); PDC-> textout (p2.x, p2.y-9, strmsg); strmsg. format ("% d", MCD (M_a, m_ B); PDC-> textout (p3.x, p3.y-9, strmsg); PDC-> SelectObject (poldbrush); brush. deleteobject (); brush2.deleteobject ();}
In the CTRL class, add the function to calculate the minimum common number and its implementation:

int CActiveXMyMcdCtrl::Mcd(int a, int b){if (b == 0) return a;return Mcd(b, a%b);}

Finally, right-click the second interface and choose add event to add a message (event) to the control. The messages of the control are divided into stock and custom)

 

After you select "stock" for the message for adding an inventory, click "OK" for the event you are interested in the external name: combo box. Note that the message here is the message sent from our control to the control user, that is to say, the control user processes our control messages.

Here I add a custom message "mymsg". After the message is added, an event interface appears in the second control. A function is displayed in the CTRL class, but this message needs to be sent by ourselves:

Here we add a message processing tool for the CTRL class: wm_lbuttondown, that is, when the left button is pressed to trigger our custom message:

void CActiveXMyMcdCtrl::OnLButtonDown(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultFireMyMsg ();COleControl::OnLButtonDown(nFlags, point);}


Register and uninstall ActiveX controls:

Win + R:

Regsvr32 "file path of the component" register the component
Regsvr32/u "file path of the component" Uninstall the component

Note: Run this command in the administrator province in winsta and later system environments.


Test ActiveX Controls: events, properties, and methods

After the connection is compiled: Tools --> Register contrl

After successful registration: Tools --> ActiveX contrl test container

In this way, the container provided by Microsoft to test ActiveX controls is opened. Right-click the client area and select Insert new contrl. Select the ActiveX control we have compiled.

Test method: Control --> invoke methods... ---> In the displayed dialog box, select setnumber (method). After setting the values of the two parameters, click invoke to view the result.

Test property: Control --> invoke methods... ---> In the displayed dialog box, select direction (propput), enter a value, set value, and then invoke to see the effect.



Use of ActiveX controls:

Create a dialog box-based program, right-click the dialog box resource, Select Insert ActiveX control, and select our control

CTRL + W use the Class Wizard to bind a member variable to the control. When binding, you are prompted to add the packaging class of the control. Then you can use the ActiveX control just like a general control.


void CUseMcdDlg::OnBtnCompute() {// TODO: Add your control notification handler code hereUpdateData ();m_wndMcd.SetNumber (m_num1, m_num2);}void CUseMcdDlg::OnBtnSetdir() {// TODO: Add your control notification handler code herem_wndMcd.SetDirection (! m_wndMcd.GetDirection ());}void CUseMcdDlg::OnBtnAbout() {// TODO: Add your control notification handler code herem_wndMcd.AboutBox ();}


Use Controls in HTML:

<HTML> 

The classid uniquely identifies the control we registered in the system. Double-click the first interface to find our control ID,









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.