Animates a circle

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
public class AnimateFace : Form {
private int x = 10, y = 10;
private int width = 200, height = 200;

private Button suspend = new Button();
private Button resume = new Button();
private Button abort = new Button();
private Thread t;

public AnimateFace() {
BackColor = Color.White;
abort.Text = “Abort”;
suspend.Text = “Suspend”;
resume.Text = “Resume”;

Controls.Add(suspend);
Controls.Add(resume);
Controls.Add(abort);

int w = 20;
suspend.Location = new Point(w, 240);
resume.Location = new Point(w += 10 + suspend.Width, 240);
abort.Location = new Point(w += 10 + resume.Width, 240);

abort.Click += new EventHandler(Abort_Click);
suspend.Click += new EventHandler(Suspend_Click);
resume.Click += new EventHandler(Resume_Click);

t = new Thread(new ThreadStart(Run));
t.Start();
}
protected void Abort_Click(object sender, EventArgs e) {
t.Abort();
}
protected void Suspend_Click(object sender, EventArgs e) {
t.Suspend();
}
protected void Resume_Click(object sender, EventArgs e) {
t.Resume();
}
protected override void OnPaint( PaintEventArgs e ) {
Graphics g = e.Graphics;
Pen green = new Pen(Color.Green, 3);
Brush red = new SolidBrush(Color.Red);
g.DrawEllipse(green, x, y, width, height);
base.OnPaint(e);
}
public void Run() {
int dx=9, dy=9;
while (true) {
for (int i = 0; i < 30; i++) { x += dx; y += dy; width -= dx; height -= dy; Invalidate(); Thread.Sleep(30); } dx = -dx; dy = -dy; } } public static void Main( ) { Application.Run(new AnimateFace()); } } [/csharp]

Button click action to move a ball


   


using System;
using System.Drawing;
using System.Windows.Forms;

public class ButtonToMove : Form {
  private int x = 50, y = 50;
  private Button move = new Button();

  public ButtonToMove() {
    move.Text = "Move";
    move.Location = new Point(5,5);
    Controls.Add(move);
    move.Click += new EventHandler(Move_Click);
  }    
  protected void Move_Click(object sender, EventArgs e) {
    x += 9;
    y += 9;
    Invalidate();
  }
  protected override void OnPaint( PaintEventArgs e )   {
    Graphics g = e.Graphics;
    Brush red = new SolidBrush(Color.Red);
    g.FillEllipse(red ,x ,y, 20 ,20);
    base.OnPaint(e);
  }
  public static void Main( ) {
    Application.Run(new ButtonToMove());
  }         
}
           
          


Object collision

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
private int dx = 4;
private System.Windows.Forms.PictureBox picTarget;
private System.Windows.Forms.PictureBox picBall;
private System.Windows.Forms.Timer timer1;

public Form1() {
InitializeComponent();
}
private void InitializeComponent() {
this.picTarget = new System.Windows.Forms.PictureBox();
this.picBall = new System.Windows.Forms.PictureBox();
this.timer1 = new System.Windows.Forms.Timer(new System.ComponentModel.Container());
this.SuspendLayout();

this.picTarget.BackColor = Color.Red;
this.picTarget.Location = new System.Drawing.Point(160, 240);
this.picTarget.Name = “picTarget”;
this.picTarget.Size = new System.Drawing.Size(56, 56);
this.picTarget.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.picTarget.TabIndex = 0;
this.picTarget.TabStop = false;

this.picBall.Image = new Bitmap(“winter.jpg”);
this.picBall.Location = new System.Drawing.Point(24, 136);
this.picBall.Name = “picBall”;
this.picBall.Size = new System.Drawing.Size(32, 32);
this.picBall.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.picBall.TabIndex = 1;
this.picBall.TabStop = false;

this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(392, 341);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.picBall,
this.picTarget});
this.Name = “Form1”;
this.Text = “Crasher”;
this.ResumeLayout(false);

}
[STAThread]
static void Main() {
Application.Run(new Form1());
}

private void timer1_Tick(object sender, System.EventArgs e) {
int newX, newY;
newX = picBall.Location.X + dx;
newY = picBall.Location.Y + dx;

if (newX > this.Width – picBall.Width){
dx = – dx;
}

if (newX < 0){ dx = - dx; } if (picBall.Bounds.IntersectsWith(picTarget.Bounds)){ this.BackColor = Color.Black; } else { this.BackColor = Color.White; } picBall.Location = new Point(newX, newY); } } [/csharp]

Load animated ani file

   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

public class Form1 : Form
{

  public Form1() {
        InitializeComponent();
    try
    {
      this.Cursor = AdvancedCursors.Create(Path.Combine(Application.StartupPath, "blob.ani"));
    }
    catch (Exception err)
    {
      MessageBox.Show(err.Message);
    }
  }

  private void InitializeComponent()
  {
    this.SuspendLayout();

    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Text = "Form1";
    this.ResumeLayout(false);
  }


  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}

public class AdvancedCursors
{

  [DllImport("User32.dll")]
  private static extern IntPtr LoadCursorFromFile(String str);

  public static Cursor Create(string filename)
  {
    IntPtr hCursor = LoadCursorFromFile(filename);
    
    if (!IntPtr.Zero.Equals(hCursor))
    {
      return new Cursor(hCursor);
    }
    else
    {
      throw new ApplicationException("Could not create cursor from file " + filename);
    }
  }
}