Connecting Tech Pros Worldwide Forums | Help | Site Map

(C#) Opacity - Animation

Atran's Avatar
Needs Regular Fix
 
Join Date: May 2007
Location: Sweden
Posts: 323
#1: Jun 6 '07
Hello EveryBody, I write this code but the code not work:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace WindowsApplication1
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.             //Set the form opacity = 0.
  17.             this.Opacity = 0.00;
  18.             for (double i = 0.00; i < 1.01; i = i + 0.01)
  19.             {
  20.                 //Make the form opacity a motion.
  21.                 this.Opacity += i;
  22.                 if (this.Opacity.Equals(1.00) == true)
  23.                 {
  24.                     break;
  25.                 }
  26.             }
  27.         }
  28.     }
  29. }
  30.  
Can anyone help me?
--------------------------------
And how I can do an animation using C# (If you can give me an example).
Thanks for anyhelp.

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Jun 6 '07

re: (C#) Opacity - Animation


What do you want this to do? because that will execute WAY to fast for you to see
TRScheel's Avatar
Expert
 
Join Date: Apr 2007
Location: Iowa
Posts: 624
#3: Jun 6 '07

re: (C#) Opacity - Animation


Quote:

Originally Posted by Plater

What do you want this to do? because that will execute WAY to fast for you to see

That and the check for opactiy.equals is redundant
Atran's Avatar
Needs Regular Fix
 
Join Date: May 2007
Location: Sweden
Posts: 323
#4: Jun 6 '07

re: (C#) Opacity - Animation


Quote:

Originally Posted by Plater

What do you want this to do? because that will execute WAY to fast for you to see

Hello, When I write:

Expand|Select|Wrap|Line Numbers
  1.   this.Opacity = 0.00;
  2.  
The form be invisible (it works but cant see).
So I want to be a motion tween, I mean when I run my app:
I want the form Opacity = 0.
and three after three grow to the opacity, I mean:

Expand|Select|Wrap|Line Numbers
  1. this.Opacity = 0.00;
  2. this.Opacity = 0.03;
  3. this.Opacity = 0.06;
  4. this.Opacity = 0.09;
  5. this.Opacity = 0.12;
  6. //.................To 1.00
  7. //So I want this like animation from "0.00 to 1.00".
  8.  
Thanks for anyhelp.
TRScheel's Avatar
Expert
 
Join Date: Apr 2007
Location: Iowa
Posts: 624
#5: Jun 6 '07

re: (C#) Opacity - Animation


Quote:

Originally Posted by Atran

Hello, When I write:

Expand|Select|Wrap|Line Numbers
  1.   this.Opacity = 0.00;
  2.  
The form be invisible (it works but cant see).
So I want to be a motion tween, I mean when I run my app:
I want the form Opacity = 0.
and three after three grow to the opacity, I mean:

Expand|Select|Wrap|Line Numbers
  1. this.Opacity = 0.00;
  2. this.Opacity = 0.03;
  3. this.Opacity = 0.06;
  4. this.Opacity = 0.09;
  5. this.Opacity = 0.12;
  6. //.................To 1.00
  7. //So I want this like animation from "0.00 to 1.00".
  8.  
Thanks for anyhelp.

Add something like

Expand|Select|Wrap|Line Numbers
  1. Thread.Sleep(1000);
between each change. It will add 1000 milliseconds ( or 1 second ) between each change. This will mean that at .01 change, it will take 100 seconds to go from invisible to visible, so I might suggest lowering it.
Atran's Avatar
Needs Regular Fix
 
Join Date: May 2007
Location: Sweden
Posts: 323
#6: Jun 6 '07

re: (C#) Opacity - Animation


Quote:

Originally Posted by TRScheel

Add something like

Expand|Select|Wrap|Line Numbers
  1. Thread.Sleep(1000);
between each change. It will add 1000 milliseconds ( or 1 second ) between each change. This will mean that at .01 change, it will take 100 seconds to go from invisible to visible, so I might suggest lowering it.

Thanks very much....
Atran's Avatar
Needs Regular Fix
 
Join Date: May 2007
Location: Sweden
Posts: 323
#7: Jun 6 '07

re: (C#) Opacity - Animation


But can anyone till me how to create a simple animation?
example: In my form I have a basic line.
So I want to make a tween motion to the line, I mean make the line move from a position to another position (make animation for the line).
Thanks for anyhelp.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#8: Jun 6 '07

re: (C#) Opacity - Animation


DirectX I think is what you want. TR can probably help you better as I've never used it.

If you wanted just static pictures, you could play with the Graphic object in the Paint functions
Atran's Avatar
Needs Regular Fix
 
Join Date: May 2007
Location: Sweden
Posts: 323
#9: Jun 6 '07

re: (C#) Opacity - Animation


Quote:

Originally Posted by Plater

DirectX I think is what you want. TR can probably help you better as I've never used it.

If you wanted just static pictures, you could play with the Graphic object in the Paint functions

Thanks.......................
TRScheel's Avatar
Expert
 
Join Date: Apr 2007
Location: Iowa
Posts: 624
#10: Jun 7 '07

re: (C#) Opacity - Animation


Quote:

Originally Posted by Plater

DirectX I think is what you want. TR can probably help you better as I've never used it.

If you wanted just static pictures, you could play with the Graphic object in the Paint functions


Actually you can use window's inherit graphics to do something simple like the line drawing. You just need to force a redraw, which is semi-redundant. The problem with the windows GDI for drawing is that if I say draw a line on my form, and then move IE over my form, then look at my form again, the line wont be there anymore unless I force a redraw. The issue is that you need to capture when and how to redraw, and know that the GDI is very... very... slow in comparison to say DirectX. Slow enough that expecting 60 - 100 updates per second on anything more complicated then say... a line... is probably not going to happen. Which is fine for your purposes, and in all reality you probably only need about 30 fps, if not less considering they are not going to notice the jumps if we lowered it to 10 fps and the line grew at a slow enough speed.

And to be honest, DirectX is overkill for a simple line.


I have to work on a few machines around this building, but when I get back I will throw up a code snippet showing how to do it.
Atran's Avatar
Needs Regular Fix
 
Join Date: May 2007
Location: Sweden
Posts: 323
#11: Jun 7 '07

re: (C#) Opacity - Animation


Quote:

Originally Posted by TRScheel

Actually you can use window's inherit graphics to do something simple like the line drawing. You just need to force a redraw, which is semi-redundant. The problem with the windows GDI for drawing is that if I say draw a line on my form, and then move IE over my form, then look at my form again, the line wont be there anymore unless I force a redraw. The issue is that you need to capture when and how to redraw, and know that the GDI is very... very... slow in comparison to say DirectX. Slow enough that expecting 60 - 100 updates per second on anything more complicated then say... a line... is probably not going to happen. Which is fine for your purposes, and in all reality you probably only need about 30 fps, if not less considering they are not going to notice the jumps if we lowered it to 10 fps and the line grew at a slow enough speed.

And to be honest, DirectX is overkill for a simple line.


I have to work on a few machines around this building, but when I get back I will throw up a code snippet showing how to do it.

Thanks TRScheel...........
TRScheel's Avatar
Expert
 
Join Date: Apr 2007
Location: Iowa
Posts: 624
#12: Jun 8 '07

re: (C#) Opacity - Animation


Sorry about not getting this up yesterday, ended up having a hell of a machine to do. Anyways...

Read through this and see if you understand whats going on.

I also suggest commenting out the

Expand|Select|Wrap|Line Numbers
  1. this.Invalidate()
line and observing the differences. It will ONLY redraw the line if ou minimize the form and then bring it back up. Invalidating it when we change the size of counter forces a redraw.

The onClosing is merely so the counter thread dies happily.

Expand|Select|Wrap|Line Numbers
  1.     public partial class Form1 : Form
  2.     {
  3.         public int counter = 0;
  4.         public readonly int maxCounter = 250;
  5.  
  6.         public Form1()
  7.         {
  8.             System.Threading.Thread thread = new System.Threading.Thread(Incrementer);
  9.             thread.Start();
  10.  
  11.             this.Paint += new PaintEventHandler(Form1_Paint);
  12.             this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
  13.  
  14.             InitializeComponent();
  15.         }
  16.  
  17.         void Form1_FormClosed(object sender, FormClosedEventArgs e)
  18.         {
  19.             counter = maxCounter + 1;
  20.         }
  21.  
  22.         void Form1_Paint(object sender, PaintEventArgs e)
  23.         {
  24.             e.Graphics.DrawLine(new Pen(Color.Blue), new Point(10, 10), new Point(10 + counter, 10));
  25.         }
  26.  
  27.         void Incrementer()
  28.         {
  29.             while (counter < maxCounter)
  30.             {
  31.                 counter++;
  32.                 this.Invalidate();
  33.                 System.Threading.Thread.Sleep(10);
  34.             }
  35.         }
  36.     }
TRScheel's Avatar
Expert
 
Join Date: Apr 2007
Location: Iowa
Posts: 624
#13: Jun 8 '07

re: (C#) Opacity - Animation


On a side note, I have heard that you should also call Update() with the Invalidate() call, but I cannot seem to find out why... it draws just fine with the invalidate.
Atran's Avatar
Needs Regular Fix
 
Join Date: May 2007
Location: Sweden
Posts: 323
#14: Jun 8 '07

re: (C#) Opacity - Animation


Thanks for your help........
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#15: Jun 11 '07

re: (C#) Opacity - Animation


Quote:

Originally Posted by TRScheel

On a side note, I have heard that you should also call Update() with the Invalidate() call, but I cannot seem to find out why... it draws just fine with the invalidate.

It should draw fine with just the Update() call too?
Invalidate only invalidates a section of it right? Update is just a blanket refresh I believe.
TRScheel's Avatar
Expert
 
Join Date: Apr 2007
Location: Iowa
Posts: 624
#16: Jun 11 '07

re: (C#) Opacity - Animation


Quote:

Originally Posted by Plater

It should draw fine with just the Update() call too?
Invalidate only invalidates a section of it right? Update is just a blanket refresh I believe.

I looked it up, and you have it backwards.

Invalidate() redoes the entire control.
Update() only updates the changed areas.

IMHO, I would think Invalidate() would be... safer... then Update() because I don't exactly trust the underlying code to accurately decide what needs to be updated or not. In addition, any sort of graphics oriented form should have the entire form redrawn when pushed to the screen.
TRScheel's Avatar
Expert
 
Join Date: Apr 2007
Location: Iowa
Posts: 624
#17: Jun 11 '07

re: (C#) Opacity - Animation


Quote:

Originally Posted by Plater

It should draw fine with just the Update() call too?
Invalidate only invalidates a section of it right? Update is just a blanket refresh I believe.

Researching some more I found this:

'The Invalidate method governs what gets painted or repainted. The Update method governs when the painting or repainting occurs. If you use the Invalidate and Update methods together rather than calling Refresh, what gets repainted depends on which overload of Invalidate you use. The Update method just forces the control to be painted immediately, but the Invalidate method governs what gets painted when you call the Update method.'

off the MSDN
Reply