?
Solved

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

Posted on 2011-02-20
4
Medium Priority
?
1,167 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 2000 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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 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