Sure, here's a complete code example that implements a desktop mask layer window using C++:
#include <windows.h>
#include <tchar.h>
// Window class and title
LPCTSTR g_szClassName = _T("MyWindowClass");
LPCTSTR g_szWindowTitle = _T("Desktop Mask Layer Window Example");
// Function prototypes
void CreateWindowClass(HINSTANCE hInstance, LPCTSTR className, WNDPROC wndProc);
HWND CreateMainWindow(HINSTANCE hInstance, LPCTSTR className, LPCTSTR windowTitle, int width, int height);
HWND CreateMaskLayerWindow(HWND hParent, int x, int y, int width, int height);
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
CreateWindowClass(hInstance, g_szClassName, WndProc);
HWND hWnd = CreateMainWindow(hInstance, g_szClassName, g_szWindowTitle, 800, 600);
if (!hWnd)
{
return 0;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void CreateWindowClass(HINSTANCE hInstance, LPCTSTR className, WNDPROC wndProc)
{
WNDCLASS wc = {};
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = wndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszClassName = className;
RegisterClass(&wc);
}
HWND CreateMainWindow(HINSTANCE hInstance, LPCTSTR className, LPCTSTR windowTitle, int width, int height)
{
HWND hWnd = CreateWindow(className, windowTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
MessageBox(NULL, _T("Failed to create main window"), _T("Error"), MB_OK | MB_ICONERROR);
return NULL;
}
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
return hWnd;
}
HWND CreateMaskLayerWindow(HWND hParent, int x, int y, int width, int height)
{
HWND hWnd = CreateWindow(_T("STATIC"), NULL, WS_CHILD | WS_VISIBLE | SS_BLACKRECT,
x, y, width, height, hParent, NULL, (HINSTANCE)GetWindowLongPtr(hParent, GWLP_HINSTANCE), NULL);
if (!hWnd)
{
MessageBox(hParent, _T("Failed to create mask layer window"), _T("Error"), MB_OK | MB_ICONERROR);
return NULL;
}
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
return hWnd;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
// Create the mask layer window
HWND hMaskLayer = CreateMaskLayerWindow(hWnd, 0, 0, 800, 600);
// Store the mask layer handle in the window's user data
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)hMaskLayer);
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// Draw your application's content normally here...
// Get the mask layer window handle from the window's user data
HWND hMaskLayer = (HWND)GetWindowLongPtr(hWnd, GWLP_USERDATA);
// Draw the custom shape on the mask layer window
HDC hMaskDc = GetDC(hMaskLayer);
HBRUSH hMaskBrush = CreateSolidBrush(RGB(0, 0, 0));
HBRUSH hOldBrush = (HBRUSH)SelectObject(hMaskDc, hMaskBrush);
HPEN hOldPen = (HPEN)SelectObject(hMaskDc, GetStockObject(NULL_PEN));
Ellipse(hMaskDc, 300, 200, 600, 500);
SelectObject(hMaskDc, hOldPen);
SelectObject(hMaskDc, hOldBrush);
DeleteObject(hMaskBrush);
ReleaseDC(hMaskLayer, hMaskDc);
EndPaint(hWnd, &ps);
break;
}
case WM_WINDOWPOSCHANGED:
case WM_SIZE:
{
// Get the mask layer window handle from the window's user data
HWND hMaskLayer = (HWND)GetWindowLongPtr(hWnd, GWLP_USERDATA);
// Update the mask layer window's position and size to match the main window
RECT rcClient;
GetClientRect(hWnd, &rcClient);
MapWindowPoints(hWnd, NULL, (LPPOINT)&rcClient, 2);
SetWindowPos(hMaskLayer, HWND_TOPMOST, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOACTIVATE);
break;
}
case WM_CLOSE:
{
DestroyWindow(hWnd);
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
This code creates a main window with a custom shape mask layer window that overlays the main window and covers it with a black mask everywhere except for the custom ellipse shape. Note that in this example the custom shape is hard-coded into the WM_PAINT handler, but you could replace it with any other shape, size or color you want.