public Monitor(UIElement element) { this.Element = element; this.HwndSource = ScreenLocation.FindTopLevelHwndSource(element); this.Element.LayoutUpdated += new EventHandler(this.OnLayoutUpdated); PresentationSource.AddSourceChangedHandler((IInputElement)element, new SourceChangedEventHandler(this.OnPresentationSourceChanged)); }
private static IntPtr FindTopLevelWindow(IntPtr hWnd) { while (hWnd != IntPtr.Zero && ScreenLocation.IsChildWindow(hWnd)) { hWnd = NativeMethods.GetParent(hWnd); } return(hWnd); }
public static HwndSource FindTopLevelHwndSource(UIElement element) { HwndSource hwndSource = (HwndSource)PresentationSource.FromVisual((Visual)element); if (hwndSource != null && ScreenLocation.IsChildWindow(hwndSource.Handle)) { hwndSource = HwndSource.FromHwnd(ScreenLocation.FindTopLevelWindow(hwndSource.Handle)); } return(hwndSource); }
private void TryUpdateLocation() { if (this.HwndSource == null || !this.Element.IsArrangeValid) { return; } Point point = DpiHelper.DeviceToLogicalUnits(this.Element.PointToScreen(new Point(0.0, 0.0))); ScreenLocation.SetLeft(this.Element, point.X); ScreenLocation.SetTop(this.Element, point.Y); ScreenLocation.SetRight(this.Element, point.X + this.Element.RenderSize.Width); ScreenLocation.SetBottom(this.Element, point.Y + this.Element.RenderSize.Height); }
private void OnPresentationSourceChanged(object sender, SourceChangedEventArgs args) { this.HwndSource = ScreenLocation.FindTopLevelHwndSource((UIElement)sender); this.TryUpdateLocation(); }