I. Summary
Although the listctrl function provided by MFC can perform some common operations, it is powerless for some specific applications. At this time, you must expand the space to meet specific needs.
This article describes how to implementCustom Row HeightAnd can select andEditThe listctrl control.
II. Implementation
(1) Implement custom Row Height.
we all know that, the row height is fixed of the listctrl control that comes with MFC. Customized changes. If you set a relatively large bitmap or simply set a relatively large font, although these methods can change the line height, they are not exposed to the essence of the problem. We use the Message reflection mechanism to customize the row height . For Message reflection mechanisms, see msdn. Listctrl, which implements custom Row Height, is based on the following fact: When we send a wm_windowposchanged message to listctrl, that is, when the size and position of the listdctrl control are changed, the listctrl control triggers the wm_mearsuritem message, of course, this is a reflection message. By ing this message, we can change the Row Height of the listctrl control. Of course, we also need to set the Owen draw fixed style of listctrl. wm_mearsuritem is only valid in the Owen draw fixed style.
To sum up, if you want to create a listctrl command with a custom Row Height, perform the following steps:
(1) define a new class and inherit from listctrl,Set the Owen draw fixed styleAnd manually MapOn_wm_measureitem_reflectMessage.
(2) added the interface for setting the row height, and triggered the on_wm_measureitem_reflect message by setting to send the wm_windowposchanged message. For example:
Void clistex: setitemheight (uint nheight)
{
M_nitemheight = nheight;
Crect rcwin;
Getwindowrect (& rcwin );
Windowpos WP;
WP. hwnd = m_hwnd;
WP. Cx = rcwin. Width ();
WP. Cy = rcwin. Height ();
WP. Flags = swp_noactivate | swp_nomove | swp_noownerzorder | swp_nozorder;
Sendmessage (wm_windowposchanged, 0, (lparam) & WP );
}
(3) map on_wm_measureitem_reflect to reflect the message, and set the Row Height in the reflected message. For example:
Void clistex: measureitem (lpmeasureitemstruct)
{
Lpmeasureitemstruct-> itemheight = m_nitemheight;
}
(4) Complete the Control Self-painting function, clistex: drawitem. Since then, the custom setting row high function has been implemented.
3. added the editing function.
The general idea of adding the Edit function is to create a cedit edit box when you click or double-click it, overwrite the edit box on the current subitem, and get the focus of the edit box. When the edit box loses focus, the text in the edit box is displayed on the listctrl control. To enable the control to know when to display the text in the edit box, the edit box is rewritten. When the edit box loses focus, the listctrl control is notified through the message.