码迷,mamicode.com
首页 > 编程语言 > 详细

MFC中透明位图原理

时间:2016-03-29 10:36:33      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:

总之,就是不断反复遮罩“过滤掉”透明色就行,先来整段代码,后面再详细介绍。

void CTransDlg::OnPaint() 
{
	CPaintDC dc(this);
	CMemDC m_dc;
	if(!m_dc)
		m_dc.LoadBitmap(IDB_BITMAP4);
	
		CMemDC dcImage(100,100,&dc);
		CBitmap bmpMask;
		bmpMask.CreateBitmap(100,100,1,1,NULL);
		CDC dcMask;
		dcMask.CreateCompatibleDC(&dc);
		dcMask.SelectObject(bmpMask);
		dcImage.BitBlt(0,0,100,100,&m_dc,0,0,SRCCOPY);
		
		dc.BitBlt(10,10,100,100,&m_dc,0,0,SRCCOPY);         //显示图1
		dc.BitBlt(120,10,100,100,&dcMask,0,0,SRCCOPY);      //显示图2
		
		dcImage.SetBkColor(RGB(255,0,0));
		dcMask.BitBlt(0,0,100,100,&dcImage,0,0,SRCCOPY);

		dc.BitBlt(230,10,100,100,&dcMask,0,0,SRCCOPY);      //显示图3
		dc.BitBlt(340,10,100,100,&dcImage,0,0,SRCCOPY);      //显示图4

		dcImage.SetBkColor(RGB(0,0,0));
		dcImage.SetTextColor(RGB(255,255,255));
		
		dc.BitBlt(450,10,100,100,&dcImage,0,0,SRCCOPY);		//显示图5

		dcImage.BitBlt(0,0,100,100,&dcMask,0,0,SRCAND);		
		
		
		dc.BitBlt(10,120,100,100,&dcImage,0,0,SRCCOPY);			//显示图6
		dc.BitBlt(120,120,100,100,&dcMask,0,0,SRCCOPY);			//显示图7

		dc.SetBkColor(RGB(255,255,255));

		dc.BitBlt(230,120,100,100,&dcMask,0,0,SRCAND);			//显示图8
		dc.BitBlt(230,120,100,100,&dcImage,0,0,SRCPAINT);		//显示图8
}
m_dc.LoadBitmap(IDB_BITMAP4);     //加载原图,就是下面这张,由红,绿,黄组成的,白色的是背景,不算原图
技术分享
CMemDC dcImage(100,100,&dc);//演示原理中一共用到了三个DC,DC就理解成一张画布,dcImage是一个临时DC,也可以理解成为一张100X100的临时画布,原图没有这么大,所以会露出背景;
CBitmap bmpMask;
CBitmap bmpMask;
bmpMask.CreateBitmap(100,100,1,1,NULL);    //创建一张单色位图,位图要想看到,必须画在画布上,也就是复制到(BitBlt)DC上
CDC dcMask;
dcMask.CreateCompatibleDC(&dc);           //创建一张掩码用的画布
dcMask.SelectObject(bmpMask);             //把那张单色位图画在画布上
dcImage.BitBlt(0,0,100,100,&m_dc,0,0,SRCCOPY);   //m_dc这张画布上的原图COPY到临时画布dcImage上<pre name="code" class="cpp">dc.BitBlt(10,10,100,100,&m_dc,0,0,SRCCOPY);         //在最终画布dc上显示原图
dc.BitBlt(120,10,100,100,&dcMask,0,0,SRCCOPY);      //在最终画布dc上显示单色掩码位图


技术分享技术分享

dcImage.SetBkColor(RGB(255,0,0));              //把红色设置成dcImage这张画布的背景色,也就是透明色
dcMask.BitBlt(0,0,100,100,&dcImage,0,0,SRCCOPY); //dcImage把自己复制给dcMask时,告诉他红色是背景色,dcMask只能显示两种颜色,所以dcImage上红色的地方变白色,其他地方是黑色

dc.BitBlt(230,10,100,100,&dcMask,0,0,SRCCOPY);      //显示dcMask这张画布
dc.BitBlt(340,10,100,100,&dcImage,0,0,SRCCOPY);      //显示显示dcImage这张画布

技术分享技术分享

dcImage.SetBkColor(RGB(0,0,0));              //dcImage将黑色设置成背景色
dcImage.SetTextColor(RGB(255,255,255));
		
dc.BitBlt(450,10,100,100,&dcImage,0,0,SRCCOPY);		//显示dcImage

技术分享

dcImage.BitBlt(0,0,100,100,&dcMask,0,0,SRCAND);			//dcImage与dcMask相与,注意此时dcImage的透明色是黑色,所以dcMask中的黑色到了dcImage上就成了透明色	
		
dc.BitBlt(10,120,100,100,&dcImage,0,0,SRCCOPY);			//显示dcImage
dc.BitBlt(120,120,100,100,&dcMask,0,0,SRCCOPY);			//显示dcMask  第一张是原来的dcImage,第二张是dcMask,第三张是相与以后的dcImage
技术分享技术分享技术分享

dc.SetBkColor(RGB(255,255,255));<span style="white-space:pre">				</span>//最终画布dc的透明色设置成白色

dc.BitBlt(230,120,100,100,&dcMask,0,0,SRCAND);			//dcMask与最终画布相与
dc.BitBlt(230,120,100,100,&dcImage,0,0,SRCPAINT);		//dcImage与最终画布相或   第一张是dcMask,第二张是dcImage,第三张是最终效果

技术分享技术分享技术分享


MFC中透明位图原理

标签:

原文地址:http://blog.csdn.net/xiangsimoyinjiu/article/details/51001784

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!