Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1189
  • Last Modified:

VB.NET 2010 - Way to get position of mouse depending on the control?

Is there a possible way to get the relative position of a mouse depending on what location is clicked in a picturebox?

Please take a look at my image. I have one image that changes depending on where you can go in the current room. North/South/East/West, and another one with Up/Down. They go 'blue' when you can go that direction. They are both pictureboxes.

What I mainly want is to get the position of a mouse click when I click in that picturebox. The directional pad in the image is 70x70. If I click where the blue arrow is, in the middle it might return me a value of 35 left 5 top as the result. So giving me the mouse position depending on the size of the actual picturebox.

I do know splitting the image is a possibility, but I want to try to refrain from doing that (some reasons like customizing clients/etc and its easier method for now.)

Thank you!
buttons.png
0
Valleriani
Asked:
Valleriani
  • 2
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Define the triangle as a series of 3 points and add it to a GraphicsPath with AddPolygon().  Now you can use the current mouse position and GraphicsPath.IsVisible() to determine if the mouse is inside the triangle.
0
 
VallerianiAuthor Commented:
Hello Idle_Mind,

Is there any websites I can read up on for this? I haven't used 'GraphicsPath'/AddPolygon() before, so I'm not sure how I should be handling this with a picturebox yet.

Thank you!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could use code as simple as this to figure out the coordinates of your triangle(s):
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        Label1.Text = "(" & e.X & ", " & e.Y & ")"
    End Sub

Open in new window


Let's say your three points were:
(100,135)
(155,60)
(190,135)

You could check if the mouse was clicked down in the triangle using code like this:
Public Class Form1

    Private GP As New System.Drawing.Drawing2D.GraphicsPath

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GP.AddPolygon(New Point() {New Point(100, 135), New Point(155, 60), New Point(190, 135)})
    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If GP.IsVisible(New Point(e.X, e.Y)) Then
            Debug.Print("Within Triangle")
        Else
            Debug.Print("Outside Traingle")
        End If
    End Sub

End Class

Open in new window


Simliar code could be used in the MouseMove() event to trigger a visual change in the triangle for user feedback.
0
 
VallerianiAuthor Commented:
Thank you huge for the example!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now