
Adding Aero Glass Window Effect
Probably everyone who worked with Windows Vista (for example, there are also avid XPshniks who didn’t see Aero in their eyes :)), they saw the so-called Aero Glass effect - one of the distinctive effects of Windows Vista in Aero mode. By default, this effect extends to the titles of non-maximized windows and looks like blurring the contents of other windows located behind the title of the active window:

This effect can be expanded to the client area of the window, which will be discussed later. As an example, the Windows Vista Explorer window, which can be seen in the screenshot above. The effect in the conductor spreads to the client area where the controls are located. Naturally, this will only work on Windows Vista. Therefore, before applying this effect, we must check the version of Windows (below and further code using C #):
You also need to know that there is a restriction on the use of the effect: the effect zone always starts from the borders of the window, that is, you cannot create a “glass” piece somewhere in the middle of the window. This can be circumvented using non-transparent controls if you apply the effect to the entire window.
So, for the effect, you need to use the Win API DwmExtendFrameIntoClientArea () located in the DwmApi.dll library: The Margins structure determines how many pixels to extend the effect zone deep into the client area of the window Now let's declare the Margins structure: Now we can actually call the method, in this case we expand the effect zone to the client area down by 50 pixels: The effect must be applied before the window appears, or it will be necessary to recreate the window:
But that's not all. If we execute the code now, then instead of “glass” we will see a black bar. This is because GDI has no idea about the alpha channel, using a black brush in the effect zone solves the problem: Now we start and see the treasured window: The principle is clear. The rest, such as spreading the effect over the entire window area or adding the ability to drag and drop the mouse onto the effect zone, is a technical matter.


This effect can be expanded to the client area of the window, which will be discussed later. As an example, the Windows Vista Explorer window, which can be seen in the screenshot above. The effect in the conductor spreads to the client area where the controls are located. Naturally, this will only work on Windows Vista. Therefore, before applying this effect, we must check the version of Windows (below and further code using C #):
if (Environment.OSVersion.Version.Major >= 6)
{
// Vista features are supported.
}
You also need to know that there is a restriction on the use of the effect: the effect zone always starts from the borders of the window, that is, you cannot create a “glass” piece somewhere in the middle of the window. This can be circumvented using non-transparent controls if you apply the effect to the entire window.
So, for the effect, you need to use the Win API DwmExtendFrameIntoClientArea () located in the DwmApi.dll library: The Margins structure determines how many pixels to extend the effect zone deep into the client area of the window Now let's declare the Margins structure: Now we can actually call the method, in this case we expand the effect zone to the client area down by 50 pixels: The effect must be applied before the window appears, or it will be necessary to recreate the window:
[DllImport("DwmApi.dll")]
public static extern int DwmExtendFrameIntoClientArea(
IntPtr hwnd,
ref Margins pMarInset);
[StructLayout(LayoutKind.Sequential)]
public struct Margins
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
private void Form1_Load(object sender, EventArgs e)
{
margins = new Margins();
margins.cxLeftWidth = 0;
margins.cxRightWidth = 0;
margins.cyTopHeight = 50;
margins.cyBottomHeight = 0;
int retValue = DwmExtendFrameIntoClientArea(this.Handle, ref margins);
if (retValue < 0)
throw new NotSupportedException();
}
this.RecreateHandle();
But that's not all. If we execute the code now, then instead of “glass” we will see a black bar. This is because GDI has no idea about the alpha channel, using a black brush in the effect zone solves the problem: Now we start and see the treasured window: The principle is clear. The rest, such as spreading the effect over the entire window area or adding the ability to drag and drop the mouse onto the effect zone, is a technical matter.
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Black, 0, 0, this.ClientSize.Width, margins.cyTopHeight);
}
