Solved

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

Posted on 2011-02-20
4
1,128 Views
Last Modified: 2012-05-11
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
Comment
Question by:Valleriani
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34937754
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
 
LVL 7

Author Comment

by:Valleriani
ID: 34938089
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 34938559
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
 
LVL 7

Author Closing Comment

by:Valleriani
ID: 34938596
Thank you huge for the example!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now