Search code examples
c#wpfui-automationtrayicon

Windows UI Automation: click tray icon button


I'd like to find and click tray icon button (Windows 10). Helpers were took from here: https://blogs.msdn.microsoft.com/oldnewthing/20141013-00/?p=43863

All works fine, but when application clicks button with name "Dropbox" really it clicks another button with name "OneDrive" which is in tray area too:

    foreach (var icon in EnumNotificationIcons())
    {
        var name = icon.GetCurrentPropertyValue(AutomationElement.NameProperty) as string;
        Console.WriteLine(name);
        if (name.StartsWith("Dropbox"))
        {
            Console.WriteLine(@"Click!");
            icon.InvokeButton();
            break;
        }
    }

What is wrong?

This machine has 2 monitors. I have tested on another computer winth win7 and 1 monitor - it works.

Update.

I have ran my application on 3 machines with different DPI settings. On all of these machines I installed target app. Naturally, the icon location should not be the same, but it seems I the the reason why tray button.Invoke in my app does not work: that in case of DPI!=100%

There are results:

1.100% - my app works ok.

Click =1741,x=1000

App:
BoundingRectangle: l:1741 t:1000,Width=40,Height=40

Inspector.exe: BoundingRectangle: {l:1741 t:1000 r:1781 b:1040}

2.150% - my app does not work

App: BoundingRectangle:X=2464,Y=1400,Width=60,Height=60

Inspector.exe: BoundingRectangle: {l:3606 t:2040 r:3666 b:2100}

3.175% - my app does not work

App: BoundingRectangle: l:1977,t:1214,Width=70,Height=70

Inspector.exe: BoundingRectangle: {l:3460 t:2020 r:3530 b:2090}

How to solve the original task : how to click the icon in any case of DPI settings?


Solution

  • Solved, the answer is here: the reason is DPI. UI Automation works incorrect if DPI != 96. Need to add manifest code or app code as described here

    How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?