none
WPF如何实现Win7窗体玻璃效果 RRS feed

答案

  • 这是我们机场看见的 DWM API,用于WPF 如下:

      public class GlassHelper
      {
        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;
        }
     
        [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)
        {
          try
          {
            if (!DwmIsCompositionEnabled())
              return false;
     
            IntPtr hwnd = new WindowInteropHelper(window).Handle;
            if (hwnd == IntPtr.Zero)
              throw new InvalidOperationException("The Window must be shown before extending glass.");
     
            // Set the background to transparent from both the WPF and Win32 perspectives
            window.Background = Brushes.Transparent;
            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;
     
            MARGINS margins = new MARGINS(margin);
            DwmExtendFrameIntoClientArea(hwnd, ref margins);
            return true;
          }
          catch (DllNotFoundException)
          {
            window.Background = Brushes.White;
            return false;
          }
        }
     
      }

     

    MainWindow.xaml:

     

      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
       GlassHelper.ExtendGlassFrame(this, new Thickness(-1));
      }
     
    
    

     

    不过我们还可以下载Windows Shell库来实现:http://archive.msdn.microsoft.com/WPFShell

    -------------

    且可以改变窗体边框的颜色或添加背景图片?

    如果是用了DWM API,那么你只有通过修改系统属性来改变颜色和背景。

     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    2011年8月21日 15:50
    版主

全部回复

  • 玻璃效果是通过dwmapi来实现的,背景图片则是可以设置window的背景来实现
    2011年8月19日 4:36
  • 这是我们机场看见的 DWM API,用于WPF 如下:

      public class GlassHelper
      {
        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;
        }
     
        [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)
        {
          try
          {
            if (!DwmIsCompositionEnabled())
              return false;
     
            IntPtr hwnd = new WindowInteropHelper(window).Handle;
            if (hwnd == IntPtr.Zero)
              throw new InvalidOperationException("The Window must be shown before extending glass.");
     
            // Set the background to transparent from both the WPF and Win32 perspectives
            window.Background = Brushes.Transparent;
            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;
     
            MARGINS margins = new MARGINS(margin);
            DwmExtendFrameIntoClientArea(hwnd, ref margins);
            return true;
          }
          catch (DllNotFoundException)
          {
            window.Background = Brushes.White;
            return false;
          }
        }
     
      }

     

    MainWindow.xaml:

     

      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
       GlassHelper.ExtendGlassFrame(this, new Thickness(-1));
      }
     
    
    

     

    不过我们还可以下载Windows Shell库来实现:http://archive.msdn.microsoft.com/WPFShell

    -------------

    且可以改变窗体边框的颜色或添加背景图片?

    如果是用了DWM API,那么你只有通过修改系统属性来改变颜色和背景。

     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    2011年8月21日 15:50
    版主