//#define DRAWTEST /******************************************************************************* * Copyright(c) 2012 dongke All rights reserved. / Confidential * 类的信息: * 1.程序名称:LayoutUtility.cs * 2.功能描述:Layout工具类 * 编辑履历: * 作者 日期 版本 修改内容 * 欧阳涛 2012/09/11 1.00 新建 *******************************************************************************/ using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; //using Microsoft.VisualBasic.PowerPacks; namespace Dongke.WinForm.Controls.InvoiceLayout { public static class LayoutUtility { #region 函数 ///// ///// 获取图形实例 ///// ///// ///// //public static Shape GetNewShape(LayoutItem item) //{ // Shape shape = null; // switch (item.ItemType) // { // case ItemType.Ellipse: // OvalShape eShape = new OvalShape(); // eShape.SelectionColor = Color.Transparent; // eShape.BorderStyle = DashStyle.Solid; // eShape.BorderColor = Color.Black; // shape = eShape; // break; // case ItemType.Rectangle: // RectangleShape rShape = new RectangleShape(); // rShape.SelectionColor = Color.Transparent; // rShape.BorderStyle = DashStyle.Solid; // rShape.BorderColor = Color.Black; // shape = rShape; // break; // case ItemType.Image: // case ItemType.Text: // case ItemType.Grid: // case ItemType.PageNum: // default: // RectangleShape dShape = new RectangleShape(); // dShape.SelectionColor = Color.Transparent; // dShape.BorderStyle = DashStyle.Custom; // dShape.BorderColor = Color.Transparent; // shape = dShape; // break; // } // return shape; //} #region 绘制直线 /// /// 绘制直线 /// /// Graphics /// 起点 /// 终点 /// 线的颜色 /// 线宽(mm) public static void DrawLine( Graphics graphics, PointF startPoint, PointF endPoint, Color lineColor, float lineWidth) { DrawLine(graphics, graphics.PageUnit, startPoint, endPoint, lineColor, lineWidth); } /// /// 绘制直线 /// /// Graphics /// GraphicsUnit /// 起点 /// 终点 /// 线的颜色 /// 线宽(mm) public static void DrawLine( Graphics graphics, GraphicsUnit unit, PointF startPoint, PointF endPoint, Color lineColor, float lineWidth) { if (lineWidth == 0 || ((startPoint.X == endPoint.X) && (startPoint.Y == endPoint.Y))) { return; } GraphicsContainer containerState = graphics.BeginContainer(); graphics.PageUnit = unit; SmoothingMode smoothingMode = graphics.SmoothingMode; try { using (Pen pen = new Pen(lineColor)) { graphics.SmoothingMode = SmoothingMode.AntiAlias; pen.Width = Math.Max(LayoutCommon.PixelToMillimeter(1), lineWidth); graphics.DrawLine(pen, startPoint, endPoint); } } finally { graphics.SmoothingMode = smoothingMode; graphics.EndContainer(containerState); } } #endregion 绘制直线 #region 绘制椭圆 /// /// 绘制椭圆 /// /// Graphics /// 图形范围 /// 边线颜色 /// 填充颜色 /// 边线宽(mm) /// 背景是否透明 public static void DrawEllipse( Graphics graphics, RectangleF rectangle, Color lineColor, Color fillColor, float lineWidth, bool isTransparent) { DrawEllipse(graphics, graphics.PageUnit, rectangle, lineColor, fillColor, lineWidth, isTransparent); } /// /// 绘制椭圆 /// /// Graphics /// GraphicsUnit /// 图形范围 /// 边线颜色 /// 填充颜色 /// 边线宽(mm) /// 背景是否透明 public static void DrawEllipse( Graphics graphics, GraphicsUnit unit, RectangleF rectangle, Color lineColor, Color fillColor, float lineWidth, bool isTransparent) { if ((rectangle.Width < 0) || (rectangle.Height < 0)) { return; } GraphicsContainer containerState = graphics.BeginContainer(); graphics.PageUnit = unit; SmoothingMode smoothingMode = graphics.SmoothingMode; try { if (!isTransparent && (rectangle.Width > 0) && (rectangle.Height > 0)) { graphics.SmoothingMode = SmoothingMode.Default; using (Brush brush = new SolidBrush(fillColor)) { graphics.FillEllipse(brush, rectangle); } } if (lineWidth != 0) { float minWidth = LayoutCommon.PixelToMillimeter(1); using (Pen pen = new Pen(lineColor)) { if ((rectangle.Width > 0) && (rectangle.Height == 0)) { pen.Width = minWidth; graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.DrawLine(pen, rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Top); } else if ((rectangle.Width == 0) && (rectangle.Height > 0)) { pen.Width = minWidth; graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.DrawLine(pen, rectangle.Left, rectangle.Top, rectangle.Left, rectangle.Bottom); } else { float num = Math.Min(rectangle.Width, rectangle.Height) / 2; num = Math.Min(num, lineWidth); pen.Width = Math.Max(minWidth, num); graphics.SmoothingMode = SmoothingMode.Default; graphics.DrawEllipse(pen, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height); } } } } finally { graphics.SmoothingMode = smoothingMode; graphics.EndContainer(containerState); } } #endregion 绘制椭圆 #region 绘制矩形 /// /// 绘制矩形 /// /// Graphics /// GraphicsUnit /// 图形范围 /// 边线颜色 /// 填充颜色 /// 边线宽(mm) /// 背景是否透明 public static void DrawRectangle( Graphics graphics, RectangleF rectangle, Color lineColor, Color fillColor, float lineWidth, bool isTransparent) { DrawRectangle(graphics, graphics.PageUnit, rectangle, lineColor, fillColor, lineWidth, isTransparent); } /// /// 绘制矩形 /// /// Graphics /// GraphicsUnit /// 图形范围 /// 边线颜色 /// 填充颜色 /// 边线宽(mm) /// 背景是否透明 public static void DrawRectangle( Graphics graphics, GraphicsUnit unit, RectangleF rectangle, Color lineColor, Color fillColor, float lineWidth, bool isTransparent) { if ((rectangle.Width < 0) || (rectangle.Height < 0)) { return; } GraphicsContainer containerState = graphics.BeginContainer(); graphics.PageUnit = unit; SmoothingMode smoothingMode = graphics.SmoothingMode; try { if (!isTransparent && (rectangle.Width > 0) && (rectangle.Height > 0)) { graphics.SmoothingMode = SmoothingMode.Default; using (Brush brush = new SolidBrush(fillColor)) { graphics.FillRectangle(brush, rectangle); } } if (lineWidth != 0) { float minWidth = LayoutCommon.PixelToMillimeter(1); using (Pen pen = new Pen(lineColor)) { if ((rectangle.Width > 0) && (rectangle.Height == 0)) { pen.Width = minWidth; graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.DrawLine(pen, rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Top); } else if ((rectangle.Width == 0) && (rectangle.Height > 0)) { pen.Width = minWidth; graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.DrawLine(pen, rectangle.Left, rectangle.Top, rectangle.Left, rectangle.Bottom); } else { float num = Math.Min(rectangle.Width, rectangle.Height) / 2; num = Math.Min(num, lineWidth); pen.Width = Math.Max(minWidth, num); graphics.SmoothingMode = SmoothingMode.Default; graphics.DrawRectangle(pen, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height); } } } } finally { graphics.SmoothingMode = smoothingMode; graphics.EndContainer(containerState); } } #endregion 绘制矩形 #region 绘制图片 /// /// 绘制图片 /// /// Graphics /// 图片范围 /// 图片 public static void DrawImage( Graphics graphics, RectangleF rectangle, Image image) { DrawImage(graphics, graphics.PageUnit, rectangle, image); } /// /// 绘制图片 /// /// Graphics /// GraphicsUnit /// 图片范围 /// 图片 public static void DrawImage( Graphics graphics, GraphicsUnit unit, RectangleF rectangle, Image image) { if (image == null || (rectangle.Width < 0) || (rectangle.Height < 0)) { return; } GraphicsContainer containerState = graphics.BeginContainer(); graphics.PageUnit = unit; graphics.DrawImage(image, rectangle); graphics.EndContainer(containerState); } #endregion 绘制图片 #region 绘制文本 /// /// 绘制文本 /// /// Graphics /// 绘制范围(余白) /// 绘制范围 /// 文本 /// 字体 /// 字体颜色 /// 行间隔 /// 字间隔 /// 行文字数 /// 文本对齐方式 /// 文本垂直对齐方式 /// 是否自动文本换行 /// 是否剪裁延伸到边框外侧的文本 /// 是否选择 public static void DrawText( Graphics graphics, RectangleF rectangleM, RectangleF rectangle, string context, Font font, Color foreColor, float lineSpace, float charSpace, int charCount, TextAlignment alignment, // 文本垂直对齐方式 modify by chenxy 2014-03-28 begin TextAlignment alignmentVertical, // 文本垂直对齐方式 modify by chenxy 2014-03-28 end bool wrap, bool clip, bool selected) { // 文本垂直对齐方式 modify by chenxy 2014-03-28 begin //DrawText(graphics, graphics.PageUnit, rectangleM, rectangle, context, font, foreColor, // lineSpace, charSpace, charCount, alignment, wrap, clip, selected); DrawText(graphics, graphics.PageUnit, rectangleM, rectangle, context, font, foreColor, lineSpace, charSpace, charCount, alignment, alignmentVertical, wrap, clip, selected); // 文本垂直对齐方式 modify by chenxy 2014-03-28 end } /// /// 绘制文本 /// /// Graphics /// GraphicsUnit /// 绘制范围(余白) /// 绘制范围 /// 文本 /// 字体 /// 字体颜色 /// 行间隔 /// 字间隔 /// 行文字数 /// 文本对齐方式 /// 文本垂直对齐方式 /// 是否自动文本换行 /// 是否剪裁延伸到边框外侧的文本 /// 是否选择 public static void DrawText( Graphics graphics, GraphicsUnit unit, RectangleF rectangleM, RectangleF rectangle, string context, Font font, Color foreColor, float lineSpace, float charSpace, int charCount, TextAlignment alignment, // 文本垂直对齐方式 modify by chenxy 2014-03-28 begin TextAlignment alignmentVertical, // 文本垂直对齐方式 modify by chenxy 2014-03-28 end bool wrap, bool clip, bool selected) { clip = false; selected = false; if (string.IsNullOrEmpty(context) || rectangle.Width <= 0 || rectangle.Height <= 0) { return; } GraphicsContainer containerState = graphics.BeginContainer(); graphics.PageUnit = unit; graphics.SetClip(rectangleM, CombineMode.Intersect); string[] lines = System.Text.RegularExpressions.Regex .Split(context, System.Environment.NewLine); using (SolidBrush textBrush = new SolidBrush(foreColor)) { #if DRAWTEST graphics.DrawRectangle(new Pen(Color.Red, 0.1f), rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); #endif // 文本垂直对齐方式 modify by chenxy 2014-03-28 begin //DrawString(graphics, lines, font, textBrush, lineSpace, charSpace, // charCount, rectangle, alignment, wrap, clip, selected); DrawString(graphics, lines, font, textBrush, lineSpace, charSpace, charCount, rectangle, alignment, alignmentVertical, wrap, clip, selected); // 文本垂直对齐方式 modify by chenxy 2014-03-28 end } graphics.EndContainer(containerState); } /// /// 横向绘制文本 /// /// Graphics /// 多行文本 /// 字体 /// 字体笔刷 /// 行间隔 /// 字间隔 /// 行文字数 /// 文本范围 /// 文本对齐方式 /// 是否自动文本换行 /// 是否剪裁延伸到边框外侧的文本 /// 是否选择 private static void DrawString( Graphics graphics, string[] lines, Font font, SolidBrush textBrush, float lineSpace, float charSpace, int charCount, RectangleF rectangle, TextAlignment alignment, // 文本垂直对齐方式 modify by chenxy 2014-03-28 begin TextAlignment alignmentVertical, // 文本垂直对齐方式 modify by chenxy 2014-03-28 end bool wrap, bool clip, bool selected) { SizeF fullSizeF = graphics.MeasureString(LayoutConsts.FULLSHAPE_TEXT, font, PointF.Empty, LayoutCommon.StringFormatHorizontal); // 单行文字数限制。 if (0 < charCount) { System.Text.StringBuilder sbLine = new System.Text.StringBuilder(); List newLines = new List(); foreach (string line in lines) { char[] chars = line.ToCharArray(); int index = 0; while (index < chars.Length) { int nextIndex = index + charCount; if (nextIndex <= chars.Length) { for (int i = index; i < nextIndex; i++) { sbLine.Append(chars[i]); } } else { for (int i = index; i < chars.Length; i++) { sbLine.Append(chars[i]); } } newLines.Add(sbLine.ToString()); sbLine.Length = 0; index = nextIndex; } } lines = newLines.ToArray(); if (charCount == 1) { alignment = TextAlignment.Evenness; } else { alignment = TextAlignment.LeftOrTop; charSpace = (rectangle.Width - fullSizeF.Width * charCount) / (charCount - 1); } } SizeF halfSizeF = graphics.MeasureString(LayoutConsts.HALFSHAPE_TEXT, font, PointF.Empty, LayoutCommon.StringFormatHorizontal); // 单行文字绘制开始 bool isRowStart = false; // 单行文字绘制结束 bool isRowEnd = false; // 一行文字绘制开始 //bool isLineStart = true; // 一行文字绘制结束 bool isLineEnd = false; // 多行文字绘制开始 bool isLinesStart = true; // 多行文字绘制结束 bool isLinesEnd = false; // 文字整体(字间隔,行间隔)范围 float pointerTop = rectangle.Top; float pointerBottom = rectangle.Top + fullSizeF.Height; float pointerLeft = rectangle.Left; // 文字范围 float textTop = rectangle.Top; float textLeft = rectangle.Left; //// 选择状态行间隔范围 //RectangleF lineSpaceSelectedRectangleF = RectangleF.Empty; // 文字范围 List textRectangleFs = new List(); // 文本垂直对齐方式 modify by chenxy 2014-03-28 begin List allTextRectFs = new List(); // 文本垂直对齐方式 modify by chenxy 2014-03-28 end // 文本绘制 for (int lineIndex = 0; lineIndex < lines.Length; lineIndex++) { // 一行文本绘制 //isLineStart = true; isLineEnd = false; int colIndex = 0; int lineCharCount = lines[lineIndex].Length; char[] chars = lines[lineIndex].ToCharArray(); while (true) { isRowStart = true; isRowEnd = false; textLeft = rectangle.Left; pointerLeft = rectangle.Left; if (colIndex < lineCharCount) { // 单行文字绘制 for (int charIndex = colIndex; charIndex < lineCharCount; charIndex++, colIndex++) { SizeF nextTeatSizeF; TextRectangleF nextTextRectangleF = new TextRectangleF(); nextTextRectangleF.Text = chars[charIndex].ToString(); // 单行文字数限制 if (0 < charCount) { nextTeatSizeF = fullSizeF; } else { // 半角空格的时候 if (LayoutConsts.SPACE_HALFSHAPE_CHAR.Equals(chars[charIndex])) { nextTeatSizeF = halfSizeF; nextTeatSizeF.Height = fullSizeF.Height; } // 全角空格的时候 else if (LayoutConsts.SPACE_FULLSHAPE_CHAR.Equals(chars[charIndex])) { nextTeatSizeF = fullSizeF; } // 其他 else { nextTeatSizeF = graphics.MeasureString(nextTextRectangleF.Text, font, PointF.Empty, LayoutCommon.StringFormatHorizontal); nextTeatSizeF.Height = fullSizeF.Height; } } if (isRowStart) { float bottom = textTop; if (!isLinesStart) { bottom += lineSpace; } // 剪裁 if (clip) { bottom += nextTeatSizeF.Height; } // 多行绘制结束 if (rectangle.Bottom < bottom) { isLinesEnd = true; break; } //// 选择状态行间隔范围填充 //if (selected // && 0 < lineSpace // && lineSpaceSelectedRectangleF != RectangleF.Empty) //{ // lineSpaceSelectedRectangleF.Height = lineSpace; // graphics.FillRectangle(LayoutCommon.SelectedBrush, lineSpaceSelectedRectangleF); // lineSpaceSelectedRectangleF = RectangleF.Empty; //} if (0 == charCount) { // 自动换行 float right = textLeft; if (rectangle.Right < right) { if (wrap) { // 单行结束 isRowEnd = true; break; } else { if (clip) { // 一行结束 isLineEnd = true; break; } } } } } else { if (0 == charCount) { float right = textLeft + charSpace + nextTeatSizeF.Width; // 自动换行 if (wrap) { if (rectangle.Right < right) { // 文本对齐方式 float correcting = 0; if (alignment == TextAlignment.RightOrBottom) { correcting = rectangle.Right - textLeft; if (0 < correcting) { foreach (TextRectangleF rf in textRectangleFs) { rf.RectangleX = rf.RectangleF.X + correcting; } } } else if (alignment == TextAlignment.Center) { correcting = (rectangle.Right - textLeft) / 2; if (0 < correcting) { foreach (TextRectangleF rf in textRectangleFs) { rf.RectangleX = rf.RectangleF.X + correcting; } } } else if (alignment == TextAlignment.Evenness) { if (1 < textRectangleFs.Count) { correcting = (rectangle.Right - textLeft) / (textRectangleFs.Count - 1); if (0 < correcting) { for (int j = 0; j < textRectangleFs.Count; j++) { textRectangleFs[j].RectangleX += correcting * j; } } } else { textRectangleFs[0].RectangleX += (rectangle.Right - textLeft) / 2; } } // 单行结束 isRowEnd = true; break; } } else if (alignment == TextAlignment.LeftOrTop || alignment == TextAlignment.Evenness) { if (clip) { if (rectangle.Right < right) { // 一行结束 isLineEnd = true; break; } } else { if (rectangle.Right < textLeft + charSpace) { // 一行结束 isLineEnd = true; break; } } } } } nextTextRectangleF.RectangleF = new RectangleF(pointerLeft, pointerTop, nextTeatSizeF.Width, nextTeatSizeF.Height); textRectangleFs.Add(nextTextRectangleF); // 一行绘制结束 if (isLineEnd) { break; } // 下一个文字位置计算 if (isRowStart) { textLeft = textLeft + nextTeatSizeF.Width; isRowStart = false; } else { textLeft = textLeft + charSpace + nextTeatSizeF.Width; } pointerLeft = pointerLeft + nextTeatSizeF.Width + charSpace; } // 多行绘制结束 if (isLinesEnd) { break; } // 一行绘制结束 if (isLineEnd) { break; } // 单行绘制结束 if (isRowEnd) { if (isLinesStart) { textTop = textTop + fullSizeF.Height; isLinesStart = false; } else { textTop = textTop + lineSpace + fullSizeF.Height; } pointerTop = pointerTop + fullSizeF.Height + lineSpace; pointerBottom = pointerTop + fullSizeF.Height; // 单行文字绘制 if (textRectangleFs.Count > 0) { //lineSpaceSelectedRectangleF = DrawStringRow(graphics, textRectangleFs, font, textBrush, selected); TextRectangleF[] trfs = new TextRectangleF[textRectangleFs.Count]; for (int i = 0; i < textRectangleFs.Count; i++) { trfs[i] = textRectangleFs[i]; } textRectangleFs.Clear(); allTextRectFs.Add(trfs); } } } else { // 空白行 if (0 == lineCharCount) { float bottom = textTop; if (!isLinesStart) { bottom += lineSpace; } // 剪裁 if (clip) { bottom += fullSizeF.Height; } // 多行绘制结束 if (rectangle.Bottom < bottom) { isLinesEnd = true; break; } //// 选择状态行间隔范围填充 //if (selected && 0 < lineSpace && lineSpaceSelectedRectangleF != RectangleF.Empty) //{ // lineSpaceSelectedRectangleF.Height = lineSpace; // graphics.FillRectangle(LayoutCommon.SelectedBrush, // lineSpaceSelectedRectangleF); // lineSpaceSelectedRectangleF = RectangleF.Empty; //} //// 选择状态文本背景绘制 //if (selected) //{ // float correcting = 0; // if (alignment == TextAlignment.RightOrBottom) // { // correcting = rectangle.Right - textLeft; // } // else if (alignment == TextAlignment.Center) // { // correcting = (rectangle.Right - textLeft) / 2; // } // else if (alignment == TextAlignment.Evenness) // { // correcting = (rectangle.Right - textLeft) / 2; // } // RectangleF textRectangleF = // new RectangleF(pointerLeft + correcting, // pointerTop, // fullSizeF.Width / 4, // fullSizeF.Height + lineSpace); // graphics.FillRectangle(LayoutCommon.SelectedBrush, textRectangleF); //} } // 一行结束 isLineEnd = true; break; } } // 多行绘制结束 if (isLinesEnd) { break; } // 一行绘制结束 if (isLineEnd) { // 下一个文字位置计算 if (isLinesStart) { textTop = textTop + fullSizeF.Height; isLinesStart = false; } else { textTop = textTop + lineSpace + fullSizeF.Height; } pointerTop = pointerTop + fullSizeF.Height + lineSpace; pointerBottom = pointerTop + fullSizeF.Height; // 一行文本绘制 //lineSpaceSelectedRectangleF = DrawLineHorizontal(graphics, textRectangleFs, // font, underline, strikeout, textBrush, rectangle, alignment, selected); if (textRectangleFs.Count > 0) { SetStringAlignmentH(textRectangleFs, rectangle, alignment, wrap, clip); //lineSpaceSelectedRectangleF = DrawStringRow(graphics, textRectangleFs, font, textBrush, selected); TextRectangleF[] trfs = new TextRectangleF[textRectangleFs.Count]; for (int i = 0; i < textRectangleFs.Count; i++) { trfs[i] = textRectangleFs[i]; } textRectangleFs.Clear(); allTextRectFs.Add(trfs); } } } // 文本绘制结束 if (isLinesEnd) { //if (selected && isLinesStart && isLinesEnd) //{ // // 选择状态空文本背景绘制 // graphics.FillRectangle(LayoutCommon.SelectedBrush, rectangle); //} if (textRectangleFs.Count > 0) { SetStringAlignmentH(textRectangleFs, rectangle, alignment, wrap, clip); //DrawStringRow(graphics, textRectangleFs, font, textBrush, selected); TextRectangleF[] trfs = new TextRectangleF[textRectangleFs.Count]; for (int i = 0; i < textRectangleFs.Count; i++) { trfs[i] = textRectangleFs[i]; } textRectangleFs.Clear(); allTextRectFs.Add(trfs); } } // 垂直方向文本位置设定 float correctingV = 0; if (alignmentVertical == TextAlignment.RightOrBottom) { float bottom = allTextRectFs[allTextRectFs.Count - 1][0].RectangleF.Bottom; correctingV = rectangle.Bottom - bottom; if (correctingV < 0) { correctingV = 0; } foreach (TextRectangleF[] rfs in allTextRectFs) { foreach (TextRectangleF rf in rfs) { rf.RectangleY = rf.RectangleY + correctingV; graphics.DrawString(rf.Text, font, textBrush, rf.RectangleF, LayoutCommon.StringFormatHorizontal); } } } else if (alignmentVertical == TextAlignment.Center) { float bottom = allTextRectFs[allTextRectFs.Count - 1][0].RectangleF.Bottom; correctingV = (rectangle.Bottom - bottom) / 2; if (correctingV < 0) { correctingV = 0; } foreach (TextRectangleF[] rfs in allTextRectFs) { foreach (TextRectangleF rf in rfs) { rf.RectangleY = rf.RectangleY + correctingV; graphics.DrawString(rf.Text, font, textBrush, rf.RectangleF, LayoutCommon.StringFormatHorizontal); } } } else { foreach (TextRectangleF[] rfs in allTextRectFs) { foreach (TextRectangleF rf in rfs) { graphics.DrawString(rf.Text, font, textBrush, rf.RectangleF, LayoutCommon.StringFormatHorizontal); } } } } /// /// 根据横向文本对齐方式,设定文本位子 /// /// 每个文字的范围 /// 文本范围 /// 文本对齐方式 /// 是否自动文本换行 /// 是否剪裁延伸到边框外侧的文本 private static void SetStringAlignmentH( List textRectangleFs, RectangleF rectangleF, TextAlignment alignment, bool wrap, bool clip) { //if (0 < textRectangleFs.Count) { float correcting = 0; float right = textRectangleFs[textRectangleFs.Count - 1].RectangleF.Right; // 文本对齐方式 if (alignment == TextAlignment.RightOrBottom) { correcting = rectangleF.Right - right; if (wrap) { if (0 < correcting) { foreach (TextRectangleF rf in textRectangleFs) { rf.RectangleX += correcting; } } } // 不自动换行 else { // 剪裁 if (clip) { int count = 0; foreach (TextRectangleF rf in textRectangleFs) { rf.RectangleX += correcting; if (rf.RectangleX < rectangleF.Left) { count++; } } if (count > 0) { textRectangleFs.RemoveRange(0, count); } } // 不剪裁 else { int count = 0; foreach (TextRectangleF rf in textRectangleFs) { rf.RectangleX += correcting; if (rf.RectangleF.Right < rectangleF.Left) { count++; } } if (count > 0) { textRectangleFs.RemoveRange(0, count); } } } } else if (alignment == TextAlignment.Center) { correcting = (rectangleF.Right - right) / 2; if (wrap) { if (0 < correcting) { foreach (TextRectangleF rf in textRectangleFs) { rf.RectangleX += correcting; } } } // 不自动换行 else { // 剪裁 if (clip) { int index = -1; int count = 0; int start = -1; int length = 0; foreach (TextRectangleF rf in textRectangleFs) { index++; rf.RectangleX += correcting; if (rf.RectangleX < rectangleF.Left) { count++; } else if (rectangleF.Right < rf.RectangleF.Right) { if (start < 0) { start = index; } length++; } } if (length > 0) { textRectangleFs.RemoveRange(start, length); } if (count > 0) { textRectangleFs.RemoveRange(0, count); } } else { int index = -1; int count = 0; int start = -1; int length = 0; foreach (TextRectangleF rf in textRectangleFs) { index++; rf.RectangleX += correcting; if (rf.RectangleF.Right < rectangleF.Left) { count++; } else if (rectangleF.Right < rf.RectangleX) { if (start < 0) { start = index; } length++; } } if (length > 0) { textRectangleFs.RemoveRange(start, length); } if (count > 0) { textRectangleFs.RemoveRange(0, count); } } } } else if (alignment == TextAlignment.Evenness) { if (1 < textRectangleFs.Count) { correcting = (rectangleF.Right - right) / (textRectangleFs.Count - 1); if (correcting > 0) { for (int i = 0; i < textRectangleFs.Count; i++) { textRectangleFs[i].RectangleX += correcting * i; } } } else { textRectangleFs[0].RectangleX += (rectangleF.Right - right) / 2; } } } } // /// // /// 横向绘制单行文本 // /// // /// Graphics // /// 每个文字的范围 // /// 字体 // /// 文本笔刷 // /// 是否选择 // /// 文本行间隔范围 // private static RectangleF DrawStringRow( // Graphics graphics, // List textRectangleFs, // Font font, // SolidBrush textBrush, // bool selected) // { // //if (0 < textRectangleFs.Count) // { // //// 文字绘制范围 // //float width = textRectangleFs[textRectangleFs.Count - 1].RectangleF.Right // // - textRectangleFs[0].RectangleF.Left; // //RectangleF selectedRectangleF = // // new RectangleF(textRectangleFs[0].RectangleF.Left, // // textRectangleFs[0].RectangleF.Top, // // width, // // textRectangleFs[0].RectangleF.Height); // //RectangleF lineSpaceSelectedRectangleF = RectangleF.Empty; // //if (selected) // //{ // // // 选择状态文字的背景绘制 // // graphics.FillRectangle(LayoutCommon.SelectedBrush, selectedRectangleF); // // lineSpaceSelectedRectangleF.X = selectedRectangleF.Left; // // lineSpaceSelectedRectangleF.Y = selectedRectangleF.Bottom; // // lineSpaceSelectedRectangleF.Width = selectedRectangleF.Width; // //} // foreach (TextRectangleF rf in textRectangleFs) // { //#if DRAWTEST // graphics.DrawRectangle(new Pen(Color.Blue, 0.1f), rf.RectangleF.X, rf.RectangleF.Y, rf.RectangleF.Width, rf.RectangleF.Height); //#endif // graphics.DrawString(rf.Text, font, textBrush, rf.RectangleF, // LayoutCommon.StringFormatHorizontal); // } // textRectangleFs.Clear(); // //return lineSpaceSelectedRectangleF; // } // } #endregion 绘制文本 #endregion 函数 } }