Advertisement
Guest User

Expose data grid listveiw item with Microsoft ui automation

a guest
May 20th, 2012
1,284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Automation;
  6. using System.Runtime.InteropServices;
  7.  
  8.  
  9. namespace ConsoleApplication6
  10. {
  11.     class Program
  12.     {
  13.  
  14.         [StructLayout(LayoutKind.Sequential)]
  15.         private struct POINT
  16.         {
  17.             public int x;
  18.             public int y;
  19.         };
  20.  
  21.         [DllImport("user32.dll")]
  22.         private static extern IntPtr WindowFromPoint(POINT pt);
  23.  
  24.         [DllImport("user32.dll")]
  25.         private static extern int GetCursorPos(out POINT pt);
  26.  
  27.  
  28.         static void Main(string[] args)
  29.         {
  30.  
  31.         Console.WriteLine("Place pointer over listview and hit return...");
  32.         Console.ReadLine();
  33.  
  34.         // Get cursor position, then the window handle at that point...
  35.         POINT pt;
  36.         GetCursorPos(out pt);
  37.         IntPtr hwnd = WindowFromPoint(pt);
  38.  
  39.         // Get the AutomationElement that represents the window handle...
  40.         AutomationElement el = AutomationElement.FromHandle(hwnd);
  41.  
  42.         // Walk the automation element tree using content view, so we only see
  43.         // list items, not scrollbars and headers. (Use ControlViewWalker if you
  44.         // want to traverse those also.)
  45.        
  46.         TreeWalker walker = TreeWalker.ContentViewWalker;
  47.         int i = 0;
  48.         for( AutomationElement child = walker.GetFirstChild(el) ;
  49.             child != null;
  50.             child = walker.GetNextSibling(child) )
  51.         {
  52.             // Print out the type of the item and its name
  53.             Console.WriteLine("item {0} is a \"- {1} -\" with name \"- {2} -\"", i++, child.Current.LocalizedControlType, child.Current.ToString() );
  54.             int k = 0;
  55.             for (AutomationElement child2 = walker.GetFirstChild(child);
  56.             child2 != null;
  57.             child2 = walker.GetNextSibling(child2))
  58.             {
  59.                 Console.WriteLine("Child2 ==  item {0} is a \"- {1} -\" with name \"- {2} -\"", k++, child2.Current.LocalizedControlType, child2.Current.ClassName);
  60.             }
  61.         }
  62.         Console.ReadKey();
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement