简体   繁体   English

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

[英]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());
    }

For more details about events go here 有关活动的更多详情,请点击此处

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.

相关问题 如何获得鼠标位置(RFC)的面板像素的颜色 - How can I get the color of a Panel pixel at the mouse location (RFC) 我可以使用WPF从DomEventArgs获取鼠标坐标吗 - Can i get mouse Coordinates from DomEventArgs using WPF 单击pictureBox时,如何将鼠标光标坐标转换为屏幕相对鼠标光标坐标? - How can I convert the mouse cursor coordinates when click on pictureBox are to the screen relative mouse cursor coordinates? 如何在屏幕上找到图像并获取鼠标坐标? - How do I find a image on the screen and get the mouse coordinates? WPF MVVM - 如何获取 Canvas 上的鼠标坐标? - WPF MVVM - How do I get the mouse coordinates on a Canvas? 如何使用ViewportPointToLocation鼠标单击来检索坐标? - How can I retrieve coordinates on a mouse click with ViewportPointToLocation? 如何在KINECT中将手坐标映射到鼠标指针? - How can I Map my hand coordinates to Mouse pointer in KINECT? 如何在Silverlight上为堆栈面板创建鼠标悬停状态? - How can I create a Mouse Hover State on Silverlight for a Stack Panel? 我如何在屏幕上获取鼠标坐标? 以及如何使用string.format格式化坐标? - How do i get mouse coordinates on screen ? And how ot format the coordinates with string.format? 如何在form1鼠标输入事件中获取鼠标坐标X和Y? - How do i get the mouse coordinates X and Y in form1 mouse enter event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM