1. 程式人生 > >c#中獲取螢幕上滑鼠的座標

c#中獲取螢幕上滑鼠的座標

獲取螢幕上的滑鼠座標,需要用到timer控制元件,在timer控制元件的timer1_Tick事件中新增:

  private void timer1_Tick(object sender, EventArgs e)
        {
            //方法1:
            textBox1.Text = Cursor.Position.X.ToString();
            textBox2.Text = Cursor.Position.Y.ToString();
            //方法2:
            //textBox1.Text = Control.MousePosition.X.ToString();
            //textBox2.Text = Control.MousePosition.Y.ToString();
        }

但是要在Form1_Load事件中啟動timer計時器

private void Form1_Load(object sender, EventArgs e)
        {
            this.timer1.Enabled = true;
            this.timer1.Interval = 10;//timer控制元件的執行頻率
        }