예제 #1
0
        /// <summary>
        /// Calculates a child's new bounds according to its Dock property and applies AutoSizeToContents
        /// </summary>
        /// <param name="control">The child control.</param>
        /// <param name="area">The area available to dock to.</param>
        /// <returns>New bounds of the control.</returns>
        public static Rectangle CalculateBounds(ControlBase control, ref Rectangle area)
        {
            if (control.IsHidden)
            {
                return(control.Bounds);
            }
            Rectangle ret = control.Bounds;

            if (control.AutoSizeToContents)
            {
                ret.Size = control.GetSizeToFitContents();
            }
            ret.Size = ClampSize(control, ret.Size);
            if (control.Dock == Dock.None)
            {
                return(ret);
            }
            Margin margin = control.Margin;

            if (control.Dock == Dock.Fill)
            {
                ret = new Rectangle(area.X + control.Margin.Left,
                                    area.Y + control.Margin.Top,
                                    area.Width - control.Margin.Width,
                                    area.Height - control.Margin.Height);
            }
            else if (control.Dock == Dock.Left)
            {
                ret = new Rectangle(area.X + margin.Left,
                                    area.Y + margin.Top,
                                    ret.Width,
                                    area.Height - margin.Height);

                int width = margin.Width + ret.Width;
                area.X     += width;
                area.Width -= width;
            }
            else if (control.Dock == Dock.Right)
            {
                ret = new Rectangle((area.X + area.Width) - ret.Width - margin.Right,
                                    area.Y + margin.Top,
                                    ret.Width,
                                    area.Height - margin.Height);

                int width = margin.Width + ret.Width;
                area.Width -= width;
            }
            else if (control.Dock == Dock.Top)
            {
                ret = new Rectangle(area.X + margin.Left,
                                    area.Y + margin.Top,
                                    area.Width - margin.Width,
                                    ret.Height);

                int height = margin.Height + ret.Height;
                area.Y      += height;
                area.Height -= height;
            }
            else if (control.Dock == Dock.Bottom)
            {
                ret = new Rectangle(area.X + margin.Left,
                                    (area.Y + area.Height) - ret.Height - margin.Bottom,
                                    area.Width - margin.Width,
                                    ret.Height);
                int height = margin.Height + ret.Height;
                area.Height -= height;
            }
            else
            {
                throw new Exception("Unhandled Dock Pos" + control.Dock);
            }
            return(ret);
        }