Sunday, December 18, 2011

4-Tutorial 4: Simple Animation, Which Shows the Use of the Timer and Location Commands Using Visual C++.NET 2010, 2008

Tutorial 4: Simple Animation, Which Shows the Use of the Timer and Location Commands Using Visual C++.NET 2010, 2008
       This tutorial will show how to move a control button around the form employing the timer.  It also shows something about how placement works and the coordinate system of the form. Click here for the source code to the Simple Animation Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. Make the form size 286, 314. Begin by designing a form which looks as follows:       
Basic animation with timers in a Microsoft Visual C++ Program
Set the timer's enabled property to false. Then code the following to the buttons:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
       {
         button1->Visible=false;
button2->Visible=true;
timer1->Enabled=true;
timer1->Interval=30;
       }
   private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
       {
         timer1->Enabled=false;
button1->Visible=true;
       }
   private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
       {
         static bool NE, NW, SW, SE;
         static int x, y;
         label1->Text=x.ToString();
         if (NE == false && NW == false && SW == false && SE== false)
         {
           x=8;
           y=64;
           SE=true;
         }
         if (NE)
         {
           x=x+4;
           y=y-4;
           if (x>=220)
           {
             SW=false;
             NE=false;
             NW=true;
             SE=false;
           }
           if (y<=0)
           {
             SW=false;
             NE=false;
             NW=false;
             SE=true;
           }
          }
 
         //*********************
         if (SE)
         {
           x=x+4;
           y=y+4;
           if (x>=220)
           {
             SW=true;
             NE=false;
             NW=false;
             SE=false;
           }
           if (y>=250)
           {
             SW=false;
             NE=true;
             NW=false;
             SE=false;
           }
         }
         //**********************
         if (SW)
         {
           x=x-4;
           y=y+4;
           if (x<=0)
           {
             SW=false;
             NE=false;
             NW=false;
             SE=true;
           }
           if (y>=250)
           {
             SW=false;
             NE=false;
             NW=true;
             SE=false;
           }
         }
         //************************
         if (NW)
         {
           x=x-4;
           y=y-4;
           if (x<=0)
           {
             SW=false;
             NE=true;
             NW=false;
             SE=false;
           }
           if (y<=0)
           {
             SW=true;
             NE=false;
             NW=false;
             SE=false;
           }
         }
         button2->Location=System::Drawing::Point(x,y);
       }
     private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
         {
         }

   (Back)

0 comments:

Post a Comment