System.Drawing.Rectangle.Union C# (CSharp) Method

Union() private method

private Union ( Rectangle a, Rectangle b ) : Rectangle
a Rectangle
b Rectangle
return Rectangle
        public static Rectangle Union(Rectangle a, Rectangle b)
        {
            int x1 = Math.Min(a.X, b.X);
            int x2 = Math.Max(a.X + a.Width, b.X + b.Width);
            int y1 = Math.Min(a.Y, b.Y);
            int y2 = Math.Max(a.Y + a.Height, b.Y + b.Height);

            return new Rectangle(x1, y1, x2 - x1, y2 - y1);
        }

Same methods

Rectangle::Union ( System a, System b ) : System.Drawing.Rectangle

Usage Example

        protected override void OnContentRendered(EventArgs e)
        {
            base.OnContentRendered(e);
            if (Shown)
            {
                return;
            }
            Shown = true;

            // make window span over all screens
            Rectangle totalSize = Rectangle.Empty;

            XOffset = 0;
            foreach (Screen screen in Screen.AllScreens)
            {
                // if bounds smaller 0 the screens are left next to primary screen
                if (screen.Bounds.X < 0)
                {
                    if (screen.Bounds.X < XOffset)
                    {
                        XOffset = screen.Bounds.X;
                    }
                }

                totalSize = Rectangle.Union(totalSize, screen.Bounds);
            }

            XOffset = Math.Abs(XOffset);

            Width  = totalSize.Width;
            Height = totalSize.Height;
            Left   = totalSize.Left;
            Top    = totalSize.Top;

            UpdateLayout();
        }