如何從消費者介面自動化提供者引發事件

本主題包含示範 Microsoft 消費者介面自動化 提供者如何引發事件的範例程式碼。

下列範例程式碼顯示實作自訂按鈕之應用程式的方法。 每當叫用自訂按鈕時,應用程式就會呼叫 方法。 方法會檢查是否有任何用戶端正在接聽事件,如果是,則會引發 UIA_Invoke_InvokedEventId 事件,以通知用戶端已叫用按鈕。

// Responds to a button click. The source of the click could 
// be the mouse, the keyboard, or a client's call to 
// IUIAutomationInvokePattern::Invoke.
void CustomButton::InvokeButton(HWND hwnd)
{
    // TODO: Perform program actions invoked by the control.

    // Check whether any clients are listening for UI Automation 
    // events.
    if (UiaClientsAreListening())
    {
        // Raise an Invoked event. GetUIAutomationProvider is an
        // application-defined method that returns a pointer to
        // the application's IRawElementProviderSimple interface.
        UiaRaiseAutomationEvent(
            GetUIAutomationProvider(hwnd), UIA_Invoke_InvokedEventId); 
    }
}

概念

UI 自動化事件概觀

消費者介面自動化提供者的操作說明主題