다음을 통해 공유


클라이언트 응용 프로그램에서 UI 자동화 공급자 구현

참고참고

이 문서는 System.Windows.Automation 네임스페이스에 정의된 관리되는 UI Automation 클래스를 사용하려는 .NET Framework 개발자를 위해 작성되었습니다.UI Automation에 대한 최신 정보는 Windows Automation API: UI Automation을 참조하십시오.

이 항목에는 응용 프로그램 내에 클라이언트 쪽 UI 자동화 공급자를 구현하는 방법을 보여 주는 예제 코드가 나와 있습니다.

그런데 이 코드는 일반적이지 않은 방법입니다. 대개의 경우 UI 자동화 클라이언트 응용 프로그램은 서버 쪽 공급자 또는 DLL 안에 있는 클라이언측 공급자를 사용합니다.

예제

다음 예제 코드에서는 콘솔 창에 대한 간단한 공급자를 구현합니다. 이 코드는 그 자체로 유용한 기능을 제공하는 것이 아니라 클라이언트 코드 안에 공급자를 설정하고 RegisterClientSideProviders를 사용하여 이를 등록하는 기본 단계를 보여 주기 위한 것입니다.


Imports System
Imports System.Windows.Automation
Imports System.Windows.Automation.Provider
Imports System.Reflection
Imports System.Runtime.InteropServices ' for DllImport
Imports System.IO


Namespace CSClientProviderImplementation
    Friend Class CSClientSideImplementationProgram
        <DllImport("kernel32.dll")>
        Shared Function GetConsoleWindow() As IntPtr
        End Function


        Shared Sub Main(ByVal args() As String)

            ClientSettings.RegisterClientSideProviders(UIAutomationClientSideProviders.ClientSideProviderDescriptionTable)

            Dim hwnd As IntPtr = GetConsoleWindow()

            ' Get an AutomationElement that represents the window. 
            Dim elementWindow As AutomationElement = AutomationElement.FromHandle(hwnd)
            Console.WriteLine("Found UI Automation client-side provider for:")

            ' The name property is furnished by the client-side provider.
            Console.WriteLine(elementWindow.Current.Name)
            Console.WriteLine()

            Console.WriteLine("Press any key to exit.")
            Console.ReadLine()
        End Sub
    End Class ' CSClientSideImplementationProgram class


    Friend Class UIAutomationClientSideProviders
        ''' <summary>
        ''' Implementation of the static ClientSideProviderDescriptionTable field. 
        ''' In this case,only a single provider is listed in the table.
        ''' </summary>
        ' Method that creates the provider object.
        ' Class of window that will be served by the provider.
        Public Shared ClientSideProviderDescriptionTable() As ClientSideProviderDescription = {New ClientSideProviderDescription(AddressOf WindowProvider.Create, "ConsoleWindowClass")}
    End Class

    Friend Class WindowProvider
        Implements IRawElementProviderSimple
        Private providerHwnd As IntPtr

        Public Sub New(ByVal hwnd As IntPtr)
            providerHwnd = hwnd
        End Sub

        Friend Shared Function Create(ByVal hwnd As IntPtr, ByVal idChild As Integer, ByVal idObject As Integer) As IRawElementProviderSimple
            Return Create(hwnd, idChild)
        End Function

        Private Shared Function Create(ByVal hwnd As IntPtr, ByVal idChild As Integer) As IRawElementProviderSimple
            ' Something is wrong if idChild is not 0.
            If idChild <> 0 Then
                Return Nothing
            Else
                Return New WindowProvider(hwnd)
            End If
        End Function

#Region "IRawElementProviderSimple"

        ' This is a skeleton implementation. The only real functionality at this stage 
        ' is to return the name of the element and the host window provider, which can 
        ' supply other properties.

        Private ReadOnly Property IRawElementProviderSimple_ProviderOptions() As ProviderOptions Implements IRawElementProviderSimple.ProviderOptions
            Get
                Return ProviderOptions.ClientSideProvider
            End Get
        End Property

        Private ReadOnly Property HostRawElementProvider() As IRawElementProviderSimple Implements IRawElementProviderSimple.HostRawElementProvider
            Get
                Return AutomationInteropProvider.HostProviderFromHandle(providerHwnd)
            End Get
        End Property

        Private Function GetPropertyValue(ByVal aid As Integer) As Object Implements IRawElementProviderSimple.GetPropertyValue
            If AutomationProperty.LookupById(aid) Is AutomationElementIdentifiers.NameProperty Then
                Return "Custom Console Window"
            Else
                Return Nothing
            End If

        End Function

        Private Function GetPatternProvider(ByVal iid As Integer) As Object Implements IRawElementProviderSimple.GetPatternProvider
            Return Nothing
        End Function
#End Region ' IRawElementProviderSimple
    End Class
End Namespace
using System;
using System.Windows.Automation;
using System.Windows.Automation.Provider;
using System.Reflection;
using System.Runtime.InteropServices;    // for DllImport
using System.IO;


namespace CSClientProviderImplementation
{
    class CSClientSideImplementationProgram
    {
        [DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();


        static void Main(string[] args)
        {

            ClientSettings.RegisterClientSideProviders(
                UIAutomationClientSideProviders.ClientSideProviderDescriptionTable);

            IntPtr hwnd = GetConsoleWindow();

            // Get an AutomationElement that represents the window. 
            AutomationElement elementWindow = AutomationElement.FromHandle(hwnd);
            Console.WriteLine("Found UI Automation client-side provider for:");

            // The name property is furnished by the client-side provider.
            Console.WriteLine(elementWindow.Current.Name);
            Console.WriteLine();

            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }
    }  // CSClientSideImplementationProgram class


    class UIAutomationClientSideProviders
    {
        /// <summary>
        /// Implementation of the static ClientSideProviderDescriptionTable field. 
        /// In this case,only a single provider is listed in the table.
        /// </summary>
        public static ClientSideProviderDescription[] ClientSideProviderDescriptionTable = 
            { new ClientSideProviderDescription(
                // Method that creates the provider object.
                WindowProvider.Create,    
                // Class of window that will be served by the provider.
                "ConsoleWindowClass") };  
    }

    class WindowProvider : IRawElementProviderSimple
    {
        IntPtr providerHwnd;

        public WindowProvider(IntPtr hwnd)
        {
            providerHwnd = hwnd;
        }

        internal static IRawElementProviderSimple Create(
            IntPtr hwnd, int idChild, int idObject)
        {
            return Create(hwnd, idChild);
        }

        private static IRawElementProviderSimple Create(
            IntPtr hwnd, int idChild)
        {
            // Something is wrong if idChild is not 0.
            if (idChild != 0) return null;
            else return new WindowProvider(hwnd);
        }

        #region IRawElementProviderSimple

        // This is a skeleton implementation. The only real functionality at this stage 
        // is to return the name of the element and the host window provider, which can 
        // supply other properties.

        ProviderOptions IRawElementProviderSimple.ProviderOptions
        {
            get
            {
                return ProviderOptions.ClientSideProvider;
            }
        }

        IRawElementProviderSimple IRawElementProviderSimple.HostRawElementProvider
        {
            get
            {
                return AutomationInteropProvider.HostProviderFromHandle(providerHwnd);
            }
        }

        object IRawElementProviderSimple.GetPropertyValue(int aid)
        {
            if (AutomationProperty.LookupById(aid) ==
                AutomationElementIdentifiers.NameProperty)
            {
                return "Custom Console Window";
            }
            else
            {
                return null;
            }

        }

        object IRawElementProviderSimple.GetPatternProvider(int iid)
        {
            return null;
        }
        #endregion IRawElementProviderSimple
    }
}

참고 항목

작업

클라이언트측 공급자 어셈블리 등록

클라이언트측 UI 자동화 공급자 만들기

개념

UI 자동화 공급자 개요

클라이언트측 UI 자동화 공급자 구현