I've created a borderless WPF window by setting the following window properties in XAML:
... WindowStyle="None" AllowsTransparency="True" ...
This causes a number of issues:
1) Resolved: It no longer has any built-in resize functionality
2) Resolved: It no longer has any built-in drag functionality
3) Resolved: Without the top toolbar, it no longer has minimize/maximize/restore/close buttons
4) Resolved: Maximizing via aero snap or setting WindowState prevents it from unsnapping.
5) Maximizing via aero snap or setting WindowState will use the whole screen as the boundary, overlapping the windows toolbar.
6) Maximizing via aero snap or setting WindowState seems to include a -7 margin, giving the window 7 pixels on each side that are beyond the edges of the window.
1-3 were solved by making a xaml window template. I used invisible rectangles as handle regions, and some code behind that was applied via overriding OnApplyTemplate() to attach functionality via user32.dll SendMessage(...) for resize/move/minimize/maximize/restore/close.
I found the answer to # 4 here
I tried solving 5-6 by intercepting the maximize message via WndProc and setting the size/position manually, but this had the issue of overwriting the RestoreRegion to the maximized size/position, removing the ability to restore the window.
What's really odd is that resizing the window from the top border to the top of the screen triggers the aero full height snap just fine, with no issues at all.
So, I've come a long way, but 5-6 is still an issue... is there a way to manually specify the maximize region? Or, is there a way to set the window size without affecting the restoreregion property?
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 <Window ... > </Window>
tags in the XAML of your application, paste this in, at the top or bottom:
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="35"/>
<WindowChrome.WindowChrome>
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:
<Grid VerticalAlignment="Top" Background="White" Name="TitleBar" Height="35">
<Label Content="Borderless Window Test" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<StackPanel WindowChrome.IsHitTestVisibleInChrome="True" VerticalAlignment="Center" HorizontalAlignment="Right" Orientation="Horizontal" Name="WindowControls">
<Button Height="35" Width="35" Content="-" Padding="0" Name="MinimizeButton"/>
<Button Height="35" Width="35" Content="+" Padding="0" Name="MaximizeButton"/>
<Button Height="35" Width="35" Content="x" Padding="0" Name="CloseButton"/>
</StackPanel>
</Grid>
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!