(C#) Opacity - Animation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Atran
    Contributor
    • May 2007
    • 319

    (C#) Opacity - Animation

    Hello EveryBody, I write this code but the code not work:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                //Set the form opacity = 0.
                this.Opacity = 0.00;
                for (double i = 0.00; i < 1.01; i = i + 0.01)
                {
                    //Make the form opacity a motion.
                    this.Opacity += i;
                    if (this.Opacity.Equals(1.00) == true)
                    {
                        break;
                    }
                }
            }
        }
    }
    Can anyone help me?
    --------------------------------
    And how I can do an animation using C# (If you can give me an example).
    Thanks for anyhelp.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

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

    Comment

    • TRScheel
      Recognized Expert Contributor
      • Apr 2007
      • 638

      #3
      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

      Comment

      • Atran
        Contributor
        • May 2007
        • 319

        #4
        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:

        Code:
          this.Opacity = 0.00;
        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:

        Code:
        this.Opacity = 0.00;
        this.Opacity = 0.03;
        this.Opacity = 0.06;
        this.Opacity = 0.09;
        this.Opacity = 0.12;
        //.................To 1.00
        //So I want this like animation from "0.00 to 1.00".
        Thanks for anyhelp.

        Comment

        • TRScheel
          Recognized Expert Contributor
          • Apr 2007
          • 638

          #5
          Originally posted by Atran
          Hello, When I write:

          Code:
            this.Opacity = 0.00;
          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:

          Code:
          this.Opacity = 0.00;
          this.Opacity = 0.03;
          this.Opacity = 0.06;
          this.Opacity = 0.09;
          this.Opacity = 0.12;
          //.................To 1.00
          //So I want this like animation from "0.00 to 1.00".
          Thanks for anyhelp.
          Add something like

          [CODE=cpp]Thread.Sleep(10 00);[/CODE]

          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.

          Comment

          • Atran
            Contributor
            • May 2007
            • 319

            #6
            Originally posted by TRScheel
            Add something like

            [CODE=cpp]Thread.Sleep(10 00);[/CODE]

            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....

            Comment

            • Atran
              Contributor
              • May 2007
              • 319

              #7
              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.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                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

                Comment

                • Atran
                  Contributor
                  • May 2007
                  • 319

                  #9
                  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......... ..............

                  Comment

                  • TRScheel
                    Recognized Expert Contributor
                    • Apr 2007
                    • 638

                    #10
                    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.

                    Comment

                    • Atran
                      Contributor
                      • May 2007
                      • 319

                      #11
                      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....... ....

                      Comment

                      • TRScheel
                        Recognized Expert Contributor
                        • Apr 2007
                        • 638

                        #12
                        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

                        [CODE=cpp]this.Invalidate ()[/CODE]

                        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.

                        [CODE=cpp]
                        public partial class Form1 : Form
                        {
                        public int counter = 0;
                        public readonly int maxCounter = 250;

                        public Form1()
                        {
                        System.Threadin g.Thread thread = new System.Threadin g.Thread(Increm enter);
                        thread.Start();

                        this.Paint += new PaintEventHandl er(Form1_Paint) ;
                        this.FormClosed += new FormClosedEvent Handler(Form1_F ormClosed);

                        InitializeCompo nent();
                        }

                        void Form1_FormClose d(object sender, FormClosedEvent Args e)
                        {
                        counter = maxCounter + 1;
                        }

                        void Form1_Paint(obj ect sender, PaintEventArgs e)
                        {
                        e.Graphics.Draw Line(new Pen(Color.Blue) , new Point(10, 10), new Point(10 + counter, 10));
                        }

                        void Incrementer()
                        {
                        while (counter < maxCounter)
                        {
                        counter++;
                        this.Invalidate ();
                        System.Threadin g.Thread.Sleep( 10);
                        }
                        }
                        }[/CODE]

                        Comment

                        • TRScheel
                          Recognized Expert Contributor
                          • Apr 2007
                          • 638

                          #13
                          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.

                          Comment

                          • Atran
                            Contributor
                            • May 2007
                            • 319

                            #14
                            Thanks for your help........

                            Comment

                            • Plater
                              Recognized Expert Expert
                              • Apr 2007
                              • 7872

                              #15
                              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.

                              Comment

                              Working...