OnDrawItem
注意如果在CButton基础上有虚函数
在类内直接通过该消息修改外观
有了hDC,CDC也有了,CDC有了,自绘还会远吗?
void CTestDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC ButtonDC;
CBitmap bitmapTrans;
BITMAP bmp;
CRect rc;
CDC mem;
ButtonDC.Attach(lpDrawItemStruct->hDC);
mem.CreateCompatibleDC(&ButtonDC);
rc=lpDrawItemStruct->rcItem;
UINT state = lpDrawItemStruct->itemState;
if(state & ODS_FOCUS)
{
bitmapTrans.LoadBitmap(IDB_GETFOCUS);
bitmapTrans.GetBitmap(&bmp);
CBitmap * old=mem.SelectObject(&bitmapTrans);
ButtonDC.StretchBlt(rc.left,rc.top,rc.right,rc.bottom,&mem,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
mem.SelectObject(old);
bitmapTrans.DeleteObject();
ButtonDC.SetBkMode(TRANSPARENT);
ButtonDC.DrawText("已选中",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
{
bitmapTrans.LoadBitmap(IDB_GETFOCUS);
CBitmap *old2 =mem.SelectObject(&bitmapTrans);
bitmapTrans.GetBitmap(&bmp);
CBitmap *old=mem.SelectObject(&bitmapTrans);
ButtonDC.StretchBlt(rc.left,rc.top,rc.right,rc.bottom,&mem,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
ButtonDC.SetBkMode(TRANSPARENT);
ButtonDC.DrawText("未选中",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
mem.SelectObject(old2);
bitmapTrans.DeleteObject();
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
评论