MFC按钮自绘
各位巨佬你们好,我自学MFC,感觉MFC按钮太老套,请问MFC按钮自绘函数怎么写?我自己准备了三个位图(禁用,正常,点击),请问如何将它们绘制到按钮上?
CDC buttonDC; CBitmap bitmapTrans; BITMAP bmp; CDC mem; CRect rc; buttonDC.Attach(lpDrawItemStruct->hDC);//得到用于绘制按钮的DC mem.CreateCompatibleDC(&buttonDC);//准备向按钮区域传输图形 if (lpDrawItemStruct->CtlID == IDC_BUTTON1) { rc = lpDrawItemStruct->rcItem;//获取按钮所占的矩形大小 UINT state = lpDrawItemStruct->itemState;//获取按钮当前的状态,不同状态绘制不同的按钮 if (state & ODS_FOCUS) { //如果按钮已经取得焦点,绘制选中状态下的按钮 bitmapTrans.LoadBitmap(IDB_BITMAP2); bitmapTrans.GetBitmap(&bmp); CBitmap * old = mem.SelectObject(&bitmapTrans); //向按钮传输位图,使用stretcnblt可以使图片随按钮大小而改变 buttonDC.StretchBlt(rc.left, rc.top, rc.right, rc.bottom, &mem, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY); mem.SelectObject(old); bitmapTrans.DeleteObject(); } else { //如果按钮已经取得焦点,绘制选中状态下的按钮 bitmapTrans.LoadBitmap(IDB_BITMAP3); 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); mem.SelectObject(old2); bitmapTrans.DeleteObject(); } }