Code:
/* Win 2000/XP+ only */
#include <windows.h>
#ifndef WS_EX_LAYERED
# define WS_EX_LAYERED 0x80000
# define LWA_COLORKEY 1
# define LWA_ALPHA 2
#endif
void OnPaint(HWND hwnd, HDC hdc)
{
RECT rc;
GetClientRect(hwnd, &rc);
SetTextColor(hdc, RGB(255,0,0));
DrawText(hdc, "Your text here", -1, &rc,
DT_CENTER | DT_VCENTER | DT_EXPANDTABS | DT_SINGLELINE);
}
static LRESULT CALLBACK WndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hwnd, &ps);
OnPaint(hwnd, hdc);
EndPaint(hwnd, &ps);
return 0;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc = { 0 };
POINT pt = { 0 };
HWND hwnd;
MSG Msg;
HRGN hrgn;
RECT rcClient, rcWnd, rcRgn;
HANDLE hDll;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("TransparentDemo");
wc.hIconSm = NULL;
if(!RegisterClassEx(&wc)) return 0;
hwnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOOLWINDOW, TEXT("TransparentDemo"), NULL,
0, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
/* Hide title bar */
ClientToScreen(hwnd, &pt);
GetWindowRect(hwnd, &rcWnd);
GetClientRect(hwnd, &rcClient);
rcRgn.left = pt.x - rcWnd.left;
rcRgn.top = pt.y - rcWnd.top;
rcRgn.right = rcRgn.left + rcClient.right;
rcRgn.bottom = rcRgn.top + rcClient.bottom;
hrgn = CreateRectRgn(rcRgn.left, rcRgn.top, rcRgn.right, rcRgn.bottom);
SetWindowRgn(hwnd, hrgn, FALSE);
ShowWindow(hwnd, nCmdShow);
/* Send to bottom */
SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
hDll = LoadLibrary(TEXT("user32"));
if (hDll)
{
typedef DWORD (WINAPI * PSLWA)(HWND, DWORD, BYTE, DWORD);
PSLWA pSetLayeredWindowAttributes;
pSetLayeredWindowAttributes =
(PSLWA) GetProcAddress(hDll,"SetLayeredWindowAttributes");
if (pSetLayeredWindowAttributes != NULL)
{
pSetLayeredWindowAttributes (hwnd, RGB(255,255,255), 0, LWA_COLORKEY);
}
FreeLibrary(hDll);
}
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}