///////////////////////////////////////////////////////////////////////////////////////////////// // // RegisterStructureChangeListener() // // Add a UIA event handler to react to structure change events sent from the browser window. // // Runs on the background thread. // ///////////////////////////////////////////////////////////////////////////////////////////////// void RegisterStructureChangeListener() { // This sample assumes we know which window we want to gets events from. // *** Note: We must get a pointer to the element we're interested in, in this thread. Do // not have the UI thread get the element and supply it through to the background thread. IntPtr hwnd = Win32.FindWindow(strBrowserWindowClass, null); if (hwnd != IntPtr.Zero) { _elementBrowser = _automation.ElementFromHandle(hwnd); if (_elementBrowser != null) { // In general it is not the IEFrame element itself which throws the structure change // events which we will receive in the handler, but rather various decendants elements. // It is these descendants which are the "senders" in the event handler later. // AddStructureChangedEventHandler() can take a cache request as other UIA methods do. // So if an event handler wanted to retrieve properties from the event sender without // having to incur the time cost for a cross-proc call, it would use a cache request // here. (For this sample, the event handler doesn't need that.) // Other types of UIA event handler can be set up with the following calls: // AddFocusChangeEventHandler() // AddAutomationEventHandler() // AddPropertyChangeEventHandler() - note that some providers might not raise all // the expected propety change events. _automation.AddStructureChangedEventHandler(_elementBrowser, TreeScope.TreeScope_Descendants, null, this); _fAddedEventHandler = true; } } }
public override void Init() { IUIAutomation uia = this.IUIAutomation; if (uia != null) { uia.AddStructureChangedEventHandler(this.Element, this.Scope, null, this); this.IsHooked = true; } }