//이건 기본 지정되있는 API이며 Msimg32.lib 이넘을 링크해야 사용가능
TransparentBlt(mydc->m_hDC, 0, 0, m_ButtonSize.cx, m_ButtonSize.cy, pMemDC->m_hDC, 0, 0, m_ButtonSize.cx, m_ButtonSize.cy, RGB(255,0,255));
// 이 아래는 커스텀형식의 마스크이미지를 자동으로 만들고 투명처리하는 함수..
// 1번과 2번...뭐 암거나...사용
TransparentBlt1(mydc->m_hDC, 0, 0, pMemDC->m_hDC, m_ButtonSize.cx, m_ButtonSize.cy, RGB(255,0,255));
TransparentBlt2(mydc, 0, 0, m_ButtonSize.cx, m_ButtonSize.cy, pMemDC, RGB(255,0,255));
void TransparentBlt1(HDC hdc, int xStart, int yStart, HDC hdcTemp, int nWidth, int nHeight, COLORREF cTransparentColor)
{
COLORREF cColor;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcSave;
POINT ptSize;
ptSize.x = nWidth;
ptSize.y = nHeight;
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.
// 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);
}
void TransparentBlt2(CDC *destDC, int nX, int nY, int nW, int nH, CDC *buffDC, COLORREF crTransparent)
{
CDC *dc = GetDC();
CDC dcTemp, dcMask2, dcMask3;
CBitmap bitmapTemp, *bitmapTempOld;
CBitmap bitmapMask2, *bitmapMask2Old;
CBitmap bitmapMask3, *bitmapMask3Old;
// DC 만드는 평범한...
dcTemp.CreateCompatibleDC(dc);
bitmapTemp.CreateCompatibleBitmap(dc, nW, nH);
bitmapTempOld = dcTemp.SelectObject(&bitmapTemp);
dcMask2.CreateCompatibleDC(dc);
bitmapMask2.CreateBitmap(nW, nH, 1, 1, NULL);
bitmapMask2Old = dcMask2.SelectObject(&bitmapMask2);
dcMask3.CreateCompatibleDC(dc);
bitmapMask3.CreateBitmap(nW, nH, 1, 1, NULL);
bitmapMask3Old = dcMask3.SelectObject(&bitmapMask3);
// 마스크 패턴 생성. 투명색 부분만 백색으로 나머지는 검은색으로
COLORREF crOldBack = buffDC->SetBkColor(crTransparent);
dcMask3.BitBlt(0, 0, nW, nH, buffDC, 0, 0, SRCCOPY);
buffDC->SetBkColor(crOldBack);
destDC->BitBlt(nX, nY, nW, nH, &dcMask3, 0, 0, SRCAND);
dcMask3.PatBlt(0, 0, nW, nH, PATINVERT);
dcTemp.BitBlt(0, 0, nW, nH, buffDC, 0, 0, SRCCOPY);
dcTemp.BitBlt(0, 0, nW, nH, &dcMask3, 0, 0, SRCAND);
destDC->BitBlt(nX, nY, nW, nH, &dcTemp, 0, 0, SRCPAINT);
// 리소스 반환
dcMask3.SelectObject(bitmapMask3Old);
dcMask3.DeleteDC();
bitmapMask3.DeleteObject();
dcTemp.SelectObject(bitmapTempOld);
dcTemp.DeleteDC();
bitmapTemp.DeleteObject();
dcMask2.SelectObject(bitmapMask2Old);
dcMask2.DeleteDC();
bitmapMask2.DeleteObject();
ReleaseDC(dc);
}
TransparentBlt(mydc->m_hDC, 0, 0, m_ButtonSize.cx, m_ButtonSize.cy, pMemDC->m_hDC, 0, 0, m_ButtonSize.cx, m_ButtonSize.cy, RGB(255,0,255));
// 이 아래는 커스텀형식의 마스크이미지를 자동으로 만들고 투명처리하는 함수..
// 1번과 2번...뭐 암거나...사용
TransparentBlt1(mydc->m_hDC, 0, 0, pMemDC->m_hDC, m_ButtonSize.cx, m_ButtonSize.cy, RGB(255,0,255));
TransparentBlt2(mydc, 0, 0, m_ButtonSize.cx, m_ButtonSize.cy, pMemDC, RGB(255,0,255));
void TransparentBlt1(HDC hdc, int xStart, int yStart, HDC hdcTemp, int nWidth, int nHeight, COLORREF cTransparentColor)
{
COLORREF cColor;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcSave;
POINT ptSize;
ptSize.x = nWidth;
ptSize.y = nHeight;
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.
// 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);
}
void TransparentBlt2(CDC *destDC, int nX, int nY, int nW, int nH, CDC *buffDC, COLORREF crTransparent)
{
CDC *dc = GetDC();
CDC dcTemp, dcMask2, dcMask3;
CBitmap bitmapTemp, *bitmapTempOld;
CBitmap bitmapMask2, *bitmapMask2Old;
CBitmap bitmapMask3, *bitmapMask3Old;
// DC 만드는 평범한...
dcTemp.CreateCompatibleDC(dc);
bitmapTemp.CreateCompatibleBitmap(dc, nW, nH);
bitmapTempOld = dcTemp.SelectObject(&bitmapTemp);
dcMask2.CreateCompatibleDC(dc);
bitmapMask2.CreateBitmap(nW, nH, 1, 1, NULL);
bitmapMask2Old = dcMask2.SelectObject(&bitmapMask2);
dcMask3.CreateCompatibleDC(dc);
bitmapMask3.CreateBitmap(nW, nH, 1, 1, NULL);
bitmapMask3Old = dcMask3.SelectObject(&bitmapMask3);
// 마스크 패턴 생성. 투명색 부분만 백색으로 나머지는 검은색으로
COLORREF crOldBack = buffDC->SetBkColor(crTransparent);
dcMask3.BitBlt(0, 0, nW, nH, buffDC, 0, 0, SRCCOPY);
buffDC->SetBkColor(crOldBack);
destDC->BitBlt(nX, nY, nW, nH, &dcMask3, 0, 0, SRCAND);
dcMask3.PatBlt(0, 0, nW, nH, PATINVERT);
dcTemp.BitBlt(0, 0, nW, nH, buffDC, 0, 0, SRCCOPY);
dcTemp.BitBlt(0, 0, nW, nH, &dcMask3, 0, 0, SRCAND);
destDC->BitBlt(nX, nY, nW, nH, &dcTemp, 0, 0, SRCPAINT);
// 리소스 반환
dcMask3.SelectObject(bitmapMask3Old);
dcMask3.DeleteDC();
bitmapMask3.DeleteObject();
dcTemp.SelectObject(bitmapTempOld);
dcTemp.DeleteDC();
bitmapTemp.DeleteObject();
dcMask2.SelectObject(bitmapMask2Old);
dcMask2.DeleteDC();
bitmapMask2.DeleteObject();
ReleaseDC(dc);
}
막막한데 유용하게 참고했습니다
감사합니다
도움이 되셨다니 제가 오히려 더 기분이 좋네요..^^;;;
글도 남겨주시고 감사합니다 (__)