Share via


公開伺服器端 UI 自動化提供者

注意

本文件適用對象為 .NET Framework 開發人員,其想要使用 System.Windows.Automation 命名空間中定義的受控 UI 自動化類別。 如需 UI 自動化的最新資訊,請參閱 Windows 自動化 API:UI 自動化

本主題包含範例程式碼,顯示如何公開裝載在 System.Windows.Forms.Control 視窗中的伺服器端使用者介面自動化提供者。

此範例會覆寫視窗程序來取得 WM_GETOBJECT,這是核心服務在用戶端應用程式要求視窗相關資訊時所傳送的訊息。

範例

/// <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

另請參閱