4148
- 收藏
- 点赞
- 分享
- 举报
Windows Mobile实现自绘多种状态按钮(Win32)
放在首页是想借助各位从事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;
结构体每项的具体取值如下:(摘自微软文档)
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)
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;
结构体每项的具体取值如下:(摘自微软文档)
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)
我来回答
回答1个
时间排序
认可量排序
认可0
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片![alt](url)
相关问答
-
2010-01-28 10:14:01
-
2010-01-28 10:15:14
-
2008-09-18 14:26:02
-
2012-12-04 13:11:08
-
2012-12-05 11:01:50
-
2012-12-04 11:37:28
-
2012-12-04 13:07:48
-
2008-08-14 01:01:56
-
2012-12-05 13:33:42
-
2012-12-04 12:57:16
-
2008-07-06 21:15:38
-
2010-07-19 15:15:27
-
2012-12-04 11:57:50
-
2012-12-04 11:57:10
-
2012-12-04 11:51:25
-
2012-12-05 14:10:27
-
2012-12-05 14:05:13
-
2008-05-26 22:18:30
-
2012-12-04 11:49:35
-
2012-12-04 13:08:38
更多相似问答
点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
悬赏问答
-
5Hi3516CV610 如何使用SD卡升级固件
-
5cat /dev/logmpp 报错 <3>[ vi] [func]:vi_send_frame_node [line]:99 [info]:vi pic queue is full!
-
50如何获取vpss chn的图像修改后发送至vo
-
5FPGA通过Bt1120传YUV422数据过来,vi接收不到数据——3516dv500
-
50SS928 运行PQtools 拼接 推到设备里有一半画面会异常
-
53536AV100的sample_vdec输出到CVBS显示
-
10海思板子mpp怎么在vi阶段改变视频数据尺寸
-
10HI3559AV100 多摄像头同步模式
-
9海思ss928单路摄像头vio中加入opencv处理并显示
-
10EB-RV1126-BC-191板子运行自己编码的程序
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认