1) 실습: 포커스 트래커(자동화 요소 속성)

이번에는 초점을 바뀔 때마다 초점을 소유한 자동화 요소의 속성 정보를 출력하는 포커스 트래커를 만들어 봅시다.

콘솔 응용 프로젝트를 생성한 후에 필요한 어셈블리를 참조 추가합시다. 필요한 어셈블리는 다음과 같습니다.

UIAutomationClient.dll

UIAutomationClientsideProviders.dll

UIAutomationTypes.dll

WindowBase.dll

먼저 AutomationFocusChangedEventHandler를 생성합니다.

utomationFocusChangedEventHandler afceh = null;
afceh = new AutomationFocusChangedEventHandler(FocusChangedEventHandler);

FocusChangedEventHandler는 입력 인자가 2개입니다. 첫번째 인자는 object 형식이고 두 번째 인자는 AutomationFocusChangedEventArgs 형식입니다. 일단 내부 로직은 비어있는 상태로 이벤트 핸들러를 정의하세요.

static void FocusChangedEventHandler(object obj, AutomationFocusChangedEventArgs e)
{
}

그리고 Automation의 정적 메서드 AddAutomationFocusChangedEventHandler를 호출하여 이벤트 핸들러를 등록합니다.

Automation.AddAutomationFocusChangedEventHandler(afceh);

포커스 트래킹은 엔터를 칠 때까지 수행하게 합시다.

Console.ReadLine();

포커스 트래킹을 마치면 이벤트 핸들러를 등록 해제합니다.

Automation.RemoveAutomationFocusChangedEventHandler(afceh);

이제 포커스 변경 이벤트 핸들러를 작성합시다.

static void FocusChangedEventHandler(object obj, AutomationFocusChangedEventArgs e)
{
   //AutomationElement의 정적 속성인 FocusedElement로 초점을 소유한 자동화 요소를 참조합니다. 그리고 자동화 요소의 정보를 출력합니다.
   AutomationElement ae = AutomationElement.FocusedElement;
   ViewAEInfo(ae);
}
private static void ViewAEInfo(AutomationElement ae)
{
    Console.WriteLine("요소명:{0}", ae.Current.Name);
    Console.WriteLine("가속화 키:{0}", ae.Current.AcceleratorKey);
    Console.WriteLine("액세스 키:{0}", ae.Current.AccessKey);
    Console.WriteLine("자동화 요소 ID:{0}", ae.Current.AutomationId);
    Console.WriteLine("사각영역:{0}", ae.Current.BoundingRectangle);
    Console.WriteLine("클래스 이름:{0}", ae.Current.ClassName);
    Console.WriteLine("컨트롤 유형:{0}", ae.Current.ControlType.ProgrammaticName);
    Console.WriteLine("Framework ID:{0}", ae.Current.FrameworkId);
    Console.WriteLine("포커스 소유 :{0}", ae.Current.HasKeyboardFocus);
    Console.WriteLine("도움말:{0}", ae.Current.HelpText);
    Console.WriteLine("컨텐츠 여부:{0}", ae.Current.IsContentElement);
    Console.WriteLine("컨트롤 여부:{0}", ae.Current.IsControlElement);
    Console.WriteLine("활성화 여부:{0}", ae.Current.IsEnabled);
    Console.WriteLine("포커스 소유 가능 여부:{0}", ae.Current.IsKeyboardFocusable);
    Console.WriteLine("화면 비표시 여부:{0}", ae.Current.IsOffscreen);
    Console.WriteLine("내용 보화(패스워드) 여부:{0}", ae.Current.IsPassword);
    Console.WriteLine("IsRequiredForForm:{0}", ae.Current.IsRequiredForForm);
    Console.WriteLine("아이템 상태:{0}", ae.Current.ItemStatus);
    Console.WriteLine("아이템 형식:{0}", ae.Current.ItemType);
    if (ae.Current.LabeledBy != null)
    {
        Console.WriteLine("LabledBy 이름:{0}", ae.Current.LabeledBy.Current.Name);
    }
    Console.WriteLine("윈도우 창 핸들:{0}", ae.Current.NativeWindowHandle);
    Console.WriteLine("컨트롤 방향:{0}", ae.Current.Orientation);
    Console.WriteLine("프로세스 ID:{0}", ae.Current.ProcessId);
}
using System;
using System.Windows.Automation;

namespace 포커스트래킹_자동화속성정보
{
    class Program
    {
        static void Main(string[] args)
        {
            AutomationFocusChangedEventHandler afceh = null;
            afceh = new AutomationFocusChangedEventHandler(FocusChangedEventHandler);
            Automation.AddAutomationFocusChangedEventHandler(afceh);
            Console.ReadLine();
            Automation.RemoveAutomationFocusChangedEventHandler(afceh);
        }

        static void FocusChangedEventHandler(object obj, AutomationFocusChangedEventArgs e)
        {
            AutomationElement ae = AutomationElement.FocusedElement;
            ViewAEInfo(ae);
        }

        private static void ViewAEInfo(AutomationElement ae)
        {
            Console.WriteLine("요소명:{0}", ae.Current.Name);
            Console.WriteLine("가속화 키:{0}", ae.Current.AcceleratorKey);
            Console.WriteLine("액세스 키:{0}", ae.Current.AccessKey);
            Console.WriteLine("자동화 요소 ID:{0}", ae.Current.AutomationId);
            Console.WriteLine("사각영역:{0}", ae.Current.BoundingRectangle);
            Console.WriteLine("클래스 이름:{0}", ae.Current.ClassName);
            Console.WriteLine("컨트롤 유형:{0}", ae.Current.ControlType.ProgrammaticName);
            Console.WriteLine("Framework ID:{0}", ae.Current.FrameworkId);
            Console.WriteLine("포커스 소유 :{0}", ae.Current.HasKeyboardFocus);
            Console.WriteLine("도움말:{0}", ae.Current.HelpText);
            Console.WriteLine("컨텐츠 여부:{0}", ae.Current.IsContentElement);
            Console.WriteLine("컨트롤 여부:{0}", ae.Current.IsControlElement);
            Console.WriteLine("활성화 여부:{0}", ae.Current.IsEnabled);
            Console.WriteLine("포커스 소유 가능 여부:{0}", ae.Current.IsKeyboardFocusable);
            Console.WriteLine("화면 비표시 여부:{0}", ae.Current.IsOffscreen);
            Console.WriteLine("내용 보화(패스워드) 여부:{0}", ae.Current.IsPassword);
            Console.WriteLine("IsRequiredForForm:{0}", ae.Current.IsRequiredForForm);
            Console.WriteLine("아이템 상태:{0}", ae.Current.ItemStatus);
            Console.WriteLine("아이템 형식:{0}", ae.Current.ItemType);
            if (ae.Current.LabeledBy != null)
            {
                Console.WriteLine("LabledBy 이름:{0}", ae.Current.LabeledBy.Current.Name);
            }
            Console.WriteLine("윈도우 창 핸들:{0}", ae.Current.NativeWindowHandle);
            Console.WriteLine("컨트롤 방향:{0}", ae.Current.Orientation);
            Console.WriteLine("프로세스 ID:{0}", ae.Current.ProcessId);
        }
    }
}

[소스 3.1] Program.cs

포커스 트래커 실행 화면
[그림 3.2] 실행화면