Draw bitmap with grayed 3D effect
Posted
by Jean-Edouard Lachand-Robert
on August 5th, 1998
//
// DitherBlt : Draw a bitmap dithered (3D grayed effect like disabled buttons in toolbars) into a destination DC
// Author : Jean-Edouard Lachand-Robert (iamwired@geocities.com), June 1997.
//
// hdcDest : destination DC
// nXDest : x coordinate of the upper left corner of the destination rectangle into the DC
// nYDest : y coordinate of the upper left corner of the destination rectangle into the DC
// nWidth : width of the destination rectangle into the DC
// nHeight : height of the destination rectangle into the DC
// hbm : the bitmap to draw (as a part or as a whole)
// nXSrc : x coordinates of the upper left corner of the source rectangle into the bitmap
// nYSrc : y coordinates of the upper left corner of the source rectangle into the bitmap
//
void DitherBlt (HDC hdcDest, int nXDest, int nYDest, int nWidth,
int nHeight, HBITMAP hbm, int nXSrc, int nYSrc)
{
ASSERT(hdcDest && hbm);
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 hbmBW = CreateDIBSection(bwDC,
(LPBITMAPINFO)&RGBBWBITMAPINFO, DIB_RGB_COLORS, &pbitsBW, NULL, 0);
ASSERT(hbmBW);
if (hbmBW)
{
// Attach the monochrome DIB section and the bitmap to the DCs
SelectObject(bwDC, hbmBW);
SelectObject(hDC, hbm);
// 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));
}
VERIFY(DeleteDC(bwDC));
}
VERIFY(DeleteDC(hDC));
}
}

Comments
MFC code here - method taking CBitmap&
Posted by inbugable on 01/04/2006 11:16pmvoid GTMainFrame::MakeDisabled(CBitmap &ioBM) { // For MFC - Adapted by Koen Zagers from Zundert CDC dc; CDC dcBW; // Create memory DCs dc.CreateCompatibleDC(NULL); dcBW.CreateCompatibleDC(NULL); // Get bitmap dimensions BITMAP bm; ioBM.GetBitmap(&bm); int nWidth = bm.bmWidth; int nHeight = bm.bmHeight; // Create a 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 hbmBW = CreateDIBSection(dcBW.m_hDC, (LPBITMAPINFO) &RGBBWBITMAPINFO, DIB_RGB_COLORS, &pbitsBW, NULL, 0); ASSERT(hbmBW); if (hbmBW) { // Attach the monochrome DIB section and the bitmap to the DCs SelectObject(dcBW.m_hDC, hbmBW); SelectObject(dc.m_hDC, ioBM.m_hObject); CBrush brush; // BitBlt the bitmap into the monochrome DIB section dcBW.BitBlt(0, 0, nWidth, nHeight, &dc, 0, 0, SRCCOPY); // Paint the destination bitmap in gray brush.CreateSysColorBrush(COLOR_3DFACE); dc.FillRect(CRect(0, 0, nWidth, nHeight), &brush); // 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 brush.DeleteObject(); brush.CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT)); dc.SelectObject(&brush); dc.BitBlt(1, 1, nWidth, nHeight, &dcBW, 0, 0, 0xB8074A); // BitBlt the black bits in the monochrome bitmap into COLOR_3DSHADOW bits in the destination DC brush.DeleteObject(); brush.CreateSolidBrush(GetSysColor(COLOR_3DSHADOW)); dc.SelectObject(&brush); dc.BitBlt(0, 0, nWidth, nHeight, &dcBW, 0, 0, 0xB8074A); brush.DeleteObject(); DeleteObject(hbmBW); } }ReplyCode stolen !
Posted by Legacy on 11/25/2003 12:00amOriginally posted by: Jeff
Like many code samples from this site, the code has been copyed from elsewhere : in this case : WTL source code !
Shame on the "author"...
-
ReplyProve that it is a stollen code
Posted by TheCric on 09/07/2005 04:13pm1st) Prove what you are saying . Why shouldn't the author be the original one! 2nd) This source doesn't use use the WTL anyway so it is useful like that!!
ReplyTo correct the bug in C++
Posted by Legacy on 01/29/2003 12:00amOriginally posted by: Leandro Gustavo Biss Becker
Hi
To correct the bug, after the last delete object inside the if, put DeleteObject(hbmBW);
ReplyCan you tell me how to draw it transparently?
Posted by Legacy on 08/14/2001 12:00amOriginally posted by: niry
How to draw 3D-gray effect transparently?
ReplyThis code don't work properly. It 'eats' memory
Posted by Legacy on 03/23/2000 12:00amOriginally posted by: Javier Santisteban
ReplyThis code don't work properly. It 'eats' memory
Posted by Legacy on 03/23/2000 12:00amOriginally posted by: Javier Santisteban
ReplyNot working
Posted by Legacy on 10/29/1999 12:00amOriginally posted by: satish ks
It is not working in 24 bit bitmap.
Reply