Implementation of glass wool using WPF

Source: Internet
Author: User

For versions 1 and 2, Microsoft. windowsapicodepack. Shell. dll and using system. Windows. InterOP are required. They can only be used in windows with the dwmapi. dll version. The common disadvantage of the two methods is that the form will flash when it is started.

I,

[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 dwmextendframeworkclientarea (intptr hwnd, ref margins pmarinset); Private void extendaeroglass (window) {try {// obtain the window handle intptr mainwindowptr = new window winterophelper (window) for the WPF program ). handle; hwndsource mainwindowsrc = hwndsource. fromhwnd (mainwindowptr); mainwindowsrc. compositiontarget. backgroundcolor = colors. transparent; // set margins = new margins (); // extend aero glass margins. cxleftwidth =-1; margins. cxrightwidth =-1; margins. cytopheight =-1; margins. cybottomheight =-1; int hR = dwmexico tendframeworkclientarea (mainwindowsrc. handle, ref margins); If (HR <0) {MessageBox. show ("dwmexico tendframeworkclientarea failed") ;}} catch (dllnotfoundexception) {application. current. mainwindow. background = brushes. white ;}} private void window_loaded (Object sender, routedeventargs e) {This. background = brushes. transparent; extendaeroglass (this );}

II,

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

III,

This method requires Microsoft. Windows. Shell. DLL to have no flash defects!

 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>

Address: http://blog.csdn.net/kingscrown/article/details/7771094

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.