WPF Borderless Window issues: Aero Snap & Maximizing

前端 未结 6 1630
独厮守ぢ
独厮守ぢ 2020-12-03 03:37

I\'ve created a borderless WPF window by setting the following window properties in XAML:

... WindowStyle=\"None\" AllowsTransparency=\"True\" ...

6条回答
  •  囚心锁ツ
    2020-12-03 04:20

    Easiest Full Solution

    Hello, The following solution fixes all of the issues detailed in your question in the simplest manner possible, and works on Windows 10 using WPF and the latest version of the C# language and .NET framework. This is as of 3/15/2017. Please let me know if it stops working.

    Step 1: To address issues 1, 2, and 4, within your tags in the XAML of your application, paste this in, at the top or bottom:

    
        
    
    

    CaptionHeight is the desired height of your window dragging area.

    Step 2: To address issue 3, you need to create your title bar and caption as well as the window controls. To do this, you simply need to give the desired title bar elements each a VerticalAlignment of Top, or put them into a grid with it's VerticalAlignment set to Top, which will do it for all of them, but make sure that their heights are not greater than the CaptionHeight property on the WindowChrome element declared in the XAML, from step 1. For all the buttons, you must assign them, or their container, the property WindowChrome.IsHitTestVisibleInChrome="True". Here is an example:

    
        

    Now, to add proper functionality to the window control buttons, within the MainWindow() constructor of your codebehind, the C# source code of your application, paste the following in, after the call to InitializeComponent();:

    CloseButton.Click += (s, e) => Close();
    MaximizeButton.Click += (s, e) => WindowState = WindowState == WindowState.Normal ? WindowState.Maximized : WindowState.Normal;
    MinimizeButton.Click += (s, e) => WindowState = WindowState.Minimized;
    

    Step 3: To address issues 5 and 6, you need to hook into WmGetMinMaxInfo. To do this, go to your codebehind, then copy and paste everything from this Pastebin into your Window class. Now, within your MainWindow() constructor, paste:

    SourceInitialized += (s, e) =>
    {
        IntPtr handle = (new WindowInteropHelper(this)).Handle;
        HwndSource.FromHwnd(handle).AddHook(new HwndSourceHook(WindowProc));
    };
    

    Via Project > Add References in the file menu, be sure to have references to:

    System.Management
    System.Windows.Interop
    System.Security.Principal
    System.Runtime.InteropServices
    Microsoft.Win32
    

    The best way to check is to click on the Assemblies tab in the top left, then select Framework, then use the search box in the top right corner of the window. Now add all of these usings (namespaces) to the top of your codebehind:

    using System.Management;
    using System.Windows.Interop;
    using System.Security.Principal;
    using System.Runtime.InteropServices;
    using Microsoft.Win32;
    

    That should cover everything. I hope this helps!

提交回复
热议问题