Search code examples
c#ui-automationmicrosoft-ui-automation

How to click on a "pane" using UI automation library?


We have an application in which I need to click on a Pane. I tried to use the following code, which I use to click on a button, but it gave Unsupported pattern exception.

InvokePattern click_pattern = (InvokePattern)adjust_button.GetCurrentPattern(InvokePattern.Pattern); click_pattern.Invoke();

Is there any other way to do it?


Solution

  • Even though the object is clickable, depending on how the click is being handled internally, you may not necessarily be able to use the InvokePattern to perform a click. That appears to be the case here.

    As an alternative, you can use some code to move the mouse cursor over the pane and issue a click using P/Invoke. Something like this:

    private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
    
    [DllImport("user32.dll")]
    private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);
    
    ...
    ...
    
    AutomationElement paneToClick;
    
    ...
    ...
    
    Cursor.Position = paneToClick.GetClickablePoint();
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new IntPtr());
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new IntPtr());