C# - How to create a semi transparent or blurred backcolor on windows form

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


C# - How to create a semi transparent or blurred backcolor on windows form



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.



enter image description here





You can make ts semi opaque, however i'm not sure you'll easily be able to blur the background
– TheGeneral
6 hours ago





thanks ! then how do I make it semi opaque
– Arwin Chua
6 hours ago





Form.Opacity Property
– TheGeneral
6 hours ago





See the DwmEnableBlurBehindWindow DWM Function. You can get most of the API declarations/structures for C# here: Borderless Form Dropshadow (on the bottom there's more of of what was required for that question).
– Jimi
4 hours ago




C#





@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
3 hours ago




DwmExtendFrameIntoClientArea()


DwmEnableBlurBehindWindow()


DWMNCRENDERINGPOLICY


DwmSetWindowAttribute()


DWMWINDOWATTRIBUTE.NCRenderingPolicy


WndProc




1 Answer
1



Since I really needed to verify whether what I said in the comments actually made sense, here is a WinForms Form with a Blur effect applied to it.


WinForms


Form



This procedure is quite simple.
You need to override the class WndProc; when a WM_DWMCOMPOSITIONCHANGED message (informing of the Aero composition state) is received, the Window needs to register the rendering policy with DwmSetWindowAttribute(), setting the DWMWINDOWATTRIBUTE enumerator to NCRenderingPolicy and enable it setting the DWMNCRENDERINGPOLICY enumerator to Enabled.


WndProc


NCRenderingPolicy


Enabled



Then, before showing the interface, call DwmEnableBlurBehindWindow(), setting its DWM_BLURBEHIND structure relevant fields (dwFlags and fEnable) to 1.
Here, I'm calling it from the Form.Load() event.


dwFlags


fEnable


1


Form.Load()



It's more clear in code:
(As a note, the BlurBehind effect does not work if the Form has a white background color)


BlurBehind



All the Windows API declarations, structure and enums are already available in another question here.
Insert all the code in a class and add a refence to this class in the using statement section of the Form.


using


public partial class frmBlurBehind : Form
{
public frmBlurBehind() => InitializeComponent();

private void frmBlurBehind_Load(object sender, EventArgs e)
{
if (CheckAeroEnabled()) { WinApi.Dwm.WindowEnableBlurBehind(this.Handle); }
}

private bool CheckAeroEnabled() => (Environment.OSVersion.Version.Major >= 6)
? WinApi.Dwm.IsCompositionEnabled()
: false;

protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case (int)WinApi.WinMessage.WM_DWMCOMPOSITIONCHANGED:
{
WinApi.Dwm.DWMNCRENDERINGPOLICY Policy = WinApi.Dwm.DWMNCRENDERINGPOLICY.Enabled;
WinApi.Dwm.WindowSetAttribute(this.Handle, WinApi.Dwm.DWMWINDOWATTRIBUTE.NCRenderingPolicy, (int)Policy);
m.Result = (IntPtr)0;
}
break;
default:
break;
}
base.WndProc(ref m);
}
}



This is a sample result with a Form.BackColor set to (64, 64, 64):


Form.BackColor


64, 64, 64



enter image description hereenter image description here


Window Selected Windows Unselected






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Will Oldham

Makefile test if variable is not empty