Glass on WinForms

Enabling the "glass sheet" effect with WinForms requires two simple steps:
  1. Set the Form background to Black
  2. Call EnableGlassSheet (for instance in the OnLoad event).

protected override void OnLoad(EventArgs e) {
	base.OnLoad(e);

	VistaControls.DWM.DWMManager.EnableGlassSheet(this);
}


This will create a completely transparent glass frame. The problem is, WinForms control are not alpha-blended and won't render correctly on the glass frame (all black elements will be completely transparent).

glass_wf1.png

Even if you can draw shapes and text on the frame correctly using GDI+, in order to use the controls you'll need to define a non-glassy area of the frame on which to place all controls. You'll also need to draw the black background only on the areas which will in fact be transparent:

protected override void OnLoad(EventArgs e) {
	base.OnLoad(e);

	VistaControls.DWM.DWMManager.EnableGlassFrame(this, new VistaControls.DWM.Margins(30));
}

protected override void OnPaint(PaintEventArgs e) {
	base.OnPaint(e);

	e.Graphics.FillRectangles(Brushes.Black, new Rectangle[] {
		new Rectangle(0, 0, this.ClientSize.Width, 30),
		new Rectangle(this.ClientSize.Width - 30, 0, 30, this.ClientSize.Height),
		new Rectangle(0, this.ClientSize.Height - 30, this.ClientSize.Width, 30),
		new Rectangle(0, 0, 30, this.ClientSize.Height)
	});
}


The following code creates a 30 pixel glass border around the center of the window, which will look like a normal WinForms window and can contain normal controls as usual. Remember to set the Form's background to the default "Control" color in the Visual Studio Form designer. This is the result:

glass_wf2.png

Remember that the Windows Vista Controls library includes several helper classes that will help you taking over the most trivial tasks of making a glass Form (like the black background painting and moving the form when the user clicks on any glass part).

Last edited Feb 28, 2008 at 1:52 PM by Lck, version 2

Comments

starkiller53861 Jun 21, 2010 at 7:59 PM 
Shouldn't:

protected override void OnLoad(EventArgs e) {
base.OnLoad(e);

VistaControls.DWM.DWMManager.EnableGlassSheet(this);
}

be:

using VistaControls.Dwn;
...
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);

VistaControls.Dwm.DwmManager.EnableGlassSheet(this);
}