• WPF实现毛玻璃效果


      1和2需要Microsoft.WindowsAPICodePack.Shell.dll 和引用using System.Windows.Interop,并只能在有DwmApi.dll 版本的Windows操作系统下使用。这两种方法的共同缺点是:在启动窗体时会一闪。

    一、

     [StructLayout(LayoutKind.Sequential)]
            public struct MARGINS
            {
                public int cxLeftWidth;
                public int cxRightWidth;
                public int cyTopHeight;
                public int cyBottomHeight;
            };
    
            [DllImport("DwmApi.dll")]
            public static extern int DwmExtendFrameIntoClientArea(
                IntPtr hwnd,
                ref MARGINS pMarInset);
    
            private void ExtendAeroGlass(Window window)
            {
                try
                {
                    // 为WPF程序获取窗口句柄
                    IntPtr mainWindowPtr = new WindowInteropHelper(window).Handle;
                    HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
                    mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;
    
                    // 设置Margins
                    MARGINS margins = new MARGINS();
    
                    // 扩展Aero Glass
                    margins.cxLeftWidth = -1;
                    margins.cxRightWidth = -1;
                    margins.cyTopHeight = -1;
                    margins.cyBottomHeight = -1;
    
                    int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
                    if (hr < 0)
                    {
                        MessageBox.Show("DwmExtendFrameIntoClientArea Failed");
                    }
                }
                catch (DllNotFoundException)
                {
                    Application.Current.MainWindow.Background = Brushes.White;
                }
            }
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                this.Background = Brushes.Transparent;
                ExtendAeroGlass(this);
            }

    二、

    [StructLayout(LayoutKind.Sequential)]
    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;
    }
    
    public class GlassHelper
    {
        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern void DwmExtendFrameIntoClientArea(
            IntPtr hWnd, ref MARGINS pMarInset);
        [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(
                "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;
        }
    }
    
            protected override void OnSourceInitialized(EventArgs e)
            {
                base.OnSourceInitialized(e);
                GlassHelper.ExtendGlassFrame(this, new Thickness(-1));
            }

    三、

    这个方法 需要Microsoft.Windows.Shell.dll   没有一闪的缺陷!

     xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Microsoft.Windows.Shell"
    <shell:WindowChrome.WindowChrome>
            <shell:WindowChrome GlassFrameThickness="-1" ResizeBorderThickness="1" 
                                CaptionHeight="15" CornerRadius="0" />
        </shell:WindowChrome.WindowChrome>
        <Window.Template>
            <ControlTemplate TargetType="{x:Type Window}">
                <Border BorderThickness="1">
                    <DockPanel>
                        <Grid x:Name="WindowHeader" DockPanel.Dock="Top" Height="15">
                            <TextBlock Text="{TemplateBinding Title}"/>
                        </Grid>
                        <ContentPresenter Margin="5,5,5,5"/>
                    </DockPanel>
                </Border>
            </ControlTemplate>
        </Window.Template>

    转载地址:http://blog.csdn.net/kingscrown/article/details/7771094

  • 相关阅读:
    C# listbox鼠标选择改变改行颜色的另一种方便方法
    非专业码农 JAVA学习笔记 4 java继承和多态
    转:Java学习笔记之方法重载,动态方法调度和抽象类
    非专业码农 JAVA学习笔记 3 抽象、封装和类(2)
    使用bootstrap简单制作Tab切换页
    转载:CSS从大图中抠取小图完整教程(background-position应用)
    xhEditor 整理用法
    SCADA开源项目lite版本
    ImageSharp源码详解之JPEG压缩原理(3)DCT变换
    ImageSharp源码详解之JPEG压缩原理(4)熵编码
  • 原文地址:https://www.cnblogs.com/candyzkn/p/3833128.html
Copyright © 2020-2023  润新知