Wednesday, January 14, 2009

Aero Glass inside Window

Taken from http://blogs.msdn.com/adam_nathan/archive/2006/05/04/589686.aspx

Modified a little such that you can extend GlassWindow instead of Window and your XAML changes from <Window to <local:GlassWindow

   1: public class GlassWindow : Window


   2:    {


   3:        protected override void OnSourceInitialized(EventArgs e)


   4:        {


   5:            base.OnSourceInitialized(e);


   6:            GlassHelper.ExtendGlassFrame(this, new Thickness(-1));


   7:        }


   8:    }


   9:    public class GlassHelper


  10:    {


  11:        public static bool ExtendGlassFrame(Window window, Thickness margin)


  12:        {


  13:            if (!DwmIsCompositionEnabled())


  14:                return false;


  15:  


  16:            IntPtr hwnd = new WindowInteropHelper(window).Handle;


  17:            if (hwnd == IntPtr.Zero)


  18:                throw new InvalidOperationException("The Window must be shown before extending glass.");


  19:  


  20:            //Set the background to transparent from both the WPF and Win32 perspectives


  21:            //window.Background = Brushes.Transparent;


  22:            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;


  23:  


  24:            MARGINS margins = new MARGINS(margin);


  25:            DwmExtendFrameIntoClientArea(hwnd, ref margins);


  26:            return true;


  27:        }


  28:  


  29:        [DllImport("dwmapi.dll", PreserveSig = false)]


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


  31:  


  32:        [DllImport("dwmapi.dll", PreserveSig = false)]


  33:        static extern bool DwmIsCompositionEnabled();


  34:    }


  35:    struct MARGINS


  36:    {


  37:        public MARGINS(Thickness t)


  38:        {


  39:            Left = (int)t.Left;


  40:            Right = (int)t.Right;


  41:            Top = (int)t.Top;


  42:            Bottom = (int)t.Bottom;


  43:        }


  44:        public int Left;


  45:        public int Right;


  46:        public int Top;


  47:        public int Bottom;


  48:    }




usage





   1: /// <summary>


   2:    /// Interaction logic for Window1.xaml


   3:    /// </summary>


   4:    public partial class Window1 : GlassWindow


   5:    {


   6:        public Window1()


   7:        {


   8:            InitializeComponent();


   9:        }


  10:    }




XAML changes to





   1: <local:GlassWindow x:Class="TryUserapp.Window1"


   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"


   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"


   4:     Title="Window1" Height="600" Width="600"


   5:     xmlns:local="clr-namespace:TryUserapp" WindowStyle="None"


   6:     >


   7:     <local:GlassWindow.Background>


   8:         <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">            


   9:             <GradientStop Color="#CC101010" Offset="1"/>            


  10:             <GradientStop Color="#AA101010" Offset="0.01"/>            


  11:         </LinearGradientBrush>


  12:     </local:GlassWindow.Background>


  13:     <!-- -->


  14: </local:GlassWindow>


No comments: