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

这篇文章是以前的补充:
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.
ValueDescription
ODT_BUTTONOwner-drawn button
ODT_LISTVIEWOwner-draw list view control
ODT_MENUOwner-drawn menu
ODT_TABTab 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.
ValueDescription
ODA_DRAWENTIREThe entire control needs to be drawn.
ODA_FOCUSThe control has lost or gained the keyboard focus. You should check the itemState member to determine whether the control has the focus.
ODA_SELECTThe 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.
ValueDescription
ODS_CHECKEDThe menu item is to be checked. Use this value only in a menu.
ODS_COMBOBOXEDITThe drawing takes place in the edit control of an owner-drawn combo box.
ODS_DEFAULTThe item is the default item.
ODS_DISABLEDThe item is to be drawn as disabled.
ODS_FOCUSThe item has the keyboard focus.
ODS_GRAYEDThe item is to be grayed. Use this value only in a menu.
ODS_SELECTEDThe 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/268668.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

search engine php,用php简单实现search engine friendly的url_php技巧

比如说我用的是虚拟主机,也想实现url优化,但是我没有服务器权限,这时候可以从PATH_INFO来下手.访问http://www.myhost.com/foo.php/a/A/b/B/c/C这个url的时候,如果apache的AllowPathinfo已经打开,用php访问$_SERVER[PATH_INFO]可以获得a/A/b/B/c/C这串字符 这时候再用php加以解…

新书品读《三级网络技术预测试卷与考点解析》,欢迎拍砖、跟砖提建议。

新书品读&#xff1a;《三级网络技术预测试卷与考点解析》已正式出版上市&#xff0c;欢迎拍砖、跟砖提建议。第1章 考前预测试卷11.1 上午试题&#xff08;考试时间120分钟&#xff0c;满分100分&#xff09;1.1.1 笔试试卷一、选择题&#xff08;每小题1分&#xff0c;共60…

PHP外部引用样式,PHP引用外部css有什么好处

PHP引用外部css的好处有&#xff1a;1、网页处理速度会更快一些&#xff1b;2、可以防止一些电脑程度较低的使用者直接看到CSS语法&#xff1b;3、维护方便。PHP引用外部css有什么好处&#xff1f;php页面外部调用css样式表时有三处优点&#xff1a;第一个好处&#xff1a;网页…

程序员最痛苦的事,就是程序出错;程序员最最痛苦的事,就是程序出错了还没有错误信息!--IIS Service Unavailable 问题如何解决...

今天有人问如何处理IIS网站出现“Service Unavailable”的情况。 在网上google了一下&#xff0c;导致这个出错的原因很多&#xff0c;而“Service Unavailable”这个出错信息根本就没有什么价值。程序员最痛苦的事&#xff0c;就是程序出错&#xff1b;程序员最最痛苦的事&…

Swift - 重写导航栏返回按钮

// 重写导航栏返回按钮方法 func configBackBtn() -> Void { // 返回按钮 let backButton UIButton(type: .custom) // 给按钮设置返回箭头图片 backButton.setImage(UIImage(named: "NavigationBar_goBack_icon"), for: .normal) // 设置frame backButton.frame…

java多张图片上传安卓,Android Rxjava+Retrofit2 多图片+文字上传

注意点&#xff1a;方案一中&#xff1a;map.put(“files\”; filename\”” file.getName(), requestBody);方案二中&#xff1a;builder.addFormDataPart(“files”, file.getName(), imageBody)&#xff1b;需与服务器一制,如&#xff1a;Content-Disposition: form-data; …

memcached全面剖析

目录译者序.................................................................................................................................................4第1 章 memcached的基础..............................................................................…

BootstrapVue UI组件

vue升级会遇到各种各样的坑&#xff0c;今天我说的是我遇到的其中一个&#xff1b;vue-strap vue可以引入的UI框架有很多&#xff0c;vue-strap就是其中之一。在vue1.0中vue-strap的表现很好。没有什么毛病。但是在vue2.0中就出现各种问题&#xff0c;在vue2.0中&#xff0c;pr…

php 缩略图 等比例 不失真,PHP自适应宽高度等比例缩略图函数 (无裁切)

对于产品类或者图片类网站来讲&#xff0c;缩略图是一个很重要的应用。其实说来很简单&#xff0c;也就是把大图缩放成一个小图&#xff0c;用于图片的列表展现&#xff0c;这样能够达到用户快速浏览的目的&#xff0c;又能节省带宽。php若是是等比例缩放&#xff0c;好比小图是…

Share Point 开发系列之一:开发方式的选择

Share Point 开发系列虽然做了一段时间的Share point开发,但Share point对于我来说还是新的东西,回想起当时刚开始接触Share point的时候,到处找资料,资料到是找了很多,可是还是不知道如何下手去做Share point开发,也曾经在Cnblog上搜索了很多文章,但是对于我这样的新手来说还是…

php怎么查自己的文件编码,php检测文件编码的方法示例

关于文件编码的检测&#xff0c;很多人建议 mb_detect_encoding 检测&#xff0c;可是不知为何不成功&#xff0c;什么都没输出、看到有人写了个增强版&#xff0c;用 BOM 判断的&#xff0c;我果断就无视了&#xff0c;这东西完全不靠谱、最终根据PHP手册里 mb_detect_encodin…

基于消息与.Net Remoting的分布式处理架构

分布式处理在大型企业应用系统中&#xff0c;最大的优势是将负载分布。通过多台服务器处理多个任务&#xff0c;以优化整个系统的处理能力和运行效率。分布式处理的技术核心是完 成服务与服务之间、服务端与客户端之间的通信。在.Net 1.1中&#xff0c;可以利用Web Service或者…

[USACO07NOV]牛继电器Cow Relays

题目描述 给出一张无向连通图&#xff0c;求S到E经过k条边的最短路。 输入输出样例 输入样例#1&#xff1a;2 6 6 4 11 4 6 4 4 8 8 4 9 6 6 8 2 6 9 3 8 9 输出样例#1&#xff1a;10题解&#xff1a;法1&#xff1a;dpfloyd倍增f[i][j][p]为从i到j经过2^p条边显然f[i][j][p]mi…

oracle 时间小于,jquery easyui 对于开始时间小于结束时间的判断示例

对于开始时间小于结束时间的判断可以参考,jquery easyui里的ValidateBox进行判断好吧!直接上代码查看内容&#xff1a;按时间&#xff1a;至var varify;//用于查询验证,验证开始时间是否小于结束时间function query2(pid){if(varify){startTime2 $(#start2).datetimebox(getVa…