FormPoint 메서드를 이용하여 화면 좌표로 자동화 요소를 조사하는 콘솔 응용 프로그램을 만들어 봅시다.
![[그림 3.4] 좌표로 자동화 요소 조사](http://ehpub.co.kr/wp-content/uploads/2018/04/그림-3.4-좌표로-자동화-요소-조사.png)
먼저 콘솔 응용 프로젝트를 생성하고 필요한 어셈블리를 참조 추가하세요.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
static void Main(string[] args) { 화면 좌표를 얻어오기 위해 Automation의 정적 속성인 RootElement를 참조하여 사각 영역을 구합니다. AutomationElement ae_root = AutomationElement.RootElement; Rect rect = ae_root.Current.BoundingRectangle; 같은 요소의 정보를 출력하지 않게 하기 위해 자동화 요소 변수를 두 개 선언합시다. AutomationElement ae,oae=null; x,y 좌표를 변경하면서 원하는 위치의 자동화 요소를 찾습니다. for (double y = 0; y < rect.Height; y++) { for (double x = 0; x < rect.Width; x++) { ae = AutomationElement.FromPoint(new Point(x,y)); 찾은 자동화 요소가 이전 자동화 요소가 아니면서 null이 아닐 때만 정보를 출력합니다. if ((ae!=oae)&&(ae != null)) { ViewAEInfo(ae,x,y); oae = ae; } } } } private static void ViewAEInfo(AutomationElement ae,double x,double y) { Console.Write("{0},{1} : 요소명:{2}",x,y, ae.Current.Name); Console.WriteLine("클래스:{0} ", ae.Current.LocalizedControlType); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.Windows.Automation; using System.Windows; namespace 좌표로_자동화_요소_조사하기 { class Program { static void Main(string[] args) { AutomationElement ae_root = AutomationElement.RootElement; Rect rect = ae_root.Current.BoundingRectangle; AutomationElement ae,oae=null; for (double y = 0; y < rect.Height; y++) { for (double x = 0; x < rect.Width; x++) { ae = AutomationElement.FromPoint(new Point(x,y)); if ((ae!=oae)&&(ae != null)) { ViewAEInfo(ae,x,y); oae = ae; } } } } private static void ViewAEInfo(AutomationElement ae,double x,double y) { Console.Write("{0},{1} : 요소명:{2}",x,y, ae.Current.Name); Console.WriteLine("클래스:{0} ", ae.Current.LocalizedControlType); } } } |
[소스 3.3] Program.cs