| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225 |
- //#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 函数
- ///// <summary>
- ///// 获取图形实例
- ///// </summary>
- ///// <param name="item"></param>
- ///// <returns></returns>
- //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 绘制直线
- /// <summary>
- /// 绘制直线
- /// </summary>
- /// <param name="graphics">Graphics</param>
- /// <param name="startPoint">起点</param>
- /// <param name="endPoint">终点</param>
- /// <param name="lineColor">线的颜色</param>
- /// <param name="lineWidth">线宽(mm)</param>
- public static void DrawLine(
- Graphics graphics,
- PointF startPoint,
- PointF endPoint,
- Color lineColor,
- float lineWidth)
- {
- DrawLine(graphics, graphics.PageUnit, startPoint, endPoint, lineColor, lineWidth);
- }
- /// <summary>
- /// 绘制直线
- /// </summary>
- /// <param name="graphics">Graphics</param>
- /// <param name="unit">GraphicsUnit</param>
- /// <param name="startPoint">起点</param>
- /// <param name="endPoint">终点</param>
- /// <param name="lineColor">线的颜色</param>
- /// <param name="lineWidth">线宽(mm)</param>
- 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 绘制椭圆
- /// <summary>
- /// 绘制椭圆
- /// </summary>
- /// <param name="graphics">Graphics</param>
- /// <param name="rectangle">图形范围</param>
- /// <param name="lineColor">边线颜色</param>
- /// <param name="fillColor">填充颜色</param>
- /// <param name="lineWidth">边线宽(mm)</param>
- /// <param name="isTransparent">背景是否透明</param>
- 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);
- }
- /// <summary>
- /// 绘制椭圆
- /// </summary>
- /// <param name="graphics">Graphics</param>
- /// <param name="unit">GraphicsUnit</param>
- /// <param name="rectangle">图形范围</param>
- /// <param name="lineColor">边线颜色</param>
- /// <param name="fillColor">填充颜色</param>
- /// <param name="lineWidth">边线宽(mm)</param>
- /// <param name="isTransparent">背景是否透明</param>
- 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 绘制矩形
- /// <summary>
- /// 绘制矩形
- /// </summary>
- /// <param name="graphics">Graphics</param>
- /// <param name="unit">GraphicsUnit</param>
- /// <param name="rectangle">图形范围</param>
- /// <param name="lineColor">边线颜色</param>
- /// <param name="fillColor">填充颜色</param>
- /// <param name="lineWidth">边线宽(mm)</param>
- /// <param name="isTransparent">背景是否透明</param>
- 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);
- }
- /// <summary>
- /// 绘制矩形
- /// </summary>
- /// <param name="graphics">Graphics</param>
- /// <param name="unit">GraphicsUnit</param>
- /// <param name="rectangle">图形范围</param>
- /// <param name="lineColor">边线颜色</param>
- /// <param name="fillColor">填充颜色</param>
- /// <param name="lineWidth">边线宽(mm)</param>
- /// <param name="isTransparent">背景是否透明</param>
- 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 绘制图片
- /// <summary>
- /// 绘制图片
- /// </summary>
- /// <param name="graphics">Graphics</param>
- /// <param name="rectangle">图片范围</param>
- /// <param name="image">图片</param>
- public static void DrawImage(
- Graphics graphics,
- RectangleF rectangle,
- Image image)
- {
- DrawImage(graphics, graphics.PageUnit, rectangle, image);
- }
- /// <summary>
- /// 绘制图片
- /// </summary>
- /// <param name="graphics">Graphics</param>
- /// <param name="unit">GraphicsUnit</param>
- /// <param name="rectangle">图片范围</param>
- /// <param name="image">图片</param>
- 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 绘制文本
- /// <summary>
- /// 绘制文本
- /// </summary>
- /// <param name="graphics">Graphics</param>
- /// <param name="rectangleM">绘制范围(余白)</param>
- /// <param name="rectangle">绘制范围</param>
- /// <param name="context">文本</param>
- /// <param name="font">字体</param>
- /// <param name="foreColor">字体颜色</param>
- /// <param name="lineSpace">行间隔</param>
- /// <param name="characterSpace">字间隔</param>
- /// <param name="charCount">行文字数</param>
- /// <param name="alignment">文本对齐方式</param>
- /// <param name="alignmentVertical">文本垂直对齐方式</param>
- /// <param name="wrap">是否自动文本换行</param>
- /// <param name="clip">是否剪裁延伸到边框外侧的文本</param>
- /// <param name="selected">是否选择</param>
- 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
- }
- /// <summary>
- /// 绘制文本
- /// </summary>
- /// <param name="graphics">Graphics</param>
- /// <param name="unit">GraphicsUnit</param>
- /// <param name="rectangleM">绘制范围(余白)</param>
- /// <param name="rectangle">绘制范围</param>
- /// <param name="context">文本</param>
- /// <param name="font">字体</param>
- /// <param name="foreColor">字体颜色</param>
- /// <param name="lineSpace">行间隔</param>
- /// <param name="characterSpace">字间隔</param>
- /// <param name="charCount">行文字数</param>
- /// <param name="alignment">文本对齐方式</param>
- /// <param name="alignmentVertical">文本垂直对齐方式</param>
- /// <param name="wrap">是否自动文本换行</param>
- /// <param name="clip">是否剪裁延伸到边框外侧的文本</param>
- /// <param name="selected">是否选择</param>
- 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);
- }
- /// <summary>
- /// 横向绘制文本
- /// </summary>
- /// <param name="graphics">Graphics</param>
- /// <param name="lines">多行文本</param>
- /// <param name="font">字体</param>
- /// <param name="textBrush">字体笔刷</param>
- /// <param name="lineSpace">行间隔</param>
- /// <param name="characterSpace">字间隔</param>
- /// <param name="charCount">行文字数</param>
- /// <param name="rectangle">文本范围</param>
- /// <param name="alignment">文本对齐方式</param>
- /// <param name="wrap">是否自动文本换行</param>
- /// <param name="clip">是否剪裁延伸到边框外侧的文本</param>
- /// <param name="selected">是否选择</param>
- 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<string> newLines = new List<string>();
- 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<TextRectangleF> textRectangleFs = new List<TextRectangleF>();
- // 文本垂直对齐方式 modify by chenxy 2014-03-28 begin
- List<TextRectangleF[]> allTextRectFs = new List<TextRectangleF[]>();
- // 文本垂直对齐方式 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);
- }
- }
- }
- }
- /// <summary>
- /// 根据横向文本对齐方式,设定文本位子
- /// </summary>
- /// <param name="textRectangleFs">每个文字的范围</param>
- /// <param name="rectangleF">文本范围</param>
- /// <param name="alignment">文本对齐方式</param>
- /// <param name="wrap">是否自动文本换行</param>
- /// <param name="clip">是否剪裁延伸到边框外侧的文本</param>
- private static void SetStringAlignmentH(
- List<TextRectangleF> 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;
- }
- }
- }
- }
- // /// <summary>
- // /// 横向绘制单行文本
- // /// </summary>
- // /// <param name="graphics">Graphics</param>
- // /// <param name="textRectangleFs">每个文字的范围</param>
- // /// <param name="font">字体</param>
- // /// <param name="textBrush">文本笔刷</param>
- // /// <param name="selected">是否选择</param>
- // /// <returns>文本行间隔范围</returns>
- // private static RectangleF DrawStringRow(
- // Graphics graphics,
- // List<TextRectangleF> 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 函数
- }
- }
|