Sending a Mouse Click to a Specific Position without Moving the Mouse

谁说胖子不能爱 提交于 2019-12-05 15:18:28

You didn't include the MOUSEEVENTF_MOVE flag, so the cursor isn't going to move.

I would recommend making 3 distinct calls (move, down, up), doing everything at the same time is iffy. But might work, experiment if you really want to cut that down. Btw, SendInput is what the SDK recommends you use.

You're probably sending the mouse click to the wrong location. mouse_event takes the X and Y values in mickeys, as described in the documentation here.

Try converting your location with something like this:

private static Point ScreenLocationToMickeyLocation( Point mouseLocation )
{
    Rectangle virtualScreen = SystemInformation.VirtualScreen;

    return new Point(
        ( ( mouseLocation.X - virtualScreen.X ) * 65535 ) / virtualScreen.Width,
        ( ( mouseLocation.Y - virtualScreen.Y ) * 65535 ) / virtualScreen.Height );
}

You might want to try capturing the current location of the mouse, move the mouse to where you want it to click, click the location,and then have the mouse move back. If this is done fast enough it appears like the mouse didn't move.

visual basic 2015 community declaration

Imports System.Runtime.InteropServices

<Flags()>
Public Enum MouseEventFlags As UInteger
    MOUSEEVENTF_ABSOLUTE = &H8000
    MOUSEEVENTF_LEFTDOWN = &H2
    MOUSEEVENTF_LEFTUP = &H4
    MOUSEEVENTF_MIDDLEDOWN = &H20
    MOUSEEVENTF_MIDDLEUP = &H40
    MOUSEEVENTF_MOVE = &H1
    MOUSEEVENTF_RIGHTDOWN = &H8
    MOUSEEVENTF_RIGHTUP = &H10
    MOUSEEVENTF_XDOWN = &H80
    MOUSEEVENTF_XUP = &H100
    MOUSEEVENTF_WHEEL = &H800
    MOUSEEVENTF_HWHEEL = &H1000
End Enum

Dim pntapi As POINTAPI
Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (ByRef lpPoint As POINTAPI) As Integer
Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal X As Integer, ByVal Y As Integer) As Integer
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As Integer)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!