Custom title bar with animation in app

DB-2710 61 Reputation points
2021-09-10T04:41:36.357+00:00

I have made a WPF app

I want custom title bar in that so, I designed the following -

130876-image.png

It shows the default title bar alongwith my custom title bar.

I want to hide the default title bar.

I did it by WindowStyle=None

But, it does not play any animations if I restore up/down my app.

Please tell how to show custom title bar with animation

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 38,251 Reputation points Microsoft Vendor
    2021-09-10T07:38:02.513+00:00

    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:
    131014-2.gif

    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:
    131818-4.gif


    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.


0 additional answers

Sort by: Most helpful