BOOL GuiKit::TransparentBlt98( HDC hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int hHeightDest,
HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, UINT crTransparent )
{
COLORREF cColor;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcSave;
POINT ptSize;
ptSize.x = nWidthDest; // Get width of bitmap
ptSize.y = hHeightDest; // Get height of bitmap
DPtoLP(hdcSrc, &ptSize, 1); // Convert from device
// to logical points
// Create some DCs to hold temporary data.
hdcBack = CreateCompatibleDC(hdcDest);
hdcObject = CreateCompatibleDC(hdcDest);
hdcMem = CreateCompatibleDC(hdcDest);
hdcSave = CreateCompatibleDC(hdcDest);
// 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(hdcDest, ptSize.x, ptSize.y);
bmSave = CreateCompatibleBitmap(hdcDest, 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(hdcSrc, GetMapMode(hdcDest));
// Save the bitmap sent here, because it will be overwritten.
//BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcSrc, 0, 0, SRCCOPY);
BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcSrc, nXOriginSrc, nYOriginSrc, 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(hdcSrc, crTransparent);
// 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, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
// Set the background color of the source DC back to the original
// color.
SetBkColor(hdcSrc, 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, hdcDest, nXOriginDest, nYOriginDest, 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(hdcSrc, nXOriginSrc, nYOriginSrc, 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, hdcSrc, nXOriginSrc, nYOriginSrc, SRCPAINT);
// Copy the destination to the screen.
BitBlt(hdcDest, nXOriginDest, nYOriginDest, ptSize.x, ptSize.y, hdcMem, 0, 0, SRCCOPY);
// Place the original bitmap back into the bitmap sent here.
BitBlt(hdcSrc, nXOriginSrc, nYOriginSrc, 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);
return TRUE;
}
HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, UINT crTransparent )
{
COLORREF cColor;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcSave;
POINT ptSize;
ptSize.x = nWidthDest; // Get width of bitmap
ptSize.y = hHeightDest; // Get height of bitmap
DPtoLP(hdcSrc, &ptSize, 1); // Convert from device
// to logical points
// Create some DCs to hold temporary data.
hdcBack = CreateCompatibleDC(hdcDest);
hdcObject = CreateCompatibleDC(hdcDest);
hdcMem = CreateCompatibleDC(hdcDest);
hdcSave = CreateCompatibleDC(hdcDest);
// 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(hdcDest, ptSize.x, ptSize.y);
bmSave = CreateCompatibleBitmap(hdcDest, 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(hdcSrc, GetMapMode(hdcDest));
// Save the bitmap sent here, because it will be overwritten.
//BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcSrc, 0, 0, SRCCOPY);
BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcSrc, nXOriginSrc, nYOriginSrc, 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(hdcSrc, crTransparent);
// 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, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
// Set the background color of the source DC back to the original
// color.
SetBkColor(hdcSrc, 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, hdcDest, nXOriginDest, nYOriginDest, 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(hdcSrc, nXOriginSrc, nYOriginSrc, 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, hdcSrc, nXOriginSrc, nYOriginSrc, SRCPAINT);
// Copy the destination to the screen.
BitBlt(hdcDest, nXOriginDest, nYOriginDest, ptSize.x, ptSize.y, hdcMem, 0, 0, SRCCOPY);
// Place the original bitmap back into the bitmap sent here.
BitBlt(hdcSrc, nXOriginSrc, nYOriginSrc, 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);
return TRUE;
}
웹에서 서핑해서 긁어 오면 다 되는것이 아니었다.
위의 소스는 긁어 온것이 정상적인 TransparentBlt기능을 못하여 수정하고 테스트한 결과이다.
'Development' 카테고리의 다른 글
[Development] FPS 구하기 ( Frame Per Second ) (0) | 2008.11.30 |
---|---|
[STL/string] 문자열 뒤집기 (0) | 2008.11.26 |
[Development]WinMgr (0) | 2008.10.21 |
dll 관련 (0) | 2008.10.19 |
[Eclipse]subclipse(Eclipse용 Subversion) (0) | 2008.10.09 |