如何在vb.net嵌入式浏览器中调用鼠标单击?

时间:2015-06-09 10:03:26

标签: vb.net

我有一个带嵌入式浏览器的VB.Net表单,想要模拟鼠标点击。好的,这个问题已被提出,:我必须使用坐标,不能使用元素。

目标元素是一个flash元素,它会点击 点击它,而不仅仅是点击

答案如此长,只需指出如何触发元素的一般点击。在这种情况下,这不是很有用。

1 个答案:

答案 0 :(得分:0)

this Stack Overflow answer开始,然后按照Nick提供的链接了解所涉及的API调用。链接的示例位于C#中,但如果您愿意,可以使用Internet上的任何代码转换器将其转换为vb.net。

以下是从MSDN Social post借来的vb.net代码。它似乎有你需要的所有例子。

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
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up

SetCursorPos(X,Y) ' Where X and Y are in pixel
'To get cursor location
Dim tempPos As Point
Dim R As Long = GetCursorPos(tempPos) ' You'll get your location in TempPos

'Left click
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
'Right click
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
'Middle Click
mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)

'To simulate drag and drop:
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
SetCursorPos(X,Y) ' Where X and Y are in pixel
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)