问题描述
- mfc显示透明背景代码看不大懂
- /*-----------------显示透明背景图片-------------------*/
void CHIT_MOUSE::TransparentBitmap(HDC hdc HBITMAP hBitmap short xStart short yStart short xadd short yadd COLORREF cTransparentColor)
{
BITMAP m_bm;
COLORREF cColor;// 创建临时DC
HDC hMem hBack hObject hTemp hSave;
hBack = CreateCompatibleDC(hdc);
hObject = CreateCompatibleDC(hdc);
hMem = CreateCompatibleDC(hdc);
hSave = CreateCompatibleDC(hdc);hTemp = CreateCompatibleDC(hdc);
// 选入位图
SelectObject(hTemp hBitmap);GetObject(hBitmap sizeof(BITMAP) (LPSTR)&m_bm);
POINT ptSize;//显示位图宽高
ptSize.x = m_bm.bmWidth; // 取得位图的宽度
ptSize.y = m_bm.bmHeight; // 取得位图的该度
DPtoLP(hTemp &ptSize 1); // 转换为逻辑点值
HBITMAP bmBack bmObject bmMem bmSave;// 创建临时位图
// 单色位图
bmBack = CreateBitmap(ptSize.x ptSize.y 1 1 NULL);bmObject = CreateBitmap(ptSize.x ptSize.y 1 1 NULL);
// 与设备兼容位图
bmMem = CreateCompatibleBitmap(hdc ptSize.x ptSize.y);
bmSave = CreateCompatibleBitmap(hdc ptSize.x ptSize.y);// 将创建的临时位图选入临时DC中
HBITMAP OldbmBack OldbmObject OldbmMem OldbmSave;
OldbmBack = (HBITMAP)SelectObject(hBack bmBack);
OldbmObject = (HBITMAP)SelectObject(hObject bmObject);
OldbmMem = (HBITMAP)SelectObject(hMem bmMem);
OldbmSave = (HBITMAP)SelectObject(hSave bmSave);// 设置映射模式
SetMapMode(hTemp GetMapMode(hdc));// 先保留原始位图
BitBlt(hSave 0 0 ptSize.x ptSize.y hTemp 0 0 SRCCOPY);// 将背景颜色设置为需透明的颜色
cColor = SetBkColor(hTemp cTransparentColor);// 创建目标屏蔽码
BitBlt(hObject 0 0 ptSize.x ptSize.y hTemp 0 0 SRCCOPY);SetBkColor(hTemp cColor);// 恢复源DC的原始背景色
// 创建反转的目标屏蔽码
BitBlt(hBack 0 0 ptSize.x ptSize.y hObject 0 0 NOTSRCCOPY);// 拷贝主DC的背景到目标DC
BitBlt(hMem 0 0 ptSize.x ptSize.y hdc xStart yStart SRCCOPY);// 屏蔽位图的显示区
BitBlt(hMem 0 0 ptSize.x ptSize.y hObject 0 0 SRCAND);// 屏蔽位图中的透明色
BitBlt(hTemp 0 0 ptSize.x ptSize.y hBack 0 0 SRCAND);// 将位图与目标DC的背景左异或操作
BitBlt(hMem 0 0 ptSize.x ptSize.y hTemp 0 0 SRCPAINT);// 拷贝目标到屏幕上
StretchBlt(hdc xStart yStart ptSize.x+xadd ptSize.y+yadd hMem 0 0 ptSize.x ptSize.ySRCCOPY);// 恢复原始位图
BitBlt(hTemp 0 0 ptSize.x ptSize.y hSave 0 0 SRCCOPY);// 删除临时内存位图
DeleteObject(SelectObject(hBack OldbmBack));
DeleteObject(SelectObject(hObject OldbmObject));
DeleteObject(SelectObject(hMem OldbmMem));
DeleteObject(SelectObject(hSave OldbmSave));// 删除临时内存DC
DeleteDC(hMem);
DeleteDC(hBack);
DeleteDC(hObject);
DeleteDC(hSave);
DeleteDC(hTemp);}
这段代码的含义及用法
解决方案
解决方案二:
已经注释很详细了,实在看不懂算了
直接调用
TransparentBitmap(HDC hdc HBITMAP hBitmap short xStart short yStart short xadd short yadd COLORREF cTransparentColor)
第一个参数传你View的 DC
第二个参数,位图句柄
然后是x y的起始坐标和结束坐标,最后是透明色(一般用白色作为透明色,看你的背景)