Created
April 29, 2022 11:32
-
-
Save emoacht/47cf916d9750dc746dbbd2c542e64228 to your computer and use it in GitHub Desktop.
Get relative position of Popup to its parent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class PopupHelper | |
{ | |
[DllImport("User32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); | |
[StructLayout(LayoutKind.Sequential)] | |
private struct RECT | |
{ | |
public int Left; | |
public int Top; | |
public int Right; | |
public int Bottom; | |
public static implicit operator Rect(RECT rect) => new(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top); | |
} | |
public static Vector GetRelativePositionToParent(Popup popup) | |
{ | |
if ((PresentationSource.FromVisual(popup.Child) is HwndSource popupSource) && | |
GetWindowRect(popupSource.Handle, out RECT popupRect)) | |
{ | |
Point parentPosition = ((Visual)popup.Parent).PointToScreen(new Point(0, 0)); | |
return ((Rect)popupRect).Location - parentPosition; | |
} | |
return default; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://stackoverflow.com/questions/9183222/determine-popup-placement-relative-to-target