Tuesday, September 9, 2008

Convert an Icon to a Bitmap image in MFC

HBITMAP CHotButton::ConvertIconToBitmap(HICON hIcon)

{

CClientDC clientDC(this);

CDC dc;

dc.CreateCompatibleDC(&clientDC);

CRect rectButton;

GetClientRect (&rectButton);

CSize sizeButton = rectButton.Size();

// bitmap size:

int cx = 0, cy = 0;

if (sizeButton.cy <=16)

{

// bitmap size:

cx = 12;

cy = 12;

}

else if (sizeButton.cy > 16)

{

// bitmap size:

cx = 16;

cy = 16;

}

CBitmap bmp;

bmp.CreateCompatibleBitmap(&clientDC, cx, cy);

CBitmap* pOldBmp = (CBitmap*)dc.SelectObject(&bmp);

::DrawIconEx( dc.GetSafeHdc(), 0, 0, hIcon, cx, cy, 0, (HBRUSH)RGB(192, 192, 192), DI_NORMAL);

dc.SelectObject( pOldBmp );

dc.DeleteDC();

// return the image

return ((HBITMAP)::CopyImage((HANDLE)((HBITMAP)bmp), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_CREATEDIBSECTION));

}

No comments: