Skip to content

Instantly share code, notes, and snippets.

@native-m
Last active October 29, 2017 07:25
Show Gist options
  • Save native-m/6c828cac11dcf6104f0081f60ec74f4a to your computer and use it in GitHub Desktop.
Save native-m/6c828cac11dcf6104f0081f60ec74f4a to your computer and use it in GitHub Desktop.
Transculent Blur Behind
#pragma once
#include <Windows.h>
const DWORD WCA_ACCENT_POLICY = 19;
enum ACCENT_STATE
{
ACCENT_DISABLED = 0,
ACCENT_ENABLE_GRADIENT = 1,
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
ACCENT_ENABLE_BLURBEHIND = 3,
ACCENT_INVALID_STATE = 4,
_ACCENT_STATE_SIZE = 0xFFFFFFFF
};
struct ACCENTPOLICY
{
ACCENT_STATE nAccentState;
DWORD nFlags;
DWORD nColor;
DWORD nAnimationId;
};
struct WINCOMPATTRDATA
{
DWORD nAttribute;
PVOID pData;
ULONG ulDataSize;
};
void BlurBehind(HWND hWnd, BOOL enable)
{
// Dynamically call the SetWindowCompositionAttribute(); function from user32.dll
const HINSTANCE hModule = LoadLibrary(L"user32.dll");
typedef BOOL(WINAPI *pSetWindowCompositionAttribute)(HWND, WINCOMPATTRDATA*);
const pSetWindowCompositionAttribute SetWindowCompositionAttribute = (pSetWindowCompositionAttribute)GetProcAddress(hModule, "SetWindowCompositionAttribute");
// Do the blur
if (hModule)
{
// if blur enabled
if (enable)
{
if (SetWindowCompositionAttribute)
{
ACCENTPOLICY policy = { ACCENT_ENABLE_BLURBEHIND, 0, 0, 0 }; // Set the accent policy data
WINCOMPATTRDATA dataAttr = { WCA_ACCENT_POLICY, &policy, sizeof(ACCENTPOLICY) }; // then we can put it in to
SetWindowCompositionAttribute(hWnd, &dataAttr);
}
else
{
MessageBox(hWnd, L"Can't apply blur", L"Error!", MB_ICONERROR | MB_OK);
}
}
FreeLibrary(hModule);
}
else
{
MessageBox(hWnd, L"Can't apply blur", L"Error!", MB_ICONERROR | MB_OK);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment