Subclassing a Windows Control

Source: Internet
Author: User
Tags reflector

Subclassing an existing windows universal control can reduce a lot of work. The new control can inherit many capabilities of the subclass control, such as drawing and responding to the mouse. When you create a project using MFC ActiveX Control wizard, you can select subclass A Windows Control to generate some necessaryCode. You can also manually add the code to an existing ActiveX project:

I,Overload colecontrol: issubclassedcontrol, precreatewindow

Bool cdemosubclassctrl: precreatewindow (createstruct & CS)
{
CS. lpszclass = _ T ("button"); // identifies the subclass from a button container
Return colecontrol: precreatewindow (CS );
}

Bool cdemosubclassctrl: issubclassedcontrol ()
{
Return true;
}

II,Modify the ondraw function.

Void cdemosubclassctrl: ondraw (CDC * PDC, const crect & rcbounds, const crect & rcinvalid)
{
Dosuperclasspaint (PDC, rcbounds );
}

III,Process Reflected container messages

Windows controls generally communicate with the parent window by sending messages to the parent window and receiving reflected messages from the parent window. The reflect message mainly aims to give the control the opportunity to control its own features, such as wm_ctlcolor and wm_drawitem.

However, the communication between ActiveX Control and Its package container is not the same. ActiveX controls send events to the container and read the ambient attribute of the container to interact with the container. Although ActiveX control is also a container, it does not need to send any messages to the parent window. Events are similar to messages on the surface, but their internal mechanisms are completely different. The event is based on the connection point, while message processing in MFC relies on a message chain from the subclass to the parent class. (For details, refer to Hou Jie's in-depth introduction to MFC)

However, when an ActiveX control subclass has a Windows Control, it automatically sends a message to its parent window, such as bn_clicked. Try to prevent it from sending messages to the package container. To this end, colecontrol generates an additional parent window named "reflector" for this type of ActiveX control, with the same size and position as the control. After receiving the message from the control, it will send a reflector message to the control to tell the control what kind of message it just sent, in this way, the control can be processed accordingly, such as sending an event to the package container.

Message sent by the Control Message reflected to the control
Wm_command Ocm_command
Wm_ctlcolorbtn Ocm_ctlcolorbtn
Wm_ctlcoloredit Ocm_ctlcoloredit
Wm_ctlcolordlg Ocm_ctlcolordlg
Wm_ctlcolorlistbox Ocm_ctlcolorlistbox
Wm_ctlcolorscrollbar Ocm_ctlcolorscrollbar
Wm_ctlcolorstatic Ocm_ctlcolorstatic
Wm_ctlcolor Ocm_ctlcolor
Wm_drawitem Ocm_drawitem
Wm_measureitem Ocm_measureitem
Wm_deleteitem Ocm_deleteitem
Wm_vkeytoitem Ocm_vkeytoitem
Wm_chartoitem Ocm_chartoitem
Wm_compareitem Ocm_compareitem
Wm_hscroll Ocm_hscroll
Wm_vscroll Ocm_vscroll
Wm_parentnotify Ocm_parentnotify
Wm_notify Ocm_policy

You need to manually add the reflected message processing:

 (1) Class cdemosubclassctrl: Public colecontrol {protected: lresult onocmcommand (wparam, lparam );....}; (2) begin_message_map (cursor, colecontrol) on_message (ocm_command, onocmcommand) end_message_map () (3) lresult Syntax: onocmcommand (wparam, lparam) {# ifdef _ win32word wnotifycode = hiword (wparam); # elseword wnotifycode = hiword (lparam); # endifswitch (W Policycode) {Case bn_clicked: fireclick ();  // generally, events are sent to the package container. You can also do other things  break;} return 0;} in the same way. You can also process messages such as ocm_ctlcolorbtn or ocm_drawitem to change the appearance of the button. 

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.