I want to position a modal dialog (progress window) at the top right corner of the parent window client area.
This code will put it in the corner of the non-client area, but how do I calculate the offset to the client area?
this.Owner=owner;
this.Left=owner.Left+owner.ActualWidth-Width;
this.Top=owner.Top;
Edit:
I found this ‘solution’ that works for normal windows:
this.Left=owner.Left+owner.ActualWidth-Width-SystemParameters.ResizeFrameVerticalBorderWidth;
this.Top=owner.Top+SystemParameters.ResizeFrameHorizontalBorderHeight+SystemParameters.WindowCaptionHeight;
This would however fail for windows that have customized borders.
EDIT:
The code should work regardless of the systems DPI setting (e.g. 120 instead of 96).
As long as your window content is a subclass of UIElement (which is normally the case), you can simply check the area covered by the Content:
If you have a strange situation where you want this to work for arbitrary
Window.Content, you’ll have to search the window’s visual tree usingVisualTreeHelper.GetChildCount()andVisualTreeHelper.GetChild()until you come to aContentPresenterwhoseContentproperty matches that of theWindow, and use its first visual child as “windowContent” in the above code.