I Code, Therefore I Am

while(!(succeed == try()));

Adding Aero Effect to your application with C# and Win32

Today we’re going to look at using Aero in your WinForms applications. I’ve been asked this so many times that I figured this would be a good thing to blog about.

NOTE: Requires Vista or Windows 7 with Aero enabled, so we’ll need to check for that.

So let’s get started, first we need to pick the Win32 API methods we’ll be using, for this we’ll be using:

We’ll also need the MARGINS structure as well. What I did was make a seperate class for this, I named mine Win32. To use the Win32 we’ll need a reference to the System.Runtime.InteropService Namespace.

The struct looks like this:

[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
    public int Left;
    public int Right;
    public int Top;
    public int Bottom;
}

Now we need to declare our Win32 methods:

[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);

[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern bool DwmIsCompositionEnabled();

So our Win32.cs looks like this:

using System;
using System.Runtime.InteropServices;

namespace Aero.Code
{
    public class Win32
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct MARGINS
        {
            public int Left;
            public int Right;
            public int Top;
            public int Bottom;
        }

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern bool DwmIsCompositionEnabled();
    }
}

Simple enough wouldn’t you say. Now that we have our class done we can implement it in our form(s). As I stated earlier we need to make sure the user is running the proper version of windows, we’ll check this in the OnLoad method:

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

    if (!Win32.DwmIsCompositionEnabled())
    {
        MessageBox.Show("To use the Aero effect you need either Vista or Windows 7 with Aero enabled", "Aero Effect Error", MessageBoxButtons.OK);
        Application.Exit();
    }
    else
        GlassRegion();
            
}

Now we check and display an error before closing the application, otherwise we can GlassRegion to create our Aero. GlassRegion is where we set the glass look, it’s very simple and straight forward:

/// <summary>
/// Use the form padding values to define a Glass margin
/// </summary>
private void GlassRegion()
{
    // Set up the glass effect using padding as the defining glass region
    if (Win32.DwmIsCompositionEnabled())
    {
        _glassMargins = new Win32.MARGINS();
        _glassMargins.Top = Padding.Top + 5;
        _glassMargins.Left = Padding.Left + 5;
        _glassMargins.Bottom = Padding.Bottom + 5;
        _glassMargins.Right = Padding.Right + 5;
        Win32.DwmExtendFrameIntoClientArea(this.Handle, ref _glassMargins);
    }
}

You can change the margin to whatever size you wish. FInally we need to override the OnPaintBackground of the form, which looks like this when we get finished with it:

/// <summary>
/// Override the OnPaintBackground method, to draw the desired
/// Glass regions black and display as Glass
/// </summary>
protected override void OnPaintBackground(PaintEventArgs e)
{
    base.OnPaint(e);

    if (Win32.DwmIsCompositionEnabled())
    {
        e.Graphics.Clear(Color.Black);

        // put back the original form background for non-glass area
        var rectangle = new Rectangle(_glassMargins.Left, _glassMargins.Top,
        this.ClientRectangle.Width - _glassMargins.Left - _glassMargins.Right,
        this.ClientRectangle.Height - _glassMargins.Top - _glassMargins.Bottom);
        var brush = new SolidBrush(this.BackColor);
        e.Graphics.FillRectangle(brush, rectangle);
    }
}

that’s it, now the form(s) we apply this to will have the Aero glass effect. Not nearly as complicated as you may have thought. I hope you find this informative.

Happy Coding!


About The Author

Richard has been a professional software developer for over 15 years now. In fact he was a geek long before being a geek was 'cool'. He has his Bachelors in Computer Science from The University of Georgia and is an avid, passionate .NET developer. Richard eats, sleeps, drinks and dreams in code

Comments

One Response to “Adding Aero Effect to your application with C# and Win32”

  1. What your stating is absolutely real. I know that everybody ought to say the identical matter, but I just believe that you position it in a way that just about every individual can comprehend. I also adore the photographs you set in right here. They suit so appropriately with what youre making an attempt to say. Im assured youll reach so numerous persons with what youve acquired to say.

Leave a Reply