Vista does not animate minimization and restoration of borderless windows ( WindowStyle = WindowStyle.None;
).
Here is a workaround. When you click the minimize button, change the window style to SingleBorderWindow, and then minimize it. Vista minimizes the animation window. When restoring (Window activation occurs), you could restore the window style to none, which is performed after restoration. You can do this by resetting the window style to Dispatcher.BeginInvoke. You could try to use it in your custom Title bar. If I misunderstood, please point it out.
The code of xaml:
<Window x:Class="customTitleBar.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:customTitleBar"
mc:Ignorable="d" WindowStyle="None"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel VerticalAlignment="Top" Width="800" Height="30" Background="Blue">
<Button Width="100" Height="30" Click="Button_Click" HorizontalAlignment="Right">minize</Button>
</StackPanel>
</Grid>
</Window>
The code of xaml.cs:
using System;
using System.Windows;
using System.Windows.Threading;
namespace customTitleBar
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
if (WindowStyle != WindowStyle.None)
{
Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, (DispatcherOperationCallback)delegate (object unused)
{
WindowStyle = WindowStyle.None;
return null;
}
, null);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Minimized;
}
}
}
The picture of result:
Update:
You could use the following code to display animation when you click the minimize button and TaskBar icon. The disadvantage is that you can actually see the default title bar reappear for a moment.
The code of xaml:
<Window x:Class="CustomTitleBarDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CustomTitleBarDemo"
mc:Ignorable="d" WindowStyle="None"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid Background="pink">
<Grid>
<StackPanel Name="sp" VerticalAlignment="Top" Width="800" Height="30" Background="Blue">
<Button Width="100" Height="30" Click="Button_Click" HorizontalAlignment="Right">minize</Button>
</StackPanel>
</Grid>
</Grid>
</Window>
The code of xaml.cs:
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace CustomTitleBarDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
internal class ApiCodes
{
public const int SC_RESTORE = 0xF120;
public const int SC_MINIMIZE = 0xF020;
public const int WM_SYSCOMMAND = 0x0112;
}
private IntPtr hWnd;
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
private void Button_Click(object sender, RoutedEventArgs e)
{
SendMessage(hWnd, ApiCodes.WM_SYSCOMMAND, new IntPtr(ApiCodes.SC_MINIMIZE), IntPtr.Zero);
}
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == ApiCodes.WM_SYSCOMMAND)
{
if (wParam.ToInt32() == ApiCodes.SC_MINIMIZE)
{
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Minimized;
handled = true;
}
else if (wParam.ToInt32() == ApiCodes.SC_RESTORE)
{
WindowState = WindowState.Normal;
WindowStyle = WindowStyle.None;
handled = true;
}
}
return IntPtr.Zero;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
hWnd = new WindowInteropHelper(this).Handle;
HwndSource.FromHwnd(hWnd).AddHook(WindowProc);
}
}
}
The picture of result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.