Mouse location

NeverDie
11-24-2001, 10:42 PM
I would like to make a program that has a sort of selection idea to it. So basically, u click and hold and drag to where u want to stop selecting (like the windows desktop). How would I find the mouse position on the form?

robot313
11-24-2001, 11:23 PM
Try this:

<pre><font color=blue>Private Sub</font color=blue> Form_MouseMove(Button <font color=blue>As Integer</font color=blue>, Shift <font color=blue>As Integer</font color=blue>, X <font color=blue>As Single</font color=blue>, Y <font color=blue>As Single</font color=blue>)
Label1.Caption = X &amp; " by " &amp; Y
<font color=blue>End Sub</font color=blue></pre>

Volte
11-25-2001, 09:23 AM
In your case, doing what robot suggested is probably sufficient, but here's a tip:

If you ever need to get the position of the cursor on the form no matter where it is (i.e. on top of a control, etc), you can use this code:

Put this in the <font color=green>General Declarations</font color=green>.
<pre>-----------
<font color=blue>Private Declare</font color=blue> <font color=blue>Function</font color=blue> GetCursorPos <font color=blue>Lib</font color=blue> "user32" (lpPoint <font color=blue>As</font color=blue> POINTAPI) <font color=blue>As Long</font color=blue>
<font color=blue>Private Type</font color=blue> POINTAPI
X <font color=blue>As Long</font color=blue>
Y <font color=blue>As Long</font color=blue>
<font color=blue>End Type</font color=blue>
-----------</pre>Put this whereever you want to check for the position (timer, mousemove, etc).

<pre>-----------
<font color=blue>Dim</font color=blue> oCoords <font color=blue>As</font color=blue> POINTAPI

GetCursorPos oCoords

Me.Cls <font color=green> 'You replace these two lines with whatever you want to do with the coords.</font color=green>
Me.Print oCoords.X &amp; ", " &amp; oCoords.Y <font color=green>'Use oCoords.X and .Y to get the X and Y position.</font color=green>
-----------</pre>

Banjo
11-25-2001, 10:00 AM
Bear in mind that GetCursorPos returns the screen co-ordinates of the cursor and it uses Pixels not Twips.

Volte
11-25-2001, 10:04 AM
Ah yes,

*smack*

Oh well, it's still probably good for something. I was thinking GetCursorPos was for the form, and you used ClientToScreen to change it to screen coords.

NeverDie
11-25-2001, 02:55 PM
Alright, all very helpful. Thanx.

andrewo
11-29-2001, 07:04 AM
'just enter this into a form and run it
'The cls seems to slow my comp down bad..maybe i got no memory left or whatever, it all should work.

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Type POINTAPI
X As Long
Y As Long
End Type

Dim MousePoint As POINTAPI
Dim DragPointX, DragPointY
Dim MousePressed

Private Sub Form_Load()
Me.ScaleMode = 3
Me.WindowState = 2
Me.Show
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Do
MousePressed = MousePressed + 1
GetCursorPos MousePoint
ScreenToClient Form1.hwnd, MousePoint
If MousePressed = 1 Then
DragPointX = MousePoint.X
DragPointY = MousePoint.Y
End If
'Cls 'Causing me problems..slowing comp down bad..maybe i should restart...
Line (DragPointX, DragPointY)-(MousePoint.X, MousePoint.Y), vbGreen, B
DoEvents
Loop Until MousePressed = 0
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MousePressed = 0
End Sub

divil
12-04-2001, 05:17 AM
Just a general tip:

Twips suck, use pixels always

Banjo
12-04-2001, 09:43 AM
Unfortunately, VB uses twips for forms and controls external co-ordinate systems so you can not avoid it completely.

Teric
12-04-2001, 11:21 AM
Banjo, I've been able to switch the form to use pixels. Just set the scalemode to pixels and you're set.

On the other hand, what do you mean when you say "controls external coordinate systems?" Do you mean that there are external controls that force you to use twips?

Banjo
12-04-2001, 11:55 AM
With a form, the Top, Left, Height and Width properties are all in twips. You can not specify them in anything else. This also applies to postioning controls on a form as well. Their postion properties do not use the Form's scalemode.

divil
12-05-2001, 05:17 AM
Banjo -

You're right about a forms .left, .top, .width and .height properties always being in twips, however these are different from their .scalewidth counterparts even when scaled, to reflect the fact that forms have titlebars and margins.

If the forms scalemode in pixels, then the dimensions/positions of child controls on that form will be in pixels.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum