C# - 需要伪单击一个窗口

本文关键字:一个 窗口 单击 | 更新日期: 2023-09-27 18:34:15

好吧,首先,我要做的是单击Web浏览器控件内的flash对象内的特定点。我不确定为什么它不起作用,但我似乎无法单击任何窗口,无论是记事本还是实际程序。

这是我正在使用的代码。

    [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的原因。

谢谢你们的帮助。

C# - 需要伪单击一个窗口

我遇到了同样的问题。

我试图在 MS 画图中画一些东西。原来我正在单击主窗口,但事实证明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);
如果你想

调试这些东西,你需要使用像Spy++这样的工具,比如C++Visual Studio包附带的Spy++(即使你用C#编程)。

希望它对某人有所帮助。

对于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));