SetWindowLong(hWnd, GWL_EXSTYLE,
GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(hWnd, 0, (255 * 70) / 100, LWA_ALPHA);
hWnd = CreateWindowEx(WS_EX_LAYERED,
s_szWndClassName, s_szWndClassName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
NULL, NULL, hInstance, NULL);
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
#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;
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented: