Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Click on a Picturebox to acquire points

Status
Not open for further replies.

DeeDee

Programmer
Jul 13, 2000
34
US
I am working on a project where I need to be able to click on a map (which I have in a picturebox) and get the point that I clicked on. Is there any way to do this using VB? Thanks for any help in advance!
 
It might be a bit of a low tech solution but you could place frames or invisble image or pictureboxes on top of your picture and depending on where the user clicks on you picture they are actually cliclking on an invisible control which has been programmed to get the required point - I hope I've explained that properly....
 
Hi DeeDee[/b}
what do your mean by 'get'? the coordinates? the color of the pixel?

This will give you both:
----------------------------------------------------------
Private Declare Function GetPixel Lib "gdi32.dll" (ByVal hdc As Long, ByVal nXPos As Long, ByVal nYPos As Long) As Long

Private Sub Form_Load()
Picture1.ScaleMode = vbPixels
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
MsgBox " You clicked at: " & CStr(x) & " " & CStr(y)
MsgBox " the color of the pixel you clicked is " & CStr(GetPixel(Picture1.hdc, CLng(x), CLng(y)))
End Sub
----------------------------------------------------------

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
I don't need the color of the pixel. I need the coordinates of where the user clicked to fly a simulation.
 
Sunaj's answer gives you just that! Let me know if this helps
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
That worked beautifully!!! Thank you!! Sunaj, do you know if there is a way to actually set the corner coordinates of the picture in the picturebox?
 
I guess the best method depends on what you want to do....
What do you want to do?

You can load the picture from a file into a certain position:
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Picture1.PaintPicture LoadPicture("c:\tmp\jdraw.bmp"), X, Y
End Sub

Or you can use the Bitblt to move a picute/ part of a picture around:
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
I have different maps that will be opened in the picturebox from a file. I need the latitude/longitude coordinates of the map to be the points that the user is clicking on. I don't even know if that is possible or not. That's why I need to be able to set the corner coordinates so they equal the lat/long of the map that is open in the picbox. Does that make sense??
 
DeeDee
That is indeed possible. However, you need to know which project the map is draw in.
The simples is a latitude-longitude plot (but it is rarely used because the distortion for high latitudes is large). If you know which projection is used, I might be able to help you.

Solutiuon for the lat-lon projection:
**************
Dim MaxX As Integer, MaxY As Integer, MinX As Integer, MinY As Integer 'the pixel coordinates of the map
Dim alon As Single, alat As Single, blat As Single, blon As Single 'the geographic coordinates of the map

'set the 8 parameter to their respective values here...

'The solution is 2 linear equations, calculate the coefficients:
alon = (MinX - MaxX) / (MinLon - MaxLon)
blon = MaxX - alon * MaxLon


'Calculate lon from coordinate
Public Function GetLon(X As Single) As Single
GetLon = (X - blon) / alon
End Function

'Calculate lat from coordinate
Public Function GetLat(Y As Single) As Single
GetLat = (Y - blat) / alat
End Function
************
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
I'm still sort of confused. Where does the MinX & MaxX come from? Thank you so much for your help!!
 
I just realized that I have to plug in the MinX & MaxX. Let me give the coordinates of the map:

NW - 35~30'00'' N
117~0'00'' W

NE - 35~30'0'' N
116~30'00'' W

SW - 35~0'00'' N
117~0'00'' W

SE - 35~0'00'' N
116~30'00'' W
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top