Share via


IUIAutomation::AddFocusChangedEventHandler Method

Registers a method that handles focus-changed events.

Syntax

HRESULT AddFocusChangedEventHandler(      
    IUIAutomationCacheRequest *cacheRequest,
    IUIAutomationFocusChangedEventHandler *handler
);

Parameters

Return Value

Returns S_OK if successful, or an error value otherwise.

Remarks

Focus-changed events are system-wide; you cannot set a narrower scope.

A UI Automation client should not use multiple threads to add or remove event handlers. Unexpected behavior can result if one event handler is being added or removed while another is being added or removed in the same client process.

Example

The following example function creates an object that implements IUIAutomationFocusChangedEventHandler and subscribes to the event by adding the handler.

HRESULT AddFocusHandler(IUIAutomation* pAutomation)
{ 
    // CFocusHandler is a class that implements IUIAutomationFocusChangedEventHandler. 
    CFocusHandler* pFocusHandler = new CFocusHandler();
    if (!pFocusHandler)
    {
        return E_OUTOFMEMORY;
    }
    IUIAutomationFocusChangedEventHandler* pHandler;
    pFocusHandler->QueryInterface(IID_IUIAutomationFocusChangedEventHandler, (void**)&pHandler);
    HRESULT hr = pAutomation->AddFocusChangedEventHandler(NULL, pHandler);
    pFocusHandler->Release();
    return hr;
}

See Also

IUIAutomationFocusChangedEventHandler, IUIAutomation::RemoveFocusChangedEventHandler, IUIAutomation::RemoveAllEventHandlers, Caching UI Automation Properties and Control Patterns, UI Automation Events for Clients