How can I get the mouse coordinates related to a panel?

后端 未结 2 774
小蘑菇
小蘑菇 2020-12-20 04:02

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 exp

相关标签:
2条回答
  • 2020-12-20 04:37

    If your are using Windows Forms then 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 讨论(0)
  • 2020-12-20 04:43

    You must subscribe to event of Panel control - Click event. You can write the code below within Form's contructor:

        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

    0 讨论(0)
提交回复
热议问题