BOOL AddObstacle(CBitmap* backBmp,
int nbackLen, int nbackWith,
CBitmap* ttBmp, int nLeft,
int nTop, int nLen, int nWith) { CClientDC dc(this); CBitmap hBMP,mBMP,backBmp; hBMP.CreateCompatibleBitmap(&dc, nLen, nWith); //创建位图内存 mBMP.CreateBitmap(nLen, nWith, 1, 1, NULL); //创建单色掩码位图 backBmp.CreateCompatibleBitmap(&dc, 800, 600);
CDC hDC0,mDC0,backDC,memDC; hDC0.CreateCompatibleDC(&dc); //创建设备句柄 mDC0.CreateCompatibleDC(&dc); //创建设备句柄 backDC.CreateCompatibleDC(&dc);
memDC.CreateCompatibleDC(&dc);
CBitmap *oldBMP = (CBitmap *)hDC0.SelectObject(hBMP); CBitmap *oldmBMP = (CBitmap *)mDC0.SelectObject(mBMP); CBitmap *oldbBMP = (CBitmap *)backDC.SelectObject(backBmp);
CCBitmap *oldmemBMP = (CBitmap *)backDC.SelectObject(memBmp); // 将源DC中的位图拷贝到临时DC中
backDC.StretchBlt(0,0,800,600,&memDC,0,0,nbackLen,nbackWidth,SRCCOPY); hDC0.SelectObject(ttBmp);
hDC0.SetBkColor(RGB(0,255,0));// 设置透明色 mDC0.BitBlt(0, 0, nLen, nWith, &hDC0, 0, 0, SRCCOPY);//生成透明区域为白色,其它区域为黑色的掩码位图
hDC0.SetBkColor(RGB(0,0,0)); hDC0.SetTextColor(RGB(255,255,255)); hDC0.BitBlt(0, 0, nLen, nWith, &mDC0, 0, 0, SRCAND);//生成透明区域为黑色,其它区域保持不变的位图
backDC.BitBlt(nLeft, nTop, nLen, nWith, &mDC0, 0, 0, SRCAND); //"与"运算,在hdc0生成掩模,透明部分保持屏幕不变,其它部分变成黑色 backDC.BitBlt(nLeft, nTop, nLen, nWith, &hDC0, 0, 0, SRCPAINT);//"或"运算,生成最终透明效果 dc.BitBlt(0,0,800,600,&backDC,0,0,SRCCOPY);
hDC0.SelectObject(&oldBMP); mDC0.SelectObject(&oldmBMP); backDC.SelectObject(&oldbBMP); memDC.SelectObject(&oldmemBMP);
hDC0.DeleteDC(); mDC0.DeleteDC(); backDC.DeleteDC(); memDC.DeleteDC();
hBMP.DeleteObject(); mBMP.DeleteObject(); backBmp.DeleteObject(); return TRUE; } |