如何获得与面板相关的鼠标坐标?

时间:2012-07-07 19:16:11

标签: c# winforms events click mouse

我正在尝试使用C#中的鼠标来获取与我的表单中的面板相关的鼠标点击坐标,但我不知道该怎么做。我是一个乞丐,我对事件没有任何经验。谢谢!

2 个答案:

答案 0 :(得分:15)

您必须订阅Panel控件事件 - Click事件。 您可以在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());
    }

有关活动的更多详情,请转到here

答案 1 :(得分: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);
}
相关问题