Skip to content

Instantly share code, notes, and snippets.

@DJm00n
Created November 12, 2022 07:34
Show Gist options
  • Save DJm00n/34fba6e2b2f57e706e780214eb490281 to your computer and use it in GitHub Desktop.
Save DJm00n/34fba6e2b2f57e706e780214eb490281 to your computer and use it in GitHub Desktop.
CTransBmp::CreateMask(CDC* pDC)
void CTransBmp::CreateMask(CDC* pDC)
{
m_hbmMask = new CBitmap;
// Nuke any existing mask
if (m_hbmMask) m_hbmMask->DeleteObject();
// Create memory DCs to work with
CDC* hdcMask = new CDC;
CDC* hdcImage = new CDC;
hdcMask->CreateCompatibleDC(pDC);
hdcImage->CreateCompatibleDC(pDC);
// Create a monochrome bitmap for the mask
m_hbmMask->CreateBitmap(GetWidth(),
GetHeight(),
1,
1,
NULL);
CBitmap* pTempBmp;
// Select the mono bitmap into its DC
CBitmap* hbmOldMask = hdcMask->SelectObject(m_hbmMask);
// Select the image bitmap into its DC
CBitmap* hbmOldImage = hdcImage->SelectObject(pTempBmp->FromHandle((HBITMAP)m_hObject));
// Set the transparency color to be the top-left pixel
hdcImage->SetBkColor(hdcImage->GetPixel(0, 0));
// Make the mask
hdcMask->BitBlt(0, 0,
GetWidth(), GetHeight(),
hdcImage,
0, 0,
SRCCOPY);
// clean up
hdcMask->SelectObject(hbmOldMask);
hdcImage->SelectObject(hbmOldImage);
delete hdcMask;
delete hdcImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment