void CBitmapDialog :: MakeWindowRgn ()
{
if (!m_bTransparent) //窗口不透明
{
// Set the window region to the full window
CRect rc;
GetWindowRect (rc);
CRgn rgn;
rgn.CreateRectRgn (0, 0, rc.Width(), rc.Height());
SetWindowRgn (rgn, TRUE);
}
else //根据位图定制窗口形状
{
// Set the region to the window rect minus the client rect
CRect rcWnd;
GetWindowRect (rcWnd);
// Subtract rgnClient from rgn
rgn.CombineRgn (&rgn, &rgnClient, RGN_XOR);
// Get a DC for the bitmap
CDC dcImage;
dcImage.CreateCompatibleDC (NULL);
CBitmap *pOldBitmap = dcImage.SelectObject (m_bmBitmap); //根据位图形状来定制对话框形状
// Get the bitmap for width and height information
BITMAP bm;
m_bmBitmap->GetBitmap (&bm);
// Get window width and height
CRect rc;
GetClientRect (rc);
// Use the minimum width and height
int width = min (bm.bmWidth, rc.Width());
int height = min (bm.bmHeight, rc.Height());
// Use RLE (run-length) style because it goes faster.
// Row start is where the first opaque pixel is found. Once
// a transparent pixel is found, a line region is created.
// Then row_start becomes the next opaque pixel.
int row_start;
// Go through all rows
for (int y=0; y
{
// Start looking at the beginning
row_start = 0;
// Go through all columns
for (int x=0; x
{
// If this pixel is transparent
if (dcImage.GetPixel(x, y) == m_colTrans) //透明底色
{
// If we haven't found an opaque pixel yet, keep searching
if (row_start == x) row_start ++;
else
{
// We have found the start (row_start) and end (x) of
// an opaque line. Add it to the region.
CRgn rgnAdd;
rgnAdd.CreateRectRgn (rcClient.left+row_start,
rcClient.top+y, rcClient.left+x, rcClient.top+y+1);
rgn.CombineRgn (&rgn, &rgnAdd, RGN_OR);
row_start = x+1;
}
}
}
// If the last pixel is still opaque, make a region.
if (row_start != x)
{
CRgn rgnAdd;
rgnAdd.CreateRectRgn (rcClient.left+row_start, rcClient.top+y,
rcClient.left+x, rcClient.top+y+1);
rgn.CombineRgn (&rgn, &rgnAdd, RGN_OR);
}
}