/// <summary> /// Indicate if the window is snapped or not on the given monitor /// </summary> /// <param name="location"></param> /// <param name="monitor"></param> /// <returns>True when window is snapped, false otherwise</returns> private static bool IsSnapped(ref Rect location, Monitor monitor) { // Otherwise, use the standard snap detection SnapBounds bounds = GetBounds(ref location, monitor); double windowHeight = location.Height; double workHeight = monitor.Work.Height; double displayHeight = monitor.Display.Height; // For corner snap, must take possible rounding into account double loHalfWorkHeight = Math.Floor(workHeight / 2.0d); double loHalfDisplayHeight = Math.Floor(displayHeight / 2.0d); double hiHalfWorkHeight = Math.Ceiling(workHeight / 2.0d); double hiHalfDisplayHeight = Math.Ceiling(displayHeight / 2.0d); // Maximized (top, left, bottom right) // Vertical snap (top, bottom) // Vertical left snap (top, bottom, left) // Vertical right snap (top, bottom, right) // Corner snap (top left, top right, bottom left, bottom right) if (windowHeight == workHeight || windowHeight == displayHeight || windowHeight == loHalfWorkHeight || windowHeight == loHalfDisplayHeight || windowHeight == hiHalfWorkHeight || windowHeight == hiHalfDisplayHeight) { return(bounds != SnapBounds.None); } return(false); }
/// <summary> /// Obtain the corresponding snapped bounds of the window /// </summary> /// <param name="location"></param> /// <param name="monitor"></param> /// <returns>The snapped bounds</returns> private static SnapBounds GetBounds(ref Rect location, Monitor monitor) { SnapBounds bounds = SnapBounds.None; Monitor.Area work = monitor.Work; Monitor.Area display = monitor.Display; double left = location.Left; double top = location.Top; double right = location.Right; double bottom = location.Bottom; // TOP if (top == work.Top || top == display.Top) { bounds |= SnapBounds.Top; } // LEFT if (left == work.Left || left == display.Left) { bounds |= SnapBounds.Left; } // RIGHT if (right == work.Right || right == display.Right) { bounds |= SnapBounds.Right; } // BOTTOM if (bottom == work.Bottom || bottom == display.Bottom) { bounds |= SnapBounds.Bottom; } return(bounds); }