Loading...

Checking Mouse Directions in Windows Form Application, Step by Step

Mouse Directions in Windows Form:

We can check the mouse directions on Windows Form Application using the events of mouse_move on the Win Form. For example we want to check that in which direction the mouse is moving in our Application such as a game like subway surfer.
 Here we will learn how to check the movement Directions of a mouse in Windows Form App using C#.

Following are the Steps:


  • First of all open the Windows Form Application is C#, and add a label and place it in middle of the Form like picture below.

  • Write the following code in Form1.cs Class:

Code:



using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int cnt = 0;  // i am taking this as i want to run a code only for the first time
        int my_x, my_y;  // we will store the mouse position (x axis, y axis) in these variables

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {

            if (cnt == 0) // we have already taken cnt as 0 
            {
                my_x = e.X;   // storing current x axis location of mouse pointer on Form
                my_y = e.Y;    // storing current y axis location of mouse pointer on Form
                cnt = 1;   // we want to run above code only for first time, so we are changing cnt value
            }
            Text = e.X.ToString() + ',' + e.Y.ToString();  // this will show on the top of Form 
            if (e.X > my_x )    // if x axis is increasing and greater than previous stored x-axis, means mouse moving to right
            {
                label1.Text = "right".ToString();
                my_x = e.X;  // storing new location as my_x 
                my_y = e.Y;   // Storing new location

            }
            if (e.X < my_x)   // if x axis is decreasing and less than previous stored x-axis, means mouse moving to left
            {
                label1.Text = "left".ToString();
                my_x = e.X;   // Storing new location
                my_y = e.Y;  // Storing new location 
            }
            if (e.Y < my_y)  // if y axis is decreasing and less than previous stored y-axis, means mouse moving to up
            {
                label1.Text = "up".ToString();
                my_y = e.Y;    // Storing new location   
                my_x = e.X;   // Storing new location 
            }
            if (e.Y > my_y)  // if y axis in increasing and greater than previous stored y-axis, means mouse moving to down
            {
                label1.Text = "down".ToString();
                my_y = e.Y;  // Storing new location
                my_x = e.X;  // Storing new location
            }           
        }
    }
}


Output:



Thanks For Reading, Like this if you understood, It helps a lot to keep us motivated

Google+

 
TOP