質問

(Maybe there is a topic in this, but unfortunately I can't find it)

How can I find mouse coordinate dependent to windows form ?

Below code in c# can show mouse coordinate :

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {

        label1.Text = string.Format("X: {0} , Y: {1}", Cursor.Position.X, Cursor.Position.Y);
    }

But this code show the coordinate in whole screen, when I want the coordinate (0,0) be top-left side of my form!

And I know that i can use Cursor.Position and PointToClient , but I cant solve my problem with them. can any one help me with several lines code?

(I use windows form)

役に立ちましたか?

解決

In the MouseEventArgs passed to your event there is a property called Location.
According to MSDN

A Point that contains the x- and y- mouse coordinates, in pixels, relative to the upper-left corner of the form.

private void Form1_MouseMove(object sender, MouseEventArgs e)
{

    label1.Text = string.Format("X: {0} , Y: {1}", e.Location.X, e.Location.Y);
}

Does it solve your problem?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top