You must Sign In to post a response.
  • Category: .NET

    How to get mouse cordinates when my mouse click somewhere?

    I want to retrieve the mouse pointer's x , y cordinates when my mouse clicks anywhere in the screen , it should be independent of windows form .
    ex. if i click mouse in notepad it should give me cordinates , i tried following but it gives cordinated only when i clicked in myform.

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
    //string x_cord = Cursor.Position.X.ToString();
    //string y_cord = Cursor.Position.Y.ToString();
    //MessageBox.Show("cordinates are " + x_cord +","+ y_cord);
    }

    please suggest me way out of this issue.
    thanks.
  • #767138
    Hi Priyanka.

    Take windows forms application and place a label control Label1.
    Then by using Cursor.Position.X.ToString() and Cursor.Position.Y.ToString() you can get the position of mouse even it is out side the form i.e., any where in the screen.
    Refer the below code

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

    namespace mouse_coordinates_example
    {
    public partial class Form1 : Form
    {

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    Timer t1 = new Timer();
    t1.Interval = 50;
    t1.Tick += new EventHandler(timer1_Tick);
    t1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
    label1.Text = Cursor.Position.X.ToString() + " " + Cursor.Position.Y.ToString();
    }

    }
    }

    The label will display the co-ordinates of mouse where ever it is placed or moved.
    This code is tested in my machine. If you still face any issue let me know.

    Sridhar Thota.
    Editor: DNS Forum.

  • #767220
    Its working nice but i want to capture time when i click on editable area of my another application. now i can capture cordinates when click on form only , because i am printing in Form1_mauseclick event. but i want to capture when it clicks anywhere on screen not only form.

  • #767222
    Hi Priyanka.

    It will work out side the form also. Once the form is loaded timer1_tick event executes and that timer1_tick event has the logic to get co-ordinates and time aslo.
    Don't use Form1_MouseClick event since it will show only when you click on form1.
    You can print or save the time by using DateTime.Now() with in that capture logic only.

    Sridhar Thota.
    Editor: DNS Forum.

  • #767300
    I appreciate your logic sir , but i want to capture co-ordinates when i click on another application area , (say notepad) when i click on notepad i want to print that cordinates. how to do this?


  • Sign In to post your comments