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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving the Mouse Pointer using VB 1

Status
Not open for further replies.

Hanss

Technical User
Feb 15, 2001
85
CH
I would like to move the mouse pointer using VB. I have a form named "form1" and a button named "button1.

When I click button1, I would like the mouse pointer to move down about 1 inch. If this is not possible, or too difficult I would also be happy if the cursor would move to a specific field, label or rectangle in form1 when button1 is clicked.

Any help would be greatly appreciated!

hanss
Zurich Switzerland
 
Not too sure what you hope to accomplish here, but in your button click code, set the last line to be:

Me.nameofcontrol.SetFocus

So Me.Textbox1.SetFocus would cause Textbox1 on the form to get the cursor when the button is clicked.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Thank you very much for your fast reply! I just tried it but while the cursor goes to textbox1 the mouse pointer does not move...

kind regards,
Hanss
 
That is what I thought you wanted to accomplish.

My first question is why? This seems to a bit "cumbersome" and "intrusive" to the user. If you are trying to automated mouse clicks for some process...there are better ways of accomplishing that. A development tool called AutoIt comes to mind.

My second statement is that this can be done, but it requires quite a bit of coding, to take advantage of API calls and the like.... I have not done it myself, but I am sure you can find examples either here or on the web directly.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
The average user would be rather annoyed at their mouse pointer moving by itself. I know I would be.

I would suggest using tab stops as the way to move from control to control, or the SetFocus method. That's the standard that everyone is used to.

 
I found the following code entitled "Set the Mouse Cursor Position" and pasted it into the code of form1 but when I click on button1 I get an error message at:


x = CLng(xPos / .TwipsPerPixelX)


Here is the code:

Option Compare Database
Option Explicit

Private Type POINTAPI
x As Long
y As Long
End Type

Private Declare Function ClientToScreen Lib "user32" _
(ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Declare Function SetCursorPos Lib "user32" _
(ByVal x As Long, ByVal y As Long) As Long
Public Function SetCursorPosition(Window As Object, xPos As _
Long, yPos As Long) As Boolean

'AUTHOR: FreeVBCode.com (
'Usage: Window = window for which you want
'to set the cursof for (form or control)

'window must support hwnd property; function will
'return false if it doesn't (e.g., won't work
'with labels)

'x = xPosition in twips
'y = yPosition in twips

'if you are not using twips as your scale mode
'you must convert the value to pixels from your
'metric. There is a function on FreeVBCode.com
'that does this, under the System/API
'category

On Error GoTo errorhandler

Dim x As Long, y As Long
Dim lRet As Long
Dim lHandle As Long
Dim typPoint As POINTAPI

lHandle = Window.hwnd
With Screen
x = CLng(xPos / .TwipsPerPixelX)
y = CLng(yPos / .TwipsPerPixelY)
End With

typPoint.x = x
typPoint.y = y

lRet = ClientToScreen(lHandle, typPoint)
lRet = SetCursorPos(typPoint.x, typPoint.y)


SetCursorPosition = (lRet <> 0)

Exit Function

errorhandler:

SetCursorPosition = False
Exit Function



Private Sub button1_Click()

Call SetCursorPosition(Forms.form1, 5000, 5000)

End Sub
 
Thank you all for your ideas! mstrmage1768 gave me the tip I needed - AutoIt. Works great.

Many Thanks!

hanss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top