Ejemplo n.º 1
0
        /// <summary>
        /// 布局文本和图片,每调用一次,执行一次布局操作
        /// </summary>
        /// <param name="layout">布局对象</param>
        public static void LayoutTextAndImage(LayoutData layout)
        {
            if (((layout.ImageSize == Size.Empty) || (layout.Text == null)) || ((layout.Text.Length == 0) || (layout.CurrentTextImageRelation == TextImageRelation.Overlay)))
            {
                Size imageSize = layout.ImageSize;
                Size textSize  = GetTextSize(layout, layout.CurrentClientRectangle.Size);

                layout.OutImageBounds = LayoutUtils.Align(imageSize, layout.CurrentClientRectangle, layout.CurrentImageAlign);
                layout.OutTextBounds  = LayoutUtils.Align(textSize, layout.CurrentClientRectangle, layout.CurrentTextAlign);
            }
            else
            {
                Size      proposedSize  = LayoutUtils.SubAlignedRegion(layout.CurrentClientRectangle.Size, layout.ImageSize, layout.CurrentTextImageRelation); //减去图片空间后的大小
                Size      textSize      = GetTextSize(layout, proposedSize);                                                                                   //文本大小
                Size      textImageSize = LayoutUtils.AddAlignedRegion(textSize, layout.ImageSize, layout.CurrentTextImageRelation);                           //两个大小相加
                Rectangle containerRect = layout.CurrentClientRectangle;                                                                                       //能包含layout.CurrentClientRectangle,textImageSize的最小容器
                containerRect.Size = LayoutUtils.UnionSizes(layout.CurrentClientRectangle.Size, textImageSize);                                                //获取两个Size中的最大值(分别计算宽和高)
                Rectangle textImageRect  = LayoutUtils.Align(textImageSize, containerRect, ContentAlignment.MiddleCenter);                                     //居中
                bool      imageNoOverlay = (ImageAlignToRelation(layout.CurrentImageAlign) & layout.CurrentTextImageRelation) != TextImageRelation.Overlay;
                bool      textNoOverlay  = (TextAlignToRelation(layout.CurrentTextAlign) & layout.CurrentTextImageRelation) != TextImageRelation.Overlay;
                Rectangle imageBounds;
                Rectangle textBounds;
                if (imageNoOverlay)
                {
                    LayoutUtils.SplitRegion(containerRect, layout.ImageSize, (AnchorStyles)layout.CurrentTextImageRelation, out imageBounds, out textBounds);
                }
                else if (textNoOverlay)
                {
                    LayoutUtils.SplitRegion(containerRect, textSize, (AnchorStyles)LayoutUtils.GetOppositeTextImageRelation(layout.CurrentTextImageRelation), out textBounds, out imageBounds);
                }
                else
                {
                    LayoutUtils.SplitRegion(textImageRect, layout.ImageSize, (AnchorStyles)layout.CurrentTextImageRelation, out imageBounds, out textBounds);
                    LayoutUtils.ExpandRegionsToFillBounds(containerRect, (AnchorStyles)layout.CurrentTextImageRelation, ref imageBounds, ref textBounds);//扩展
                }
                layout.OutImageBounds = LayoutUtils.Align(layout.ImageSize, imageBounds, layout.CurrentImageAlign);
                layout.OutTextBounds  = LayoutUtils.Align(textSize, textBounds, layout.CurrentTextAlign);
            }

            //调整
            switch (layout.CurrentTextImageRelation)
            {
            case TextImageRelation.TextBeforeImage:
            case TextImageRelation.ImageBeforeText:
            {
                int num = Math.Min(layout.OutTextBounds.Bottom, layout.CurrentClientRectangle.Bottom);
                layout.OutTextBounds.Y      = Math.Max(Math.Min(layout.OutTextBounds.Y, layout.CurrentClientRectangle.Y + ((layout.CurrentClientRectangle.Height - layout.OutTextBounds.Height) / 2)), layout.CurrentClientRectangle.Y);
                layout.OutTextBounds.Height = num - layout.OutTextBounds.Y;
                break;
            }

            case TextImageRelation.TextAboveImage:
            case TextImageRelation.ImageAboveText:
            {
                int num2 = Math.Min(layout.OutTextBounds.Right, layout.CurrentClientRectangle.Right);
                layout.OutTextBounds.X     = Math.Max(Math.Min(layout.OutTextBounds.X, layout.CurrentClientRectangle.X + ((layout.CurrentClientRectangle.Width - layout.OutTextBounds.Width) / 2)), layout.CurrentClientRectangle.X);
                layout.OutTextBounds.Width = num2 - layout.OutTextBounds.X;
                break;
            }

            default:
                break;
            }
            if ((layout.CurrentTextImageRelation == TextImageRelation.ImageBeforeText) && (layout.OutImageBounds.Size.Width != 0))
            {
                layout.OutImageBounds.Width = Math.Max(0, Math.Min(layout.CurrentClientRectangle.Width - layout.OutTextBounds.Width, layout.OutImageBounds.Width));
                layout.OutTextBounds.X      = layout.OutImageBounds.X + layout.OutImageBounds.Width;
            }
            if ((layout.CurrentTextImageRelation == TextImageRelation.ImageAboveText) && (layout.OutImageBounds.Size.Height != 0))
            {
                layout.OutImageBounds.Height = Math.Max(0, Math.Min(layout.CurrentClientRectangle.Height - layout.OutTextBounds.Height, layout.OutImageBounds.Height));
                layout.OutTextBounds.Y       = layout.OutImageBounds.Y + layout.OutImageBounds.Height;
            }
            layout.OutTextBounds = Rectangle.Intersect(layout.OutTextBounds, layout.CurrentClientRectangle);
            //
            int num3 = Math.Min(layout.OutTextBounds.Bottom, layout.CurrentClientRectangle.Bottom);

            layout.OutTextBounds.Y      = Math.Max(layout.OutTextBounds.Y, layout.CurrentClientRectangle.Y);
            layout.OutTextBounds.Height = num3 - layout.OutTextBounds.Y;

            //偏移
            layout.OutImageBounds.Offset(layout.ImageOffset);
            layout.OutTextBounds.Offset(layout.TextOffset);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 测试文本大小
 /// </summary>
 /// <param name="layout">布局对象</param>
 /// <param name="proposedSize">可提供的大小</param>
 /// <returns>文本大小</returns>
 public static Size GetTextSize(LayoutData layout, Size proposedSize)
 {
     return(Size.Ceiling(layout.Graphics.MeasureString(layout.Text, layout.Font, new SizeF((float)proposedSize.Width, (float)proposedSize.Height), layout.CurrentStringFormat)));
 }