How to associate custom control (MFC Control) with MFC cwnd and Win32 hwnd
<Reference msdn mfc tno 15>
This document provides an overview of the three methods for customizing custom controls in MFC:
Owner Drawing Control and menu self-Drawing Control and menu and subclass)
1. Use the MFC method to customize the following basic concepts:
Controls several standard Windows messages for custom controls:
Wm_measureitem
Wm_compareitem
Wm_deleteitem
Wm_drawitem
Message Details:
1.1 wm_measureitem
When the owner draw control (owner-drawn button, combo box, list box, List View control, or menu item) when the widget is created, the parent form of the widget will receive the message used to customize the widget size.
Wm_measureite
Midctl = (uint) wparam; // Control ID
Lpmis = (lpmeasureitemstruct) lparam; // control size information
Return Value of message processing:
When the function processes this message, it must return true.
1.2 wm_compareitem
The system sends this message to set the position information of a new entry (for example, a cbs_sort-style ComboBox and a lbs_sort-style ListBox ).
wm_compareitem
idctl = wparam; // Control ID
lpcis = (lpcompareitemstruct) lparam; // two subitem information for comparison
Return Value of message processing:
-The position of subitem 1 before subitem 2
0 subitem 1 and 2 have equal sorting positions
1. Position of subitem 1 after subitem 2
1.3 wm_deleteitem
When the ListBox or combo box is destroyed or a subitem of The ListBox or combo box is removed (for example, the message lb_deletestring tcontent, cb_deletestring cb_resetcontent) the system sends a message to the parent form of each deleted control subitem.
Wm_deleteitem
Idctl = wparam; // Control ID
Lpdis = (lpdeleteitemstruct) lparam; // Delete subitem Information
Message Return Value
When the function processes this message, it must return true.
1.4 wm_drawitem
The wm_drawitem message is sent to the owner form when the visual mode of the Self-drawn button, combo box, list box, or menu needs to be changed.
Wm_drawitem
Idctl = (uint) wparam; // Control ID
Lpdis = (lpdrawitemstruct) lparam; // draw control information
Function return value
When the function processes this message, it must return true.
2 The following describes three common methods for customizing custom controls:
2.1 owner drawing controls or menus (owner draw controls/menu)
Windows supports controls with the ws_ownerdraw style to send a specified message to the parent form (control or menu owner) of the control so that the parent form can customize the visual style or behavior of these controls.
In its message routing, MFC directly supports the processing of four types of messages:
Cwnd: ondrawitem
Cwnd: onmeasureitem
Cwnd: oncompareitem
Cwnd: ondeleteitem
You can implement these methods in the cwnd derived class (usually cmainframe or cdialog) to customize the control.
Note:
This method has many drawbacks,CodeThe reuse rate is low, so that custom controls can only copy code from one place to another when being reused in another place.
2.2 self-drawn controls or menus
By default, MFC implements the owner draw standard message method. It decodes the Drawn work messages implemented by the parent form and sends them to the specified control to process these messages, this elegant processing method makes it easy to implement custom controls with a high reuse rate.
In the MFC classes (cmenu, cbutton, clistbox, and clistctrl etc) of these encapsulated controls, you only need to derive a new class and override the corresponding virtual function to easily customize the custom style controls.
For example, the drawitem () and mesureitem () functions of cmenu
2.3 dynamic subset)
2.3.1 concepts of subclass:
In Windows programming, a new window process (subclass winproc) is used to replace the old window process. Instead, superclass winproc is used as the default window process, to make the form display new features;
The concept of superclass and subclass in Windows programming can be understood by the relationship between the base class and the derived class in C ++.
You can use API: setwindowlong to implement the Win32 process.
3.4.2 three methods associated with MFC cwnd and Win32 hwnd
Method 1 when a cwnd is created, the cwnd object creates an hwnd. The hwnd style can be changed at this time, for example, using CREATE ()
Method 2: Create a cwnd and associate it with an existing hwnd. At this time, the hwnd style cannot be changed, for example, attach ()
Method 3 create a cwnd association with an existing hwnd. In this case, the hwnd style can be changed. This is the so-called dynamic subset (Dynamic subclassing) in this way, you can change the behavior of a form in the running time.
for dynamic subsets of MFC, two functions are provided:
cwnd :: subclasswindow
cwnd: subclassdlgitem.
using dynamic subsets is the most elegant style and method for controlling the interface style, high code reuse rate and dynamic runtime changes
generally, some excellent third-party interface libraries of MFC are implemented based on this technology