Last active
March 20, 2018 18:09
-
-
Save JavierJF/694ee9904dfe605f3e4cada0a04a83ab to your computer and use it in GitHub Desktop.
Simple Solution to trigger TrayIcon through settings with UIA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* App.cs | |
* Copyright 2018 Raising the Floor - International | |
* | |
* Licensed under the New BSD license. You may not use this file except in | |
* compliance with this License. | |
* | |
* The R&D leading to these results received funding from the | |
* Department of Education - Grant H421A150005 (GPII-APCP). However, | |
* these results do not necessarily represent the policy of the | |
* Department of Education, and you should not assume endorsement by the | |
* Federal Government. | |
* | |
* You may obtain a copy of the License at | |
* https://github.com/GPII/universal/blob/master/LICENSE.txt | |
*/ | |
using System; | |
using System.Threading; | |
using interop.UIAutomationCore; | |
namespace UIA_GPII_TaskBar_Icon | |
{ | |
class App | |
{ | |
static void Main(string[] args) | |
{ | |
System.Diagnostics.Process.Start("ms-settings:taskbar"); | |
Console.WriteLine(":: Starting ms-settings."); | |
Console.WriteLine(":: Initializing UI Automation."); | |
// The first step in calling UIA, is getting a CUIAutomation object. | |
var _automation = new CUIAutomation(); | |
// The properties are used from the actual constant values here: | |
// "https://msdn.microsoft.com/en-us/library/windows/desktop/ee684017(v=vs.85)." | |
int _propertyName = 30005; | |
int _propertyAutomationId = 30011; | |
int _propertyClassName = 30012; | |
// The patterns are used from the actual constant values here: | |
// "https://msdn.microsoft.com/en-us/library/windows/desktop/ee671195(v=vs.85).aspx" | |
int _invokePattern = 10000; | |
int _tooglePattern = 10015; | |
// Application name to be showed as taskbar icon. | |
string appToLock = "ApplicationName"; | |
Console.WriteLine(":: Getting root node."); | |
// The first step in calling UIA, is getting a CUIAutomation object. | |
var rootElement = _automation.GetRootElement(); | |
IUIAutomationCondition waitSettingsOpenning = | |
_automation.CreatePropertyCondition( | |
_propertyAutomationId, | |
"SystemSettings_Taskbar_Lock_ToggleSwitch" | |
); | |
Console.WriteLine(":: Waiting for 'ms-settings' app to be ready."); | |
var settingsOpennedMark = rootElement.FindFirst(TreeScope.TreeScope_Subtree, waitSettingsOpenning); | |
while (settingsOpennedMark == null) | |
{ | |
Thread.Sleep(2000); | |
settingsOpennedMark = rootElement.FindFirst(TreeScope.TreeScope_Subtree, waitSettingsOpenning); | |
} | |
Console.WriteLine(":: 'ms-settings' app oppened sucessfully."); | |
// Triggers the first link to the list of possible applications icons. | |
// SystemSettings_Taskbar_SelectIconsToAppearOnTaskbar_HyperlinkButton | |
IUIAutomationCondition searchTaskBarIconsLink = | |
_automation.CreatePropertyCondition( | |
_propertyAutomationId, | |
"SystemSettings_Taskbar_SelectIconsToAppearOnTaskbar_HyperlinkButton" | |
); | |
Console.WriteLine(":: Openning 'ms-settings' app section with icon toogling."); | |
IUIAutomationElement selectIconsLink = rootElement.FindFirst(TreeScope.TreeScope_Subtree, searchTaskBarIconsLink); | |
if (selectIconsLink == null) | |
{ | |
Console.WriteLine("Error: No link to icon section found."); | |
return; | |
} | |
var invokeSelecIcons = (IUIAutomationInvokePattern)selectIconsLink.GetCurrentPattern(_invokePattern); | |
invokeSelecIcons.Invoke(); | |
IUIAutomationCondition changedToSelectIconMode = | |
_automation.CreatePropertyCondition( | |
_propertyAutomationId, | |
"SystemSettings_Notifications_ShowIconsOnTaskbar_ToggleSwitch" | |
); | |
Console.WriteLine(":: Waiting for 'ms-settings' to be in icon toggling section."); | |
var markFound = rootElement.FindFirst(TreeScope.TreeScope_Subtree, changedToSelectIconMode); | |
while (markFound == null) | |
{ | |
Thread.Sleep(1000); | |
markFound = rootElement.FindFirst(TreeScope.TreeScope_Subtree, changedToSelectIconMode); | |
} | |
IUIAutomationCondition searchGPIIAppInIconListName = | |
_automation.CreatePropertyCondition( | |
_propertyName, | |
appToLock | |
); | |
IUIAutomationCondition searchGPIIAppInIconListClass = | |
_automation.CreatePropertyCondition( | |
_propertyClassName, | |
"ToggleSwitch" | |
); | |
var searchGPIITriggerButton = | |
_automation.CreateAndCondition(searchGPIIAppInIconListName, searchGPIIAppInIconListClass); | |
Console.WriteLine(":: Finding GPII app icon switch."); | |
var gpiiIconLockButton = rootElement.FindFirst(TreeScope.TreeScope_Subtree, searchGPIITriggerButton); | |
if (gpiiIconLockButton == null) | |
{ | |
Console.WriteLine("Error: No application with specified name found."); | |
return; | |
} | |
var triggerLockButton = (IUIAutomationTogglePattern)gpiiIconLockButton.GetCurrentPattern(_tooglePattern); | |
Console.WriteLine(":: Toogling GPII app icon switch."); | |
triggerLockButton.Toggle(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment