<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Win32: Semi-Transparent Window

Published on
16,335 Points
10,135 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
1 Comment

Expert Comment

by:Ahmad Raza
It will make window fully transparent. What if someone want to get transparent window partially. I mean some its part will be transparent and some will be opaque. Any guidance ?
0

Featured Post

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

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…
Overview of OneDrive and collaboration.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month