Solved

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

Posted on 2011-02-20
4
1,141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

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 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crystal reports login after closing form 3 36
WPF user control poped up from a window 1 35
Need help with a query 14 39
Import a excel sheet in a grid 2 13
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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