Domanda

I am working with mouse code. I am trying to move a mouse to an external application point or make it go to button from external application? For example, move it to a windows calculator button and press it with a program.

Is there a way to invoke a button press on an external application or change the focus to another application using vb?

È stato utile?

Soluzione

If you know the position of the button to click, lets say x,y in screen coordinates you can do:

Public Declare Auto Function SetCursorPos Lib "User32.dll" _
       (ByVal X As Integer, ByVal Y As Integer) As Long
Public Declare Auto Function GetCursorPos Lib "User32.dll" _
       (ByRef lpPoint As Point) As Long
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" _
      (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, _
       ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

SetCursorPos(x, y) 'moves cursor to x,y position

mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) 'Invoke mouse down event
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 'Invoke mouse up event

The mouse down and mouse up events simulate the click of the button. Hope that helps

valter

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top