2017-06-08 47 views
-2

我知道,有很多像这样的问题。但是我读到的每一个答案都没有帮助我。所以我在这里问。如何执行双击?

我想执行鼠标双击。在此之前,我已经设置光标到正确的位置:

Cursor.Position = new System.Drawing.Point(2225, 154); 

我已经使用了几乎每一个类,我发现在这里,没有人帮助我。 (光标已设置,但未执行双击操作。)

希望任何人都可以帮助我。谢谢!

电贺

编辑: 的我用类:

using System; 
using System.Runtime.InteropServices; 

namespace AutoClicker 
{ 

    public class MouseSimulator 
    { 

     public void test_Click(System.Drawing.Point p) 
      { 
      //Move the mouse to the button position 

      //Perform button click. 
      INPUT structInput = new INPUT(); 
      structInput.type = SendInputEventType.InputMouse; 
      structInput.mkhi.mi.dwFlags = MouseEventFlags.ABSOLUTE | MouseEventFlags.LEFTDOWN | MouseEventFlags.LEFTUP; 
      structInput.mkhi.mi.dx = p.X; 
      structInput.mkhi.mi.dy = p.Y; 
      uint i = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT())); 
     } 

     [DllImport("user32.dll")] 
     static extern IntPtr GetMessageExtraInfo(); 

     [DllImport("user32.dll", SetLastError = true)] 
     static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize); 

     [DllImport("user32.dll")] 
     static extern bool SetCursorPos(int X, int Y); 

     [Flags] 
     public enum MouseEventFlags 
     { 
      LEFTDOWN = 0x00000002, 
      LEFTUP = 0x00000004, 
      MIDDLEDOWN = 0x00000020, 
      MIDDLEUP = 0x00000040, 
      MOVE = 0x00000001, 
      ABSOLUTE = 0x00008000, 
      RIGHTDOWN = 0x00000008, 
      RIGHTUP = 0x00000010 
     } 

     /// <summary> 
     /// The event type contained in the union field 
     /// </summary> 
     enum SendInputEventType : int 
     { 
      /// <summary> 
      /// Contains Mouse event data 
      /// </summary> 
      InputMouse, 
      /// <summary> 
      /// Contains Keyboard event data 
      /// </summary> 
      InputKeyboard, 
      /// <summary> 
      /// Contains Hardware event data 
      /// </summary> 
      InputHardware 
     } 


     /// <summary> 
     /// The mouse data structure 
     /// </summary> 
     [StructLayout(LayoutKind.Sequential)] 
     struct MouseInputData 
     { 
      /// <summary> 
      /// The x value, if ABSOLUTE is passed in the flag then this is an actual X and Y value 
      /// otherwise it is a delta from the last position 
      /// </summary> 
      public int dx; 
      /// <summary> 
      /// The y value, if ABSOLUTE is passed in the flag then this is an actual X and Y value 
      /// otherwise it is a delta from the last position 
      /// </summary> 
      public int dy; 
      /// <summary> 
      /// Wheel event data, X buttons 
      /// </summary> 
      public uint mouseData; 
      /// <summary> 
      /// ORable field with the various flags about buttons and nature of event 
      /// </summary> 
      public MouseEventFlags dwFlags; 
      /// <summary> 
      /// The timestamp for the event, if zero then the system will provide 
      /// </summary> 
      public uint time; 
      /// <summary> 
      /// Additional data obtained by calling app via GetMessageExtraInfo 
      /// </summary> 
      public IntPtr dwExtraInfo; 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     struct KEYBDINPUT 
     { 
      public ushort wVk; 
      public ushort wScan; 
      public uint dwFlags; 
      public uint time; 
      public IntPtr dwExtraInfo; 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     struct HARDWAREINPUT 
     { 
      public int uMsg; 
      public short wParamL; 
      public short wParamH; 
     } 

     /// <summary> 
     /// Captures the union of the three three structures. 
     /// </summary> 
     [StructLayout(LayoutKind.Explicit)] 
     struct MouseKeybdhardwareInputUnion 
     { 
      /// <summary> 
      /// The Mouse Input Data 
      /// </summary> 
      [FieldOffset(0)] 
      public MouseInputData mi; 

      /// <summary> 
      /// The Keyboard input data 
      /// </summary> 
      [FieldOffset(0)] 
      public KEYBDINPUT ki; 

      /// <summary> 
      /// The hardware input data 
      /// </summary> 
      [FieldOffset(0)] 
      public HARDWAREINPUT hi; 
     } 

     /// <summary> 
     /// The Data passed to SendInput in an array. 
     /// </summary> 
     /// <remarks>Contains a union field type specifies what it contains </remarks> 
     [StructLayout(LayoutKind.Sequential)] 
     struct INPUT 
     { 
      /// <summary> 
      /// The actual data type contained in the union Field 
      /// </summary> 
      public SendInputEventType type; 
      public MouseKeybdhardwareInputUnion mkhi; 
     } 
    } 

} 

,我用它像这样:

System.Drawing.Point p = new System.Drawing.Point(2225, 154); 
     Cursor.Position = p; 
     MouseSimulator ms = new MouseSimulator(); 
     ms.test_Click(p); 
+0

你只显示代码显示,你改变了位置,你如何执行你双击?你是怎么试着去的? –

+1

使用SendInput()。如果您在短时间内使按钮上下两次,并且应用程序愿意识别它,则会生成双击。 –

+0

我已经添加了我使用的课程和我使用的方式。 –

回答

0

如果您需要TI模拟双击,你需要使用SendInput()发送4 INPUT

public void test_Click(System.Drawing.Point p) { 
    //Move the mouse to the button position 

    //Perform button click. 
    INPUT down = new INPUT(); 
    down.type = SendInputEventType.InputMouse; 
    down.mkhi.mi.dx = p.X; 
    down.mkhi.mi.dy = p.Y; 
    down.mkhi.mi.dwFlags = MouseEventFlags.LEFTDOWN; 

    INPUT up = new INPUT(); 
    up.type = SendInputEventType.InputMouse; 
    up.mkhi.mi.dx = p.X; 
    up.mkhi.mi.dy = p.Y; 
    up.mkhi.mi.dwFlags = MouseEventFlags.LEFTUP; 

    INPUT[] dbl = {down, up, down, up}; 

    SendInput(4, dbl, Marshal.SizeOf(down)); 
} 

此外,定义SendInput()如下:

static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize); 
+0

@DavidHeffernan修订。试图做一个简单的修补程序:第 –