Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions MahApps.Metro/Behaviours/BorderlessWindowBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class BorderlessWindowBehavior : Behavior<Window>
private IntPtr handle;
private HwndSource hwndSource;
private WindowChrome windowChrome;
private PropertyChangeNotifier borderThicknessChangeNotifier;
private Thickness? savedBorderThickness;

protected override void OnAttached()
Expand Down Expand Up @@ -57,7 +58,10 @@ protected override void OnAttached()
}
}
AssociatedObject.WindowStyle = WindowStyle.None;

savedBorderThickness = AssociatedObject.BorderThickness;
borderThicknessChangeNotifier = new PropertyChangeNotifier(this.AssociatedObject, Window.BorderThicknessProperty);
borderThicknessChangeNotifier.ValueChanged += BorderThicknessChangeNotifierOnValueChanged;

AssociatedObject.Loaded += AssociatedObject_Loaded;
AssociatedObject.Unloaded += AssociatedObject_Unloaded;
Expand All @@ -67,6 +71,11 @@ protected override void OnAttached()
base.OnAttached();
}

private void BorderThicknessChangeNotifierOnValueChanged(object sender, EventArgs e)
{
savedBorderThickness = AssociatedObject.BorderThickness;
}

private void UseNoneWindowStylePropertyChangedCallback(object sender, EventArgs e)
{
var metroWindow = sender as MetroWindow;
Expand Down Expand Up @@ -144,25 +153,8 @@ private void AssociatedObject_Unloaded(object sender, RoutedEventArgs e)
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
var returnval = IntPtr.Zero;
var metroWindow = AssociatedObject as MetroWindow;


switch (msg) {
case Constants.WM_NCPAINT:
var enableDWMDropShadow = EnableDWMDropShadow;

if (metroWindow != null)
{
enableDWMDropShadow = metroWindow.GlowBrush == null && (metroWindow.EnableDWMDropShadow || EnableDWMDropShadow);
}
if (enableDWMDropShadow)
{
var val = 2;
UnsafeNativeMethods.DwmSetWindowAttribute(hwnd, 2, ref val, 4);
var m = new MARGINS { bottomHeight = 1, leftWidth = 1, rightWidth = 1, topHeight = 1 };
UnsafeNativeMethods.DwmExtendFrameIntoClientArea(hwnd, ref m);
}
handled = true;
break;
case Constants.WM_NCACTIVATE:
/* As per http://msdn.microsoft.com/en-us/library/ms632633(VS.85).aspx , "-1" lParam "does not repaint the nonclient area to reflect the state change." */
returnval = UnsafeNativeMethods.DefWindowProc(hwnd, msg, wParam, new IntPtr(-1));
Expand All @@ -180,13 +172,21 @@ private void OnAssociatedObjectHandleMaximize(object sender, EventArgs e)

private void HandleMaximize()
{
borderThicknessChangeNotifier.ValueChanged -= BorderThicknessChangeNotifierOnValueChanged;

var metroWindow = AssociatedObject as MetroWindow;
var enableDWMDropShadow = EnableDWMDropShadow;
if (metroWindow != null)
{
enableDWMDropShadow = metroWindow.GlowBrush == null && (metroWindow.EnableDWMDropShadow || EnableDWMDropShadow);
}

if (AssociatedObject.WindowState == WindowState.Maximized)
{
// remove resize border and window border, so we can move the window from top monitor position
windowChrome.ResizeBorderThickness = new Thickness(0);
AssociatedObject.BorderThickness = new Thickness(0);

var metroWindow = AssociatedObject as MetroWindow;
var ignoreTaskBar = metroWindow != null && metroWindow.IgnoreTaskbarOnMaximize;
if (ignoreTaskBar)
{
Expand All @@ -210,7 +210,10 @@ private void HandleMaximize()
else
{
windowChrome.ResizeBorderThickness = SystemParameters2.Current.WindowResizeBorderThickness;
AssociatedObject.BorderThickness = savedBorderThickness.GetValueOrDefault(new Thickness(0));
if (!enableDWMDropShadow)
{
AssociatedObject.BorderThickness = savedBorderThickness.GetValueOrDefault(new Thickness(0));
}

// fix nasty TopMost bug
// - set TopMost="True"
Expand All @@ -222,6 +225,8 @@ private void HandleMaximize()
AssociatedObject.Topmost = false;
AssociatedObject.Topmost = topMost;
}

borderThicknessChangeNotifier.ValueChanged += BorderThicknessChangeNotifierOnValueChanged;
}

private void AssociatedObject_SourceInitialized(object sender, EventArgs e)
Expand Down Expand Up @@ -257,8 +262,10 @@ private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
window.SetIsHitTestVisibleInChromeProperty<ContentControl>("PART_WindowButtonCommands");
}

[Obsolete("This property will be deleted in the next release. Use the MetroWindow EnableDWMDropShadow property instead.")]
public static readonly DependencyProperty EnableDWMDropShadowProperty = DependencyProperty.Register("EnableDWMDropShadow", typeof(bool), typeof(BorderlessWindowBehavior), new PropertyMetadata(false));

[Obsolete("This property will be deleted in the next release. Use the MetroWindow EnableDWMDropShadow property instead.")]
public bool EnableDWMDropShadow
{
get { return (bool)GetValue(EnableDWMDropShadowProperty); }
Expand Down
4 changes: 4 additions & 0 deletions MahApps.Metro/Controls/GlowWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public GlowWindow(Window owner, GlowDirection direction)
b.Source = owner;
glow.SetBinding(Glow.NonActiveGlowBrushProperty, b);

b = new Binding("BorderThickness");
b.Source = owner;
glow.SetBinding(Glow.BorderThicknessProperty, b);

switch (direction)
{
case GlowDirection.Left:
Expand Down
23 changes: 22 additions & 1 deletion MahApps.Metro/Controls/MetroWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class MetroWindow : Window
public static readonly DependencyProperty UseNoneWindowStyleProperty = DependencyProperty.Register("UseNoneWindowStyle", typeof(bool), typeof(MetroWindow), new PropertyMetadata(false, OnUseNoneWindowStylePropertyChangedCallback));
public static readonly DependencyProperty OverrideDefaultWindowCommandsBrushProperty = DependencyProperty.Register("OverrideDefaultWindowCommandsBrush", typeof(SolidColorBrush), typeof(MetroWindow));

public static readonly DependencyProperty EnableDWMDropShadowProperty = DependencyProperty.Register("EnableDWMDropShadow", typeof(bool), typeof(MetroWindow), new PropertyMetadata(false));
public static readonly DependencyProperty EnableDWMDropShadowProperty = DependencyProperty.Register("EnableDWMDropShadow", typeof(bool), typeof(MetroWindow), new PropertyMetadata(false, OnEnableDWMDropShadowPropertyChangedCallback));
public static readonly DependencyProperty IsWindowDraggableProperty = DependencyProperty.Register("IsWindowDraggable", typeof(bool), typeof(MetroWindow), new PropertyMetadata(true));

UIElement icon;
Expand Down Expand Up @@ -132,6 +132,22 @@ public bool EnableDWMDropShadow
set { SetValue(EnableDWMDropShadowProperty, value); }
}

private static void OnEnableDWMDropShadowPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != e.OldValue && (bool)e.NewValue)
{
var window = (MetroWindow)d;
window.UseDropShadow();
}
}

private void UseDropShadow()
{
this.BorderThickness = new Thickness(0);
this.BorderBrush = null;
this.GlowBrush = Brushes.Black;
}

public bool IsWindowDraggable
{
get { return (bool)GetValue(IsWindowDraggableProperty); }
Expand Down Expand Up @@ -675,6 +691,11 @@ public MetroWindow()

private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
{
if (EnableDWMDropShadow)
{
this.UseDropShadow();
}

if (this.WindowTransitionsEnabled)
{
VisualStateManager.GoToState(this, "AfterLoaded", true);
Expand Down
15 changes: 13 additions & 2 deletions MahApps.Metro/Styles/Clean/CleanWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,22 @@
</ControlTemplate.Resources>
<ControlTemplate.Triggers>
<!-- handle active/inactive state -->
<Trigger Property="IsActive"
Value="False">
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsActive"
Value="False" />
<Condition Property="GlowBrush"
Value="{x:Null}" />
</MultiTrigger.Conditions>
<Setter TargetName="PART_Border"
Property="BorderBrush"
Value="{Binding Path=NonActiveBorderBrush, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}}" />
</MultiTrigger>
<Trigger Property="BorderBrush"
Value="{x:Null}">
<Setter TargetName="PART_Border"
Property="Visibility"
Value="Collapsed" />
</Trigger>
<!-- no icon and no icon content template -> collapse the icon content control -->
<MultiTrigger>
Expand Down
17 changes: 10 additions & 7 deletions MahApps.Metro/Themes/Glow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
TargetType="{x:Type Controls:Glow}">
<Grid Margin="{TemplateBinding Padding}"
Background="Transparent">
<Border Grid.Row="1"
Grid.Column="1"
Background="{TemplateBinding Background}" />
<Border Background="{TemplateBinding Background}" />
<Border x:Name="glowSource"
Grid.Row="1"
Grid.Column="1"
Margin="-1,0"
Opacity="0.6"
Background="{TemplateBinding Background}">
Expand All @@ -27,14 +23,21 @@
Property="Margin"
Value="0,-1" />
</Trigger>
<Trigger Property="BorderThickness" Value="0">
<Setter TargetName="glowSource"
Property="Opacity"
Value="1" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>

<Style TargetType="{x:Type Controls:Glow}">
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="Background"
Value="{Binding RelativeSource={RelativeSource Self}, Path=GlowBrush}" />
<Setter Property="Width"
Value="1" />
Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderThickness.Left, Mode=OneWay}" />
<Setter Property="Padding"
Value="0,8" />
<Setter Property="Template"
Expand All @@ -50,7 +53,7 @@
<Setter Property="Width"
Value="Auto" />
<Setter Property="Height"
Value="1" />
Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderThickness.Top, Mode=OneWay}" />
<Setter Property="Padding"
Value="0" />
</Trigger>
Expand Down
20 changes: 18 additions & 2 deletions MahApps.Metro/Themes/MetroWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,28 @@
</ControlTemplate.Resources>
<ControlTemplate.Triggers>
<!-- handle active/inactive state -->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsActive"
Value="False" />
<Condition Property="GlowBrush"
Value="{x:Null}" />
</MultiTrigger.Conditions>
<Setter TargetName="PART_Border"
Property="BorderBrush"
Value="{Binding Path=NonActiveBorderBrush, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}}" />
</MultiTrigger>
<Trigger Property="IsActive"
Value="False">
<Setter TargetName="PART_WindowTitleBackground"
Property="Fill"
Value="{Binding Path=NonActiveWindowTitleBrush, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}}" />
</Trigger>
<Trigger Property="BorderBrush"
Value="{x:Null}">
<Setter TargetName="PART_Border"
Property="BorderBrush"
Value="{Binding Path=NonActiveBorderBrush, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}}" />
Property="Visibility"
Value="Collapsed" />
</Trigger>
<!-- no icon and no icon content template -> collapse the icon content control -->
<MultiTrigger>
Expand Down Expand Up @@ -238,6 +252,8 @@
</ControlTemplate>

<Style TargetType="{x:Type Controls:MetroWindow}">
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="UseLayoutRounding"
Value="True" />
<Setter Property="WindowTitleBrush"
Expand Down
4 changes: 4 additions & 0 deletions samples/MetroDemo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}, Path=IgnoreTaskbarOnMaximize}" />
<MenuItem IsCheckable="True" Header="Toggle FullScreen (no taskbar, window style = none)"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}, Path=ToggleFullScreen}" />
<Separator />
<MenuItem Header="Window with a Border" Click="MenuWindowWithBorderOnClick" />
<MenuItem Header="Window with a Glow" Click="MenuWindowWithGlowOnClick" />
<MenuItem Header="Window with drop shadow" Click="MenuWindowWithShadowOnClick" />
</MenuItem>
</Menu>

Expand Down
42 changes: 42 additions & 0 deletions samples/MetroDemo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using MetroDemo.ExampleWindows;
Expand Down Expand Up @@ -288,5 +290,45 @@ private async void MetroWindow_Closing(object sender, System.ComponentModel.Canc
if (_shutdown)
Application.Current.Shutdown();
}

private MetroWindow testWindow;

private MetroWindow GetTestWindow()
{
if (testWindow != null) {
testWindow.Close();
}
testWindow = new MetroWindow() { Owner = this, WindowStartupLocation = WindowStartupLocation.CenterOwner, Title = "Another Test...", Width = 500, Height = 300 };
testWindow.Closed += (o, args) => testWindow = null;
return testWindow;
}

private void MenuWindowWithBorderOnClick(object sender, RoutedEventArgs e)
{
var w = this.GetTestWindow();
w.Content = new TextBlock() { Text = "MetroWindow with a Border", FontSize = 28, FontWeight = FontWeights.Light, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center };
w.BorderThickness = new Thickness(1);
w.GlowBrush = null;
w.BorderBrush = this.FindResource("AccentColorBrush") as Brush;
w.Show();
}

private void MenuWindowWithGlowOnClick(object sender, RoutedEventArgs e)
{
var w = this.GetTestWindow();
w.Content = new TextBlock() { Text = "MetroWindow with a Glow", FontSize = 28, FontWeight = FontWeights.Light, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center };
w.BorderThickness = new Thickness(1);
w.BorderBrush = null;
w.GlowBrush = this.FindResource("AccentColorBrush") as SolidColorBrush;
w.Show();
}

private void MenuWindowWithShadowOnClick(object sender, RoutedEventArgs e)
{
var w = this.GetTestWindow();
w.Content = new TextBlock() { Text = "MetroWindow with a Glow", FontSize = 28, FontWeight = FontWeights.Light, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center };
w.EnableDWMDropShadow = true;
w.Show();
}
}
}