Transparent 之 SetLayeredWindowAttributes

  • 函数
    • The SetLayeredWindowAttributes function sets the opacity and transparency color key of a layered window.
    • BOOL SetLayeredWindowAttributes(          
          HWND hwnd,//Handle to the layered window
          COLORREF crKey,//specifies the transparency color key
          BYTE bAlpha,//describe the opacity of the layered window
          DWORD dwFlags//LWA_ALPHA | LWA_COLORKEY
      );
  • 代码
    • 	static HBITMAP hBmp1;
      	static int cxBmp;
      	static int cyBmp;
      
      	case WM_CREATE:
      		{
      			//Set Window Attributes
      			SetWindowLong(hWnd,GWL_EXSTYLE,GetWindowLong(hWnd,GWL_EXSTYLE) | WS_EX_LAYERED);
      			SetLayeredWindowAttributes(hWnd,RGB(255,255,255),160,LWA_ALPHA);
      			//load bmp
      			hBmp1 = LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP1));
      			if (hBmp1 == NULL)
      			{
      				MessageBox(hWnd,L"failed to load bmp1!",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);
      			SelectObject(hMemDC1, hBmp1);
      			BitBlt(hdc, 0, 0,cxBmp, cyBmp, hMemDC1, 0, 0, SRCCOPY);	
      			DeleteDC(hMemDC1);
      		}
      		EndPaint(hWnd, &ps);
      		break;
      
  • 结果
原文地址:https://www.cnblogs.com/dahai/p/2103095.html