A Journey 2 Eternity

Convert an Icon to a Bitmap image in MFC

Posted on: September 9, 2008

HBITMAP CEMCHotButton::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));
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Pages

Categories

September 2008
M T W T F S S
« Aug   Dec »
1234567
891011121314
15161718192021
22232425262728
2930  

Blog Stats

  • 26,968 hits
%d bloggers like this: