Win32: Semi-Transparent Window

AID: 1783
  • Status: Published

3020 points

  • Bypgnatyuk
  • TypeTips/Tricks
  • Posted on2009-10-16 at 12:09:28
In order to make any window semi-transparent, you need to apply the following code to the window handle:
SetWindowLong(hWnd, GWL_EXSTYLE,
	GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(hWnd, 0, (255 * 70) / 100, LWA_ALPHA);
                                    
1:
2:
3:

Select allOpen in new window

semi-transparent-window.png
  • 566 KB
  • Application screen with layered window
Application screen with layered window


The first line in this code modifies the extended window style - add WS_EX_LAYERED. The second line changes the window layered attribute - make this window with 70% alpha.

Of course the window could be created with this WS_EX_LAYERED style:
hWnd = CreateWindowEx(WS_EX_LAYERED, 
	s_szWndClassName, s_szWndClassName, 
	WS_OVERLAPPEDWINDOW,
	CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 
	NULL, NULL, hInstance, NULL);
                                    
1:
2:
3:
4:
5:

Select allOpen in new window



This alphablended/layered window technique was introduced in Windows 2000, so the application code should contain the following definitions:
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
                                    
1:
2:

Select allOpen in new window



I wrote a small application to show this technique in action:
#define WIN32_LEAN_AND_MEAN

#define WINVER 0x0501
#define _WIN32_WINNT 0x0501

#include <windows.h>

LPCWSTR s_szWndClassName = L"Semi-Transparent Window";
LPCWSTR s_szFile = L"sunset.bmp";
HBITMAP s_hBitmap = NULL;

ATOM RegisterWndClass(HINSTANCE, LPCWSTR);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HBITMAP LoadPicture(LPCWSTR);
BOOL DrawPicture(HDC, LPRECT);

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
	LPWSTR lpCmdLine, int nCmdShow)
{
	HWND hWnd;
	MSG msg;

	s_hBitmap = LoadPicture(L"sunset.bmp"); 

	RegisterWndClass(hInstance, s_szWndClassName);

	hWnd = CreateWindowEx(WS_EX_LAYERED, 
		s_szWndClassName, s_szWndClassName, 
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 
		NULL, NULL, hInstance, NULL);

	if (hWnd != NULL)
	{
		SetLayeredWindowAttributes(hWnd,  0,  
				(255 * 70) / 100, LWA_ALPHA);

		ShowWindow(hWnd, nCmdShow);
		UpdateWindow(hWnd);

		while (GetMessage(&msg, NULL, 0, 0))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	if (s_hBitmap != NULL)
		DeleteObject(s_hBitmap);

	return (int)msg.wParam;
}

ATOM RegisterWndClass(HINSTANCE hInstance, LPCWSTR lpszWndClassName)
{
	WNDCLASSEX wcex		= { 0 };
	wcex.cbSize		= sizeof(WNDCLASSEX);
	wcex.style		= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc		= WndProc;
	wcex.hInstance		= hInstance;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= lpszWndClassName;
	return RegisterClassEx(&wcex);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, 
	 WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	RECT rect;

	switch (message)
	{
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		GetClientRect(hWnd, &rect);
		DrawPicture(hdc, &rect);
		EndPaint(hWnd, &ps);
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

HBITMAP LoadPicture(LPCWSTR lpszFileName)
{
    HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, lpszFileName,
	IMAGE_BITMAP, 0, 0, 				
	LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE); 
    return hBitmap;
}

BOOL DrawPicture(HDC hDC, LPRECT lpRect)
{
	BITMAP bitmap;
	HDC hMemDC;
	HGDIOBJ hBitmapOld;
	if (s_hBitmap == NULL)
		return FALSE;
	GetObject(s_hBitmap, sizeof(BITMAP), &bitmap);
	hMemDC = CreateCompatibleDC(hDC);
	hBitmapOld = SelectObject(hMemDC, s_hBitmap);
	StretchBlt(hDC, 
		lpRect->left, lpRect->top, 
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top, 
		hMemDC, 
		0, 0, bitmap.bmWidth, bitmap. bmHeight, 
		SRCCOPY);
	SelectObject(hMemDC, hBitmapOld);
	DeleteDC(hMemDC);
	return TRUE;
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:

Select allOpen in new window



This application creates a semi-transparent window and draws an image in it, as shown in the earlier screenshot.

More information you can find in MSDN:
Layered Windows
    Asked On
    2009-10-16 at 12:09:28ID1783
    Tags

    Win32

    ,

    SetLayeredWindowAttributes

    Topic

    Windows Programming

    Views
    2362

    Comments

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Join Experts Exchange Today

    Gain Access to all our Tech Resources

    Get personalized answers

    Ask unlimited questions

    Access Proven Solutions

    Search 3.2 million solutions

    Read In-Depth How-To Guides

    1000+ articles, demos, & tips

    Watch Step by Step Tutorials

    Learn direct from top tech pros

    And Much More!

    Your complete tech resource

    See Plans and Pricing

    30-day free trial. Register in 60 seconds.

    Loading Advertisement...

    Top Win OS Dev Experts

    1. DanRollins

      17,145

      90 points yesterday

      Profile
      Rank: Genius
    2. jkr

      7,800

      0 points yesterday

      Profile
      Rank: Savant
    3. sarabande

      5,000

      0 points yesterday

      Profile
      Rank: Sage
    4. sinisav

      4,000

      0 points yesterday

      Profile
      Rank: Master
    5. Zoppo

      3,500

      0 points yesterday

      Profile
      Rank: Genius
    6. burrcm

      2,800

      0 points yesterday

      Profile
      Rank: Genius
    7. DTHConsulting

      2,000

      0 points yesterday

      Profile
      Rank: Guru
    8. Geert_Gruwez

      2,000

      0 points yesterday

      Profile
      Rank: Genius
    9. ImaCircularSaw

      2,000

      0 points yesterday

      Profile
      Rank: Guru
    10. Geisrud

      2,000

      0 points yesterday

      Profile
      Rank: Guru
    11. Run5k

      2,000

      0 points yesterday

      Profile
      Rank: Genius
    12. ShareefHuddle

      2,000

      0 points yesterday

      Profile
      Rank: Guru
    13. tampnic

      2,000

      0 points yesterday

      Profile
      Rank: Master
    14. pgnatyuk

      1,960

      20 points yesterday

      Profile
      Rank: Genius
    15. Thommy

      1,800

      0 points yesterday

      Profile
      Rank: Wizard
    16. mrwad99

      1,800

      0 points yesterday

      Profile
      Rank: Wizard
    17. momi_sabag

      1,800

      1,800 points yesterday

      Profile
      Rank: Genius
    18. TommySzalapski

      1,600

      0 points yesterday

      Profile
      Rank: Genius
    19. ve3ofa

      1,300

      1,300 points yesterday

      Profile
      Rank: Genius
    20. cpkilekofp

      1,300

      1,300 points yesterday

      Profile
      Rank: Sage
    21. Darr247

      1,000

      0 points yesterday

      Profile
      Rank: Genius
    22. thinkpads_user

      1,000

      0 points yesterday

      Profile
      Rank: Genius
    23. matrixnz

      999

      0 points yesterday

      Profile
      Rank: Genius
    24. joshbula

      900

      0 points yesterday

      Profile
      Rank: Master
    25. adminnrg

      750

      0 points yesterday

      Profile

    Hall Of Fame