public static string GetPlacement(this Window window) { return(WindowPlacement.GetPlacement(new WindowInteropHelper(window).Handle)); }
public static void SetPlacement(IntPtr windowHandle, RECT placementRect) { try { WINDOWPLACEMENT lpwndpl; using (XmlReader xmlReader = XmlReader.Create((Stream) new MemoryStream(WindowPlacement.encoding.GetBytes(WindowPlacement.GetPlacement(windowHandle))))) lpwndpl = (WINDOWPLACEMENT)WindowPlacement.serializer.Deserialize(xmlReader); lpwndpl.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT)); lpwndpl.flags = 0; lpwndpl.showCmd = lpwndpl.showCmd == 2 ? 1 : lpwndpl.showCmd; IntPtr hMonitor = WindowPlacement.MonitorFromRect(ref placementRect, 2U); MONITORINFO monitorinfo = new MONITORINFO() { cbSize = Marshal.SizeOf(typeof(MONITORINFO)) }; ref MONITORINFO local = ref monitorinfo; if (WindowPlacement.GetMonitorInfo(hMonitor, ref local) && !WindowPlacement.RectangleEntirelyInside(monitorinfo.rcMonitor, placementRect)) { lpwndpl.normalPosition = WindowPlacement.PlaceOnScreen(monitorinfo.rcMonitor, placementRect); } WindowPlacement.SetWindowPlacement(windowHandle, ref lpwndpl); }