English 中文(简体)
Animate Window Resize (Width and Height) C# WPF
原标题:

I m looking for some help on animating window resize of an open window! Cant seem to figure this one out!

I m just using atm.

this.Width = 500;

Any help would be great! Thanks.

最佳回答

I have answered this question myself. Here is some sample code.

    static System.Windows.Forms.Timer _Timer = new System.Windows.Forms.Timer(); 
    int _Stop = 0;

    private void This_Loaded(object sender, RoutedEventArgs e)
    {
        _Timer.Tick += new EventHandler(timer_Tick);
        _Timer.Interval = (20); 

        resize(500,500)
    }

    private void timer_Tick(Object myObject, EventArgs myEventArgs)
    {
        if (_Stop == 0)
        {
            _RatioHeight    = ((this.Height -   _Height)    / 12)* -1;
            _RatioWidth     = ((this.Width -    _Width)     / 12)* -1;
        }
        _Stop++;

        this.Height += _RatioHeight;
        this.Width  += _RatioWidth;

        if (_Stop == 12)
        {
            _Timer.Stop();
            _Timer.Enabled = false;
            _Timer.Dispose();

            _Stop = 0;

            this.Height = _Height;
            this.Width  = _Width;
        }
    }

    public void resize(double _PassedHeight, double _PassedWidth)
    {
        _Height = _PassedHeight;
        _Width  = _PassedWidth;

        _Timer.Enabled = true;
        _Timer.Start();
    }

Resizes the window in 12 "ticks" very quickly, can be slowed down in _Timer.Interval. After 12 ticks will polish it off with a final resize to exact size.

Hope this helps someome.

问题回答

you can use window animation for this here is xaml

<Window x:Class="dlgControls" Name="dlgControls"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="" Height="300" Width="300">

    <Window.Resources>
        <Storyboard x:Key="showWin">
            <DoubleAnimation Storyboard.TargetName="dlgControls" Storyboard.TargetProperty="Height" Duration="0:0:.5" To="300" BeginTime="0:0:1"/>
        </Storyboard>

        <Storyboard x:Key="hideWin">
            <DoubleAnimation Storyboard.TargetName="dlgControls" Storyboard.TargetProperty="Height" Duration="0:0:.5" To="150" BeginTime="0:0:1"/>
        </Storyboard>
    </Window.Resources>
    <Grid RenderTransformOrigin="0.5,0.5">
        <Button Name="btnOpen" Content="Open" HorizontalAlignment="Left" Margin="184,98,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Name="btnClose" Content="Close" HorizontalAlignment="Left" Margin="184,222,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>

</Window>

to animation use code like this

Imports System.Windows.Media.Animation

Public Class dlgControls
    Dim showWin As Storyboard
    Dim hideWin As Storyboard

    Private Sub btnOpen_Click(sender As Object, e As Windows.RoutedEventArgs) Handles btnOpen.Click
        BeginStoryboard(showWin)
    End Sub

    Private Sub dlgControls_Loaded(sender As Object, e As Windows.RoutedEventArgs) Handles Me.Loaded
        showWin = Me.Resources("showWin")
        hideWin = Me.Resources("hideWin")
    End Sub

    Private Sub btnClose_Click(sender As Object, e As Windows.RoutedEventArgs) Handles btnClose.Click
        BeginStoryboard(hideWin)
    End Sub
End Class
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0,0,0,0,10); // Control animation speed / how often the tick will be called.
dispatcherTimer.Start();


private void dispatcherTimer_Tick(object sender, EventArgs e)
{
     if ( this.Width < 500 )
     {
           this.Width += 10;
     } 
     else 
     {
           DispatcherTimer timer = (DispatcherTimer)sender;
           timer.Stop();
     }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签