123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace LYFZ.Software.MainBusiness.ReportPrint
- {
- /// <summary>
- /// <类名> PrinterDrawMethod
- /// <描述> 用于打印的绘图函数
- /// <作者> 藏锋
- /// <日期> 2017年11月23日
- /// </summary>
- public static class PrinterDrawMethod
- {
- #region 静态变量;
- // 画刷;
- public static SolidBrush _drawBrush = new SolidBrush( Color.Black );
- // 自动缩放是否使用页边距;
- public static bool AutoScaleNotMargion = true;
- #endregion
- /// <summary>
- /// 文本填充类型
- /// </summary>
- public enum TextPaddingType
- {
- // 正常模式;
- TP_Normal = 0X00001,
- // 自动换行;
- TP_AutoLine = 0X00002,
- // 自动缩放;
- TP_AutoScale = 0X00004,
- // cell自动
- TP_AutoCell = 0X00008
- }
- #region 打印绘图函数
- /// <summary>
- /// 绘制文本框;(换行的字符未处理!!!!)
- /// </summary>
- /// <param name="grahpics">绘图对象</param>
- /// <param name="strText">要绘制的文本</param>
- /// <param name="rcDisplay">文本要显示的区域</param>
- /// <param name="fontSize">文本字号</param>
- /// <param name="fontName">文本字体</param>
- /// <param name="autoWrap">是否自动换行,如果使用自动换行,则自动缩放不处理</param>
- /// <param name="autoScale">字号是否自动缩放,如果自动缩放,则自动换行不处理</param>
- /// <param name="showFrame">是否显示边框</param>
- /// <param name="nWrapLineSpace">换行时的行间距</param>
- public static int DrawCellTextAutroWrap( Graphics grahpics, string strText, Rectangle rcDisplay, Font textFont, bool autoWrap = true, int nWrapLineSpace = 3, bool showFrame = true )
- {
- // 文本索引;
- int nIndex = 0;
- // 行数;
- int nRowsCount = 0;
- // 所有行总高;
- int nRowsHeight = 0;
- // 显示区域结果;
- Rectangle rcResult = rcDisplay;
- // 获取指定的 Font 绘制的指定字符串大小;
- SizeF textfontSize = grahpics.MeasureString( strText, textFont );
- // 换行的文本;
- List<string> listText = new List<string>();
- // 画刷;
- SolidBrush drawBrush = new SolidBrush( Color.Black );
- if ( autoWrap )
- { // 自动换行;
- // 换行符字符串数组;
- List<string> listWrapText = Regex.Split( strText, "\r\n", RegexOptions.IgnoreCase ).ToList();
- foreach ( string text in listWrapText )
- {
- strText = text;
- nIndex = strText.Length;
- textfontSize = grahpics.MeasureString( strText, textFont );
- while ( textfontSize.Width > rcDisplay.Width )
- {
- textfontSize = grahpics.MeasureString( strText.Substring( 0, --nIndex ), textFont );
- if ( textfontSize.Width < rcDisplay.Width )
- {
- // 行数自增;
- nRowsCount++;
- // 添加新行文本;
- listText.Add( strText.Substring( 0, nIndex ) );
- // 剩余文本;
- strText = strText.Substring( nIndex );
- // 重置索引;
- nIndex = strText.Length;
- // 重新获取剩余行字体宽高;
- textfontSize = grahpics.MeasureString( strText, textFont );
- }
- }
- // 是否有剩余文本;
- if ( !string.IsNullOrEmpty( strText ) )
- {
- listText.Add( strText );
- nRowsCount++;
- }
- }
- // 计算行总高;
- nRowsHeight = (int)Math.Ceiling( nRowsCount * ( textfontSize.Height + nWrapLineSpace ) );
- // 设置显示结果区域;
- rcResult.Height = nRowsHeight; //向上取整;
- // 重新输出所有文本;
- for ( int i = 0; i < listText.Count; i++ )
- {
- grahpics.DrawString( listText[i], textFont, drawBrush, new RectangleF( rcDisplay.Left, rcDisplay.Top + i * textfontSize.Height + ( i + 1 ) * nWrapLineSpace, rcDisplay.Width, textfontSize.Height ) );
- }
- }
- else
- { // 不换行,则可能显示不完全;
- grahpics.DrawString( strText, textFont, drawBrush, new RectangleF( rcDisplay.Left, rcDisplay.Top + nWrapLineSpace, rcDisplay.Width, textfontSize.Height ) );
- }
- // 是否画边框;
- if ( showFrame )
- grahpics.DrawRectangle( new Pen( drawBrush ), rcResult );
- // 返回所有行总高;
- return nRowsHeight;
- }
- /// <summary>
- /// 绘制文本框;
- /// </summary>
- /// <param name="grahpics">绘图对象</param>
- /// <param name="strText">要绘制的文本</param>
- /// <param name="rcDisplay">文本要显示的区域</param>
- /// <param name="fontSize">文本字号</param>
- /// <param name="fontName">文本字体</param>
- /// <param name="autoWrap">是否自动换行,如果使用自动换行,则自动缩放不处理</param>
- /// <param name="autoScale">字号是否自动缩放,如果自动缩放,则自动换行不处理</param>
- /// <param name="showFrame">是否显示边框</param>
- /// <param name="nWrapLineSpace">换行时的行间距</param>
- public static int DrawCellTextAutroWrap( Graphics grahpics, string strText, Rectangle rcDisplay, Single fontSize, string fontName, bool autoWrap = true, int nWrapLineSpace = 3, bool showFrame = true )
- {
- // 文本索引;
- int nIndex = 0;
- // 行数;
- int nRowsCount = 0;
- // 所有行总高;
- int nRowsHeight = 0;
- // 显示区域结果;
- Rectangle rcResult = rcDisplay;
- // 创建字体;
- Font textFont = new Font( fontName, fontSize );
- // 获取指定的 Font 绘制的指定字符串大小;
- SizeF textfontSize = grahpics.MeasureString( strText, textFont );
- // 换行的文本;
- List<string> listText = new List<string>();
- // 画刷;
- SolidBrush drawBrush = new SolidBrush( Color.Black );
- if ( autoWrap )
- { // 自动换行;
- // 换行符字符串数组;
- List<string> listWrapText = Regex.Split( strText, "\r\n", RegexOptions.IgnoreCase ).ToList();
- foreach ( string text in listWrapText )
- {
- strText = text;
- nIndex = strText.Length;
- textfontSize = grahpics.MeasureString( strText, textFont );
- while ( textfontSize.Width > rcDisplay.Width )
- {
- textfontSize = grahpics.MeasureString( strText.Substring( 0, --nIndex ), textFont );
- if ( textfontSize.Width < rcDisplay.Width )
- {
- // 行数自增;
- nRowsCount++;
- // 添加新行文本;
- listText.Add( strText.Substring( 0, nIndex ) );
- // 剩余文本;
- strText = strText.Substring( nIndex );
- // 重置索引;
- nIndex = strText.Length;
- // 重新获取剩余行字体宽高;
- textfontSize = grahpics.MeasureString( strText, textFont );
- }
- }
- // 是否有剩余文本;
- if ( !string.IsNullOrEmpty( strText ) )
- {
- listText.Add( strText );
- nRowsCount++;
- }
- }
- // 计算行总高;
- nRowsHeight = (int)Math.Ceiling( nRowsCount * ( textfontSize.Height + nWrapLineSpace ) );
- // 设置显示结果区域;
- rcResult.Height = nRowsHeight; //向上取整;
- // 重新输出所有文本;
- for ( int i = 0; i < listText.Count; i++ )
- {
- grahpics.DrawString( listText[i], textFont, drawBrush, new RectangleF( rcDisplay.Left, rcDisplay.Top + i * textfontSize.Height + ( i + 1 ) * nWrapLineSpace, rcDisplay.Width, textfontSize.Height ) );
- }
- }
- else
- { // 不换行,则可能显示不完全;
- grahpics.DrawString( strText, textFont, drawBrush, new RectangleF( rcDisplay.Left, rcDisplay.Top + nWrapLineSpace, rcDisplay.Width, textfontSize.Height ) );
- }
- // 是否画边框;
- if ( showFrame )
- grahpics.DrawRectangle( new Pen( drawBrush ), rcResult );
- // 返回所有行总高;
- return nRowsHeight;
- }
- /// <summary>
- /// 计算缩放字体;
- /// </summary>
- /// <param name="grahpics"></param>
- /// <param name="strText"></param>
- /// <param name="rcDisplay"></param>
- /// <param name="textFont"></param>
- /// <param name="minFontSize"></param>
- /// <returns></returns>
- public static Font ComputeAutoScaleFont( Graphics grahpics, string strText, Rectangle rcDisplay, Font textFont, Single minFontSize = 6 )
- {
- // 字符索引;
- int nIndex = 0;
- // 行数;
- int nRowsCount = 0;
- // 所有行总高;
- int nRowsHeight = 0;
- // 缩放字体;
- Font scaleFont = textFont;
- // 字体名称;
- string fontName = textFont.Name;
- // 字体大小;
- Single fontSize = textFont.Size;
- // 获取指定的 Font 绘制的指定字符串大小;
- SizeF textfontSize = grahpics.MeasureString( strText, scaleFont );
- // 换行符字符串数组;
- List<string> listWrapText = Regex.Split( strText, "\r\n", RegexOptions.IgnoreCase ).ToList();
- again:
- foreach ( string text in listWrapText )
- {
- strText = text;
- nIndex = strText.Length;
- textfontSize = grahpics.MeasureString( strText, scaleFont );
- while ( textfontSize.Width > rcDisplay.Width )
- {
- textfontSize = grahpics.MeasureString( strText.Substring( 0, --nIndex ), scaleFont );
- if ( textfontSize.Width < rcDisplay.Width )
- {
- // 行数自增;
- nRowsCount++;
- // 剩余文本;
- strText = strText.Substring( nIndex );
- // 重置字符索引;
- nIndex = strText.Length;
- // 重新计算剩余文本大小;
- textfontSize = grahpics.MeasureString( strText, scaleFont );
- }
- }
- // 是否有剩余文本;
- if ( !string.IsNullOrEmpty( strText ) )
- {
- nRowsCount++;
- }
- }
- // 计算行总高;
- nRowsHeight = (int)Math.Ceiling( nRowsCount * textfontSize.Height );
- if ( rcDisplay.Height < nRowsHeight && fontSize > minFontSize )
- {
- // 字号递减;
- fontSize--;
- // 新建缩放字体;
- scaleFont = new Font( fontName, fontSize );
- // 重置行数;
- nRowsCount = 0;
- // 回执goto;
- goto again;
- }
- // 返回缩放字体;
- return scaleFont;
- }
- public static Font ComputeAutoScaleFontEx( Graphics grahpics, string strText, Rectangle rcDisplay, Font textFont, Single minFontSize = 6 )
- {
- Font scaleFont = textFont;
- string fontName = textFont.Name;
- Single fontSize = textFont.Size;
- Rectangle rcResult = ComputeTextCell( grahpics, strText, rcDisplay, scaleFont );
- while ( rcResult.Height > rcDisplay.Height && fontSize > minFontSize )
- rcResult = ComputeTextCell( grahpics, strText, rcDisplay, scaleFont = new Font( fontName, --fontSize ) );
- // 返回缩放字体;
- return scaleFont;
- }
- /// <summary>
- /// 绘制文本框;
- /// </summary>
- /// <param name="grahpics">绘图对象</param>
- /// <param name="strText">要绘制的文本</param>
- /// <param name="rcDisplay">文本要显示的区域</param>
- /// <param name="fontSize">文本字号</param>
- /// <param name="fontName">文本字体</param>
- /// <param name="autoWrap">是否自动换行,如果使用自动换行,则自动缩放不处理</param>
- /// <param name="autoScale">字号是否自动缩放,如果自动缩放,则自动换行不处理</param>
- /// <param name="showFrame">是否显示边框</param>
- /// <param name="nWrapLineSpace">换行时的行间距</param>
- /// <param name="minFontSize"></param>
- public static int DrawCellTextAutroScale( Graphics grahpics, string strText, Rectangle rcDisplay, Font textFont, bool autoScale = true, bool showFrame = true, Single minFontSize = 6 )
- {
- // 新建缩放字体 ;
- Font scaleFont = textFont;
- // 字体名称;
- string fontName = textFont.Name;
- // 字号;
- Single fontSize = textFont.Size;
- // 获取指定的 Font 绘制的指定字符串大小;
- SizeF textfontSize = grahpics.MeasureString( strText, textFont );
- // 画刷;
- SolidBrush drawBrush = new SolidBrush( Color.Black );
- // 字体递减;
- if ( autoScale )
- {
- while ( textfontSize.Width > rcDisplay.Width && fontSize > minFontSize )
- {
- fontSize--;
- textfontSize = grahpics.MeasureString( strText, scaleFont = new Font( fontName, fontSize ) );
- }
- }
- // 上边空白区域值;
- int nTopMargin = ( rcDisplay.Height - (int)Math.Ceiling( textfontSize.Height ) ) <= 0 ? 0 : (int)Math.Ceiling( ( rcDisplay.Height - textfontSize.Height ) / 2 );
- // 垂直居中显示;
- grahpics.DrawString(
- strText,
- scaleFont,
- drawBrush,
- new RectangleF( rcDisplay.Left, rcDisplay.Top + ( AutoScaleNotMargion ? nTopMargin : 0 ), rcDisplay.Width, textfontSize.Height ) );
- // 是否显示边框;
- if ( showFrame )
- grahpics.DrawRectangle( new Pen( drawBrush ), rcDisplay );
- // 返回新高;
- return (int)Math.Ceiling( textfontSize.Height ) + ( AutoScaleNotMargion ? nTopMargin : 0 );
- }
- /// <summary>
- /// 绘制文本框;
- /// </summary>
- /// <param name="grahpics">绘图对象</param>
- /// <param name="strText">要绘制的文本</param>
- /// <param name="rcDisplay">文本要显示的区域</param>
- /// <param name="fontSize">文本字号</param>
- /// <param name="fontName">文本字体</param>
- /// <param name="autoWrap">是否自动换行,如果使用自动换行,则自动缩放不处理</param>
- /// <param name="autoScale">字号是否自动缩放,如果自动缩放,则自动换行不处理</param>
- /// <param name="showFrame">是否显示边框</param>
- /// <param name="nWrapLineSpace">换行时的行间距</param>
- /// <param name="minFontSize"></param>
- public static int DrawCellTextAutroScale( Graphics grahpics, string strText, Rectangle rcDisplay, Single fontSize, string fontName, bool autoScale = true, bool showFrame = true, Single minFontSize = 6 )
- {
- Font textFont = new Font( fontName, fontSize );
- // 获取指定的 Font 绘制的指定字符串大小;
- SizeF textfontSize = grahpics.MeasureString( strText, textFont );
- // 画刷;
- SolidBrush drawBrush = new SolidBrush( Color.Black );
- if ( autoScale )
- {
- while ( textfontSize.Width > rcDisplay.Width && fontSize > minFontSize )
- {
- fontSize--;
- textfontSize = grahpics.MeasureString( strText, textFont = new Font( fontName, fontSize ) );
- }
- }
- // 上边空白区域值;
- int nTopMargin = ( rcDisplay.Height - (int)Math.Ceiling( textfontSize.Height ) ) <= 0 ? 0 : (int)Math.Ceiling( ( rcDisplay.Height - textfontSize.Height ) / 2 );
- // 垂直居中显示;
- grahpics.DrawString(
- strText,
- textFont,
- drawBrush,
- new RectangleF( rcDisplay.Left, rcDisplay.Top + ( AutoScaleNotMargion ? nTopMargin : 0 ), rcDisplay.Width, textfontSize.Height ) );
- // 是否显示边框;
- if ( showFrame )
- grahpics.DrawRectangle( new Pen( drawBrush ), rcDisplay );
- // 返回新高;
- return (int)Math.Ceiling( textfontSize.Height ) + ( AutoScaleNotMargion ? nTopMargin : 0 );
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="grahpics"></param>
- /// <param name="strText"></param>
- /// <param name="rcText"></param>
- /// <param name="fText"></param>
- /// <param name="brush"></param>
- /// <returns></returns>
- public static Rectangle ComputeTextCell( Graphics grahpics, string strText, Rectangle rcDisplay, Font textFont )
- {
- // 字符索引;
- int nIndex = 0;
- // 换行行数;
- int nRowsCount = 0;
- // 获取文本宽高;
- SizeF textfontSize = grahpics.MeasureString( strText, textFont );
- // 换行符字符集;
- List<string> listText = Regex.Split( strText, "\r\n", RegexOptions.IgnoreCase ).ToList();
- foreach ( string text in listText )
- {
- strText = text;
- nIndex = strText.Length;
- textfontSize = grahpics.MeasureString( strText, textFont );
- while ( textfontSize.Width > rcDisplay.Width )
- {
- textfontSize = grahpics.MeasureString( strText.Substring( 0, --nIndex ), textFont );
- if ( textfontSize.Width < rcDisplay.Width )
- {
- // 行数自增;
- nRowsCount++;
- // 剩余文本;
- strText = strText.Substring( nIndex );
- // 重置索引;
- nIndex = strText.Length;
- // 重新获取剩余行字体宽高;
- textfontSize = grahpics.MeasureString( strText, textFont );
- }
- }
- // 是否有剩余文本;
- if ( !string.IsNullOrEmpty( strText ) )
- {
- nRowsCount++;
- }
- }
- // 返回新区域;
- return new Rectangle( rcDisplay.X, rcDisplay.Y, rcDisplay.Width, (int)Math.Ceiling( textfontSize.Height * nRowsCount ) );
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="grahpics"></param>
- /// <param name="strText"></param>
- /// <param name="rcDisplay"></param>
- /// <param name="textFont"></param>
- /// <param name="colorBackground"></param>
- /// <param name="paddingType"></param>
- /// <param name="showFrame"></param>
- public static void DrawCell( Graphics grahpics, string strText, Rectangle rcDisplay, Font textFont, Color colorBackground, TextPaddingType paddingType, bool showFrame = true )
- {
- }
- #endregion
- }
- }
|