Results 1 to 5 of 5

Thread: [RESOLVED] MousePosition to use stage as 0, 0 position, not screen.

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    9

    Resolved [RESOLVED] MousePosition to use stage as 0, 0 position, not screen.

    Hey,

    What I'm trying to do is to get the X and Y position of mouse but not starting from the screen upper left corner but the stage's upper left corner.

    right now all I got is:
    Code:
    mouseCoordsLabel.Text = ("X: " & MousePosition.X - Me.Location.X & "     Y: " & MousePosition.Y - Me.Location.Y)
    But it gets the upper left corner of form, not stage.

    Is there any way I could get the width and height of the borders that the form uses?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: MousePosition to use stage as 0, 0 position, not screen.

    You can use any control's PointToClient method to convert a Point relative to the screen origin into a Point relative to the control's origin. PointToScreen does the opposite.

    The Bounds property of a form will give you the Rectangle occupied by the form including non-client area. The ClientRectangle property will give you the Rectangle occupied by the form excluding the non-client area.

    Note that a form's origin with regards to the PointToClient and PointToScreen methods is the upper-left corner of its client area.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    9

    Re: MousePosition to use stage as 0, 0 position, not screen.

    Thanks. I don't quite understand the PointToClient method. I tried to use it but failed. Could someone possibly make me a simple example?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: MousePosition to use stage as 0, 0 position, not screen.

    Quote Originally Posted by ecugetsone View Post
    Thanks. I don't quite understand the PointToClient method. I tried to use it but failed. Could someone possibly make me a simple example?
    Rather than posting "I tried, I failed, can you do it for me", I would suggest that you post "I tried, this is what I did, can you help me fix it". If you want an example, the MSDN documentation for the method includes one, which is often the case with documentation.

    As for how it works, consider a big rectangle whose dimensions are 100 across and 200 down. Now consider a smaller rectangle inside that whose upper left corner is at (20, 40) and whose dimensions are 50 across and 40 down. All PointToClient does is translate from the outer rectangle, which is the screen, to the inner rectangle, which is your control. (0, 0) in the inner rectangle is at (20, 40) in the outer rectangle, so any time you call PointToClient all you're doing is subtract 20 from X coordinate and 40 from the Y. Call PointToClient and pass (20, 40) and you get (0, 0). Pass (40, 100) and you get (20, 60).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    9

    Re: MousePosition to use stage as 0, 0 position, not screen.

    Thanks,
    And to anyone who might randomly find this topic having the same problem. Here is how I fixed it:

    Code:
    Dim mousePoint As Point
    ...
    mousePoint = PointToClient(Cursor.Position)
    mouseCoordsLabel.Text = ("X: " & mousePoint.X.ToString & "     Y: " & mousePoint.Y.ToString)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width