C# - WPF Window with Aero Glass

By , 8/17/2013
(0 ratings)
Code needed for using the windows in applications

Add this code in the constructor of the App class (App.xaml.cs):

public partial class App : System.Windows.Application
{
public App()
{
// Create new instance of our GW class
GlassWindows gw = new GlassWindows();
// Set GW instance as a global property
this.Properties["GlassWindows"] = gw;
}
}


Now we have to override the "OnSourceInitialized" event of the respective Windows and add our Window to our GW collection.

protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
((GlassWindows)App.Current
.Properties["GlassWindows"])
.Add(this, new Thickness(-1));
}


Author: Koopakiller, translation by Michael List
Translate to VB
using System.Runtime.InteropServices;

static class DWMApi
{
    // This Windows Message constants are needed later,
    // to be able to react to changes.
    public const int WM_DWMCOMPOSITIONCHANGED = 0x031E;
    public const int WM_DWMNCRENDERINGCHANGED = 0x031F;
    public const int WM_DWMCOLORIZATIONCOLORCHANGED = 0x0320;
    public const int WM_DWMWINDOWMAXIMIZEDCHANGE = 0x0321;

    // The margins structure that determines the window area
    // to receive the glass effect.
    public struct MARGINS
    {
         public MARGINS(Thickness t)
         {
             Left = (int)t.Left;
             Right = (int)t.Right;
             Top = (int)t.Top;
             Bottom = (int)t.Bottom;
         }
         public int Left;
         public int Right;
         public int Top;
         public int Bottom;
    }

    // This function enables the glass effect
    [DllImport("dwmapi.dll")]
    public static extern void DwmExtendFrameIntoClientArea(
                              IntPtr hwnd, ref MARGINS margins);
    // This function checks whether the glass effect is enabled in the system
    [DllImport("dwmapi.dll")]
    public static extern bool DwmIsCompositionEnabled();
}

using System.Collections.Generic;
using System.Windows.Interop;
using System.Windows.Media;

class GlassWindows
{

    // The generic list for our windows
    private List<WindowItem> windows = new List<WindowItem>();

    // This function adds a window and an area of glass effect to our list
    public void Add(Window window, Thickness margin)
    {
        // Create instance of our Item class
        WindowItem wi = new WindowItem();
        // Find out handle of the WPF "Window" class
        wi.Handle = new WindowInteropHelper(window).Handle;
        // Assign glass area
        wi.margin = margin;
        // Assign WPF "Window"
        wi.window = window;
        // Add list entry
        windows.Add(wi);
        // Add Windows Message Hook
        HwndSource.FromHwnd(wi.Handle).AddHook(WndProc);
        // Enable glass effect
        EnableGlass(wi, true);
    }

    // Returns the list entry by the window handle
    // This allows matching of a Windows Message to an entry
    private WindowItem WindowItemByHandle(IntPtr Handle)
    {
        // Iterate over all windows in the list
        foreach (WindowItem wi in windows)
        {
            // If handle matches current entry - return and leave function
            if (wi.Handle == Handle)
                return wi;
        }
        return null;
    }

    // Enable/Disable the glass effect
    private void EnableGlass(WindowItem windowitem, bool enabled)
    {
        if (enabled && DWMApi.DwmIsCompositionEnabled())
        {
            // Set the window background color to transparent
            windowitem.window.Background = Brushes.Transparent;
            // Set color on where the glass effect should
            // be applied (Transparent)
            HwndSource.FromHwnd(windowitem.Handle).
                CompositionTarget.BackgroundColor
                    = Colors.Transparent;
            // Define area for the glass effect
            DWMApi.MARGINS margins =
                       new DWMApi.MARGINS(windowitem.margin);
            // Enable glass effect
            DWMApi.DwmExtendFrameIntoClientArea
                       (windowitem.Handle, ref margins);
        }
        else
        {
            // Change window background color back to system color
            windowitem.window.Background
                = SystemColors.WindowBrush;
        }
    }

    // Catch Windows Messages (Hook)
    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam,
                           IntPtr lParam, ref bool handled)
    {
        // If glass effect enabled/disabled - treat appropriate
    if (msg == DWMApi.WM_DWMCOMPOSITIONCHANGED)
        {
            EnableGlass(WindowItemByHandle(hwnd),
                        DWMApi.DwmIsCompositionEnabled());
            handled = true;
        }
        return IntPtr.Zero;
    }

}

// Small class as Item for our Collection
class WindowItem
{
    public IntPtr Handle;
    public Window window;
    public Thickness margin;
}
Tagged with WPF, Glass, WDM, Aero.

Comments

 

Log in, to comment!