protected virtual void OnCurrentHwndSourceChanged(DependencyPropertyChangedEventArgs e)
{
Initialize();
// Unregister the old keyboard input site.
IKeyboardInputSite keyboardInputSite = ((IKeyboardInputSink)this).KeyboardInputSite;
if (keyboardInputSite != null) {
((IKeyboardInputSink)this).KeyboardInputSite = null;
keyboardInputSite.Unregister();
}
// Register the new keyboard input site with the containing
// HwndSource.
IKeyboardInputSink sink = CurrentHwndSource;
if (sink != null) {
((IKeyboardInputSink)this).KeyboardInputSite = sink.RegisterKeyboardInputSink(this);
}
// Set the owner of the RedirectedWindow to our CurrentHwndSource.
// This keeps the RedirectedWindow on top of the HwndSource.
if (CurrentHwndSource != null) {
HWND hwndSource = new HWND(CurrentHwndSource.Handle);
HWND hwndRoot = hwndSource; // User32NativeMethods.GetAncestor(hwndSource, GA.ROOT); // need to get the top-level window?
NativeMethods.SetWindowLongPtr(
_redirectedWindow.Handle,
GWL.HWNDPARENT,
hwndRoot.DangerousGetHandle());
}
}