Tuesday, September 9, 2008

Draw a Disabled Bitmap into the destination DC in MFC

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;

},

{

{ 0x00, 0x00, 0x00, 0x00 },

{ 0xFF, 0xFF, 0xFF, 0x00 }

}

};

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));

}

}

No comments: