|
1 在控件中添加普通的button控件,并设置属性——>Owner Draw为True
2 在消息处理中,对该button控件进行重绘,其中iCtrlID为控件的ID
- case WM_DRAWITEM:
- DrawImgButton(HWND hDlg, WPARAM wParam, LPARAM lParam,int iCtrlID,HBITMAP bitmap);
复制代码
3 在自定义的函数DrawImgButton()中,
1)位图输出相关
从lParam参数中取出如下参数,
dc= lpDIS->hDC 设备描述符
itemRectlpDIS->rcItem 绘图区域
创建位图的内存描述符hMemDC并关联
HDC hMemDC = CreateCompatibleDC(dc);
2)button按钮动作相关
从Iparam中取出如下参数
lpDIS->itemAction 控件动作
lpDIS->itemState 控件状态
来判断按钮的按下、释放状态
利用StretchBlt函数来输出位图到按钮所在区域,这样就完成了自绘按钮,示例代码如下:
- DrawImgButton(HWND hDlg, WPARAM wParam, LPARAM lParam,int iCtrlID,HBITMAP bitmap)
- {
- LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)lParam;
- HDC dc = lpDIS->hDC;
- RECT itemRect = lpDIS->rcItem;
- HDC hMemDC = CreateCompatibleDC(dc);
- SelectObject(hMemDC, bitmap);
- if(lpDIS->CtlID == 0)
- {
- return (0);
- }
- else if(lpDIS->CtlID == iCtrlID)
- {
- if((lpDIS->itemAction & ODA_DRAWENTIRE))//|| ((lpDIS->itemAction | ODA_SELECT) && (lpDIS->itemState == ODS_FOCUS) ) )
- {
- UINT uState = DFCS_BUTTONPUSH |
- ((bIsPressed) ? DFCS_PUSHED : 0);
-
- SetStretchBltMode(dc,COLORONCOLOR);
- StretchBlt(dc,
- itemRect.left,itemRect.top, itemRect.right-itemRect.left, itemRect.bottom-itemRect.top,
- hMemDC,
- 0,0, 70,70,
- SRCCOPY);
-
- }
- else if( (lpDIS->itemAction | ODA_SELECT) && (lpDIS->itemState == (ODS_FOCUS | ODS_SELECTED) ) )
- {
- SetStretchBltMode(dc,COLORONCOLOR);
- StretchBlt(dc,
- itemRect.left,itemRect.top, itemRect.right-itemRect.left, itemRect.bottom-itemRect.top,
- hMemDC,
- 0,0, 70,70,
- SRCCOPY);
- }
- DeleteDC(hMemDC);
- }
- //pDCUnpress->DeleteDC();
- return (INT_PTR)FALSE;
- }
复制代码 |
|