コード例 #1
0
        static void Main(string[] args)
        {
            try
            {
                //Launch settings
                var appActiveManager = new ApplicationActivationManager();
                appActiveManager.ActivateApplication(
                    "windows.immersivecontrolpanel_cw5n1h2txyewy!microsoft.windows.immersivecontrolpanel",
                    "page=SettingsPageAppsDefaults",
                    ActivateOptions.None, //TODO: Find a way to hide the splash screen. Or maybe just hide the window until the splash screen is gone
                    out var pid
                    );

                var uiAutomation = new CUIAutomationClass();

                var window = FindAndWait(uiAutomation.GetRootElement(), TreeScope.TreeScope_Children, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Settings"), TimeSpan.FromSeconds(3));
                try
                {
                    //Can't hide the window since that prevents UI Automation from seeing it
                    ((IUIAutomationWindowPattern)window.GetCurrentPattern(UIA_PatternIds.UIA_WindowPatternId)).SetWindowVisualState(WindowVisualState.WindowVisualState_Minimized);

                    var defaultWebBrowserDropDown = FindAndWait(window, TreeScope.TreeScope_Descendants, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "SystemSettings_DefaultApps_Browser_Button"), TimeSpan.FromSeconds(1));
                    ((IUIAutomationInvokePattern)defaultWebBrowserDropDown.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId)).Invoke();

                    var chooseAnApplication = FindAndWait(window, TreeScope.TreeScope_Descendants, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "DefaultAppsFlyoutPresenter"), TimeSpan.FromSeconds(1));
                    var googleChromeOption  = FindAndWait(
                        chooseAnApplication,
                        TreeScope.TreeScope_Descendants,
                        uiAutomation.CreateAndCondition(
                            uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Google Chrome"),
                            uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ButtonControlTypeId)
                            ),
                        TimeSpan.FromSeconds(1)
                        );
                    ((IUIAutomationInvokePattern)googleChromeOption.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId)).Invoke();

                    //If switching from Edge for the first time, it shows a confirmation prompt
                    try
                    {
                        var switchAnyway = FindAndWait(
                            window,
                            TreeScope.TreeScope_Descendants,
                            uiAutomation.CreateAndCondition(
                                uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Switch anyway"),
                                uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_HyperlinkControlTypeId)
                                ),
                            TimeSpan.FromSeconds(1)
                            );
                        ((IUIAutomationInvokePattern)switchAnyway.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId)).Invoke();
                    }
                    catch (TimeoutException) { } //All good
                }
                finally
                {
                    ((IUIAutomationWindowPattern)window.GetCurrentPattern(UIA_PatternIds.UIA_WindowPatternId)).Close();
                }
            }
            catch //Ensures all nested finally blocks execute
            {
                throw;
            }
        }