【问题标题】:Send mouse click to process发送鼠标点击处理
【发布时间】:2012-11-05 19:58:55
【问题描述】:

我正在尝试向应用程序发送鼠标点击。

我目前有:

    int hWnd = FindWindow(null, "Window Title Here");
            SendMessage(hwnd, 0x201, 0, 0); //Mouse left down
            SendMessage(hwnd, 0x202, 0, 0); //Mouse left up

我正在使用

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

    [DllImportAttribute("User32.dll")]
    private static extern int FindWindow(String ClassName, String WindowName);

但我得到错误,这些是:

“当前上下文中不存在名称‘hwnd’”

我该如何解决这个问题?

【问题讨论】:

    标签: c# process mouseevent message send


    【解决方案1】:

    已经有库这样做了,Windows Input Simulator 就是一个例子,如果你可以使用它,它会让你不必编写这种代码。见CodePlex

    【讨论】:

    • 我用这个 IntPtr hWnd = FindWindow(null, "iw5mp.exe"); PostMessage(hWnd, 0x201, 0, 0); //鼠标左下 PostMessage(hWnd, 0x202, 0, 0); //鼠标左起,当我把它放在计时器中时,什么也没有发生。为什么?
    【解决方案2】:

    C# 区分大小写。 hwndhWnd 是不同的东西。

    【讨论】:

    • 我刚刚将 hwnd 更改为 hWnd,现在我收到此错误:“'Cross_Click.Form1.SendMessage(System.IntPtr, int, int, int)' 的最佳重载方法匹配有一些无效参数"
    • 'Cross_Click.Form1.SendMessage(System.IntPtr, int, int, int)' 的最佳重载方法匹配有一些无效参数
    • 可能是因为IntPtrint不一样?
    • With IntPtr hWnd = FindWindow(null, "Window Title Here");我无法将类型“int”隐式转换为“System.IntPtr”。存在显式转换(您是否缺少演员表?)
    • 还有,你错过了演员阵容吗?您是否缺少IntPtr structure 上的文档?
    猜你喜欢
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 2021-10-01
    • 1970-01-01
    相关资源
    最近更新 更多