zoukankan      html  css  js  c++  java
  • 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;
        
    • 结果:
  • 相关阅读:
    网页中输出漂亮格式的Php数组神器
    从对轮播图的封装中体会 面对接口 编程的思想
    UITabBarController 详解
    UINavigationController 详解
    UITabBarController 和 UINavigationController 的详解
    【iOS开发】canOpenURL: failed for URL
    SDK里报错[NSConcreteMutableData wbsdk_base64EncodedString]
    UIScrollView 几乎所有的属性和方法
    oracle服务器和客户端字符集的查看和修改
    查看、修改oracle字符集,查看oracle版本
  • 原文地址:https://www.cnblogs.com/dahai/p/2102727.html
Copyright © 2011-2022 走看看