OsiriX
OsiriX

Reputation: 400

Get position of mouse in form VB6

In a program I'm writing, I need the position of the mouse absolute to the left upper corner of the form. I'm using this code:

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

    Debug.Print "x: " & X & " - y: " & Y

End Sub

When I use this code, the upper left corner has coordinates 0,0. But the problem is that the values are 15 times too big when I move inside the form.

So that's why I used:

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

    Debug.Print "x: " & X / 15 & " - y: " & Y / 15

End Sub

This gives the correct coordinates, but why do I need to devide this by 15? I'm not sure if this code will be compatible on other systems.

Upvotes: 0

Views: 9472

Answers (2)

Daniel
Daniel

Reputation: 13142

Looking at the documentation for MouseMove. The X and Y values returned correspond to the "ScaleHeight, ScaleWidth, ScaleLeft, and ScaleTop properties of the object."

Therefore looking at the documentation for ScaleHeight, ScaleWidth and for ScaleLeft, ScaleTop, it is clear that you can dictate how the X and Y coordinates are determined. You are not limited to Twips or Pixels, but can use whatever numbering system you dictate.

Here's a quote from the ScaleHeight, ScaleWidth page:

For example, the statement ScaleHeight = 100 changes the units of measure of the actual interior height of the form. Instead of the height being n current units (twips, pixels, ...), the height will be 100 user-defined units. Therefore, a distance of 50 units is half the height/width of the object, and a distance of 101 units will be off the object by 1 unit.

In this regard to assure your results match your expectation, you can tell the form exactly how many user-defined units it contains.

Upvotes: 5

Alex K.
Alex K.

Reputation: 175956

The units are in Twips (runtime conversion factor: screen.TwipsPerPixelX and Y). You can also change the forms ScaleMode to use Pixels.

Upvotes: 2

Related Questions