Transparent 之 mask bmp

  • 条件:
    • the colour image must be black in all areas that we want to display as transparent
    • the mask image must be white in the areas we want transparent, and black elsewhere
  • 原理:
    • SRCAND:Combines the colors of the source and destination rectangles by using the Boolean AND operator.
    • SRCPAINT:Combines the colors of the source and destination rectangles by using the Boolean OR operator.
    • SelectObject(hMemDC, hMaskBmp);//mask bmp
      BitBlt(hdc, 0, 0, cxBmp, cyBmp, hMemDC, 0, 0, SRCAND);
      
      SelectObject(hMemDC, hColorBmp);//color bmp
      BitBlt(hdc, 0, 0, cxBmp, cyBmp, hMemDC, 0, 0, SRCPAINT);
      

  • 代码:
    • HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
      {
      	HDC hdcMem, hdcMem2;
      	HBITMAP hbmMask;
      	BITMAP bm;
      
      	// Create monochrome (1 bit) mask bitmap.  
      
      	GetObject(hbmColour, sizeof(BITMAP), &bm);
      	hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
      
      	// Get some HDCs that are compatible with the display driver
      
      	hdcMem = CreateCompatibleDC(0);
      	hdcMem2 = CreateCompatibleDC(0);
      
      	SelectObject(hdcMem, hbmColour);
      	SelectObject(hdcMem2, hbmMask);
      
      	// Set the background colour of the colour image to the colour
      	// you want to be transparent.
      	SetBkColor(hdcMem, crTransparent);
      
      	// Copy the bits from the colour image to the B+W mask... everything
      	// with the background colour ends up white while everythig else ends up
      	// black...Just what we wanted.
      
      	BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
      
      	// Take our new mask and use it to turn the transparent colour in our
      	// original colour image to black so the transparency effect will
      	// work right.
      	BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
      
      	// Clean up.
      
      	DeleteDC(hdcMem);
      	DeleteDC(hdcMem2);
      
      	return hbmMask;
      }
    • 	static HBITMAP hColorBmp;
      	static HBITMAP hMaskBmp;
      	static int cxBmp;
      	static int cyBmp;
      
      	case WM_CREATE:
      		{
      			//load bmp and get bmp info
      			hColorBmp = LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BALL));
      			if (hColorBmp == NULL)
      			{
      				MessageBox(hWnd,L"failed to load color bmp!",NULL,MB_OK);
      			}
      			hMaskBmp = CreateBitmapMask(hColorBmp,RGB(255,255,255));
      			if (hMaskBmp == NULL)
      			{
      				MessageBox(hWnd,L"failed to create mask bmp!",NULL,MB_OK);
      			}
      
      			BITMAP bmpInfo;
      			GetObject(hColorBmp,sizeof(BITMAP),&bmpInfo);
      			cxBmp = bmpInfo.bmWidth;
      			cyBmp = bmpInfo.bmHeight;
      			//put window int the middle screen
      			int cxScr = GetSystemMetrics(SM_CXSCREEN);
      			int cyScr = GetSystemMetrics(SM_CYSCREEN);
      			int cxWnd = 400;
      			int cyWnd = 400;
      			MoveWindow(hWnd,(cxScr-cxWnd)/2,(cyScr-cyWnd)/2,cxWnd,cyWnd,TRUE);
      		}
      		break;
      
      	case WM_PAINT:
      		hdc = BeginPaint(hWnd, &ps);
      		{
      			//bg color
      			RECT rcWnd;
      			HBRUSH hBrush;
      			GetClientRect(hWnd,&rcWnd);
      			hBrush = CreateSolidBrush(RGB(0,128,128));
      			FillRect(hdc,&rcWnd,hBrush);
      			DeleteObject(hBrush);
      			//disp bmp
      			HDC hMemDC = CreateCompatibleDC(hdc);
      
      			SelectObject(hMemDC, hMaskBmp);
      			BitBlt(hdc, 73,58, cxBmp, cyBmp, hMemDC, 0, 0, SRCAND);
      
      			SelectObject(hMemDC, hColorBmp);
      			BitBlt(hdc, 73,58, cxBmp, cyBmp, hMemDC, 0, 0, SRCPAINT);
      
      			DeleteDC(hMemDC);
      
      		}
      		EndPaint(hWnd, &ps);
      		break;
      
  • 结果:
    • 2011071110113472.jpg

转载于:https://www.cnblogs.com/dahai/archive/2011/07/11/2102727.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值