在特定点模拟单击C#Web浏览器

时间:2014-07-15 21:39:21

标签: c# .net webbrowser-control

我试图在特定点强行点击我的网页浏览器。

我使用以下代码:

    public void DoMouseClick(int X, int Y)
    {
        //Call the imported function with the cursor's current position
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
    }



[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    private const int MOUSEEVENTF_LEFTDOWN = 0x02;
    private const int MOUSEEVENTF_LEFTUP = 0x04;
    private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
    private const int MOUSEEVENTF_RIGHTUP = 0x10;

但是这段代码看起来根本不起作用,它不能点击那里?我也不确定这段代码是否真的会移动光学鼠标,我需要一些模拟点击的东西,而不是将鼠标移动到那里并进行点击。

我无法使用HTMLElement的东西,因为它不允许你点击元素中的特定坐标,我需要点击一个非常具体的位置。

任何人都可以帮助解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我得到了这个,我用它来做一些自动点击工具..我不能也不会因此而受到任何赞誉,我甚至认为我在堆栈上发现了代码..

[Flags]
public enum MouseEventFlags
{
    LeftDown = 0x00000002,
    LeftUp = 0x00000004,
    MiddleDown = 0x00000020,
    MiddleUp = 0x00000040,
    Move = 0x00000001,
    Absolute = 0x00008000,
    RightDown = 0x00000008,
    RightUp = 0x00000010
}

[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetCursorPos(int X, int Y);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(out MousePoint lpMousePoint);

[DllImport("user32.dll")]
private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

public static void SetCursorPosition(int X, int Y)
{
    SetCursorPos(X, Y);
}

public static void SetCursorPosition(MousePoint point)
{
    SetCursorPos(point.X, point.Y);
}

public static MousePoint GetCursorPosition()
{
    MousePoint currentMousePoint;
    var gotPoint = GetCursorPos(out currentMousePoint);
    if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
    return currentMousePoint;
}

public static void MouseEvent(MouseEventFlags value)
{
    MousePoint position = GetCursorPosition();

    mouse_event
        ((int)value,
         position.X,
         position.Y,
         0,
         0)
        ;
}

[StructLayout(LayoutKind.Sequential)]
public struct MousePoint
{
    public int X;
    public int Y;

    public MousePoint(int x, int y)
    {
        X = x;
        Y = y;
    }
}