static extern bool GetWindowPlacement(IntPtr hWnd, out WindowPlacement lpwndpl);
/// <summary> /// Sets the placement of a window /// </summary> /// <param name="placement">The position of the window</param> /// <param name="window">The window to position</param> public static void SetWindowPlacement(WindowPlacement placement, Window window) { SetWindowPlacement(placement, window, true, true); }
/// <summary> /// Sets the placement of a window /// </summary> /// <param name="placement">The position of the window</param> /// <param name="window">The window to position</param> /// <param name="allowMaximized">Whether or not maximized windows are positioned as such</param> /// <param name="allowMinimized">Whether or not minimized windows are positioned as such</param> public static void SetWindowPlacement(WindowPlacement placement, Window window, bool allowMaximized, bool allowMinimized) { 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. if (placement.ShowCommand != 0) { WindowPlacement windowPlacement = placement; windowPlacement.Length = Marshal.SizeOf(typeof(WindowPlacement)); windowPlacement.Flag = 0; if ((!allowMinimized && placement.ShowCommand == WindowShowMinimized) || (!allowMaximized && placement.ShowCommand == WindowShowMaximized)) { placement.ShowCommand = WindowShowNormal; } IntPtr windowHandle = new WindowInteropHelper(window).Handle; SetWindowPlacement(windowHandle, ref windowPlacement); } } catch { } }
/// <summary> /// Gets the WindowPlacement structure for a window. /// </summary> /// <param name="window">The Window to get the placement info for.</param> /// <returns>The WindowPlacement structure with window placement info</returns> public static WindowPlacement GetWindowPlacement(Window window) { // Persist window placement details to application settings WindowPlacement windowPlacement = new WindowPlacement(); IntPtr windowHandle = new WindowInteropHelper(window).Handle; GetWindowPlacement(windowHandle, out windowPlacement); return windowPlacement; }