A Journey 2 Eternity

Draw a Disabled Bitmap into the destination DC in MFC

Posted on: September 9, 2008

void CEMCHotButton::DisabledBlt (HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HBITMAP hBitmap, int nXSrc, int nYSrc)
{
	ASSERT (hdcDest && hBitmap);
	ASSERT (nWidth > 0 && nHeight > 0);

	// Create a generic DC for all BitBlts
	HDC hDC = CreateCompatibleDC (hdcDest);
	ASSERT (hDC);

	if (hDC) {
		// Create a DC for the monochrome DIB section
		HDC bwDC = CreateCompatibleDC(hDC);
		ASSERT (bwDC);

		if (bwDC) {
			// Create the monochrome DIB section with a black and white palette
			struct
			{
				BITMAPINFOHEADER bmiHeader;
				RGBQUAD bmiColors[2];
			} RGBBWBITMAPINFO = 
			{
				{ // a BITMAPINFOHEADER
					sizeof(BITMAPINFOHEADER), // biSize
					nWidth, // biWidth;
					nHeight, // biHeight;
					1, // biPlanes;
					1, // biBitCount
					BI_RGB, // biCompression;
					0, // biSizeImage;
					0, // biXPelsPerMeter;
					0, // biYPelsPerMeter;
					0, // biClrUsed;
					0 // biClrImportant;
				},
				{
					{ 0×00, 0×00, 0×00, 0×00 },
					{ 0xFF, 0xFF, 0xFF, 0×00 }
				}
			};

			void* pbitsBW;
			HBITMAP hBitmapBW = CreateDIBSection (bwDC, (LPBITMAPINFO) &RGBBWBITMAPINFO, DIB_RGB_COLORS, &pbitsBW, NULL, 0);
			ASSERT (hBitmapBW);

			if (hBitmapBW) {
				// Attach the monochrome DIB section and the bitmap to the DCs
				SelectObject (bwDC, hBitmapBW);
				SelectObject (hDC, hBitmap);

				// BitBlt the bitmap into the monochrome DIB section
				BitBlt (bwDC, 0, 0, nWidth, nHeight, hDC, nXSrc, nYSrc, SRCCOPY);

				// Paint the destination rectangle in gray
				FillRect (hdcDest, CRect (nXDest, nYDest, nXDest + nWidth, nYDest + nHeight), GetSysColorBrush (COLOR_3DFACE));

				// BitBlt the black bits in the monochrome bitmap into COLOR_3DHILIGHT bits in the destination DC
				// The magic ROP comes from the Charles Petzold’s book

				HBRUSH hb = CreateSolidBrush (GetSysColor (COLOR_3DHILIGHT));
				HBRUSH oldBrush = (HBRUSH) SelectObject (hdcDest, hb);
				BitBlt (hdcDest, nXDest + 1, nYDest + 1, nWidth, nHeight, bwDC, 0, 0, 0xB8074A);

				// BitBlt the black bits in the monochrome bitmap into COLOR_3DSHADOW bits in the destination DC
				hb = CreateSolidBrush (GetSysColor (COLOR_3DSHADOW));
				DeleteObject (SelectObject(hdcDest, hb));

				BitBlt (hdcDest, nXDest, nYDest, nWidth, nHeight, bwDC, 0, 0, 0xB8074A);

				DeleteObject (SelectObject (hdcDest, oldBrush));
				DeleteObject (hBitmapBW);
			}

			VERIFY (DeleteDC (bwDC));
		}

		VERIFY (DeleteDC(hDC));
	}
}
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: