/// <summary> /// Gets the main window (first window on desktop with the same process as this window) /// </summary> private Window GetMainWindow() { if (IsMainWindow) { return(this); } var mainWindow = BasicAutomationElement.Automation.GetDesktop().FindFirstChild(ConditionFactory.ByProcessId(Current.ProcessId)).AsWindow(); return(mainWindow ?? this); }
public virtual AutomationSearchCondition WithProcessId(int processId) { _conditions.Add(ConditionFactory.ByProcessId(processId)); return(this); }
public Menu GetContextMenuByFrameworkType(FrameworkType frameworkType) { var mainWindow = this; if (!IsMainWindow) { // Find the main application window first var newMainWindow = BasicAutomationElement.Automation.GetDesktop().FindFirstChild(ConditionFactory.ByProcessId(Current.ProcessId)).AsWindow(); if (newMainWindow != null) { mainWindow = newMainWindow; } } if (frameworkType == FrameworkType.WinForms) { var ctxMenu = mainWindow.FindFirstChild(ConditionFactory.ByControlType(ControlType.Menu).And(ConditionFactory.ByName("DropDown"))); return(ctxMenu.AsMenu()); } if (frameworkType == FrameworkType.Wpf) { // In WPF, there is a window without title and inside is the menu var windows = mainWindow.FindAllChildren(ConditionFactory.ByControlType(ControlType.Window).And(ConditionFactory.ByText(""))); foreach (var window in windows) { var ctxMenu = window.FindFirstChild(ConditionFactory.ByControlType(ControlType.Menu)); return(ctxMenu.AsMenu()); } } if (frameworkType == FrameworkType.Win32) { // The main menu is directly under the desktop with the name "Context" or in a few cases "System" var desktop = BasicAutomationElement.Automation.GetDesktop(); var nameCondition = ConditionFactory.ByName("Context").Or(ConditionFactory.ByName("System")); var ctxMenu = desktop.FindFirst(TreeScope.Children, ConditionFactory.ByControlType(ControlType.Menu).And(nameCondition)).AsMenu(); if (ctxMenu != null) { ctxMenu.IsWin32ContextMenu = true; return(ctxMenu); } } return(null); }