Creating a colored, transparent CStatic
-
I've been looking around in the articles on CStatics as well as in stuff on Bitmaps and the GDI, but I'm not quite finding what I need, so here goes.... What I would like to create is a CStatic-derived class that will make what looks like a partially transparent CStatic. Think of it as a piece of colored glass with text written on it, hovering over the background. I can handle making a CStatic transparent (there are a couple of nifty articles on that). In essence, taking an image of the part of the parent control behind the CStatic and painting that as the background of the CStatic. To be more explicit, here's a code snippet for doing basic transparent CStatic: This is in OnPaint for the CStatic-derived class (so dc is the supplied DC):
CDC\* pdcT; pdcT = new CDC; pdcT->CreateCompatibleDC(&dc); CBitmap bitmap; CBitmap\* pOldBitmap; pOldBitmap = pdcT->SelectObject(&bitmap); dc.BitBlt(0, 0, rc.Width(), rc.Height(), pdcT, 0, 0, SRCCOPY); pdcT->SelectObject(pOldBitmap); delete pdcT;
So what I need to be able to do is modify that "parent" image to add a wash of color, then paint that to the background of the CStatic. It seems that I need to do the modifying of the bitmap after it has been selected into the pdcT, but I don't know where to go on that. Or perhaps I'm missing something that I can do in the BitBlt function to accomplish this. Can it be done? And if so, how? Thanks.
-
I've been looking around in the articles on CStatics as well as in stuff on Bitmaps and the GDI, but I'm not quite finding what I need, so here goes.... What I would like to create is a CStatic-derived class that will make what looks like a partially transparent CStatic. Think of it as a piece of colored glass with text written on it, hovering over the background. I can handle making a CStatic transparent (there are a couple of nifty articles on that). In essence, taking an image of the part of the parent control behind the CStatic and painting that as the background of the CStatic. To be more explicit, here's a code snippet for doing basic transparent CStatic: This is in OnPaint for the CStatic-derived class (so dc is the supplied DC):
CDC\* pdcT; pdcT = new CDC; pdcT->CreateCompatibleDC(&dc); CBitmap bitmap; CBitmap\* pOldBitmap; pOldBitmap = pdcT->SelectObject(&bitmap); dc.BitBlt(0, 0, rc.Width(), rc.Height(), pdcT, 0, 0, SRCCOPY); pdcT->SelectObject(pOldBitmap); delete pdcT;
So what I need to be able to do is modify that "parent" image to add a wash of color, then paint that to the background of the CStatic. It seems that I need to do the modifying of the bitmap after it has been selected into the pdcT, but I don't know where to go on that. Or perhaps I'm missing something that I can do in the BitBlt function to accomplish this. Can it be done? And if so, how? Thanks.
OK, it looks like I solved my own problem. Just in case anyone else is interested, here's what I found:
CPaintDC dc(this); //get a DC for the control
//The next several lines create a compatible bitmap in memory
// and draw a solid light green "bitmap" on it,
// then select that bitmap into the DC in memory
pDCMem = new CDC;
pDCMem->CreateCompatibleDC(&dc);
bmp.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
pOldBitmap = pDCMem->SelectObject(&bmp);
CBrush brush;
brush.CreateSolidBrush(RGB(122,255,122));
pDCMem->FillRect(rc, &brush);
//After drawing on the DC in memory (text and whatever else)
// use BitBlt to merge the bitmap in memory with what's on screen
dc.BitBlt(0,0,rc.Width(),rc.Height(),pDCMem,0,0,SRCAND);
//SRCAND combines the src and dest DCs using boolean AND
//Then clean up
pDCMem->SelectObject ( pOldBitmap ) ;
delete pDCMem;And it works. Now the CStatic is light green, with text on it (which I did not include in the code snippet) and the bitmap "behind" it can be seen greenishly colored through it.