How to move form on screen programmatically with animated effect?

Asked

Viewed 120 times

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 can not disappear from one point and go to another, need to look like it moved there.

  • Hello Pedro. Your question is a bit confusing. Could you explain better what you want? If possible by adding an example or the code you already have?

  • @Randrade I want to know how I can get the form to move itself on the screen. I’ve already got this but it doesn’t give Location. X = 1000; Location. Y = 1000;

  • You want an animated effect?

  • @Guilhermenascimento yes I want to put the form moved on the screen

2 answers

5


I think the simplest way to do what you want is to use the library Winformanimation.

To install, simply use the following command: Package Manager Console:

Install-Package Winformanimation

Once done, 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 this is the Form current, but you can change to any element (other form, button, text, etc).

For more details, look at official documentation.

In the same question as the @Guillhermenascimento posted, has this example who can also help you.

  • 2

    What an elegant and clean solution +1 :)

  • Works :)!!!!

2

If I understood you want an animated effect when the element is moved, I found this answer https://stackoverflow.com/a/6103677/1518921

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);
}
  • The program says it is a type and cannot be used in context

  • @Pedro do not know what to say, have to put the exact error that occurs, randrade edited and corrected, see if it was lack of classes.

  • in this part Util.Animate(form1, Util.Effect.Slide, 150, 180); Form1 says that form 1 is a type that is out of context I’ve tried Form1 Form1 Form form and all give error

  • @Pedro Mas Form1 is an example, I thought it was intuitive for those who use VS, but ok, change Form1 for the form or window you want to move.

  • The form called Form1

  • @Peter called the Util.Animate(form1, Util.Effect.Slide, 150, 180); where?

  • @Pedro If you pass the exact mistake will be easier to help you.

  • @Pedro the randrade told me that maybe in your case you have to do this Util.Animate(this, Util.Effect.Slide, 100, 100);

  • @Guilhermenascimento is no longer making that mistake

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.