Animates an image

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

public class AnimateImage : Form {
private int width = 10;
private int height = 10;

Image pic = Image.FromFile(“winter.jpg”);
private Button abort = new Button();
Thread t;

public AnimateImage() {
abort.Text = “Abort”;
abort.Location = new Point(50, 230);
abort.Click += new EventHandler(Abort_Click);
Controls.Add(abort);

SetStyle(ControlStyles.DoubleBuffer
| ControlStyles.AllPaintingInWmPaint
| ControlStyles.UserPaint, true);

t = new Thread(new ThreadStart(Run));
t.Start();
}
protected void Abort_Click(object sender, EventArgs e) {
t.Abort();
}

protected override void OnPaint( PaintEventArgs e ) {
Graphics g = e.Graphics;
g.DrawRectangle(Pens.Black, 8, 8, width+3, height+3);
g.DrawImage(pic, 10, 10, width, height);
base.OnPaint(e);
}

public void Run() {
int dx=5, dy=5;
while (true) {
for(int i = 0; i < 500; i++) { width += dx; height += dy; Invalidate(); Thread.Sleep(30); } dx = -dx; dy = -dy; } } public static void Main( ) { Application.Run(new AnimateImage()); } } [/csharp]

Uses a thread to Animate a ball

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

public class AnimateBall : Form {
private int x, y;
private Button suspend = new Button();
private Button resume = new Button();
private Button abort = new Button();
Thread t;

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

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

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

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

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;
g.FillEllipse(Brushes.Red, 100 ,y, 4 ,4);
base.OnPaint(e);
}
public void Run() {
int dx=2, dy=2;
y = 1;

while (true) {
for(int i=0; i<140; i++) { y+=dy; Invalidate(); Thread.Sleep(10); } dx = -dx; dy = -dy; } } public static void Main( ) { Application.Run(new AnimateBall()); } } [/csharp]

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);
    }
  }
}


           
          


Using XPath to get string value, integer value and boolean value

   
 
//---------------------------------------------------------------------
// File: XPathValidator.cs
// 
// Summary: 
//
// Copyright (c) http://bizunitextensions.codeplex.com. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Xml.XPath;
using System.Xml;

namespace BizUnit.Extensions.Utilities
{
  /// <summary>
  /// A utility class which applies XPath expressions to xml files and returns the value
  /// in a variety of data types
  /// </summary>
  public class XPathValidator
  {
    /// <summary>
    /// basic constructor
    /// </summary>
    public XPathValidator()
    {
      //
      // TODO: Add constructor logic here
      //
    }
    /// <summary>
    /// Evaluates the XPath expression and returns a string value
    /// </summary>
    /// <param name="InputXmlFile">full path of the xml file to parse</param>
    /// <param name="XPathString">XPath expression to apply</param>
    /// <returns>string</returns>
    public string GetStringValue(string InputXmlFile,string XPathString)
    {
      string retval ;
      object obj = MakeXPathExpression(InputXmlFile,XPathString);
      retval = (string)obj;
      return(retval);
    }

    /// <summary>
    /// Evaluates the XPath expression and returns a integer value
    /// </summary>
    /// <param name="InputXmlFile">full path of the xml file to parse</param>
    /// <param name="XPathString">XPath expression to apply</param>
    /// <returns>int</returns>
    public int GetIntegerValue(string InputXmlFile,string XPathString)
    {
      int retval ;
      object obj = MakeXPathExpression(InputXmlFile,XPathString);
      retval = System.Convert.ToInt32(obj);
      return(retval);
    }

    /// <summary>
    /// Evaluates the XPath expression and returns a bool value
    /// </summary>
    /// <param name="InputXmlFile">full path of the xml file to parse</param>
    /// <param name="XPathString">XPath expression to apply</param>
    /// <returns>bool</returns>
    public bool GetBooleanValue(string InputXmlFile,string XPathString)
    {
      bool retval ;
      object obj = MakeXPathExpression(InputXmlFile,XPathString);
      retval = System.Convert.ToBoolean(obj);
      return(retval);
    }
    private object MakeXPathExpression(string inputXmlFile,string XPathString)
    {
      
      XmlDocument xDoc = new XmlDocument();
      xDoc.Load(inputXmlFile);
      XPathNavigator nav = xDoc.CreateNavigator();
      XPathExpression expr = nav.Compile( XPathString);

      return(nav.Evaluate(expr));
    }
  }
}