m_bmpBk.LoadBitmap(IDB_BK); //将背景图载入 m_bmpFore.LoadBitmap(IDB_FORE); //将前景图载入 CClientDC dc(this); //获得当前客户区设备环境 m_dcBk.CreateCompatibleDC(&dc); //创建与当前设备相兼容的设备 m_dcFore.CreateCompatibleDC(&dc); |
CBitmap* poldBk=m_dcBk.SelectObject(&m_bmpBk); //选入背景图 CBitmap* poldFore=m_dcFore.SelectObject(&m_bmpFore); CRect rect; GetClientRect(&rect); //得到客户区矩形 CDC maskDc; //创建设备环境maskDc CBitmap maskBitmap; maskDc.CreateCompatibleDC(pDC); //创建与当前设备相兼容的设备 maskBitmap.CreateBitmap(rect.Width(),rect.Height(),1,1,NULL ); //创建一个单色图 CBitmap* pOldMaskDCBitmap = maskDc.SelectObject( &maskBitmap ); //选入单色图 CBrush brush(RGB(255,255,255)); CBrush * oldbrush; oldbrush=maskDc.SelectObject(&brush); maskDc.FillRect(&rect,&brush); //取得要消除的背景色值 COLORREF clrTrans= m_dcFore.GetPixel(2, 2); // 设置前景图的背景色 COLORREF clrSaveBk = m_dcFore.SetBkColor(clrTrans); //将前景图拷贝到maskDc maskDc.BitBlt(0,0,rect.Width(),rect.Height(), &m_dcFore, 0,0,SRCCOPY); //将前景图拷贝到maskDc,此时maskDc如下图: //前景图与mask做‘与’运算 m_dcFore.SetBkColor(RGB(0,0,0)); m_dcFore.SetTextColor(RGB(255,255,255)); m_dcFore.BitBlt(0,0,rect.Width(), rect.Height(),&maskDc,0,0,SRCAND); //背景图与mask做‘与’运算 m_dcBk.SetBkColor(RGB(255,255,255)); m_dcBk.SetTextColor(RGB(0,0,0)); m_dcBk.BitBlt(0,0,rect.Width(),rect.Height(),&maskDc,0,0,SRCAND); //背景图与前景图做‘或’运算 m_dcBk.BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),&m_dcFore,0,0,SRCPAINT); //将合成后的图像显示 pDC->BitBlt(0,0,rect.Width(),rect.Height(),&m_dcBk,0,0,SRCCOPY); pDC->SelectObject(oldbrush); m_bmpBk.SelectObject(poldBk); m_bmpFore.SelectObject(poldFore); |