Transparent 之 AlphaBlend

  • 函数:
    • The AlphaBlend function displays bitmaps that have transparent or semitransparent pixels.
    • BOOL AlphaBlend(
        HDC hdcDest,                 // handle to destination DC
        int nXOriginDest,            // x-coord of upper-left corner
        int nYOriginDest,            // y-coord of upper-left corner
        int nWidthDest,              // destination width
        int nHeightDest,             // destination height
        HDC hdcSrc,                  // handle to source DC
        int nXOriginSrc,             // x-coord of upper-left corner
        int nYOriginSrc,             // y-coord of upper-left corner
        int nWidthSrc,               // source width
        int nHeightSrc,              // source height
        BLENDFUNCTION blendFunction  // alpha-blending function
      );
      
  • 代码:
    • 	static HBITMAP hBmp1;
      	static HBITMAP hBmp2;
      	static int cxBmp;
      	static int cyBmp;
      
      	case WM_CREATE:
      		{
      			//load bmp
      			hBmp1 = LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP1));
      			if (hBmp1 == NULL)
      			{
      				MessageBox(hWnd,L"failed to load bmp1!",NULL,MB_OK);
      			}
      			hBmp2 = LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP2));
      			if (hBmp2 == NULL)
      			{
      				MessageBox(hWnd,L"failed to load bmp2!",NULL,MB_OK);
      			}
      			//get bmp info
      			BITMAP bmpInfo;
      			GetObject(hBmp1,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 = cxBmp + 2 * GetSystemMetrics(SM_CXFRAME);
      			int cyWnd = cyBmp + 2 * GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CYCAPTION);
      			MoveWindow(hWnd,(cxScr-cxWnd)/2,(cyScr-cyWnd)/2,cxWnd,cyWnd,TRUE);
      		}
      		break;
      
      	case WM_PAINT:
      		hdc = BeginPaint(hWnd, &ps);
      		{
      			//disp bmp
      			HDC hMemDC1 = CreateCompatibleDC(hdc);
      			HDC hMemDC2 = CreateCompatibleDC(hdc);
      			SelectObject(hMemDC1, hBmp1);
      			SelectObject(hMemDC2, hBmp2);
      			
      			BLENDFUNCTION bf;
      			bf.BlendOp = AC_SRC_OVER;
      			bf.BlendFlags = 0;
      			bf.SourceConstantAlpha = 128;//alpha transparency value
      			bf.AlphaFormat = 0;
      			AlphaBlend(hMemDC1,0,0,cxBmp,cyBmp,hMemDC2,0,0,cxBmp,cyBmp,bf);
      
      			BitBlt(hdc, 0, 0,cxBmp, cyBmp, hMemDC1, 0, 0, SRCCOPY);			
      
      			DeleteDC(hMemDC1);
      			DeleteDC(hMemDC2);
      
      		}
      		EndPaint(hWnd, &ps);
      		break;
      
  • 结果:
posted @ 2011-07-11 14:35  iThinking  阅读(297)  评论(0编辑  收藏  举报