[英]How can I get the mouse coordinates related to a panel?
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. 我试图用C#中的鼠标获取点击的坐标与我的表格中的面板相关,但我不知道该怎么做。 I'm a begginer and I don't have any experience with events. 我是一个乞丐,我对事件没有任何经验。 Thanks! 谢谢!
You must subscribe to event of Panel control - Click event. 您必须订阅Panel控件事件 - Click事件。 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());
}
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.