发送单击user32.dll,消息显示在spy ++中,但未单击按钮

时间:2012-07-12 08:08:12

标签: c# dllimport user32 spy++

我在使用user32.dll发送Click到应用程序时遇到问题。没有单击该按钮,但在spy ++中,该消息确实出现了。 我正在使用win7 x64

代码用c#编写:

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern IntPtr FindWindow(string strClassName, string strWindowName);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lpClassName, string lpWindowTitle);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll")]
    static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);

    //
    // Finds a window in the whole tree of childs for a parent window.
    //
    static IntPtr FindWindowRecursive(IntPtr hParent, string szClass, string szCaption)
    {
        IntPtr hResult = FindWindowEx(hParent, IntPtr.Zero, szClass, szCaption);
        if (hResult != IntPtr.Zero)
            return hResult; // found it

        // enumerate all childs and if found one that has childs go in
        IntPtr hChild = FindWindowEx(hParent, IntPtr.Zero, null, null); // first child
        if (hChild != IntPtr.Zero)
        {
            // let's enumerate
            do
            {
                hResult = FindWindowRecursive(hChild, szClass, szCaption);
                if (hResult != IntPtr.Zero)
                    return hResult;  // found it
            } while ((hChild = GetWindow(hChild, GW_HWNDNEXT)) != IntPtr.Zero);
        }

        return IntPtr.Zero; // no childs, so no window was found
    }

    static void Main(string[] args)
    {
        IntPtr win = FindWindow("Omnipage17_MainWnd_Class", "Unbenanntes OmniPage-Dokument 1 - OmniPage");
                SetForegroundWindow(win);
        ShowWindowAsync(win, SW_RESTORE);
        IntPtr ButtonHandle = FindWindowRecursive(win, "BarButton", "c");
        SetActiveWindow(win);
        //sEND Lbuttondown
        IntPtr ptr = SendMessage(ButtonHandle, 0x0201, new IntPtr(0x0001), MakeLParam(81,28));
        //Thread.Sleep(10);
        //Mousemove
        ptr = SendMessage(ButtonHandle, 0x0200, new IntPtr(0x0001), MakeLParam(86,24));
        //lbuttonup
        ptr = SendMessage(ButtonHandle, 0x0202, new IntPtr(0x0001), MakeLParam(81, 28));

        //SendMessage(ButtonHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
    }

以下是该按钮的spy ++消息: enter image description here

如果我发送以下消息: 我不是,如果这是那个问题,但lbuttondown,buttoup出现2次(S + R),如果我点击它manuelly它得到1消息(P) 还试图用WM_CLICK做到这一点,但后来我遇到了同样的问题

编辑: 现在使用PostMessage,因此间谍++会显示与手动点击相同的消息,但仍然无法点击该按钮

enter image description here


使用这个库我有同样的问题。

代码:

SetForegroundWindow(win);
Rectangle re;
GetWindowRect(ButtonHandle, out re);

Cursor.Position = new Point((re.X + re.Width)/2, (re.Y + re.Height)/2);
WindowsInput.InputSimulator.SimulateKeyDown(WindowsInput.VirtualKeyCode.LBUTTON);
WindowsInput.InputSimulator.SimulateKeyUp(WindowsInput.VirtualKeyCode.LBUTTON);

发送消息,但不单击按钮 enter image description here

修改

感谢该链接(http://www.hanselman.com/blog/IntroducingLync2010SuperSimpleAutoAnswerVideoKioskWithFullScreen.aspx),但是对于这个库我也有同样的问题:/

代码:

SetForegroundWindow(win);
Rectangle re;
GetWindowRect(ButtonHandle, out re);

Cursor.Position = new Point((re.X + re.Width)/2, (re.Y + re.Height)/2);
WindowsInput.InputSimulator.SimulateKeyDown(WindowsInput.VirtualKeyCode.LBUTTON);
WindowsInput.InputSimulator.SimulateKeyUp(WindowsInput.VirtualKeyCode.LBUTTON);

发送消息,但不单击按钮 enter image description here

编辑2:

用户的答案被删除了,因为我发表了我的评论作为答案:

  

这不是答案,这属于你的问题。它不匹配   你的代码,显然你还在发布BM_CLICK。这是错的,它   应该发送,你应该发送BM_CLICK或发布鼠标   消息。而你正在看错窗口,它是按钮   获取BN_CLICK通知并对其执行操作的父级。有了   进程的键盘状态错误将是典型的故障模式。 - 汉斯   Passant 18小时前

关于那个,为什么它应该是父母的窗户?间谍++中的bcs(下面的屏幕截图我为该按钮(类:BarButton),我从user32.dll获取的句柄也与间谍++中的那个相同 enter image description here

1 个答案:

答案 0 :(得分:2)