Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

animation works only after the window is moved

Status
Not open for further replies.

theovaf

Programmer
Jun 14, 2006
1
GR
Hi all,

I am trying to make a dummy animation program.There is a button and a picturebox.When I click the button I want a line to move from the top to the bottom of the picturebox.
In my program when I click the button the line is drawn in its initial position and stays there BUT when I move the window or minimize it then maximize it again the line appears in another position.Why is this happening?

Sorry if the question is silly but i am new at this.

My Form1.h code follows.Thanks
//
#pragma once

namespace ScintiVCpp
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Drawing::Drawing2D;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public: static System::Windows::Forms::Timer *timer1=new System::Windows::Forms::Timer(); //th
public: int x; //th
public: int y; //th
//public: int Button1Clicked;

public:
Form1(void)
{
InitializeComponent();
//enabling double-buffering to avoid flick
SetStyle(System::Windows::Forms::ControlStyles::UserPaint, true);
SetStyle(System::Windows::Forms::ControlStyles::AllPaintingInWmPaint, true);
SetStyle(System::Windows::Forms::ControlStyles::DoubleBuffer, true);
}

protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::pictureBox * pictureBox1;
private: System::Windows::Forms::Button * button1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->pictureBox1 = new System::Windows::Forms::pictureBox();
this->button1 = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
this->pictureBox1->Location = System::Drawing::point(152, 64);
this->pictureBox1->Name = S"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(288, 224);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
this->pictureBox1->Click += new System::EventHandler(this, pictureBox1_Click);
//
// button1
//
this->button1->Location = System::Drawing::point(32, 152);
this->button1->Name = S"button1";
this->button1->TabIndex = 1;
this->button1->Text = S"button1";
this->button1->Click += new System::EventHandler(this, button1_Click);
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(568, 366);
this->Controls->Add(this->button1);
this->Controls->Add(this->pictureBox1);
this->Name = S"Form1";
this->Text = S"Form1";
this->Load += new System::EventHandler(this, Form1_Load);
this->ResumeLayout(false);
}

private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
{

}
void pictureBox1_Paint(Object * sender, System::Windows::Forms::paintEventArgs* e)
{
Graphics* g = e->Graphics;
x=pictureBox1->Width-100;
g->DrawLine(new System::Drawing::pen(System::Drawing::Color::Black,2.),x,y,x,y+10);
}


private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{
timer1->Start();
timer1->Interval=5;
timer1->Tick += new System::EventHandler(this,&Form1::Ticker);
pictureBox1->BackColor = Color::White;
pictureBox1->Paint += new System::Windows::Forms::paintEventHandler(this, &Form1::pictureBox1_Paint);
this->Invalidate();
}
void Ticker(System::Object* sender, System::EventArgs* e)
{
if(y>0)
y-=1;
else
y=pictureBox1->Height;

this->Invalidate();
}

};
}



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top