Writing a WinForm
Application
In this example we will show
how to create a windows application using VS.NET.
using System;
using System.Drawing;
namespace MouseWatcher
{
public class Watcher
{
private Point watchPoint
= new Point(100, 100);
public Point watchLocation
{
get { return watchPoint; }
set { watchPoint = value; }
}
public void
draw(Graphics g)
{
//
Face
Brush brush
= new SolidBrush(Color.Blue);
g.FillEllipse(brush, 0, 0,
100, 100);
//
Eyes
brush
= new SolidBrush(Color.Wheat);
g.FillEllipse(brush, 20, 20,
20, 40);
g.FillEllipse(brush, 60, 20,
20, 40);
//
Eyeballs
brush
= new SolidBrush(Color.Brown);
int x, y;
getEyeballLocation(20, 20,
20, 40, out x, out
y);
g.FillEllipse(brush, x, y,
10, 10);
getEyeballLocation(60, 20,
20, 40, out x, out
y);
g.FillEllipse(brush, x, y,
10, 10);
}
private void getEyeballLocation(int topX, int topY,
int
width, int height,
out int locationX, out int locationY)
{
int halfWidth = (int)((double)(width)
/ 2);
int halfHeight = (int)((double)(height)
/ 2);
int centerX = topX + halfWidth;
int centerY = topY + halfHeight;
int XOffset = watchPoint.X - centerX;
if (XOffset >
halfWidth/2) XOffset = halfWidth
- 11;
if (XOffset < -halfWidth) XOffset = -halfWidth + 1;
int YOffset = watchPoint.Y - centerY;
if (YOffset > halfHeight) YOffset = halfHeight - 11;
if (YOffset < -halfHeight) YOffset = -halfHeight + 1;
locationX = centerX + XOffset;
locationY = centerY + YOffset;
}
}
}
private void MouseWather_Load(object sender, System.EventArgs
e)
{
Size = new Size(100,
100);
theWatcher = new
Watcher();
}
protected
override void OnPaint(PaintEventArgs pe)
{
theWatcher.draw(pe.Graphics);
}
private void MouseWatcher_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
Point point
= new Point(e.X, e.Y);
theWatcher.watchLocation
= point;
Refresh();
}
protected override void OnPaint(PaintEventArgs pe)
{
theWatcher.draw(pe.Graphics);
}
private void
fire(Object sender,
System.Timers.ElapsedEventArgs e)
{
Point point
= PointToClient(MousePosition);
theWatcher.watchLocation
= point;
Refresh();
}
private void MouseWatcher_Load(object sender,
System.EventArgs e)
{
Size
= new Size(100, 100);
theWatcher = new Watcher();
timer
= new
System.Timers.Timer(10);
timer.Elapsed
+= new
System.Timers.ElapsedEventHandler(fire);
timer.Start();
}
private System.Timers.Timer
timer;
private void MouseWatcher_Load(object sender,
System.EventArgs e)
{
SetStyle(ControlStyles.AllPaintingInWmPaint,
true);
SetStyle(ControlStyles.DoubleBuffer,
true);
Size
= new Size(100, 100);
theWatcher = new
Watcher();
System.Timers.Timer timer = new
System.Timers.Timer(1);
timer.Elapsed += new
System.Timers.ElapsedEventHandler(fire);
timer.Start();
}
private void exitItem_Click(object sender,
System.EventArgs e)
{
new Thread(new
ThreadStart(this.runAndExit)).Start();
}
private void runAndExit()
{
timer.Close();
Random rand = new Random();
int i = 0;
for (i = 0; i < 100; i++)
{
Left = rand.Next(600);
Top = rand.Next(600);
Thread.Sleep(10);
}
for (int j =
0; j < 100; j++)
{
Left = i + j;
Top = i + j;
Thread.Sleep(10);
}
for (i = 0; i < 50; i++)
{
theWatcher.watchLocation
= new
Point(rand.Next(100), rand.Next(100));
Refresh();
System.Drawing.Drawing2D.GraphicsPath
grPath =
new
System.Drawing.Drawing2D.GraphicsPath();
grPath.AddEllipse(i, i, 100 - 2 * i,
100 - 2 * i);
Region = new Region(grPath);
Thread.Sleep(100);
}
Close();
}
}
private void MouseWatcher_Load(object sender,
System.EventArgs e)
{
ShowInTaskbar
= false;
TopMost
= true;
SetBounds(0, 0, 100, 100);
System.Drawing.Drawing2D.GraphicsPath
grPath =
new
System.Drawing.Drawing2D.GraphicsPath();
grPath.AddEllipse(0, 0, 100,
100);
Region = new Region(grPath);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
theWatcher = new Watcher();
timer = new System.Timers.Timer(1);
timer.Elapsed += new
System.Timers.ElapsedEventHandler(fire);
timer.Start();
}
private void MouseWatcher_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
moveIt = true;
}
private void MouseWatcher_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (moveIt == true)
{
Point point = PointToScreen(new Point(e.X,
e.Y));
SetBounds(point.X - (int)(width /
2.0),
point.Y -
(int)(height/2.0),
width,
height);
}
}
private void MouseWatcher_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
moveIt = false;
}
private
bool moveIt = false;