lpDrawItemStruct->hDC);
//得到绘制的设备环境CDC
VERIFY(lpDrawItemStruct- >CtlType==ODT_BUTTON);
if (lpDrawItemStruct- >itemAction & ODA_DRAWENTIRE){
//重绘控件(正常状态)
mBtnStats=BTN_NORMAL;
if (m_pBitmap!=0){
CDC memDC;
memDC.CreateCompatibleDC(&dc);
memDC.SelectObject(m_pBitmap);
dc.BitBlt(0, 0, rect.Width(), rect.Height(),
&memDC, 0, 0, SRCCOPY);
memDC.DeleteDC();
}
//显示按键标题
GetWindowText(sCaption);
dc.SetBkMode(TRANSPARENT);
if (m_pFont!=0){
CFont* m_pOldFont=dc.SelectObject(m_pFont);
dc.DrawText(sCaption,&rect,
DT_CENTER|DT_VCENTER|DT_SINGLELINE);
dc.SelectObject(m_pOldFont);
}
}
if ((lpDrawItemStruct- >itemState & ODS_SELECTED) &&
(lpDrawItemStruct- >itemAction & ODA_SELECT)){
//按下鼠标
mBtnStats=BTN_DOWN;
dc.Draw3dRect(&rect,RGB(128,128,128),RGB(192,192,192));
rect.top=rect.top+1;rect.bottom=rect.bottom-1;
rect.left=rect.left+1;rect.right=rect.right-1;
dc.Draw3dRect(&rect,RGB(0,0,0),RGB(255,255,255));
}
if(!(lpDrawItemStruct- >itemState & ODS_SELECTED) &&
(lpDrawItemStruct- >itemAction & ODA_SELECT)){
//释放鼠标或鼠标进入按键区域
mBtnStats=BTN_UP;
dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0));
rect.top=rect.top+1;rect.bottom=rect.bottom-1;
rect.left=rect.left+1;rect.right=rect.right-1;
dc.Draw3dRect(&rect,RGB(192,192,192),RGB(128,128,128));
}
dc.Detach();
}
接着就必须一些初始化工作,其中最关键就是把被按键覆盖了的区域保存进CBitmap类中,我们知道CDC::StretchBlt()函数可以把位图的指定区域从一个设备拷贝到另一个设备中,这样可以很方便地把窗口或对话框的某个区域保存,条件是获得其DC:
void CDrawButton::LoadBack(CWnd *pParent)
{
ASSERT(GetStyle() & BS_OWNERDRAW);
if (m_pBitmap!=0) return;
CRect rect;
GetWindowRect(&rect