Mostrando entradas con la etiqueta Aero. Mostrar todas las entradas
Mostrando entradas con la etiqueta Aero. Mostrar todas las entradas

jueves, 26 de agosto de 2010

Aero Glass en ventanas WPF

Una forma fácil y linda de decorar nuestras ventanas , y que ademas quede integrada con Windows, es usar Aero Glass.



Lo que devemos hacer es crear una clase en nuestro proyecto llamada GlassHelper


public class GlassHelper
{
         [DllImport("dwmapi.dll", PreserveSig = false)]

        static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern bool DwmIsCompositionEnabled();
        public static bool ExtendGlassFrame(Window window, Thickness margin)
        {                
             if (!DwmIsCompositionEnabled())
                 return false;
             IntPtr hwnd = new WindowInteropHelper(window).Handle;
             if (hwnd == IntPtr.Zero)
                 throw new InvalidOperationException("ERROR");

             window.Background = Brushes.Transparent;
             HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;

             MARGINS margins = new MARGINS(margin);
             DwmExtendFrameIntoClientArea(hwnd, ref margins);
             return true;
      }
}

Luego creamos una struct así:

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;
}

Y en nuestra ventana colocamos esto:
protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e); 
    GlassHelper.ExtendGlassFrame(this, new Thickness(-1));
}

Listo, ya tenemos nuestra ventana decorada con Aero Glass