Per abilitare lo stile "vetrato" in una finestra ridimensiobile, ovvero con FormBorderStyle = FormBorderStyle.Sizable è sufficiente utilizzare l'API "DwmExtendFrameIntoClientArea".
Al contrario nel caso in cui la finestra a cui vogliamo applicare lo stile vetrato sia senza bordo o non ridimensionabile è necessario utilizzare l'API "DwmEnableBlurBehindWindow", vediamo come:
Per prima cosa occorre creare un nuovo progetto di tipo Windows Forms Application, quindi nella classe Form1 inseriamo le dichiarazioni di cui abbiamo bisogno per l'invocazione di "DwmEnableBlurBehindWindow":
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct DWM_BLURBEHIND
{
public uint dwFlags;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
public bool fEnable;
public IntPtr hRegionBlur;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
public bool fTransitionOnMaximized;
public const uint DWM_BB_ENABLE = 0x00000001;
public const uint DWM_BB_BLURREGION = 0x00000002;
public const uint DWM_BB_TRANSITIONONMAXIMIZED = 0x00000004;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct RECT
{
public int left, top, right, bottom;
public RECT(int left, int top, int right, int bottom)
{
this.left = left; this.top = top;
this.right = right; this.bottom = bottom;
}
}
[System.Runtime.InteropServices.DllImport("dwmapi.dll", PreserveSig = false)]
public static extern int DwmEnableBlurBehindWindow(System.IntPtr hWnd, ref DWM_BLURBEHIND pBlurBehind);
...
Nel costruttore impostiamo la proprietà FormBorderStyle a FixedDialog:
public Form1()
{
InitializeComponent();
FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
}
Quindi sovrascriviamo il metodo OnLoad per abilitare lo stile nella finestra:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
DWM_BLURBEHIND dbb;
dbb.fEnable = true;
dbb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
using (Graphics g = CreateGraphics())
dbb.hRegionBlur = new Region(new Rectangle(0, 0, Width, Height)).GetHrgn(g);
dbb.fTransitionOnMaximized = false;
DwmEnableBlurBehindWindow(this.Handle, ref dbb);
}
Per ultimo occorre sovrascrivere il metodo OnPaintBackgroud per essere sicuri che lo sfondo della finestra sia di colore nero:
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
e.Graphics.FillRectangle(Brushes.Black, ClientRectangle);
}
Questo è il risultato: