9 Replies - 9168 Views - Last Post: 14 April 2012 - 05:13 AM Rate Topic: -----

#1 Andrei95   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 17-December 11

Animation inside user control, is it possible?

Posted 13 April 2012 - 01:38 AM

Hi!

Guys I have a simple question. Is it possible to create an animation inside an user control without having to call userControl.Invalidate() on its PaintEvent in a winform? (Think about the MonthCalendar control, it is animated and I don't have to type anything on none of its Events).

I also have to mention that everytime I type this.Invalidate() on the PaintEvent in the actual userControl, when I try to put that userControl onto a winform one of two things happen: the control doesn't show up or it does show up but it flickers.
In both cases the program crashes soon after the start and sometimes even C# crashes as well.

Guys, please tell me if this is possible, and if it is, could you point me in the right direction?

Thanks :bigsmile:

Is This A Good Question/Topic? 0
  • +

Replies To: Animation inside user control, is it possible?

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Animation inside user control, is it possible?

Posted 13 April 2012 - 08:17 AM

WPF does a lot with animation.

You could make sure your WinForm is double-buffered.

Smarter animation and reduce flickering: Don't clear and repaint everything. Just repaint regions that have changed.

Don't animate at 1,000 times a second.
Was This Post Helpful? 0
  • +
  • -

#3 Andrei95   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 17-December 11

Re: Animation inside user control, is it possible?

Posted 13 April 2012 - 11:03 AM

I know that WPF is better for my kind of project :turned: , but I want to learn more about WinForms.

By the way what do you mean by "Don't animate at 1,000 times a second"?

To animate something in a Winform application I create a timer and I change some of the properties of my object in my timer's TickEvent. After that I call this.Invalidate() method and all works fine.

Are you using a different type of "animation"?
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Animation inside user control, is it possible?

Posted 13 April 2012 - 11:09 AM

He is talking about the frame rate of the animation.

There is no point in redrawing at 1000 times each second (a.k.a having a Timer interval of 1). Not only will this not happen in reality (as you program will not be able to keep up), but the human eye cannot react to such high frame rates.

Normally you have a frame rate of around 30 'redraws' per second which to the human eye looks like a smooth animation.
Was This Post Helpful? 1
  • +
  • -

#5 Andrei95   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 17-December 11

Re: Animation inside user control, is it possible?

Posted 13 April 2012 - 11:20 AM

Oh, now I get it :bigsmile:

Anyway, I've never used such a high rate.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Animation inside user control, is it possible?

Posted 13 April 2012 - 11:35 AM

So what is the .Interval of your timer that is controlling the animation?
Was This Post Helpful? 0
  • +
  • -

#7 Andrei95   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 17-December 11

Re: Animation inside user control, is it possible?

Posted 13 April 2012 - 11:47 AM

My interval doesn't matter (usually I keep it at 100 or more). The problem only occurs if I have this.Invalidate()on my user control.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Animation inside user control, is it possible?

Posted 13 April 2012 - 11:51 AM

The .Interval of the timer is what's driving the refreshing of your control, isn't it? So yeah, it does matter what its set to.

This is making less and less sense with each post.

Please show us the code you are using.

:code:

This post has been edited by tlhIn`toq: 13 April 2012 - 11:52 AM

Was This Post Helpful? 0
  • +
  • -

#9 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Animation inside user control, is it possible?

Posted 13 April 2012 - 12:51 PM

When you implement custom animations/painting of some kind in a custom user control, every time you drag the control onto your form, it will redraw itself. The flickering is occurring because when you are dragging the control around, the control is having to redraw itself constantly.

I don't think that your application is crashing but just hanging. Are any exceptions being thrown? If you are doing all your animations on the one thread then its possible that the program is becoming unresponsive. You could try doing the work on another thread so that the main GUI thread is kept 'free'.
Was This Post Helpful? 0
  • +
  • -

#10 Andrei95   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 17-December 11

Re: Animation inside user control, is it possible?

Posted 14 April 2012 - 05:13 AM

Guys, thank you very much for your patience.
I've manage to find the answer. :turned:

The code below is a very simple program that shows how to move a rectangle inside an user control.

Take a look:

public partial class Animation : UserControl
    {
        public Form1()
        {
            InitializeComponent();
            
            rect = CreateRectangle();
        }
private Rectangle rect;

        private Rectangle CreateRectangle()
        {
            Rectangle _rect = new Rectangle(50, 50, 50, 50);
            return _rect;
        }

        private void DrawRectangle(Graphics g)
        {
            timer1.Start();

            g.FillRectangle(Brushes.Red, rect);
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            if (rect.X<1000)
            {
                rect.X++; this.Invalidate();// nor here
            }
            this.Invalidate();// but here
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            timer1.Start();            
            DrawRectangle(e.Graphics);
            //this.Invalidate(); I've figured out that this statement does not belong here,
        }
    }
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1