1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| //OnInitDialog()中 CRgn wndRgn;
m_bitmapBack.LoadBitmap(IDB_BITMAP_BACK); CreateRgn(m_bitmapBack, RGB(255, 255, 255), wndRgn); SetWindowRgn(wndRgn, TRUE);
//根据图片创建区域的函数 void CClientBrowserDlg::CreateRgn(CBitmap cBitmap, COLORREF dwColorKey, CRgn wndRgn) { CDC *pDC = this->GetDC(); CDC memDC; //创建与传入DC兼容的临时DC memDC.CreateCompatibleDC(pDC);
CBitmap *pOldMemBmp=NULL; //将位图选入临时DC pOldMemBmp = memDC.SelectObject(cBitmap);
//创建总的窗体区域,初始region为0 wndRgn.CreateRectRgn(0,0,0,0);
BITMAP bit; cBitmap.GetBitmap (bit);//取得位图参数,这里要用到位图的长和宽
int y; for(y=0; y <= bit.bmHeight; y++) { CRgn rgnTemp; int iX = 0; do { //跳过透明色找到下一个非透明色的点. while (iX <= bit.bmWidth memDC.GetPixel(iX, y) == dwColorKey) iX++; //记住这个起始点 int iLeftX = iX; //寻找下个透明色的点 while (iX <= bit.bmWidth memDC.GetPixel(iX, y) != dwColorKey) ++iX; //创建一个包含起点与重点间高为1像素的临时“region” rgnTemp.CreateRectRgn(iLeftX, y, iX, y+1); //合并到主"region". wndRgn.CombineRgn(wndRgn, rgnTemp, RGN_OR); //删除临时"region",否则下次创建时和出错 rgnTemp.DeleteObject(); } while(iX < bit.bmWidth ); iX = 0; }
if(pOldMemBmp) memDC.SelectObject(pOldMemBmp); }
|