问题
I'm looking to send a mouse click to a specific position on a screen without affecting the mouse cursor's location.
I've scoured and have tried everything under the sun with mouse_event (which is supposed to do this)
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
However this ends up only sending a mouse click to wherever the position of the mouse is. So I end up having to move the mouse to get it to work.
Cursor.Position = new Point(X, Y);
Anyone have any idea how I can do this WITHOUT having to move the mouse? I've also tried the following code block with no success as it still only clicked where the mouse cursor position was which really threw me for a loop as I figured this would have worked...
Cursor.Position = new Point(X, Y);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
Cursor.Position = new Point(oldX, oldY);
Thanks in advance for any help!
回答1:
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.
回答2:
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 );
}
回答3:
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.
回答4:
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)
来源:https://stackoverflow.com/questions/4204566/sending-a-mouse-click-to-a-specific-position-without-moving-the-mouse