日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

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

發布時間:2023/12/10 windows 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的Windows Mobile 6.0下实现自绘多种状态按钮(Win32) 续的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。