3

嗯,首先,我想要做的是单击一个 Flash 对象内的一个特定点,在一个 webbrowser 控件内。我不确定它为什么不起作用,但我似乎无法单击任何窗口,无论是记事本还是实际程序。

这是我正在使用的代码。

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(String sClassName, String sAppName);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

    public IntPtr find()
    {
        return this.Handle;//FindWindow("", "Form1");
    }

    public enum WMessages : int
    {
        WM_LBUTTONDOWN = 0x201, //Left mousebutton down
        WM_LBUTTONUP = 0x202,   //Left mousebutton up
        WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
        WM_RBUTTONDOWN = 0x204, //Right mousebutton down
        WM_RBUTTONUP = 0x205,   //Right mousebutton up
        WM_RBUTTONDBLCLK = 0x206, //Right mousebutton do
    }

    private int MAKELPARAM(int p, int p_2)
    {
        return ((p_2 << 16) | (p & 0xFFFF));
    }

    /** This is the non-working code **/
    public void DoMouseLeftClick(IntPtr handle, Point x)
    {
        SendMessage(handle, (int)WMessages.WM_LBUTTONDOWN, 0, MAKELPARAM(x.X, x.Y));
        SendMessage(handle, (int)WMessages.WM_LBUTTONUP, 0, MAKELPARAM(x.X, x.Y));

        return;

        //I have tried PostMessage, and SendMessage, and both of them at the same time, and neither works.

        PostMessage(handle, (uint)WMessages.WM_LBUTTONDOWN, 0, MAKELPARAM(x.X, x.Y));
        PostMessage(handle, (uint)WMessages.WM_LBUTTONUP, 0, MAKELPARAM(x.X, x.Y));
    }


    private void timer2_Tick(object sender, EventArgs e)
    {
        //I try hovering my mouse over a button I added to the form, and nothing happens.
        DoMouseLeftClick(find(), Cursor.Position);
    }

所以,我尝试过使用 PostMessage 和 SendMessage,但它们似乎都不起作用。我需要做的就是点击一个特定的点。

另外,我需要提到我不能使用mouse_event,因为据我所知,窗口需要处于活动状态,而且光标需要在您单击的点上方。我正在制作一个自动在 Flash 对象中执行进程的机器人,所以这就是我不能使用 mouse_event 的原因。

谢谢你们的帮助。

4

2 回答 2

0

我有同样的问题。

我试图在 MS Paint 中画一些东西。原来我是在点击主窗口,但结果是 MS Paint(和大多数应用程序)由许多子窗口组成,而您实际上想要点击子窗口。所以我不得不改变我的代码:

IntPtr handle = FindWindow(null, "Untitled - Paint");
PostMessage(handle, (uint)MOUSE_BUTTONS.LEFT_DOWN, 0, lparam);

IntPtr handle = FindWindow(null, "Untitled - Paint");
handle = FindWindowEx(handle, IntPtr.Zero, "MSPaintView", null);
canvasHandle = FindWindowEx(handle, IntPtr.Zero, "Afx:00007FF676DD0000:8", null);
PostMessage(canvasHandle, (uint)MOUSE_BUTTONS.LEFT_DOWN, 0, lparam);

如果你想调试这些东西(即使你用 C# 编程),你需要使用 C++ Visual Studio 包附带的 Spy++ for Windows 等工具

希望它可以帮助某人。

于 2018-02-21T09:16:25.197 回答
-1

对于WM_LBUTTONDOWN,您可能需要指定哪个按钮。参考:https ://msdn.microsoft.com/en-us/library/windows/desktop/ms645607(v=vs.85).aspx

我用了:

SendMessage(hWnd, (int)WMessages.WM_RBUTTONDOWN, (int)KeyDownMessage.MK_LBUTTON, MAKELPARAM(x, y));
于 2016-01-18T09:00:04.257 回答