tags:

views:

52

answers:

1

Ok So I'm creating a "Windows Forms Application" for some guy using C# and I want to make the UI a bit more interesting for him.

The main form looks like a big dial with custom buttons arranged on it. (I'm saying custom buttons since they are actually simple user controls I've created that have a PictureBox and a Label that enlarges when the user points it and then shrink when the mouse cursor is moved outside it. There's also an Image property which sets the PictureBox's Image and is used as the icon of the so called custom button.)

I used two timers named tmrEnlarge and tmrShrink which activate on MouseEnter and MouseLeave events respectively. Basically nothing but a couple of simple functions to increase and decrease the size of the PictureBox used in my custom control and making it look like it's zooming in...

It works just fine but the problem is, when the mouse hovers several controls at the same time, the animation slows dows (which in my opinion is normal, since timers are not the best way for doing something like I did!) I tried using threads as well but the problem's still here! :-(

What I want to know is what's the best way for doing something like this?

EDIT:

Here's the code I used for drawing the image directly on the control without using a PictureBox:

(It's just a quick version and it leaves residues after drawing the image, which is not important for me right now)

public partial class DDAnimatingLabel : UserControl
{
    public Image Image { get; set; }

    public DDAnimatingLabel()
    {
        InitializeComponent();
    }

    private void DDAnimatingLabel_MouseEnter(object sender, EventArgs e)
    {
        tmrEnlarge.Enabled = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (Image != null)
        {
            e.Graphics.DrawImage(this.Image, e.ClipRectangle);
        }
        else
            base.OnPaint(e);
    }

    private void tmrEnlarge_Tick(object sender, EventArgs e)
    {
        if (Size.Width >= MaximumSize.Width)
        {
            tmrEnlarge.Enabled = false;
            return;
        }

        Size s = Size;
        s.Height += 4;
        s.Width += 4;
        Size = s;

        Point p = Location;
        p.X -= 2;
        p.Y -= 2;
        Location = p;
    }

    private void tmrShrink_Tick(object sender, EventArgs e)
    {
        if (tmrEnlarge.Enabled)
            return;

        if (Size.Width == MinimumSize.Width)
        {
            tmrShrink.Enabled = false;
            return;
        }

        Size s = Size;
        s.Height -= 4;
        s.Width -= 4;
        Size = s;

        Point p = Location;
        p.X += 2;
        p.Y += 2;
        Location = p;
    }

    private void DDAnimatingLabel_MouseLeave(object sender, EventArgs e)
    {
        tmrShrink.Enabled = true;
    }
}
A: 

I originally misunderstood your question. Some time ago I built an animation system for this kind of thing. Here you can find that code: http://pastebin.com/k1XmRapH.

That is an older 'version' of my animation system. It's crude, but relatively small and easy to understand. Below is a demonstration app that uses the animation system.

The key points of this system are:

  • Utilize a single timer that provides a timed 'pulse' to each runnning animation
  • The pulse uses elapsed time to calculate how much change to apply

The demonstration app uses an AnimationGroup to alter both Size and Location properties. When the mouse enters the button we first cancel any existing animation and then run an animation to grow the button. When the mouse leaves the button we again cancel any existing animation and then run an animation to return it to normal. Specifying AnimationTerminate.Cancel to AnimationManager.Remove cancels a running animation without calling its End method (which would complete the animation). This leaves the button's properties in the state they were in at the time we cancelled and smoothly returns the button to normal size.

using System;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    const int PulseInterval = 20;
    const int ButtonAnimateTime = 250;

    AnimationManager animate;
    Point[] buttonLocationsNormal, buttonLocationsHover;
    Size buttonSizeNormal, buttonSizeHover;

    public Form1()
    {
        Text = "Demo";
        ClientSize = new Size(480, 100);

        animate = new AnimationManager { PulseInterval = PulseInterval };

        buttonLocationsNormal = new Point[4];
        buttonLocationsHover = new Point[4];
        buttonSizeNormal = new Size(100, 80);
        buttonSizeHover = new Size(120, 100);

        for (int n = 0, x = 10; n < 4; n++, x += 120)
        {
            Point normalLocation = new Point(x, 10);
            buttonLocationsNormal[n] = normalLocation;
            buttonLocationsHover[n] = new Point(x - 10, 0);
            Button button = new Button { Text = "Text", Location = normalLocation, Size = buttonSizeNormal };
            button.MouseEnter += (s, e) => AnimateButton(s as Button, true);
            button.MouseLeave += (s, e) => AnimateButton(s as Button, false);
            Controls.Add(button);
        }
    }

    private void AnimateButton(Button button, bool hovering)
    {
        int index = Controls.IndexOf(button);
        AnimationGroup group = button.Tag as AnimationGroup;
        if (group != null)
            animate.Remove(group, AnimationTerminate.Cancel);

        Point newLocation;
        Size newSize;
        if (hovering)
        {
            newLocation = buttonLocationsHover[index];
            newSize = buttonSizeHover;
        }
        else
        {
            newLocation = buttonLocationsNormal[index];
            newSize = buttonSizeNormal;
        }
        group = new AnimationGroup(
            new PropertyAnimation("Location", button, newLocation, TimeSpan.FromMilliseconds(ButtonAnimateTime), new PointAnimator())
            , new PropertyAnimation("Size", button, newSize, TimeSpan.FromMilliseconds(ButtonAnimateTime), new SizeAnimator())
        );
        button.Tag = group;
        animate.Add(group);
    }
}
Tergiver
Thank you for your response. Alright I see your code is directly drawing the controls but the animating effect is not what I'm looking for! :( What I want is that when you hover the mouse, the PsuedoButton zooms-in gradually until a maximum size is reached and when the mouse leaves the component's area, it zooms out gradually. Just a smooth zoom-in/zoom-out transition! I don't know if I could clearly explain myself! :-)
M2X
I've updated the answer with a better answer, now that I understand what you were trying to accomplish. This animation system does allow using actual controls, though if you really want to produce a quality product, you may still consider abandoning the controls.
Tergiver
One more thing I would add: If that crude animation system looks intimidating, remember that WPF has a full-featured, fantastic animation system already built in. =)
Tergiver
Thank you for your prompt response. But in the above code you used a class named "AnimationManager". Is it a 3rd party class?
M2X
I didn't want to post it all here as it's fairly long. There is a link to it in the first paragraph.
Tergiver
Oh thanks! I didn't notice you already shared the link! Thank you for your post! It almost does what I wanted. And the performance is awesome. But the code is really complicated :D If I create a WPF control, I suppose I can't use it in a WindowsFormsApplication, right?
M2X
I've heard that you can mix-and-match WPF and WinForms (and vise versa), but I have never done it. Personally I would go to great lengths to avoid any such cobbling.
Tergiver