Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

c# - 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. I'm a begginer and I don't have any experience with events. Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...