# Help with the Visual Basic MouseDown event

## Recommended Posts

I have this code:-

' this takes where the user clicked the board
' it takes the mouse button pressed, whether the shift button is down and the mouse coords
' it returns nothing
Private Sub imgBoard_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Dim nX As Double ' the real mouse x
Dim nY As Double ' the real mouse y

' get the real mouse coords
nX = (X / imgBoard.Width) * 10
nY = (Y / imgBoard.Height) * 10

' get the space that we are over
nX = 100 * (nX / BOARD_W_PREC)
nY = 100 * (nY / BOARD_H_PREC)

' output the space for validation reasons
MsgBox nX & " : " & nY

' the mouse is down
g_bMouseDown = True

End Sub ' imgBoard_MouseDown


What this code is supposed to do is output the percentage coords of the image that was clicked, ie. 30%, 62%. I'm sure I have the math correct, but then i'm not sure. The passed in mouse coords seem to be weird aswell. I have the form set to Pixels not Twips. Thank you [Edited by - rpg_code_master on June 5, 2005 3:36:39 PM]

Anybody?

##### Share on other sites
iMalc    2466
I wouldn't use a msgbox in your mousedown handler. Try a debug.print instead so that you don't have to release the mouse button to click the dialog button.

That piece of random advice aside, what's actually the problem?

##### Share on other sites
Well I have an image box with an image in it. What I want is the coordinates that were clicked in percentage form. i.e. If the user clicked in the middle, I would get the coords 50 50, if the user clicked in the top left somewhere, I would get 20 15 or whatever. The above code is what I have to compute that, but it doesn't work.

##### Share on other sites
Where are you getting the values for BOARD_W_PREC and BOARD_H_PREC?

##### Share on other sites
My recommendation would be to not try to use pixels to get mouse coordinates. There are two reasons for this:

1. The MouseDown event gets the mouse coordinates in twips anyway, no matter what the form's ScaleMode is. Maybe it doesn't make sense, but that's how it is.

2. If you try to use the scale values to translate the X, Y position from twips to pixels (as I tried initially), there's another problem: the title bar. ScaleHeight will contain the y-coordinate using the bottom of the title bar as 0, while the Form.Height is the y-coordinate from the top of the entire form (including title bar and border) as 0.

Getting Y coord in pixels with
Ypix = Y * Form1.ScaleHeight / Form1.Height
fails, since two different scales are in use. You would need to determine the height of the title bar, and account for that in the math:
Ypix = Y * Form1.ScaleHeight / (Form1.Height - title bar's height).

I don't know of a good way to get the title bar's height--remember that it will vary because of different Windows themes.

The solution is very easy by staying with twips:

Private Sub imgBoard_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)' this takes where the user clicked the board' it takes the mouse button pressed, whether the shift button is down and the mouse coords' it returns nothing    ' percentage is simple    MsgBox "X:  " & X & vbCrLf & "Y:  " & Y    MsgBox "Perc X: " & X * 100 / imgBoard.Width & vbCrLf & "Perc Y: " & Y * 100 / imgBoard.HeightEnd Sub ' imgBoard_MouseDown

##### Share on other sites
That would have been great if it would have worked.

Here is the application that I am having trouble with:-

Its in a VB 6 project file.

##### Share on other sites
Change frmMain's ScaleMode to twip.

Remember that MouseDown's (X,Y) values are always in twips, and there's not a good way that I know of to translate that (X,Y) values to pixels properly. The problem is the Y value in pixels will be off due to the title bar issue described last post.

##### Share on other sites
shmoove    821
Quote:
 Original post by Jojosh_the_Piand there's not a good way that I know of to translate that (X,Y) values to pixels properly.

Screen.TwipsPerPixelX and Screen.TwipsPerPixelY (from memory, so the actual names might be slightly different).

But I agree that your best bet is to stick with twips all the way, especially if you just want percentages.

shmoove