1 Replies - 686 Views - Last Post: 12 March 2011 - 04:25 PM

#1 Cyclone  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 13-February 09

Image stops moving when a button is added to the form

Posted 12 March 2011 - 12:52 PM

Hi,

I'm using the arrow keys to move an animated gif around a form. However when I add a button control to the form the animated gif no longer responds to the arrow keys.



#include "stdafx.h"
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;


public ref class Form1 : public Form 
{
	public:
			Bitmap^ Animated_Gif; // Create a Bitmap Object.
			bool Initialized_Gif; // Used to check if the animated gif has started playing or not.
			float x;
			float y;
		
			bool downKeyDown;
			bool rightKeyDown;
			bool upKeyDown;
			bool leftKeyDown;
			
			Form1() // Class constructor.
			{
				downKeyDown = false;
				rightKeyDown = false;
				upKeyDown = false;
				leftKeyDown = false;
				Animated_Gif = gcnew Bitmap("AnimatedGif.gif"); // Create bitmap with the specified image.
				this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form_KeyDown);
				this->KeyUp += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form_KeyUp);
				Button^ button1 = gcnew Button();
				button1->Left = 200;

				//Add the controls to the form
				Controls->Add(button1);
			}

			virtual void OnPaint(PaintEventArgs^ e) override
			{
				if(downKeyDown == true){y++;}
				if(rightKeyDown == true){x++;}
				if(upKeyDown == true){y--;}
				if(leftKeyDown == true){x--;}

				if (!Initialized_Gif)// Initialize the animation and make sure it only starts once. If this if statement is removed the animation won't play. I think because the function never stops starting the gif sequence, which means the animation will always be at the begining(the first frame).
				{
					ImageAnimator::Animate(Animated_Gif,gcnew EventHandler(this, &Form1::OnFrameChanged));
					Initialized_Gif = true; // Once the animation has been initialized(started playing) set the initialize variable to false to prevent the animation from constantly being started at the begining in an endless loop.
				}

				ImageAnimator::UpdateFrames(); // Get the next frame.
				e->Graphics->DrawImage(this->Animated_Gif,Point(x, y)); // Draw the next frame in the animation.
			}			
			
			void OnFrameChanged(Object^ , EventArgs^ ) 
			{
				this->Invalidate(); // Force a call to the Paint event handler. I think that means force the bitmap(control?) to be redrawn. "this->" refers to the class "Form1".
			}

			System::Void Form_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e)
			{
				if(e->KeyCode == Keys::Down){downKeyDown=true;}
				if(e->KeyCode == Keys::Right){rightKeyDown=true;}
				if(e->KeyCode == Keys::Up){upKeyDown=true;}
				if(e->KeyCode == Keys::Left){leftKeyDown=true;}
			}

			System::Void Form_KeyUp(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e)
			{
				if(e->KeyCode == Keys::Down){downKeyDown=false;}
				if(e->KeyCode == Keys::Right){rightKeyDown=false;}
				if(e->KeyCode == Keys::Up){upKeyDown=false;}
				if(e->KeyCode == Keys::Left){leftKeyDown=false;}
			}



};

[STAThread]
int main()
{	
    Application::Run(gcnew Form1());
}



Is This A Good Question/Topic? 0
  • +

Replies To: Image stops moving when a button is added to the form

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,210
  • Joined: 23-August 08

Re: Image stops moving when a button is added to the form

Posted 12 March 2011 - 04:25 PM

Moved to CLI C++
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1