using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Windows.Forms; namespace OnTopReplica { /// /// Common geometry extension methods. /// static class GeometryExtensions { /// /// Returns the difference (offset vector) between two points. /// public static Point Difference(this Point a, Point b) { return new Point(a.X - b.X, a.Y - b.Y); } /// /// Expands a size value by a padding distance. /// public static Size Expand(this Size size, Padding padding) { return new Size(size.Width + padding.Horizontal, size.Height + padding.Vertical); } /// /// Expands a size value by a size distance. /// public static Size Expand(this Size size, Size expandSize) { return new Size(size.Width + expandSize.Width, size.Height + expandSize.Height); } /// /// Computes the difference between two size values. /// public static Size Difference(this Size a, Size b) { return new Size(a.Width - b.Width, a.Height - b.Height); } /// /// Ensures that the minimum size of a control respects a minimum /// client size area. /// /// Control whose MinimumSize should be altered. /// Minimum client size value to ensure. public static void EnsureMinimumClientSize(this Control ctrl, Size minimumClientSize) { Size offset = ctrl.Size.Difference(ctrl.ClientSize); ctrl.MinimumSize = minimumClientSize.Expand(offset); } /// /// Attempts to fit a size structure to another fixed destination size, by maintaining /// the original aspect ratio. /// public static Size Fit(this Size sourceSize, Size destinationSize) { double sourceRatio = (double)sourceSize.Width / (double)sourceSize.Height; double clientRatio = (double)destinationSize.Width / (double)destinationSize.Height; Size ret; if (sourceRatio >= clientRatio) { ret = new Size(destinationSize.Width, (int)((double)destinationSize.Width / sourceRatio)); } else { ret = new Size((int)((double)destinationSize.Height * sourceRatio), destinationSize.Height); } return ret; } } }