public static string GetPlacement(IntPtr windowHandle) { Win32.WINDOWPLACEMENT placement = new Win32.WINDOWPLACEMENT(); Api.GetWindowPlacement(windowHandle, out placement); using (MemoryStream memoryStream = new MemoryStream()) { serializer.Serialize(memoryStream, placement); return(Convert.ToBase64String(memoryStream.ToArray())); } }
public static void SetPlacement(IntPtr windowHandle, string placementXml) { if (!string.IsNullOrEmpty(placementXml)) { try { Win32.WINDOWPLACEMENT placement = (Win32.WINDOWPLACEMENT)serializer.Deserialize(new MemoryStream(Convert.FromBase64String(placementXml))); placement.length = Marshal.SizeOf(typeof(Win32.WINDOWPLACEMENT)); placement.flags = 0; placement.showCmd = (placement.showCmd == (int)SW.SW_SHOWMINIMIZED ? (int)SW.SW_SHOWNORMAL : placement.showCmd); Api.SetWindowPlacement(windowHandle, ref placement); } catch (Exception) { } } }
public static WindowPlacement SaveWindowPlacement(Window window) { WINDOWPLACEMENT wp = new WINDOWPLACEMENT(); IntPtr hwnd = new WindowInteropHelper(window).Handle; GetWindowPlacement(hwnd, out wp); var p = new WindowPlacement(); p.MinX = wp.minPosition.X; p.MinY = wp.minPosition.Y; p.MaxX = wp.maxPosition.X; p.MaxY = wp.maxPosition.Y; p.Left = wp.normalPosition.Left; p.Top = wp.normalPosition.Top; p.Right = wp.normalPosition.Right; p.Bottom = wp.normalPosition.Bottom; p.ShowMaximized = wp.showCmd == SW_SHOWMAXIMIZED; return p; }
public static void LoadWindowPlacement(Window window, WindowPlacement placement) { try { // Load window placement details for previous application session from application settings // Note - if window was closed on a monitor that is now disconnected from the computer, // SetWindowPlacement will place the window onto a visible monitor. WINDOWPLACEMENT wp = new WINDOWPLACEMENT(); wp.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT)); wp.flags = 0; var p = placement; wp.showCmd = p.ShowMaximized ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL; wp.minPosition = new POINT(p.MinX, p.MinY); wp.maxPosition = new POINT(p.MaxX, p.MaxY); wp.normalPosition = new RECT(p.Left, p.Top, p.Right, p.Bottom); IntPtr hwnd = new WindowInteropHelper(window).Handle; SetWindowPlacement(hwnd, ref wp); } catch { } }
public static extern int SetWindowPlacement(HWND hwnd, ref WINDOWPLACEMENT lpwndpl);
static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);
public static string GetPlacement(IntPtr windowHandle) { Win32.WINDOWPLACEMENT placement = new Win32.WINDOWPLACEMENT(); Api.GetWindowPlacement(windowHandle, out placement); using (MemoryStream memoryStream = new MemoryStream()) { serializer.Serialize(memoryStream, placement); return Convert.ToBase64String(memoryStream.ToArray()); } }