1: 2: 3: 4: | SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED); SetLayeredWindowAttributes(hWnd, 0, (255 * 70) / 100, LWA_ALPHA); |
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:
1: 2: 3: 4: 5: 6: | hWnd = CreateWindowEx(WS_EX_LAYERED, s_szWndClassName, s_szWndClassName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); |
This alphablended/layered window technique was introduced in Windows 2000, so the application code should contain the following definitions:
1: 2: 3: | #define WINVER 0x0501 #define _WIN32_WINNT 0x0501 |
I wrote a small application to show this technique in action:
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: 123: |
#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;
} |
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
