模拟a单击没有明显句柄的按钮

时间:2015-03-18 10:09:02

标签: c#

我想模拟一个不可见的窗口按钮上的mousedown(因为另一个窗口在它前面)。 (对于那些关心/想知道的人,我想这样做的原因是绕过Lync的行为,See here

我有一个很好的方法来处理隐藏的'窗口,我也可以枚举该窗口中的控件。但是,所有这些控件都将句柄设置为0,因此我无法直接使用SendMessage ...

private static void ClickAccept()
{
        var hwnd = Win32.FindWindowsWithText("Toast").FirstOrDefault();

        if (hwnd == IntPtr.Zero)
            return;
        //Get parent window.
        AutomationElement element = AutomationElement.FromHandle(hwnd);
        //Get all descendants
        AutomationElementCollection elements = element.FindAll(TreeScope.Descendants, Condition.TrueCondition);
        //loop through descendants
        foreach (AutomationElement elementNode in elements)
        {

            Console.WriteLine("class: " + elementNode.Current.ClassName + ", name: " + elementNode.Current.Name + ", Handle: " + elementNode.Current.NativeWindowHandle);

        }
}  

结果:

class: NetUINativeHWNDHost, name: , Handle: 525312
class: NetUIHWNDElement, name: , Handle: 1050608
class: NetUINetUIDialog, name: , Handle: 0
class: NetUINUILyncElement, name: , Handle: 0
class: NetUINUILyncElement, name: , Handle: 0
class: NetUISimpleButton, name: Accept, Handle: 0
class: NetUIImage, name: , Handle: 0
class: NetUIImage, name: , Handle: 0
class: NetUIAccessibilityAnnouncer, name: , Handle: 0
class: NetUILabel, name: , Handle: 0
class: NetUIImage, name: , Handle: 0
class: NetUIImage, name: , Handle: 0
class: NetUILabel, name: site_110020@askroger.nl, Handle: 0
class: NetUIAccessibilityAnnouncer, name: , Handle: 0
class: NetUILabel, name: sip:lkjdf@lkjd.nl: SITE, Handle: 0
class: NetUIAccessibilityAnnouncer, name: Incoming Skype for Business Toast: site_110020@askroger.nl sip:lkjdf@lkjd.nl: SITE. Press Windows+Shift+O to accept, Windows+Escape to decline., Handle: 0
class: NetUINUILyncElement, name: , Handle: 0
class: NetUINUILyncElement, name: , Handle: 0
class: NetUIAnchor, name: , Handle: 0
class: NetUIButton, name: Options, Handle: 0
class: NetUINUILyncElement, name: , Handle: 0
class: NetUIButton, name: Ignore, Handle: 0

我猜测它与HWNDHost有关,但我找不到合适的信息。如何模拟鼠标点击"接受"按钮现在?

1 个答案:

答案 0 :(得分:0)

感谢@DavidHeffernan和@Damien_The_Unbeliever 使用自动化点击它可以工作!

        private static void ClickAccept()
    {
        var hwnd = Win32.FindWindowsWithText("Toast").FirstOrDefault();

        if (hwnd == IntPtr.Zero)
            return;
        //Get parent window.
        AutomationElement element = AutomationElement.FromHandle(hwnd);
        //Get all descendants
        AutomationElementCollection elements = element.FindAll(TreeScope.Descendants, Condition.TrueCondition);
        //loop through descendants
        foreach (AutomationElement elementNode in elements)
        {
            if (elementNode.Current.Name == "Accept")
            {
                object pattern;
                if(elementNode.TryGetCurrentPattern(InvokePattern.Pattern, out pattern))
                {
                    (pattern as InvokePattern).Invoke();
                    return;
                }
            }

        }
    }