コード例 #1
0
 public async Task Inspect(bool acceptNewWindows)
 {
     if (!Inspecting)
     {
         Inspecting = true;
         await Task.Run(() =>
         {
             try
             {
                 System.Windows.Application.Current.Dispatcher.Invoke(delegate
                 {
                     foreach (System.Windows.Window w in System.Windows.Application.Current.Windows)
                     {
                         if (w is AddWindowPopup)
                         {
                             w.Close();
                         }
                     }
                 });
                 var screenPos            = Mouse.Position;
                 var hov                  = Automation.FromPoint(screenPos);
                 ElementBO hoveredElement = hov != null ? new ElementBO(hov) : null;
                 var win                  = hoveredElement != null ?  GetRootFromElement(hoveredElement) : null;
                 if (win == null)
                 {
                     Inspecting = false;
                     return;
                 }
                 if (SavedSettingsWindows.Any(x => x.Identifier == win.AutomationId || x.Identifier == win.Name))
                 {
                     if (!Elements.Any(x => x.AutomationElement.Equals(win) && x.AutomationElement.ProcessId == win.ProcessId))
                     {
                         System.Windows.Application.Current.Dispatcher.Invoke(delegate
                         {
                             GetDesktop();
                         });
                     }
                     else
                     {
                         DrawHighlight(hov);
                     }
                 }
                 else
                 {
                     Inspecting = false;
                     if (acceptNewWindows)
                     {
                         CreateAddWindowPopUp(win);
                     }
                     return;
                 }
                 if (hoveredElement != null)
                 {
                     var selected = SelectedItem;
                     if (selected == null || (selected != null && !selected.Equals(hoveredElement)))
                     {
                         SelectedItem = hoveredElement;
                         ElementToSelectChanged(hoveredElement.AutomationElement);
                         Inspecting = false;
                     }
                 }
                 ;
             }
             catch (Exception ex) when(ex is COMException || ex is UnauthorizedAccessException || ex is Win32Exception || ex is FileNotFoundException || ex is TimeoutException)
             {
                 System.Diagnostics.Debug.WriteLine(ex.GetType().Name);
                 Inspecting = false;
                 System.Windows.Application.Current.Dispatcher.Invoke(delegate
                 {
                     GetDesktop();
                 });
             }
         });
     }
 }