|
Just noticed an error here, the Create call has to have ILC_COLOR24 | ILC_MASK if you still want to use transparency. The initial example only works because TRUE and ILC_MASK happen to both be 1.
|
|
|
|
|
Hi, I appreciate your efforts in attempting to share this with all but couldn't you have tested it properly first?
I have just wasted the best part of an hour trying to get it to work!
|
|
|
|
|
So how about telling the rest of us what you did to get it to work?
|
|
|
|
|
I had the same problem, here's how I solved it
----------- In CMyDlg::DrawTransparentBitmap -----------
...
<br />
CBitmap* hbmT = hdcMem.SelectObject(&m_bmpMask);<br />
<br />
pDC->BitBlt( xStart, yStart, wWidth, wHeight, &hdcMem,<br />
xSource, ySource, SRCAND);<br />
<br />
<br />
hdcMem.SelectObject(&m_bmpPlmain);
pDC->BitBlt(xStart, yStart, wWidth, wHeight, pTmpDC,<br />
xSource, ySource, SRCPAINT);<br />
...
------------------
Best Regards
Sergio Del Valle
|
|
|
|
|
Hi,
This code was very helpful. However, I had to fix a small problem to get it to work correctly. Before BitBlt-ing the monochrome mask onto the destination DC, you must set the background color of the destination DC to white and the text color to black. Then, when the mask is converted to color before the actual blt, the colors are such that the SRCAND raster op has the desired effect.
If I'm in error, I apologize. It does seem like somebody would have seen this earlier.
dave
|
|
|
|
|
You are correct. Spot-on.
Without doing the above, one ends up with odd results - what appeared on my machine (Vista 32 running default theme) as a smeared look to the resulting image (I adapted the above code along with CImageButtonWithStyle, also on CodeProject) to vastly improve the look of those buttons, and fix the problems XP has with themed BS_PUSH_LIKE buttons.
I found two things necessary to fix this example code to fully work:
1. Apply David's "problem" above.
2. Copy the source bitmap to a target-device compatible DC, and always use the GetPixel to extract the clipping color (rather than rely upon RGB(n,n,n)), as the color of the pixel is defined by the pixel depth.
Now that I'm using a target-dc compatible source bitmap and I'm setting the target DC's text & bkcolor's before clipping it, I find that all works perfectly.
Cheers!
Steve Wolf
|
|
|
|
|
I'm trying to use the code you posted, but I'm getting a whole bunch of errors as soon as I include CMemDC.h file. Errors start with the line:
CMemDC(CDC* pDC ) : CDC() { pDC->GetClipBox(&m_rect); Construct(pDC); }
I tried several versions of the file, useless.
First error:
error C2059: syntax error : ')'
pointing to the line above.
Please help!
|
|
|
|
|
Add this function to your code and then call it.
The following code could be used to call this function
// IDB_BITMAP1 is the resource id
HBITMAP bmA = ::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1));
// Source dc
ClientDC dc(this);
// X coord. to draw the bitmap on the distination dc
short xStart = 0;
// Y coord. to draw the bitmap on the distination dc
short yStart = 0;
// The color inside the HBITMAP that should be transparent
COLORREF cTransparentColor = RGB(255,0,255);
DrawTransparentBitmap(dc.m_hDC, bmA, xStart, yStart, cTransparentColor);
------------------------------------------------------------
void CRollupCtrl::DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap, short xStart,
short yStart, COLORREF cTransparentColor)
{
BITMAP bm;
COLORREF cColor;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
POINT ptSize;
hdcTemp = CreateCompatibleDC(hdc);
SelectObject(hdcTemp, hBitmap); // Select the bitmap
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
ptSize.x = bm.bmWidth; // Get width of bitmap
ptSize.y = bm.bmHeight; // Get height of bitmap
DPtoLP(hdcTemp, &ptSize, 1); // Convert from device
// to logical points
// Create some DCs to hold temporary data.
hdcBack = CreateCompatibleDC(hdc);
hdcObject = CreateCompatibleDC(hdc);
hdcMem = CreateCompatibleDC(hdc);
hdcSave = CreateCompatibleDC(hdc);
// Create a bitmap for each DC. DCs are required for a number of
// GDI functions.
// Monochrome DC
bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
// Monochrome DC
bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
// Each DC must select a bitmap object to store pixel data.
bmBackOld = (HBITMAP)SelectObject(hdcBack, bmAndBack);
bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject);
bmMemOld = (HBITMAP)SelectObject(hdcMem, bmAndMem);
bmSaveOld = (HBITMAP)SelectObject(hdcSave, bmSave);
// Set proper mapping mode.
SetMapMode(hdcTemp, GetMapMode(hdc));
// Save the bitmap sent here, because it will be overwritten.
BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
// Set the background color of the source DC to the color.
// contained in the parts of the bitmap that should be transparent
cColor = SetBkColor(hdcTemp, cTransparentColor);
// Create the object mask for the bitmap by performing a BitBlt
// from the source bitmap to a monochrome bitmap.
BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
SRCCOPY);
// Set the background color of the source DC back to the original
// color.
SetBkColor(hdcTemp, cColor);
// Create the inverse of the object mask.
BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
NOTSRCCOPY);
// Copy the background of the main DC to the destination.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
SRCCOPY);
// Mask out the places where the bitmap will be placed.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
// Mask out the transparent colored pixels on the bitmap.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
// XOR the bitmap with the background on the destination DC.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
// Copy the destination to the screen.
BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
SRCCOPY);
// Place the original bitmap back into the bitmap sent here.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
// Delete the memory bitmaps.
DeleteObject(SelectObject(hdcBack, bmBackOld));
DeleteObject(SelectObject(hdcObject, bmObjectOld));
DeleteObject(SelectObject(hdcMem, bmMemOld));
DeleteObject(SelectObject(hdcSave, bmSaveOld));
// Delete the memory DCs.
DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);
}
|
|
|
|
|
The word CRollupCtrl should be replaced by the name of the class in which you are implementing this function.
|
|
|
|
|
This is a best way, thank you. The way the author presented doesn't work well, the colorts of my bitmap get changed.
Late
|
|
|
|
|
thank you for your best code~!!
|
|
|
|
|
Hours dealing with the sample code and copy paste with Tim @ Fit code, god bless you buddy.
|
|
|
|
|
I was trying to attach several images to one file, and bmp, jpg worked well.. but icon didn't work as intended.
bool CxImageICO::Decode(CxFile *hFile)
{
...
hFile->Seek(icon_list[page].dwImageOffset, SEEK_SET);
...
}
because this code reset current reading point to first of file, invalid data will be read if other data exists before icon images.
so I patched like this (yeah, I referenced ximajpg.cpp's decode func.)
bool CxImageICO::Decode(CxFile *hFile)
{
DWORD off = hFile->Tell();
...
hFile->Seek(off + icon_list[page].dwImageOffset, SEEK_SET);
..
}
simple. but works well =)
and... WMF file also has same problem -_-;;
after searching bug, I'll post again.
|
|
|
|
|
sorry posted to wrong position.. but can't delete this msg, cuz I didn't logged on when posted =_=;
|
|
|
|
|
NULL is as same value as black(0x00000000) color.
Though user specify black as a clrpTransColor value,
if (clrpTransColor == NULL)
{
clrTrans = hdcSrc.GetPixel(iTransPixelX, iTransPixelY);
}
This code will regard it as default value NULL.
As valid RGB value is from 0x00000000 to 0x00FFFFFF, I suggest default value 0xFF000000 or bigger than that.
|
|
|
|
|
Hi everyone
I try to do a random function on the pictures of my pocket pc 2002 (does with embedded visual c++), the pictures do not change even when the whole program itself reloaded!!
can you help me please to know why ????
my e-mail is: nelaji@yahoo.com
thank-you!
nelaji@yahoo.com
|
|
|
|
|
Hi everyone
I try to do a random function on the pictures of my pocket pc 2002 (does with embedde visual c + +), the pictures do not change even when the whole program itself reloaded!!
can you help me please to know why ????
my e-mail is: nelaji@yahoo.com
thank-you!
nelaji@yahoo.com
|
|
|
|
|
Hi everyone : -)
I try to do a random function on the pictures of my pocket pc 2002 (does with embedde visual c + +), the pictures do not change even when the whole program itself reloaded!!
can you help me please to know why : - ( (
my e-mail is: nelaji@yahoo.com
thank-you
nelaji@yahoo.com
|
|
|
|
|
Hi plese if any one knows where to download the above said please inform me as i'm in very need of it as i've got a project in vc++7.0 and in need to convert it to vc ++ 6.0 as i'm working on loading PNG's from resource using GDI+ in VC++ 6.0 Please Help
Thanks & Regards
|
|
|
|
|
Awesome code, has really helped me quite a bit!
I'm trying to add a background image to a tree control that i'm using, and i'm having a few problems. Here's my current line of thought: Create two transparent images, one of the tree control text (generated by calling CWnd::DefWindowProc) and one of the background image. Then, simply combine these two images, and finally copy them out to the main dc in OnPaint.
This doesn't seem to working quite right, i tried just creating another mask in the paint method with white being the mask color, but nothing is showing through...
I guess i would just like to know if this is possible, and should i continue with trying to add a background image this way??
If my line of thinking is correct, then i assume i could just create two temporary mem DC's and copy the two transparent images into these DC's. I'm just a bit confused on how i might BitBlt the two temp DC's together(SRCPAINT???) and finally copy the combined dc out to the main paint DC...
Any suggestions??
|
|
|
|
|
Some compile errors - CMemDC should be CDC
m_bmpPlmain was not used consistently.
But it's just what I needed.
Thanks
|
|
|
|
|
I got this code working fine, for my static bitmap. However, it's in a 'designer' where the user can resize the bitmap, hence I need to get a StretchBlt in here somewhere.
I think I need to StretchBlt m_bmpPLMain into itself, then PrepareMask and DrawTransparentBitmap all in my draw event. However, try as I might, I cannot get StretchBlt to work.
Sometimes I get the original bitmap masked, but not resized. Sometimes I get the non-bitmap area of the rectangle to resize (you can see my underlying drawing grid resize, but not the bitmap).
Can someone give me some pointers?
Cheers,
Dan
|
|
|
|
|
Progress: I now have StretchBlt after DisplayTransparentBitmap, it works but displays the black-masked original bitmap.
I was trying to StretchBlt the composite (i.e. transparent image). But obviously, that is not the way to go....or is it?
|
|
|
|
|
Sussed it...change the BitBlts in DrawTransparentBitmap to StretchBlts.
Lovely
|
|
|
|
|
How do i display the pixels using 'C' with using Microsoft visual studio's complier?
|
|
|
|
|