question

DB2710 avatar image
0 Votes"
DB2710 asked DB2710 commented

Custom title bar with animation in app

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-wpf
image.png (1.4 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@ DivyanshBalooni-2710. What animation did you set for the Custom title bar? Could you show me your code for analysis?

0 Votes 0 ·

I have not set any custom animation. I am talking of Windows default animation.

If you want to try, then do the following -

  1. Open File explorer, and restore up/down it. You will see that there is Windows default animation which is currently on my app.

  2. Now, turn off animation in Windows and then restore up/down File Explorer. You will see that there is no animation.


If I write the code WindowStyle=None , it doesn't plays the default Windows Animation



0 Votes 0 ·

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered DB2710 commented

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.



2.gif (442.6 KiB)
4.gif (654.6 KiB)
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for sincere efforts, but build errors are coming

131035-image.png



Please suggest any change

0 Votes 0 ·
image.png (166.6 KiB)

Please suggest any further change

0 Votes 0 ·

Hi,@ DivyanshBalooni-2710. Did you add the following namespaces? If not, you could try to add them and build. I also updated the complete code in the answer, you can check it.

 using System;
 using System.Windows;
 using System.Windows.Threading;
0 Votes 0 ·

I followed your steps.

Now it is showing an animation but there is another problem.

While it animates I can still see two title bars - one defualt and one custom

I don't want that default title bar to show during the animation.

You can see how MS Word minimizes.

IIt does not shows a default title bar. Only we can see the custom.

And when we minimize our app from taskbar, we cannot see the animation.

Please suggest further reforms

0 Votes 0 ·

Hi,@ DivyanshBalooni-2710. Have you set WindowStyle="None" in the Window of the xaml code?(You could check my xaml code in the Answer.) After adding it, the default title bar will not be displayed. And you can use your custom title bar normally.

0 Votes 0 ·

Yes, I have added WindowStyle=None

The default title bar is not always visible.

It is visible at the time of minimizing the app, When I click the minimize button and while it minimizes

0 Votes 0 ·
Show more comments

Thanks a lot. Default title bar appears for a moment. People won't be able to figure it out

0 Votes 0 ·