Expose a Server-side UI Automation Provider

Note

This documentation is intended for .NET Framework developers who want to use the managed UI Automation classes defined in the System.Windows.Automation namespace. For the latest information about UI Automation, see Windows Automation API: UI Automation.

This topic contains example code that shows how to expose a server-side UI Automation provider that is hosted in a System.Windows.Forms.Control window.

The example overrides the window procedure to trap WM_GETOBJECT, which is the message sent by the UI Automation core service when a client application requests information about the window.

Example

/// <summary>
/// Handles WM_GETOBJECT message; others are passed to base handler.
/// </summary>
/// <param name="m">Windows message.</param>
/// <remarks>
/// This method enables UI Automation to find the control.
/// In this example, the implementation of IRawElementProvider is in the same class
/// as this method.
/// </remarks>
protected override void WndProc(ref Message m)
{
    const int WM_GETOBJECT = 0x003D;

    if ((m.Msg == WM_GETOBJECT) && ((int)(long)m.LParam ==
        AutomationInteropProvider.RootObjectId))
    {
        m.Result = AutomationInteropProvider.ReturnRawElementProvider(
                this.Handle, m.WParam, m.LParam,
                (IRawElementProviderSimple)this);
        return;
    }
    base.WndProc(ref m);
}
''' <summary>
''' Handles WM_GETOBJECT message; others are passed to base handler.
''' </summary>
''' <param name="m">Windows message.</param>
''' <remarks>
''' This method enables UI Automation to find the control.
''' In this example, the implementation of IRawElementProvider is in the same class
''' as this method.
''' </remarks>
Protected Overrides Sub WndProc(ByRef m As Message)
    Const WM_GETOBJECT As Integer = &H3D

    If m.Msg = WM_GETOBJECT AndAlso CInt(CLng(m.LParam)) = AutomationInteropProvider.RootObjectId Then
        m.Result = AutomationInteropProvider.ReturnRawElementProvider(Me.Handle, m.WParam, m.LParam, DirectCast(Me, IRawElementProviderSimple))
        Return
    End If
    MyBase.WndProc(m)

End Sub

See also