Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Animated Button

Tags:

c#

winforms

I have button it possible create animate on click?

enter image description here

with photoshop i have created a two image (enabled and disabled). Insert the picturebox in Windows Forms and Click event..Click the image changes from enabled to disabled, but you can have an animation?

Like this:

enter image description here

like image 490
jolly Avatar asked Nov 18 '11 00:11

jolly


2 Answers

It looks like you mentioned WinForms so I'll address that. Yes animation is possible but in general it's going to be a bit of work.

There appears to be an implementation of a general purpose animation framework (although limited) over on CodeProject. In the comments schallos posted a better implementation of the reflection code using expression trees.

The general principle is:

  • Use a PictureBox so you get double buffering
  • Use a timer control to control repainting (calling Invalidate() on your PictureBox)
  • You'll probably want to add some easing into the animation so it appears smoother; a bit of acceleration added to it when the user clicks goes a long way.
like image 127
Ron Warholic Avatar answered Sep 21 '22 13:09

Ron Warholic


A timer is not required using a GIF.

Using windows forms you could start by creating an animated GIF for each button. It would have to include both the forward and backwards direction. You can do this through Photoshop via the "Animation" panel. On the PictureBox click event you can set the image to play or stop.

The code below would set your image on the beginning frame.

imgButtonDimension = new FrameDimension(imgButtonDimension.FrameDimensionsList[0]);
imgButton.SelectActiveFrame(imgButtonDimension, 0);
pictureBox.Image = imgButton;`

If this is the approach you'd like to take I can provide further elaboration.

like image 34
brpyne Avatar answered Sep 20 '22 13:09

brpyne