"Reprint" ActiveX control development based on MFC (3)

Source: Internet
Author: User

Original: http://iysm.net/?p=122

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.

"Reprint" ActiveX control development based on MFC (3)

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.