这篇文章是以前的补充:
http://www.cnblogs.com/wangkewei/archive/2009/02/24/1397490.html

放在首页是想借助各位从事Windows Mobile本地代码开发的前辈们力量,把这方面的资料完善一下,我会总结更多有关这方面的文章。

1.原理介绍


DRAWITEMSTRUCT结构体的定义如下:

typedef struct tagDRAWITEMSTRUCT { UINT CtlType; //控件类型 UINT CtlID; //控件id   UINT itemID; //菜单项、列表框或组合框中某一项的索引值  UINT itemAction; //控件行为  UINT itemState; //控件状态  HWND hwndItem; //父窗口句柄或菜单句柄   HDC hDC; //控件对应的绘图设备句柄  RECT rcItem; //控件所占据的矩形区域 ULONG_PTR itemData; //列表框或组合框中某一项的值
} DRAWITEMSTRUCT; 

结构体每项的具体取值如下:(摘自MSDN文档)
CtlType

Unsigned integer that specifies the control type. It can be one of the following values.

Value Description
ODT_BUTTON Owner-drawn button
ODT_LISTVIEW Owner-draw list view control
ODT_MENU Owner-drawn menu
ODT_TAB Tab control
CtlID
Unsigned integer that specifies the identifier of the combo box, list box, or button. This member is not used for a menu.
itemID
Unsigned integer that specifies the menu item identifier for a menu item or the index of the item in a list box or combo box. For an empty list box or combo box, this member can be –1. This value allows the application to draw only the focus rectangle at the coordinates specified by the rcItem member, even though the control contains no items. This focus rectangle indicates to the user whether the list box or combo box has the focus. The value of the itemAction member determines whether the rectangle is to be drawn as though the list box or combo box has the focus.
itemAction
Unsigned integer that specifies the drawing action required. This member can have one or more of the following values.

Value Description
ODA_DRAWENTIRE The entire control needs to be drawn.
ODA_FOCUS The control has lost or gained the keyboard focus. You should check the itemState member to determine whether the control has the focus.
ODA_SELECT The selection status has changed. You should check the itemState member to determine the new selection state.
itemState
Unsigned integer that specifies the visual state of the item after the current drawing action takes place. It can be a combination of the following values.

Value Description
ODS_CHECKED The menu item is to be checked. Use this value only in a menu.
ODS_COMBOBOXEDIT The drawing takes place in the edit control of an owner-drawn combo box.
ODS_DEFAULT The item is the default item.
ODS_DISABLED The item is to be drawn as disabled.
ODS_FOCUS The item has the keyboard focus.
ODS_GRAYED The item is to be grayed. Use this value only in a menu.
ODS_SELECTED The status of the menu item is selected.
hwndItem
Handle to the control for combo boxes, list boxes, buttons, and static controls. For menus, this member is a handle to the menu containing the item.
hDC
Handle to a device context. You must use this device context when performing drawing operations on the control.
rcItem
RECT structure that specifies a rectangle that defines the boundaries of the control to be drawn. This rectangle is in the device context that you specified with the hDC member. The OS automatically clips anything that the owner window draws in the device context for combo boxes, list boxes, and buttons, but does not clip menu items. When drawing menu items, the owner window must not draw outside the boundaries of the rectangle defined by the rcItem member.
itemData
Pointer to an unsigned long that specifies the application-defined 32-bit value associated with the menu item. For a control, this member 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 LB_HASSTRINGS or CB_HASSTRINGS style, this value is initially zero. Otherwise, this value is initially the value passed to the list box or combo box in the lParam parameter of one of the following messages:

  • CB_ADDSTRING
  • CB_INSERTSTRING
  • LB_ADDSTRING
  • LB_INSERTSTRING

If CtlType is ODT_BUTTON, itemData is zero.

文档中有这句话:
“The owner window of the owner-drawn control or menu item receives a pointer to this structure as the lParam parameter of the WM_DRAWITEM message.”
即lParam参数中包含指向一个DRAWITEMSTRUCT结构的指针。
另外wParam参数用来指定发送WM_DRAWITEM消息的控件标识符。如果该消息是由菜单发送的,则该参数为零。

2.示例(修改自codeproject)

在创建主窗口获得主窗口句柄hWnd后创建按钮:

hLevelUpButton = CreateWindow(_T("button"), NULL, WS_CHILD | WS_VISIBLE | BS_OWNERDRAW, 0, 0, 32, 32, hWnd, (HMENU) IDC_LEVELUPBUTTON, g_hInst, NULL);
if (NULL == hLevelUpButton) { MessageBox(hWnd, L"Create up button fails", L"Message", MB_OK);
} 
hLevelDnButton = CreateWindow(_T("button"), NULL, WS_CHILD | WS_VISIBLE | BS_OWNERDRAW, 32, 0, 32, 32, hWnd, (HMENU) IDC_LEVELDNBUTTON, g_hInst, NULL);
if (NULL == hLevelDnButton) { MessageBox(hWnd, L"Create down button fails", L"Message", MB_OK);
} 

在主窗口消息处理里定义结构体:

DRAWITEMSTRUCT* pdis; 

添加消息处理:

case WM_DRAWITEM: pdis = (DRAWITEMSTRUCT*) lParam; // (winuser.h) Maybe you also want to account for pdis->CtlType (ODT_MENU, ODT_LISTBOX,ODT_COMBOBOX, ODT_BUTTON, ODT_STATIC) switch(pdis->CtlID) { case IDC_LEVELUPBUTTON: // Fall through (you would use a "break" otherwise): case IDC_LEVELDNBUTTON: result = OwnerDrawButton(pdis, g_hInst); if (FALSE == result) { MessageBox(hWnd, L"OwnerDrawButton return fasle", L"Message", MB_OK); return(FALSE); } break; // Other case labels if any... default: break; } //如果处理该消息,则必须返回TRUE return(TRUE); 

OwnerDrawButton函数的定义如下:

BOOL OwnerDrawButton(DRAWITEMSTRUCT* pdis, HINSTANCE hInstance)
{ static RECT rect; static int iCount = 0; // Icon handles: static HICON hCurrIcon, hUpIconI, hDnIconI, hUpIconA, hDnIconA; // Use copy of rectangle: rect = pdis->rcItem; //只载入一次Icon if (iCount < 1) { // LoadIcon only loads once, but LoadImage does not,  // so in case you call the latter, use a static counter: iCount++; // Inactive buttons: hUpIconI = (HICON) LoadIcon(hInstance, MAKEINTRESOURCE(ID_UP_ICONI)); if (!hUpIconI) { MessageBox(NULL, L"Loading ID_UP_ICONI icon fails", L"Message", MB_OK); } hDnIconI = (HICON) LoadIcon(hInstance, MAKEINTRESOURCE(ID_DN_ICONI)); if (!hDnIconI) { MessageBox(NULL, L"Loading ID_DN_ICONI icon fails", L"Message", MB_OK); } // Active buttons: hUpIconA = (HICON) LoadIcon(hInstance, MAKEINTRESOURCE(ID_UP_ICONA)); if (!hUpIconA) { MessageBox(NULL, L"Loading ID_UP_ICONA icon fails", L"Message", MB_OK); } hDnIconA = (HICON) LoadIcon(hInstance, MAKEINTRESOURCE(ID_DN_ICONA)); if (!hDnIconA) { MessageBox(NULL, L"Loading ID_DN_ICONA icon fails", L"Message", MB_OK); } } // If the control's id is that of the "Up" button: if (IDC_LEVELUPBUTTON == pdis->CtlID) { // If the button is selected, display the  // "active" icon, else the "inactive" icon: if (pdis->itemState & ODS_SELECTED) hCurrIcon = hUpIconA; else hCurrIcon = hUpIconI; } // If the control's id is that of the "Down" button: if (IDC_LEVELDNBUTTON == pdis->CtlID) { // If the button is selected, display the  // "active" icon, else the "inactive" icon: if (pdis->itemState & ODS_SELECTED) hCurrIcon = hDnIconA; else hCurrIcon = hDnIconI; } // Center the icon inside the control's rectangle: if (!DrawIconEx( pdis->hDC, (int) 0.5 * (rect.right - rect.left - ICON_WIDTH), (int) 0.5 * (rect.bottom - rect.top - ICON_HEIGHT), (HICON) hCurrIcon,//根据上面指定的Icon绘制 ICON_WIDTH, ICON_HEIGHT, 0, NULL, DI_NORMAL )) { MessageBox(NULL, L"Drawing icon fails", L"Message", MB_OK); } return TRUE;
} 

附件项目的环境是:
Win32/Windows Mobile 6 Professional(CHS)/Visual Studio 2008(CHS) 
/Files/wangkewei/OwnerDrawButton.rar

Windows Mobile 6.0下实现自绘多种状态按钮(Win32) 续相关推荐

  1. 在Windows mobile 5.0下操作INI文件

    对于Windows mobile 5.0来说没有像window那样操作INI文件的API,所以只能自已来实现.其实操作INI文件就是操作普通的文本文件,只是要麻烦一些.以下是我实现的一些常用的操作的函 ...

  2. Windows Mobile 5 0编程—奥运场馆速查

    虽然前不久买了一个HP基于windows Mobile 5.0的PDA,由于工作太为繁忙,并没有为之开发相应的程序.没想到微软最近开展了酷炫应用争霸赛,征集"奥运"相关的作品,我忙 ...

  3. windows mobile 5.0 PocketPC模拟器上网的设置 【正确】

    找了很多方法终于找到了 在确保主机已连上互联网的情况下,按以下步骤设置: 1.打开ActiveSync ,点击"文件"-->"连接设置",在"允 ...

  4. Windows Mobile 5.0 中为开发人员提供的新功能(3)

    Windows Mobile 5.0 中为开发人员提供的新功能(3) Pocket Outlook 增强功能 Pocket Outlook 向用户和应用程序开发人员提供了易于使用的 PIM.将 Poc ...

  5. 在Visual Studio 2005里,用ActiveSync来同步模拟器(Windows Mobile 5.0 )

    一.前期环境搭建     1.Windows Mobile5.0模拟器安装步骤以及下载地址,如下: http://news.csdn.net/news/newstopic/22/22102.shtml ...

  6. Windows Mobile和Wince下使用TinyXML进行Native C++的开发

    背景 继续讲述Mobile Radio项目的开发,上回讲到如何把自于 www.1radio.com.au 网站的电台数据从JSON转换成XML.这回讲述使用tinyXML在windows mobile ...

  7. Windows Mobile 5.0

    自上星期以来,开始系统化的学习Windows Mobile 5.0,希望通过系统化的学习,能够掌握移动开发. Mobile 5.0开发配置: 软件安装顺序: 1. 安装Microsoft Active ...

  8. Windows Mobile 5.0新增API介绍(转自MSDN)

    Windows Mobile 5.0 提供很多新的 API.尽管这些新的 API 分布在许多不同的服务中,但它们都是为提高开发人员工作效率这一共同目标服务的.通过将很多经常执行的任务合并到操作系统中, ...

  9. Windows Mobile 5.0 认知篇

    (本文只作为本人学习用途) 1.什么是微软Windows Mobile 5.0? 最新的Windows Mobile 5.0不叫Windows Mobile 5.0 2005,而是Windows Mo ...

最新文章

  1. sql server 2005 学习心得(select查询语句用法)
  2. 钽电容正负极_固态电容怎么看正负极,固态电容正负极区分方法
  3. 服务器监听端口信息,服务器监听端口信息
  4. LeetCode-滑动窗口-209. 长度最小的子数组
  5. make: *** 没有规则可以创建“default”需要的目标“build”
  6. Websocket--- long loop--ajax轮询
  7. 【经典回放】多种语言系列数据结构算法:二叉树(C#版)
  8. 转载——CVE-2019-0807
  9. 丁丁打折网卷能用吗_微信群控还能用吗?现在什么群控还能使用吗?
  10. 小白学算法:买卖股票的最佳时机!
  11. xshell 中使用vim 显示Xmanager运行失败:
  12. MessageBox.Show常用的2个方法
  13. 推荐一本好书《应用框架的设计与实现 .NET平台》电子工业出版社
  14. java jvm dump文件_各种获取JVM DUMP的方法
  15. cocos2dx[3.x](11)——拖尾渐隐效果MotionStreak
  16. bcm2837linux编程_树莓派gpio接口及编程方法
  17. 高德,微信公众号,企业微信获取定位
  18. 小米笔记本第一排按键功能失灵 [ 解决办法 ]
  19. 灰色预测的MATLAB程序
  20. java贝叶斯_贝叶斯算法Java实现

热门文章

  1. Log4j详细设置说明
  2. oracle存储过程+游标处理select数据
  3. 新手求大神,有其他swit-case的思路写这个程序么?
  4. 解决 用户'sa'登录失败。错误:18456 问题
  5. 各种Exit退出函数用法
  6. JQuery 文本框高亮显示插件
  7. 必然的宿命,绚然的《暗花》
  8. Exception和RuntimeException的区别
  9. C# 特性(Attribute)
  10. 如何才能优雅地书写JS代码