Solved

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

Posted on 2011-02-20
4
1,116 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you huge for the example!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

9 Experts available now in Live!

Get 1:1 Help Now