using System;
using System.Drawing;
using
System.Windows .Forms;
public partial class RunawayForm : Form
{
private Timer timer;
private Random random;
public RunawayForm()
{
InitializeComponent();
InitializeForm();
}
private void InitializeForm()
{
Width = 200;
Height = 100;
FormBorderStyle = FormBorderStyle.None;
StartPosition = FormStartPosition.Manual;
Location = GetRandomLocation();
timer = new Timer();
timer.Interval = 10;
timer.Tick += Timer_Tick;
random = new Random();
}
private Point GetRandomLocation()
{
return new Point(
random.Next (Screen.PrimaryScreen.Bounds.Width - Width),
random.Next (Screen.PrimaryScreen.Bounds.Height - Height));
}
private void Timer_Tick(object sender, EventArgs e)
{
MoveForm();
}
private void MoveForm()
{
Location = new Point(Location.X +
random.Next (-5, 6), Location.Y +
random.Next (-5, 6));
if (Location.X < 0)
Location = new Point(0, Location.Y);
else if (Location.X + Width > Screen.PrimaryScreen.Bounds.Width)
Location = new Point(Screen.PrimaryScreen.Bounds.Width - Width, Location.Y);
if (Location.Y < 0)
Location = new Point(Location.X, 0);
else if (Location.Y + Height > Screen.PrimaryScreen.Bounds.Height)
Location = new Point(Location.X, Screen.PrimaryScreen.Bounds.Height - Height);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
timer.Start();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run (new RunawayForm());
}
}