2012-07-07 95 views

回答

0

如果您使用的是Windows窗體,然後Cursor.Position

private void panel1_MouseMove(object sender, MouseEventArgs e) 
{ 
    textBox1.Text = string.Format("X: {0} , Y: {1}", Cursor.Position.X, Cursor.Position.Y); 
} 
+0

我更新了我的答案代碼位於panel1_MouseMove insted form1_MouseMove – HatSoft 2012-07-07 19:39:32

14

您必須訂閱Panel控件的事件 - 點擊事件。 您可以窗體的構造器中編寫的代碼如下:

System.Windows.Forms.Panel panel; 

    public Form() 
    { 
     InitializeComponent(); 

     panel = new System.Windows.Forms.Panel(); 
     panel.Location = new System.Drawing.Point(82, 132); 
     panel.Size = new System.Drawing.Size(200, 100); 
     panel.Click += new System.EventHandler(this.panel_Click); 
     this.Controls.Add(this.panel); 
    } 

    private void panel_Click(object sender, EventArgs e) 
    { 
     Point point = panel.PointToClient(Cursor.Position); 
     MessageBox.Show(point.ToString()); 
    } 

有關詳細信息有關事件去here