C# 如何获取与面板相关的鼠标坐标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11377938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the mouse coordinates related to a panel?
提问by Isa Guzmán
I am trying to get the coordinates of a click with the mouse in C# related to a panel in my form, but I don't know how to do that. I'm a begginer and I don't have any experience with events. Thanks!
我试图在 C# 中获取与表单中面板相关的鼠标单击坐标,但我不知道如何做到这一点。我是初学者,我没有任何事件经验。谢谢!
回答by HatSoft
If your are using Windows Forms then Cursor.Position
如果您使用的是 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);
}
回答by ZuTa
You must subscribe to event of Panel control - Click event. You can write the code below within Form's contructor:
您必须订阅面板控件的事件 - 单击事件。您可以在 Form 的构造函数中编写以下代码:
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());
}
For more details about events go here
有关活动的更多详细信息,请访问此处

