Search code examples
c#cursor-position

C# - Cursor position (all screen)


help me please! :) My program should get cursor position (all screen) every ~50 ms and them write in text Box. How it make?

Example:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
   textBox1.Text = e.X.ToString();
   textBox2.Text = e.Y.ToString();
}

but we get position only in window

it's really do?


Solution

  • you can use Cursor.Position :

       textBox1.Text = Cursor.Position.X.ToString();
       textBox2.Text = Cursor.Position.Y.ToString();
    

    btw , welcome to SO , please Consider searching the site before asking questions.

    and for getting these result every 50 ms you need to use Timer , here's a tutorial for Timer : C# Timer Tutorial

    Update :

        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)
        {
            textBox1.Text = Cursor.Position.X.ToString();
            textBox2.Text = Cursor.Position.Y.ToString();
        }