Writing a WinForm Application

 

 

In this example we will show how to create a windows application using VS.NET.

 

  1. Start VS.NET and select File -> New -> Project.
  2. Create a Windows Application called MouseWatcher as shown below:

  1. Click on OK.
  2. Right click on the Form window and select Properties. In the Properties, change the following values: Text: Mouse Watcher; Name: MouseWatcher.

  1. In the solutions explorer, change the file name from Form1.cs to MouseWatcher.cs. Modify any occurrence of Form1 to MouseWatcher.
  2. Right click on the solutions explorer and select Add and Add Class as shown below:

  1. Provide the name for the file as Watcher.cs as shown below:

  1. Edit the Watcher.cs as follows:

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;

            }                

      }

}

 

  1. Go to the class view and right click on MouseWatcher. Click on Add and Add Field. Add the field as shown:

  1. Go to the solution explorer view and right click on MouseWatch.cs and go to the design view by selecting view designer. Double click on the Form window. Notice that the MouseWatcher_Load handler has been added. Scroll up to the section that says “Windows Form Designer generated code.” Expand that section and notice that an even handler has been added for the Load.
  2. Edit the MouseWatcher_Load function as follows:

      private void MouseWather_Load(object sender, System.EventArgs e)

      {

                  Size = new Size(100, 100);

                  theWatcher = new Watcher();

      }

  1. Override the OnPaint method of the base as shown below:

      protected override void OnPaint(PaintEventArgs pe)

      {

            theWatcher.draw(pe.Graphics);

      }

  1. In the solutions explorer, right click on the MouseWather.cs and view Designer. Right click on the designer view and select Properties.
  2. In the properties, look for MouseMove event and type MouseWatcher_MouseMove and hit return.

  1. This will generate a handler for the mouse move and also register the delegate.
  2. Edit the function as follows:

      private void MouseWatcher_MouseMove(object sender,

System.Windows.Forms.MouseEventArgs e)

      {

            Point point = new Point(e.X, e.Y);

            theWatcher.watchLocation = point;

            Refresh();

   }

  1. Compile and test.
  2. Now we will modify it so that it will roll its eyes even when the mouse is not within the application window. Remove the MouseMove function from the Properties window, the same place where you added it to create the method.
  3. Edit the OnPaint method as follows:

            protected override void OnPaint(PaintEventArgs pe)

            {

                  theWatcher.draw(pe.Graphics);

            }

  1. Write a method called fire as shown below:

            private void fire(Object sender,

System.Timers.ElapsedEventArgs e)

            {

                  Point point = PointToClient(MousePosition);

                  theWatcher.watchLocation = point;

                  Refresh();

            }

  1. Modify the Load method as follows:

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

            }

  1. Add a private variable in the MouseWatcher class.

            private System.Timers.Timer timer;

  1. Compile and test.
  2. Notice while the eyes follow the mouse, the window refreshing causes undesirable effect. This can be avoided by using double buffering:

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

            }

  1. Compile and test.

 

  1. OK, could not stop at that, so here is more!.
  2. Go to the designer view so you can see the form. Right click on the form and click on Properties. Set the FormBorderStyle to None.
  3. Go to the Toolbox and drag ContextMenu to the form. Click on the ContextMenu and type Exit for the first menu item.

  1. Right click on Exit and go to Properties. Modify the name to exitMenuItem.
  2. Click on the Events Bolt in the Properties window and type the methods name for Click:

  1. Edit the exitItem_Click method as follows and add a method runAndExit:

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

            }

      }

  1. Also modify the Load method as follows:

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

            }

  1. Finally, add move down, move and up events and implement them as shown below:

            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;

            }

  1. Declare a private variable in MouseWatcher class:

            private bool moveIt = false;

  1. Compile and test. Right click on the application form window and click Exit when you want to exit.