.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Animate a window resize event??
 

Animate a window resize event??

Is it possible to animate the resize event of a window? I have a custom window that I've defined a different look for (including the titlebar, etc) ... I want to try and implement a transitional effect whenever the window's size is changed ... perhaps on the OnResize event (or similar). Is there a good way to do that?

Basically I'm trying to achieve the effect of a smooth animated transition from one size to another whenever the window's height and width are changed.
Kofoed
Hi,

You can animate the Width property of Window when you want the size changes. In the following example, when we scroll the mouse whell, the Window's Width property will be animated acroding to the current value of mouse wheel.
XAML code:

<Window x:Class="_temple.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="300"

x:Name="window1" MouseWheel="Window_MouseWheel">

<Grid x:Name="g1">

</Grid>

</Window>
In the code behind:

using System;

using System.Windows;

using System.Windows.Input;

using System.Windows.Media.Animation;

namespace _temple

{

public partial class Window1 : Window

{

public Window1()

{

InitializeComponent();

}

private void Window_MouseWheel(object sender, MouseWheelEventArgs e)

{

int delta = e.Delta;

double w1 = this.window1.ActualWidth;

double w2 = w1 + 2 * delta / 10;

DoubleAnimation anima = new DoubleAnimation();

anima.Duration = new Duration(TimeSpan.FromSeconds(0.3));

anima.From = w1;

anima.To = w2;

anima.FillBehavior = FillBehavior.HoldEnd;

window1.BeginAnimation(Window.WidthProperty, anima);

}

}

}

Thanks.


Jim Zhou -MSFT
Jim Zhou - MSFT
Actually I was looking more for the ability to animate when the size of the window is changed from some external workflow ...

Basically what I was looking to do was in something like the SizeChanged event handler, animate from the e.OldSize to the e.NewSize. So whatever the new size is, it doesn't just jump to that size but animates nicely (grows, shrinks). Is that possible?
Kofoed
Hi,

-->Basically what I was looking to do was in something like the SizeChanged event handler, animate from the e.OldSize to the e.NewSize. So whatever the new size is, it doesn't just jump to that size but animates nicely (grows, shrinks).

Of course, you can animate the window width from one value to another not justjump to the target. But based on my experience and research, it willnot create a smooth animation in the SizeChanged event handler, because in the process of the animation, the size of window will be changed simultaneously. In turn the SizeChanged event fires continuously. Alternatively, you can just call a method to perform the animation effect with two parameters, including the starting value anding end value.

Code snippet:

void StartAnimation(double oldValue, double newValue)

{

double newWidth = newValue;

DoubleAnimation animation = new DoubleAnimation();

animation.From = oldValue;

animation.To = newWidth;

animation.Duration = new Duration(TimeSpan.FromSeconds(5));

Storyboard story = new Storyboard();

story.Children.Add(animation);

Storyboard.SetTargetName(this, this.Name);

Storyboard.SetTargetProperty(animation, new PropertyPath(Window.WidthProperty));

story.Begin(this);

}

Thanks.

Jim Zhou -MSFT
Jim Zhou - MSFT

My problem is that I don't necessarily know the ending value to apply to an animation. My window control has the SizeToContent="WidthAndHeight" property set and it basically sizes to it's content. I don't know what the content will be so it's difficult to specify a "new value" to animate to ... that's why I was thinking about the SizeChanged event handler, because you know the previous and new size.

Any ideas?

Kofoed
WPF Windowis basically a hwnd. Directly animating the window size will not be an optimal experience, as you are animating a GDI object, andGDI is not designed for animation scenario.

You can try using a layered window,setting Window.AllowsTransparency property to true and Window.WindowStyle property to WindowStyle.None, and animate the visual contents placed inside the window.

For more information have a look at the following thread

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/e7b2038c-7544-438c-9960-f420655039b5/

Shreedhar Thirugnanasambantham
That's an interesting suggestion. I actually am already setting the AllowsTransparency and WindowStyle and creating my own look for the window, so in theory I could follow this road.

The thing I'm struggling with is that I don't know what the size of my window should be until it loads. The content of my window is populated by consumers of my control, and I'm setting the "SizeToContent" property to WidthAndHeight. So I'm not sure how to animate this effectively since I don't know the size of the contents until completely rendered on screen

Any ideas?
Kofoed
Hi Kofoed,
-->So I'm not sure how to animate this effectively since I don't know the size of the contents until completely rendered on screen
Yes, you are right, only when the content control are completely rendered can you get the actual size. Since the rendering stage is finished when you invoke the Loaded, so how about starting the animation in theLoaded event handler?

Thanks.
Sincerely.

Jim Zhou -MSFT
Jim Zhou - MSFT
Thx for the reply.

I've thought of that, starting in a load somewhere - but the problem is that I have a Tab control inside my window. There are 2 TabItems, and each has different content that's different size. So if the second tab is selected and it's content is larger than the first tab, the window will increase in size since I have the SizeToContent property set to WidthAndHeight.

I tried doing some animation stuff in the TabControl's SelectionChanged event, but it appears that we don't know the actual size of the content from that handler - it's too soon.

It's almost like I need some sort of ContentRendered event handler on the Tab control that i could tap into ... but there doesn't appear to be anything like that.

Thoughts?
Kofoed

You can use google to search for other answers

Custom Search

More Threads

• Trigger in ComboBoxItem causes an exception when the popup opens
• Modifyig tabControl via code
• How to bind ContextMenu to Columns of ListView
• how to bind to the button content after click
• Font Leading - FLowDocument/RichTextBox
• WritableBitmap performance question
• Loading Styles from My Assembly
• Access child properties from a Trigger / Cast in a Trigger.Property
• Mouse handling in Viewport3D
• Event Triggers and Animations: Firing Order