Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animations: Sliding & Fading controls on a C# form (winforms)

I'm trying to implement a way to animate (translate, fade) controls around (more than one at the same time possibly) elegantly. For example, lets say I had a picture in the top left corner, and a textbox in the bottom right corner, I'd like to be able to have them smoothly slide across the window and switch places. I've been working for awhile but have not come up with anything that achieves this smoothly or easily.

like image 511
caesay Avatar asked May 27 '10 01:05

caesay


People also ask

What is animation slider?

Slide animations are similar to transitions, but they are applied to individual elements on a single slide—a title, chart, image, or individual bullet point. Animations can make a presentation more lively and memorable.

How do you animate something sliding?

How to add an animation: Open a presentation in Google Slides. Click the text or image you want to animate. Then click Insert > Animation. Click on Select an object to animate, scroll down and choose the animation you want to use.

What are the 4 types of animation effects?

There are four types of animation effects in PowerPoint – entrance, emphasis, exit and motion paths. These reflect the point at which you want the animation to occur.


2 Answers

Check out the dot-net-transitions project on Google Code. There's now a clone on Github here. It's also available on nuget as dot-net-transitions. It supports a variety of linear/non-linear transitions including composite transitions that can be used for more complex effects such as ripple.

Here is a working sample that demonstrates your desired behavior:

var pictureBox = new PictureBox
    {
        ImageLocation = "http://icons2.iconarchive.com/icons/klukeart/summer/128/hamburger-icon.png",
        SizeMode = PictureBoxSizeMode.AutoSize
    };
var textBox = new TextBox
    {
        Text = "Hello World",
        Location = new Point(140, 140)
    };
var form = new Form
    {
        Controls =
        {
            textBox,
            pictureBox
        }
    };
form.Click += (sender, e) =>
    {
        // swap the Left and Top properties using a transition
        var t = new Transition(new TransitionType_EaseInEaseOut(1000));
        t.add(pictureBox, "Left", textBox.Left);
        t.add(pictureBox, "Top", textBox.Top);
        t.add(textBox, "Left", pictureBox.Left);
        t.add(textBox, "Top", pictureBox.Top);
        t.run();
    };
form.ShowDialog();
like image 106
Nathan Baulch Avatar answered Sep 27 '22 18:09

Nathan Baulch


I recommend that you switch to WPF; that would make it far easier.

It is completely impossible to fade controls in WinForms; Windows controls cannot have opacity.
The closest you can get would be to render the control and its area on the form to a pair of bitmaps, then crossfade the bitmaps in a PictureBox using a ColorMatrix.

To slide controls in WinForms, you can use a Timer to gradually change the Top and/or Left properties of the controls and move them across the form. However, you'll get an annoying flicker, which is (AFAIK) impossible to remove.

like image 44
SLaks Avatar answered Sep 27 '22 16:09

SLaks