参考资料:MSDN Library for Visual Studio 2008 SP1 -> Visual C++ -> WM_DRAWITEM (and so on...)
一、What's the OwnerDraw?
为了满足界面需要制定一个特殊控件时,我们常会选择自己从通用控件中派生出自己的控件类,而OwnerDraw是必经之途。只有为控件指定了OWNERDRAW属性,如BS_OWNERDRAW,才能随心所欲的去绘制我们的控件。为响应这一属性,coder必须重载响应WM_DRAWITEM这一消息,当然这是在父窗口中进行(毕竟Owner)。
二、WM_DRAWITEM Notification
The WM_DRAWITEM message is sent to the parent window of an owner-draw button,combo box,list box,or menu when a visual aspect of the button,combo box,list box,or menu has changed.
A window receives this message through its WindowProc function.
Syntax
WM_DRAWITEM
WPARAM wParam
lPDrawItem = (LPDRAWITEMSTRUCT) lParam;
Parameters
wParam : Specifies the identifier of the control that sent the WM_DRAWITEM message.If the message was sent by a menu,this parameter is zero.
lpDrawItem : Pointer to a DRAWITEMSTRUCT structer containing information about the item to be drawn and the type of drawing required.
wParam 参数标识了发送WM_DRAWITEM消息的控件,lpDrawItem指向一个DRAWITEMSTRUCT结构体,其中包含了绘制、控件ID、控件类型等消息。
Return value
If an application processes this message,it should return TRUE.
当处理了这个消息则应返回TRUE。像对话框过程函数中,被处理过的消息都应该返回TRUE,标识消息已被处理,不需要进行默认处理,返回FALSE表明消息需要进行默认处理。
Remarks
By default, the DefWindowProc function draws the focus rectangle for an owner-drawn list box item.
The itemAction member of theDRAWITEMSTRUCT structure specifies the drawing operation that an application should perform.
Before returning from processing this message, an application should ensure that the device context identified by thehDC member of the DRAWITEMSTRUCT structure is in the default state.
Notification Requirements
(from MSDN)
Minimum DLL Version None Header Declared in Winuser.h, include Windows.h Minimum operating systems Windows 95, Windows NT 3.1
三、DRAWITEMSTRCUT Structure
Provides necessary information the owner window to determine how to paint an owner-draw control or menu item.The owner window of the owner-draw control or menu item rececives a pointer to this structure as the lParam parameter of the WM_DRAWITEM message.
DRAWITEMSTRUCT 向自绘控件的所有者窗口提供了必须的绘制信息,WM_DRAWITEM消息的lParam的值为一个指向这个结构的指针。
Syntax
<span style="font-family:KaiTi_GB2312;color:#6600cc;">typedef struct tagDRAWITEMSTRUCT {
UINT CtlType;
UINT CtlID;
UINT itemID;
UINT itemAction;
UINT itemState;
HWND hwndItem;
HDC hDC;
RECT rcItem;
ULONG_PTR itemData;
} DRAWITEMSTRUCT;</span>
Members
CtlType
The control type.This member can be one of the following values.See remarks.
ODT_BUTTON (按钮):Owner-draw button
ODT_COMBOBOX : owner-draw Combo box
ODT_LISTBOX : own-draw List box
ODT_LISTVIEW : owner-draw list view
ODT_MENU : owner-draw menu item.
ODT_STATIC : owner-drawn static control
ODT_TAB : tab control
CtlID 控件ID
itemID菜单项或list box、combo box的索引。 当 list box 或 combo box为空时,值为-1.itemID使的可以只绘制rcItem成员所指定的具有焦点区域。
itemAction
The required drawing action.This member can be one or more of the values.
指示了所需要的绘制行为,其值为以下值的组合。
ODA_DRAWENTIRE 整个控件都需要绘制
ODA_FOCUS 控件拥有了或失去了焦点。通过检测itemState成员判断控件是否具有焦点。
ODA_SELECT 选中状态发生变化。通过检测itemState成员判断新的选择状态。
itemState
The visual state of the item after the current drawing action takes place. This member can be a combination of the values shown in the following table.
标识了当前绘制动作发生后的可视状态。由一下值组合而成:
ODS_CHECKED 标识菜单项被勾选,仅用于菜单项
ODS_COMBOBOXEDIT 绘制Combo box的编辑框
ODS_DEFAULT 默认项
ODS_DISABLED 该项需要被绘制为禁用状态
ODS_GRAYED 灰显,仅用会菜单项
ODS_FOCUS 该项具有焦点
ODS_HOTLIGHT