Go Premium for a chance to win a PS4. Enter to Win


How to determine xy coordinate within image

Posted on 2002-06-28
Medium Priority
Last Modified: 2007-11-27
i load an image into form by loadpicture. after the image has been loaded, i want to determine the xy coordinate of the location within the image where i point the mouse. how can i do that.

the image is a map. with the coordinate i can link with my database to describe details of what i point to. Example when i point to a building in the map, base on the coordinate my program will read databse and return the building details.

Question by:azsoft
  • 3
  • 2
  • 2
  • +5

Accepted Solution

marconovaro earned 200 total points
ID: 7115752

Just use the mouseMove event:

(e.g. for a PictureBox)

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

the X and Y parameters tell you the position of the mouse related to the upper-left corner of the picturebox itself.
LVL 54

Expert Comment

by:Ryan Chong
ID: 7115753

Try use GetCursorPos API to get the mouse position.

LVL 17

Expert Comment

ID: 7115833
The answer is simple, no need for API calls, try this:

Option Explicit
Dim StoreX
Dim StoreY
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    StoreX = X
    StoreY = Y
End Sub

Private Sub Form_Click()
    'Now you hace you co-ordinates in
    'StoreX and StoreY
End Sub
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 17

Expert Comment

ID: 7115840
Take my advice when working with graphics ALWAYS set your form scalemode to use pixels. But be warned there is a bug in VB some of the mousemove/drag & drop co-ordinates  should return pixels but sometimes they return twips so always check and convert if necessary.


Expert Comment

ID: 7115927
as the experts have told you the mouse events give you the coordinates of the mouse
to know whats under the mouse you need the coordinates of the contour of every building on your form and determine if the x,y fals within that surface
very powerfull and very difficult to implement
but maybe you can determine what building the mouse is on by its color
if so its easier to use the point (x,y) method wich gives you the color under the mouse
easier still and what i usually do for such things, is make 'hotspots' on the form
a hotspot is simply a transparent label
a label has all the events you need,and as an added bonus it has a tooltiptext property so you can even show a small description when the mouse moves over it
LVL 16

Expert Comment

ID: 7116364

Author Comment

ID: 7117847
Hotpots using label is a good idea and it will simplified the coding.
but how can i load a label onto image during runtime not during design.
LVL 17

Expert Comment

ID: 7118164
The coding to find where you are is not so hard.  I created a marina system where the berths were on a map. To find a location simply loop through an array.

I used a UDT (user defined type) to fold the coordinates.  This is where I discovered that twips are a problem.

' Shared Berth Details
Public Type BerthDef
    Top As Single
    Left As Single
    Width As Single
    Height As Single
    item As Long ' record number in database
    MainID as Long
End Type
Global Berths() As BerthDef
Global ActualPosBerths() As BerthDef
Global BerthsCount As Long

The click routine went like this:

cberth = -1
For bc = 0 To BerthsCount
    If MouseX >= Berths(bc).Left And MouseX <= Berths(bc).Left + Berths(bc).Width Then
        If MouseY >= Berths(bc).Top And MouseY <= Berths(bc).Top + Berths(bc).Height Then
            ' They clicked on a berth
            ' launch form to display the Berth data
            cberth = bc
            Dim frm As Form
            Set frm = New frmBerth
            frm.pBerth = Berths(bc).id
            Exit Sub
        End If
    End If
Next bc


1) You need to hold 2 copies of the co-ordinates, 1 is loaded from a database and the other is set-up in the form_resize event.  So that as your picture is resized you chane the ordinates by proprtion of the full image to the displayed image.

Factor = CurrentWidth/LoadedWidth
For bc = 0 To BerthsCount
    Berths(bc).Width=ActualPosBerths(bc).Width * Factor


2) The co-ordinates of each object may be defined more than once becuase it may go round corneres.

I used a mousedown and mouseup and the mousemove events to aquire a new object.  I drew the object with 4 line objects.

So the mousedown recorder the start position and set an indicator Mousedown=True, if the mousemove detected the mouse was down the lines could be displayed.  I also used drag & drop to copy similar objects.

3) In my case the objects were simplefied because they were running only north and south but in your case this may be different. An object could be made from a series of boxes that have been squished over parallelogram or tapeezoid. So you need to extend your click routine as follows:

First check that the mouse is between the upper and lower co-ordiantes and beweet the right and right coordinates this only takes just a split second (as shown in the previous example.

You then need to check the gradient of the vertical lines and make sure the the click is on or to the right of the left line and on or to the left of the left line.

So if topleft coordinates are tx and ty and the bottom left coordiantes are bx and by:

Height of object is:

h = ty - by

The line offset is from north south is:

oset = tx - bx

So did they click to the right of the line?

So the gradient of the line is:

g = h / oset

Now calculate the gradient of the mouse click

hm = mousey - by
moset = mousex - bx

mg = hm / mouset

if mg >= G then
   msgbox "Clicked on the line or to the right"
   msgbox "they did not click in the squished box"
end if

So you now ned to use the same principle to check the right edge.

Hope this helps,

still inthedark.....

LVL 54

Expert Comment

by:Ryan Chong
ID: 7121331
See this too:

Dim WithEvents NewMenuItem As VB.Label:

Expert Comment

ID: 7121700
or just place a label1 on your form at design time and give it index 0
and at runtime:
for i=1 to numberoflabelsneeded
    load label1(i)
and in:
Private Sub Label1_Click(Index As Integer)
        select case index
               case 0
                    msgbox "this is building0"
               case 1
                    msgbox "this is building1"
        end select

End Sub

LVL 49

Expert Comment

ID: 7964660
Hi azsoft,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept marconovaro's comment(s) as an answer.

azsoft, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer

Expert Comment

ID: 8022542
Per recommendation, force-accepted.

EE Admin

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

916 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