I am creating a Speed Meter to measure my internet speed and stays at top of every app, then I how do I make Its background semi transparent or blurred something like this.
-
You can make ts semi opaque, however i'm not sure you'll easily be able to blur the background – TheGeneral Jul 29 '18 at 07:14
-
thanks ! then how do I make it semi opaque – Arwin Chua Jul 29 '18 at 07:17
-
[Form.Opacity Property](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.opacity(v=vs.110).aspx) – TheGeneral Jul 29 '18 at 07:24
-
1See the [DwmEnableBlurBehindWindow](https://learn.microsoft.com/en-us/windows/desktop/api/dwmapi/nf-dwmapi-dwmenableblurbehindwindow) DWM Function. You can get most of the API declarations/structures for `C#` here: [Borderless Form Dropshadow](https://stackoverflow.com/questions/49395005/borderless-form-dropshadow?answertab=active#tab-top) (on the bottom there's more of of what was required for that question). – Jimi Jul 29 '18 at 09:33
-
1It is the Aero glass effect. On winforms only DwmExtendFrameIntoClientArea() is practical since the rendering model of most the controls in the toolbox is not compatible with it. If you try anyway then you'll see text becoming transparent as well. Do beware it is yesteryear's effect and considered outdated on Win10. Sample code [is here](https://code.msdn.microsoft.com/windowsapps/CSWinFormExAeroToClient-3b123c56). – Hans Passant Jul 29 '18 at 09:37
-
1@Hans Passant IIRC, calling `DwmExtendFrameIntoClientArea()` is not even required for the Blur Behind effect. One can call `DwmEnableBlurBehindWindow()`) after having registered `DWMNCRENDERINGPOLICY` as Enabled. Calling then `DwmSetWindowAttribute()` with `DWMWINDOWATTRIBUTE.NCRenderingPolicy`. Both in `WndProc` and before showing the Form (I guess it's a Form). – Jimi Jul 29 '18 at 10:03
1 Answers
A WinForms
Form
with a Blur effect applied to it, using:
Windows 7 ⇒ DwmEnableBlurBehindWindow() DWM function.
Windows 10 / 11 ⇒ SetWindowCompositionAttribute()
User32
function (undocumented)
How to apply the Blur Behind effect:
Windows 7:
The Window needs to register a rendering policy with DwmSetWindowAttribute(), setting the DWMWINDOWATTRIBUTE enumerator to NCRenderingPolicy
and enable it, setting the DWMNCRENDERINGPOLICY enumerator to Enabled
.
You may also want to override a Form's class WndProc
; when a WM_DWMCOMPOSITIONCHANGED message is received (informing of the Aero composition state), to verify whether DWM Composition is enabled.
Before showing the Window interface, call DwmEnableBlurBehindWindow(), setting its DWM_BLURBEHIND structure relevant fields (dwFlags
and fEnable
) to 1
.
Here, I'm calling it from the Form's constructor.
Windows 10 and Windows 11:
This code is using the not yet documented SetWindowCompositionAttribute()
function.
The User32
function only requires the use of an internal structure, here called WinCompositionAttrData
, where the Attribute
member is set to DWMWINDOWATTRIBUTE.AccentPolicy
and the Data
member is set to the pointer of an internal structure, here called AccentPolicy
, where the AccentState
member is set to DWMACCENTSTATE.ACCENT_ENABLE_BLURBEHIND
DWM Compostion is always enabled. No need to verify it or override WndProc
to handle a DWM Composition change.
It's more clear in code:
(As a note, the BlurBehind
effect does not work if the Form has a white background color)
See this other question about a DwmExtendFrameIntoClientArea()
implementation.
public partial class frmBlurBehind : Form
{
public frmBlurBehind()
{
InitializeComponent();
// Remove the border if this is meant to be a border-less Form
// FormBorderStyle = FormBorderStyle.None;
if (IsDWMCompositionEnabled()) {
if (Environment.OSVersion.Version.Major > 6) {
Dwm.Windows10EnableBlurBehind(Handle);
}
else {
Dwm.WindowEnableBlurBehind(Handle);
}
// Set Drop shadow of a border-less Form
if (FormBorderStyle == FormBorderStyle.None) {
Dwm.WindowBorderlessDropShadow(Handle, 2);
}
}
}
private bool IsDWMCompositionEnabled() =>
Environment.OSVersion.Version.Major >= 6 && Dwm.IsCompositionEnabled();
// Eventually, if this is a border-less Form, reapply the attribute if DWM Composition changes
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg) {
case Dwm.WM_DWMCOMPOSITIONCHANGED:
if (IsDWMCompositionEnabled()) {
Dwm.DWMNCRENDERINGPOLICY policy = Dwm.DWMNCRENDERINGPOLICY.Enabled;
Dwm.WindowSetAttribute(Handle, Dwm.DWMWINDOWATTRIBUTE.NCRenderingPolicy, (int)policy);
Dwm.WindowBorderlessDropShadow(Handle, 2);
m.Result = IntPtr.Zero;
}
break;
default:
break;
}
}
}
The Dwm
class contain all interop functions and structures used above, plus a bunch of other methods that could become useful in this context
[SuppressUnmanagedCodeSecurity]
public class Dwm {
public const int WM_DWMCOMPOSITIONCHANGED = 0x031E;
public struct MARGINS {
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
public MARGINS(int LeftWidth, int RightWidth, int TopHeight, int BottomHeight)
{
leftWidth = LeftWidth;
rightWidth = RightWidth;
topHeight = TopHeight;
bottomHeight = BottomHeight;
}
public void NoMargins()
{
leftWidth = 0;
rightWidth = 0;
topHeight = 0;
bottomHeight = 0;
}
public void SheetOfGlass()
{
leftWidth = -1;
rightWidth = -1;
topHeight = -1;
bottomHeight = -1;
}
}
[Flags]
public enum DWM_BB {
Enable = 1,
BlurRegion = 2,
TransitionOnMaximized = 4
}
// https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
public enum DWMWINDOWATTRIBUTE : uint {
NCRenderingEnabled = 1, //Get atttribute
NCRenderingPolicy, //Enable or disable non-client rendering
TransitionsForceDisabled,
AllowNCPaint,
CaptionButtonBounds, //Get atttribute
NonClientRtlLayout,
ForceIconicRepresentation,
Flip3DPolicy,
ExtendedFrameBounds, //Get atttribute
HasIconicBitmap,
DisallowPeek,
ExcludedFromPeek,
Cloak,
Cloaked, //Get atttribute. Returns a DWMCLOACKEDREASON
FreezeRepresentation,
PassiveUpdateMode,
UseHostBackDropBrush,
AccentPolicy = 19, // Win 10 (undocumented)
ImmersiveDarkMode = 20, // Win 11 22000
WindowCornerPreference = 33, // Win 11 22000
BorderColor, // Win 11 22000
CaptionColor, // Win 11 22000
TextColor, // Win 11 22000
VisibleFrameBorderThickness, // Win 11 22000
SystemBackdropType // Win 11 22621
}
public enum DWMCLOACKEDREASON : uint {
DWM_CLOAKED_APP = 0x0000001, //cloaked by its owner application.
DWM_CLOAKED_SHELL = 0x0000002, //cloaked by the Shell.
DWM_CLOAKED_INHERITED = 0x0000004 //inherited from its owner window.
}
public enum DWMNCRENDERINGPOLICY : uint {
UseWindowStyle, // Enable/disable non-client rendering based on window style
Disabled, // Disabled non-client rendering; window style is ignored
Enabled, // Enabled non-client rendering; window style is ignored
};
public enum DWMACCENTSTATE {
ACCENT_DISABLED = 0,
ACCENT_ENABLE_GRADIENT = 1,
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
ACCENT_ENABLE_BLURBEHIND = 3,
ACCENT_INVALID_STATE = 4
}
[Flags]
public enum CompositionAction : uint {
DWM_EC_DISABLECOMPOSITION = 0,
DWM_EC_ENABLECOMPOSITION = 1
}
// Values designating how Flip3D treats a given window.
enum DWMFLIP3DWINDOWPOLICY : uint {
Default, // Hide or include the window in Flip3D based on window style and visibility.
ExcludeBelow, // Display the window under Flip3D and disabled.
ExcludeAbove, // Display the window above Flip3D and enabled.
};
public enum ThumbProperties_dwFlags : uint {
RectDestination = 0x00000001,
RectSource = 0x00000002,
Opacity = 0x00000004,
Visible = 0x00000008,
SourceClientAreaOnly = 0x00000010
}
[StructLayout(LayoutKind.Sequential)]
public struct AccentPolicy {
public DWMACCENTSTATE AccentState;
public int AccentFlags;
public int GradientColor;
public int AnimationId;
public AccentPolicy(DWMACCENTSTATE accentState, int accentFlags, int gradientColor, int animationId)
{
AccentState = accentState;
AccentFlags = accentFlags;
GradientColor = gradientColor;
AnimationId = animationId;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct DWM_BLURBEHIND {
public DWM_BB dwFlags;
public int fEnable;
public IntPtr hRgnBlur;
public int fTransitionOnMaximized;
public DWM_BLURBEHIND(bool enabled)
{
dwFlags = DWM_BB.Enable;
fEnable = (enabled) ? 1 : 0;
hRgnBlur = IntPtr.Zero;
fTransitionOnMaximized = 0;
}
public Region Region => Region.FromHrgn(hRgnBlur);
public bool TransitionOnMaximized {
get => fTransitionOnMaximized > 0;
set {
fTransitionOnMaximized = (value) ? 1 : 0;
dwFlags |= DWM_BB.TransitionOnMaximized;
}
}
public void SetRegion(Graphics graphics, Region region)
{
hRgnBlur = region.GetHrgn(graphics);
dwFlags |= DWM_BB.BlurRegion;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct WinCompositionAttrData {
public DWMWINDOWATTRIBUTE Attribute;
public IntPtr Data; //Will point to an AccentPolicy struct, where Attribute will be DWMWINDOWATTRIBUTE.AccentPolicy
public int SizeOfData;
public WinCompositionAttrData(DWMWINDOWATTRIBUTE attribute, IntPtr data, int sizeOfData)
{
Attribute = attribute;
Data = data;
SizeOfData = sizeOfData;
}
}
private static int GetBlurBehindPolicyAccentFlags()
{
int drawLeftBorder = 20;
int drawTopBorder = 40;
int drawRightBorder = 80;
int drawBottomBorder = 100;
return (drawLeftBorder | drawTopBorder | drawRightBorder | drawBottomBorder);
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/aa969508(v=vs.85).aspx
[DllImport("dwmapi.dll")]
internal static extern int DwmEnableBlurBehindWindow(IntPtr hwnd, ref DWM_BLURBEHIND blurBehind);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmEnableComposition(CompositionAction uCompositionAction);
//https://msdn.microsoft.com/it-it/library/windows/desktop/aa969512(v=vs.85).aspx
[DllImport("dwmapi.dll")]
internal static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
//https://msdn.microsoft.com/en-us/library/windows/desktop/aa969515(v=vs.85).aspx
[DllImport("dwmapi.dll")]
internal static extern int DwmGetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE attr, ref int attrValue, int attrSize);
//https://msdn.microsoft.com/en-us/library/windows/desktop/aa969524(v=vs.85).aspx
[DllImport("dwmapi.dll")]
internal static extern int DwmSetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE attr, ref int attrValue, int attrSize);
[DllImport("User32.dll", SetLastError = true)]
internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WinCompositionAttrData data);
[DllImport("dwmapi.dll")]
internal static extern int DwmIsCompositionEnabled(ref int pfEnabled);
public static bool IsCompositionEnabled()
{
int pfEnabled = 0;
int result = DwmIsCompositionEnabled(ref pfEnabled);
return (pfEnabled == 1) ? true : false;
}
public static bool IsNonClientRenderingEnabled(IntPtr hWnd)
{
int gwaEnabled = 0;
int result = DwmGetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.NCRenderingEnabled, ref gwaEnabled, sizeof(int));
return gwaEnabled == 1;
}
public static bool WindowSetAttribute(IntPtr hWnd, DWMWINDOWATTRIBUTE attribute, int attributeValue)
{
int result = DwmSetWindowAttribute(hWnd, attribute, ref attributeValue, sizeof(int));
return (result == 0);
}
public static void Windows10EnableBlurBehind(IntPtr hWnd)
{
DWMNCRENDERINGPOLICY policy = DWMNCRENDERINGPOLICY.Enabled;
WindowSetAttribute(hWnd, DWMWINDOWATTRIBUTE.NCRenderingPolicy, (int)policy);
AccentPolicy accPolicy = new AccentPolicy() {
AccentState = DWMACCENTSTATE.ACCENT_ENABLE_BLURBEHIND,
};
int accentSize = Marshal.SizeOf(accPolicy);
IntPtr accentPtr = Marshal.AllocHGlobal(accentSize);
Marshal.StructureToPtr(accPolicy, accentPtr, false);
var data = new WinCompositionAttrData(DWMWINDOWATTRIBUTE.AccentPolicy, accentPtr, accentSize);
SetWindowCompositionAttribute(hWnd, ref data);
Marshal.FreeHGlobal(accentPtr);
}
public static bool WindowEnableBlurBehind(IntPtr hWnd)
{
DWMNCRENDERINGPOLICY policy = DWMNCRENDERINGPOLICY.Enabled;
WindowSetAttribute(hWnd, DWMWINDOWATTRIBUTE.NCRenderingPolicy, (int)policy);
DWM_BLURBEHIND dwm_BB = new DWM_BLURBEHIND(true);
int result = DwmEnableBlurBehindWindow(hWnd, ref dwm_BB);
return result == 0;
}
public static bool WindowExtendIntoClientArea(IntPtr hWnd, MARGINS margins)
{
// Extend frame on the bottom of client area
int result = DwmExtendFrameIntoClientArea(hWnd, ref margins);
return result == 0;
}
public static bool WindowBorderlessDropShadow(IntPtr hWnd, int shadowSize)
{
MARGINS margins = new MARGINS(0, shadowSize, 0, shadowSize);
int result = DwmExtendFrameIntoClientArea(hWnd, ref margins);
return result == 0;
}
public static bool WindowSheetOfGlass(IntPtr hWnd)
{
MARGINS margins = new MARGINS();
//Margins set to All:-1 - Sheet Of Glass effect
margins.SheetOfGlass();
int result = DwmExtendFrameIntoClientArea(hWnd, ref margins);
return result == 0;
}
public static bool WindowDisableRendering(IntPtr hWnd)
{
int ncrp = (int)DWMNCRENDERINGPOLICY.Disabled;
// Disable non-client area rendering on the window.
int result = DwmSetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.NCRenderingPolicy, ref ncrp, sizeof(int));
return result == 0;
}
} //DWM
This is a sample result with a Form.BackColor
set to MidnightBlue
:
Window Selected Windows Unselected
With some controls on it:

- 29,621
- 8
- 43
- 61
-
Hello **Jimi** I edited the code example because it had some errors and was non-functional. But still I can't get this blur effect on my `winform`!!! Is this solution still "working"? I am working on Windows 10 Edition Windows 10 Pro, 22H2. – Simos Sigma Jan 06 '23 at 14:58
-
1@SimosSigma I've rewritten the whole thing. It should be easier to put to work. The Blur Behind functionality, as implemented here, works in Windows 7, Windows 10 and Windows 11. Tested in all – Jimi Jan 06 '23 at 16:18
-
I applied your code but I still have issues... First into `public struct THUMBNAIL_PROPERTIES` of `public class Dwm` I get `The type or namespace name 'RECT' could not be found` error. I changed from `RECT` to `Rectangle` and started my program. The form appears but without **Blur Effect** plus it has some white borders at the right and bottom sides when I click outside [ https://i.stack.imgur.com/up6HZ.gif ]. I think something is wrong into `if (IsDWMCompositionEnabled())` of `protected override void OnHandleCreated(EventArgs e)`!!! ---> – Simos Sigma Jan 07 '23 at 12:51
-
---> If I change `if (osVersion.Major > 6)` to `if (osVersion.Major == 6 && osVersion.Minor > 1)`, I get the blur effect but, controls of the form are transparent and blurred too. And if I make my form borderless it looses color too and looks blurred but white!!! – Simos Sigma Jan 07 '23 at 12:51
-
@SimosSigma I've simplified the code further (doesn't support handle recreation, but this rarely happens with Forms) -- `Environment.OSVersion.Version.Major` in Windows 10 must return `10`. If you have another value, this may suggest you're in a VM -- In Windows 10, this form of composition is not fully supported (it looks better in Windows 11). Child Controls may become semi-transparent when the background is very bright. – Jimi Jan 07 '23 at 15:27
-
I have also found this ( https://foxlearn.com/windows-forms/how-to-create-acrylic-transparency-effect-in-csharp-490.html ) which applies the acrylic effect but with a lot of lag!!! About the Windows version, no I am not in a VM!!! The `Environment.OSVersion.Version` gives me `6.2.9200.0` but I am using `Windows 10 Pro, 22H2`. For this issue I found this ( https://www.prugg.at/2019/09/09/properly-detect-windows-version-in-c-net-even-windows-10/?fbclid=IwAR3sUZzGV09-j33_jTnsWJeetDuAJDpYocoZOP8lT7VgpG5ifEIkM8qmxzs ) and this ( https://stackoverflow.com/q/33328739/6336312 ). – Simos Sigma Jan 08 '23 at 09:40