#include <windows.
h>
#include <dwmapi.h>
#include <vector>
#include <memory>
#pragma comment(lib, "dwmapi.lib")
#pragma comment(lib, "user32.lib")
// Global variables
HWND g_hwnd = NULL;
bool g_isMaximized = false;
float g_opacity = 0.5f; // 50% transparency
const wchar_t CLASS_NAME[] = L"BlurOverlay";
// Function declarations
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void EnableBlur(HWND hwnd);
void UpdateOverlay();
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine,
int nCmdShow) {
// Register the window class
WNDCLASSEX wc = {};
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClassEx(&wc);
// Get primary monitor dimensions
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
// Create the window
g_hwnd = CreateWindowEx(
WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TOOLWINDOW, // Extended window style
CLASS_NAME, // Window class
L"Blur Overlay", // Window text
WS_POPUP, // Window style
0, 0, // Position
screenWidth, screenHeight, // Size
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional data
);
if (g_hwnd == NULL) {
return 0;
}
// Enable blur effect
EnableBlur(g_hwnd);
// Set window transparency
SetLayeredWindowAttributes(g_hwnd, 0, (BYTE)(255 * g_opacity), LWA_ALPHA);
// Show the window
ShowWindow(g_hwnd, nCmdShow);
// Set timer for monitoring display changes
SetTimer(g_hwnd, 1, 1000, NULL); // Check every second
// Message loop
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void EnableBlur(HWND hwnd) {
const HINSTANCE hModule = LoadLibrary(TEXT("user32.dll"));
if (hModule) {
struct ACCENTPOLICY {
int nAccentState;
int nFlags;
int nColor;
int nAnimationId;
};
struct WINCOMPATTR {
int nAttribute;
PVOID pData;
ULONG ulDataSize;
};
typedef BOOL(WINAPI*pSetWindowCompositionAttribute)(HWND, WINCOMPATTR*);
const pSetWindowCompositionAttribute SetWindowCompositionAttribute =
(pSetWindowCompositionAttribute)GetProcAddress(hModule,
"SetWindowCompositionAttribute");
if (SetWindowCompositionAttribute) {
ACCENTPOLICY policy = { 3, 0, 0, 0 }; // Enable blur
WINCOMPATTR data = { 19, &policy, sizeof(ACCENTPOLICY) };
SetWindowCompositionAttribute(hwnd, &data);
}
FreeLibrary(hModule);
}
}
void UpdateOverlay() {
// Get information about all monitors
std::vector<RECT> monitors;
EnumDisplayMonitors(NULL, NULL,
[](HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) ->
BOOL {
std::vector<RECT>* monitors =
reinterpret_cast<std::vector<RECT>*>(dwData);
monitors->push_back(*lprcMonitor);
return TRUE;
},
reinterpret_cast<LPARAM>(&monitors)
);
// Calculate the bounding rectangle that encompasses all monitors
RECT bounds = {0, 0, 0, 0};
for (const auto& monitor : monitors) {
bounds.left = min(bounds.left, monitor.left);
bounds.top = min(bounds.top, monitor.top);
bounds.right = max(bounds.right, monitor.right);
bounds.bottom = max(bounds.bottom, monitor.bottom);
}
// Update window position and size
SetWindowPos(g_hwnd, HWND_TOPMOST,
bounds.left, bounds.top,
bounds.right - bounds.left,
bounds.bottom - bounds.top,
SWP_SHOWWINDOW);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_TIMER:
UpdateOverlay();
return 0;
case WM_KEYDOWN:
switch (wParam) {
case VK_ESCAPE: // Press ESC to exit
PostQuitMessage(0);
return 0;
case VK_UP: // Increase opacity
g_opacity = min(1.0f, g_opacity + 0.1f);
SetLayeredWindowAttributes(hwnd, 0, (BYTE)(255 * g_opacity),
LWA_ALPHA);
return 0;
case VK_DOWN: // Decrease opacity
g_opacity = max(0.1f, g_opacity - 0.1f);
SetLayeredWindowAttributes(hwnd, 0, (BYTE)(255 * g_opacity),
LWA_ALPHA);
return 0;
}
break;
case WM_DISPLAYCHANGE:
UpdateOverlay();
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}