#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
static HWND hLabel = NULL; // Handle to the label
static HWND hTrackBar = NULL; // Handle to the track bar
switch (uMsg) {
case WM_CREATE:
// Initialize common controls (needed for the track bar)
InitCommonControls();
// Create label
hLabel = CreateWindowEx(
0, L"STATIC", L"Transparent Label",
WS_CHILD | WS_VISIBLE | SS_CENTER,
10, 10, 150, 20,
hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL
);
// Create track bar (slider)
hTrackBar = CreateWindowEx(
0, TRACKBAR_CLASS, NULL,
WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS,
10, 50, 200, 30,
hwnd, (HMENU)2, ((LPCREATESTRUCT)lParam)->hInstance, NULL
);
// Set range and tick frequency for the track bar
SendMessage(hTrackBar, TBM_SETRANGE, TRUE, MAKELPARAM(0, 100));
SendMessage(hTrackBar, TBM_SETTICFREQ, 10, 0);
SendMessage(hTrackBar, TBM_SETPOS, TRUE, 50);
break;
case WM_CTLCOLORSTATIC:
// If the static control is the label we created
if ((HWND)lParam == hLabel) {
HDC hdcStatic = (HDC)wParam;
SetTextColor(hdcStatic, RGB(0, 0, 0)); // Set text color
SetBkMode(hdcStatic, TRANSPARENT); // Set background mode to transparent
return (LRESULT)GetStockObject(NULL_BRUSH); // Use NULL_BRUSH to prevent background drawing
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow) {
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // Set a background for the parent window
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0, CLASS_NAME, L"Transparent Label and Track Bar",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL
);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
// Forward declaration of the track bar subclass procedure
LRESULT CALLBACK TrackBarProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR, DWORD_PTR);
// Global variable to store original track bar procedure
WNDPROC originalTrackBarProc;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
static HWND hLabel = NULL;
static HWND hTrackBar = NULL;
switch (uMsg) {
case WM_CREATE:
InitCommonControls();
// Create label
hLabel = CreateWindowEx(
0, L"STATIC", L"Transparent Label",
WS_CHILD | WS_VISIBLE | SS_CENTER,
10, 10, 150, 20,
hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL
);
// Create track bar
hTrackBar = CreateWindowEx(
0, TRACKBAR_CLASS, NULL,
WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS,
10, 50, 200, 30,
hwnd, (HMENU)2, ((LPCREATESTRUCT)lParam)->hInstance, NULL
);
// Set range and tick frequency for the track bar
SendMessage(hTrackBar, TBM_SETRANGE, TRUE, MAKELPARAM(0, 100));
SendMessage(hTrackBar, TBM_SETTICFREQ, 10, 0);
SendMessage(hTrackBar, TBM_SETPOS, TRUE, 50);
// Subclass the track bar to override its painting behavior
originalTrackBarProc = (WNDPROC)SetWindowLongPtr(hTrackBar, GWLP_WNDPROC, (LONG_PTR)TrackBarProc);
break;
case WM_CTLCOLORSTATIC:
if ((HWND)lParam == hLabel) {
HDC hdcStatic = (HDC)wParam;
SetTextColor(hdcStatic, RGB(0, 0, 0)); // Set text color
SetBkMode(hdcStatic, TRANSPARENT); // Set background mode to transparent
return (LRESULT)GetStockObject(NULL_BRUSH); // Use NULL_BRUSH to prevent background drawing
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK TrackBarProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
switch (uMsg) {
case WM_ERASEBKGND:
return TRUE; // Return TRUE to indicate background is erased
case WM_PAINT: {
// Custom paint the track bar
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// You can add code here to paint the track bar manually if needed.
EndPaint(hwnd, &ps);
return 0;
}
default:
// Call the original window procedure for default processing
return CallWindowProc(originalTrackBarProc, hwnd, uMsg, wParam, lParam);
}
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow) {
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // Set a background for the parent window
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0, CLASS_NAME, L"Transparent Label and Track Bar",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL
);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}