OwnerDraw (一)

参考资料: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

Minimum DLL VersionNone
HeaderDeclared in Winuser.h, include Windows.h
Minimum operating systemsWindows 95, Windows NT 3.1
(from MSDN)
三、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

Windows 98/Me, Windows 2000/XP: The item is being hot-tracked, that is, the item will be highlighted when the mouse is on the item.
ODS_INACTIVE
Windows 98/Me, Windows 2000/XP: 该项以及与该窗口相关的菜单为非活动状态
ODS_NOACCEL
Windows 2000/XP: 无键盘加速
ODS_NOFOCUSRECT
Windows 2000/XP: The control is drawn without focus indicator cues.
ODS_SELECTED
菜单项被选中
hwndItem  发送WM_DRAWITEM控件的句柄
hDC
该控件的设备上下文句柄
rcItem
需要绘制的边界矩形。
itemData
The application-defined value associated with the menu item. For a control, this parameter specifies the value last assigned to the list box or combo box by the LB_SETITEMDATA or CB_SETITEMDATA message. If the list box or combo box has the LBS_HASSTRINGS or CBS_HASSTRINGS style, this value is initially zero. Otherwise, this value is initially the value that was passed to the list box or combo box in thelParam parameter of one of the following messages:
CB_ADDSTRING
CB_INSERTSTRING
LB_ADDSTRING
LB_INSERTSTRING
If CtlType isODT_BUTTON or ODT_STATIC,itemData is zero.

Remarks
部分控件,像状态栏,并不会设置CtlTpye的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值