|
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?
|
|
|
|
|
msdn.microsoft.com. Seeing a MFC is a C++ class library, this is really not the place to ask, you should ask in the C++ forum, the closest thing we have to a C forum. But in this case, you're asking something that you can easily find out for yourself. You basically want to create a HDC, a HPEN and then use ::SelectObject to get the pen into the HDC, you can then draw to your hearts content. the msdn website will fill the gaps.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Hi, I got the function to work fine, but when I try to make the bitmap animate across the screen it flickers as it's moving. Anyway to avoid this flicker?
Thanks in advance...
|
|
|
|
|
Draw in OnEraseBackground, and try drawing to a memory DC which you then draw to the screen.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Great code, but do you know if there is any way of doing this on a CStatic object?
JT.
|
|
|
|
|
Hi,
i am working on Wiz97 model. In the Background Watermark Bitmap, When i place the ReadOnly Listbox( for Licence Infor). then i cannt see the content of the Lice Info.
Can u help me....
Rajesh. S
|
|
|
|
|
Hi as a beginner in VC, i woul like to know is it possible to animate the movement of a bitmap from pt x to pt y to and fro on a SDI prj CView like the following :
pt X---------------------->pt Y
<----------------------
if is possbile how can i do it, i have loaded in the bmp using the CPicture class so if possible how can i do it pls i am in a rush.
any ref or example will be greatly appreciated
|
|
|
|
|
Create coordinates that change every time you draw the bitmap, then use something like a timer to re-draw your bitmap.
<><><><><><><><><><><><><><>
Matthew R. Miller
mattrmiller@computersmarts.net
www.computersmarts.net
|
|
|
|
|