How to move form on screen programmatically with animated effect?

0

How can I make my form go from location 0.0 to location 100,100 on the screen without user interaction, ie through programming, this movement needs to be animated, ie it can not disappear from a point and going to another, needs to look like it has moved there.

    
asked by anonymous 03.10.2016 / 16:06

2 answers

5

I think the simplest way to do what you want to do is to use the% WinFormAnimation .

To install, just use the following command the library :

  

Install-Package WinFormAnimation

Once you've done this, just use the following code:

private void button1_Click(object sender, EventArgs e)
{
    new Animator2D(new Path2D(new Float2D(-100, -100), this.Location.ToFloat2D(), 500))
        .Play(this, Animator2D.KnownProperties.Location);
}

Where Package Manager Console is this current, but you can change to any element (other Form , button, text, etc).

For more details, look at official documentation .

In the same question that the @GuilhermeNascimento posted, has this example which can also help you.

    
03.10.2016 / 16:48
2

If I understand you want an animated effect when the element is moved, I found this response link

Create / declare the following class:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public static class Util {
    public enum Effect { Roll, Slide, Center, Blend }

    public static void Animate(Control ctl, Effect effect, int msec, int angle) {
        int flags = effmap[(int)effect];
        if (ctl.Visible) { flags |= 0x10000; angle += 180; }
        else {
            if (ctl.TopLevelControl == ctl) flags |= 0x20000; 
            else if (effect == Effect.Blend) throw new ArgumentException();
        }
        flags |= dirmap[(angle % 360) / 45];
        bool ok = AnimateWindow(ctl.Handle, msec, flags);
        if (!ok) throw new Exception("Animation failed");
        ctl.Visible = !ctl.Visible;
    }

    private static int[] dirmap = { 1, 5, 4, 6, 2, 10, 8, 9 };
    private static int[] effmap = { 0, 0x40000, 0x10, 0x80000 };

    [DllImport("user32.dll")]
    private static extern bool AnimateWindow(IntPtr handle, int msec, int flags);
}

And to use it do something like:

private void button2_Click(object sender, EventArgs e) {
    Util.Animate(form1, Util.Effect.Slide, 150, 180);
}
    
03.10.2016 / 16:28