<

Win32: Semi-Transparent Window

Published on
15,669 Points
9,469 Views
2 Endorsements
Last Modified:
Approved
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);

Open in new 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);

Open 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

Open 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;
}

Open 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
2
Comment
Author:pgnatyuk
0 Comments

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Join & Write a Comment

This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month