#pragma once
namespace MoveOrigin
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <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:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Timer * timer1;
private: System::ComponentModel::IContainer * components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = new System::ComponentModel::Container();
this->timer1 = new System::Windows::Forms::Timer(this->components);
//
// timer1
//
this->timer1->Enabled = true;
this->timer1->Interval = 10;
this->timer1->Tick += new System::EventHandler(this, timer1_Tick);
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->BackColor = System::Drawing::Color::Black;
this->ClientSize = System::Drawing::Size(292, 266);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;
this->Name = S"Form1";
this->Text = S"Form1";
this->WindowState = System::Windows::Forms::FormWindowState::Maximized;
this->KeyPress += new System::Windows::Forms::KeyPressEventHandler(this, Form1_KeyPress);
this->Load += new System::EventHandler(this, Form1_Load);
}
private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
{
Cursor->Hide();
}
private: System::Void Form1_KeyPress(System::Object * sender,
System::Windows::Forms::KeyPressEventArgs * e)
{
if( e->KeyChar == Keys::Escape )
Close();
}
private: System::Void timer1_Tick(System::Object * sender, System::EventArgs * e)
{
// Get the graphics object of this form
Graphics *graph = Graphics::FromHwnd(this->Handle);
// These variables are declared static because they will keep changing
// The X origin
static int x = 0;
// The Y origin
static int y = 0;
// This will check whether the origin is moving right or left
static bool IsMovingRight;
// This will check whether the origin is moving down or up
static bool IsMovingDown;
// If the status of the origin indicates that it is moving right,
// then increase the horizontal axis
if( IsMovingRight == true )
x++;
else // If it's moving left, then decrease the horizontal movement
x--;
// If the status of the origin indicates that it is moving down,
// then increase the vertical axis
if( IsMovingDown == true )
y++;
else // Other wise, decrease it
y--;
// Collision: if the right side of the circle hits the right side of the screen,
// then set the horizontal moving status to "Right", which will be used
// by the above code
if( (x+40) > this->ClientSize.Width )
IsMovingRight = false;
if( x < 0 )
IsMovingRight = true;;
if( (y+40) > this->ClientSize.Height )
IsMovingDown = false;
if( y < 0 )
IsMovingDown = true;
// Before doing what we want, erase the screen
graph->Clear(Color::Black);
// Draw the new axis
graph->DrawLine(Pens::Aqua, x+20, 0, x+20, this->ClientSize.Height);
graph->DrawLine(Pens::Aqua, 0, y+20, ClientSize.Width, y+20);
// Draw the circle
graph->DrawEllipse(Pens::Fuchsia, x, y, 40, 40);
// Restart over
}
};
}
|