如何双击光标位置

时间:2014-06-18 16:19:34

标签: 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

SetCursorPos(2681,792)
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

确保导入正确的API函数并设置常量。

Public Declare Function SetCursorPos Lib "User32.dll" Alias "SetCursorPos" (ByVal x As Integer, ByVal y As Integer) As Boolean
Public Declare Sub mouse_event Lib "User32.dll" Alias "mouse_event" (dwFlags As UInt32, dx As UInt32, dy As UInt32, dwData As UInt32, dwExtraInfo As Int32)

Public Const MOUSEEVENTF_ABSOLUTE As UInt32 = &HE8000
Public Const MOUSEEVENTF_LEFTDOWN As UInt32 = &HE0002
Public Const MOUSEEVENTF_LEFTUP As UInt32 = &HE0004
Public Const MOUSEEVENTF_HWHEEL As UInt32 = &HE1000
Public Const MOUSEEVENTF_MIDDLEDOWN As UInt32 = &HE0020
Public Const MOUSEEVENTF_MIDDLEUP As UInt32 = &HE0040
Public Const MOUSEEVENTF_MOVE As UInt32 = &HE0001
Public Const MOUSEEVENTF_RIGHTDOWN As UInt32 = &HE0008
Public Const MOUSEEVENTF_RIGHTUP As UInt32 = &HE0010
Public Const MOUSEEVENTF_WHEEL As UInt32 = &HE0800
Public Const MOUSEEVENTF_XDOWN As UInt32 = &HE0080
Public Const MOUSEEVENTF_XUP As UInt32 = &HE0100

Sub Main()
    SetCursorPos(2681, 792)
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub

这些都在MSDN文档中找到:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms648394(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646260(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/172wfck9.aspx

答案 1 :(得分:1)

这将做你想要的。

Imports System
Imports System.Threading

Public Class Form1
    Private Declare Function apimouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32) As Boolean
    Private Const MOUSEEVENTF_LEFTDOWN As Integer = 2
    Private Const MOUSEEVENTF_LEFTUP As Integer = 4
    Private Const MOUSEEVENTF_RIGHTDOWN As Integer = 8
    Private Const MOUSEEVENTF_RIGHTUP As Integer = 16

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' move cursor to 200,200 and double-click
        move_and_dblclick(200, 200)
    End Sub

    Private Sub move_and_dblclick(ByVal x As Integer, ByVal y As Integer)
        Windows.Forms.Cursor.Position = New Point(x, y)
        apimouse_event((MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP), x, y, 0, 0)
        Thread.Sleep(150)
        apimouse_event((MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP), x, y, 0, 0)
    End Sub
End Class