www.xbdev.net xbdev - software development
Friday October 18, 2019
home | about | contact | Donations
>>
     
 

Programming..

With C# and .Net...

 


Mouse Input

 

 

For a simple introduction to the mouse and of course grabbing some data from it - I've done a simple demo which shows you how to capture mouse clicks!  Which could lead you to a paiting program with enough time..hehe

 

 

DownloadSourceCode

 

The thing to look for, is if you click the mouse in the window - the title caption will change to the mouse position coordinates.... simple, but it lets you see how it works.

 

 

 

So shall we take a looksy at the code and see how easy it is to respond to mouse input?... well here is the whole code for our simple mouse input demo window:

 

 

Code: mouse.cs

// mouse.cs

 

// C:>csc /t:winexe mouse.cs

 

// Well we want our program to be in a GUI window, so we need to inherit

// from Form, which is in the library 'System.Windows.Forms'.

 

class SimpleWindow : System.Windows.Forms.Form

{

 

      private void Screen_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)

      {

            // Added just so you can see how you would handle a mouse up

      }

      private void Screen_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

      {

            int mouse_x = e.X;

            int mouse_y = e.Y;

 

            string title = "MousePos: " + mouse_x.ToString() + " " + mouse_y.ToString();

            this.Text = title;

      }

 

      // Window constructor

      SimpleWindow()

      {

            this.MouseUp   += new System.Windows.Forms.MouseEventHandler(this.Screen_MouseUp);

            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Screen_MouseDown);

      }

 

      // Our program entry point.

      static void Main()

      {

            System.Windows.Forms.Application.Run(new SimpleWindow());

      }

}// End of SimpleWindow Class

 

 

I've demonstrated how to capture mouse up and down clicks...but you could also capture mouse movement our single for double clicks....and even that middle wheelie mouse thing.

 

 

 

 

 

 
 Visitor: 9534626  { 209.237.238.175 } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.