PrinterDrawMethod.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. namespace LYFZ.Software.MainBusiness.ReportPrint
  7. {
  8. /// <summary>
  9. /// <类名> PrinterDrawMethod
  10. /// <描述> 用于打印的绘图函数
  11. /// <作者> 藏锋
  12. /// <日期> 2017年11月23日
  13. /// </summary>
  14. public static class PrinterDrawMethod
  15. {
  16. #region 静态变量;
  17. // 画刷;
  18. public static SolidBrush _drawBrush = new SolidBrush( Color.Black );
  19. // 自动缩放是否使用页边距;
  20. public static bool AutoScaleNotMargion = true;
  21. #endregion
  22. /// <summary>
  23. /// 文本填充类型
  24. /// </summary>
  25. public enum TextPaddingType
  26. {
  27. // 正常模式;
  28. TP_Normal = 0X00001,
  29. // 自动换行;
  30. TP_AutoLine = 0X00002,
  31. // 自动缩放;
  32. TP_AutoScale = 0X00004,
  33. // cell自动
  34. TP_AutoCell = 0X00008
  35. }
  36. #region 打印绘图函数
  37. /// <summary>
  38. /// 绘制文本框;(换行的字符未处理!!!!)
  39. /// </summary>
  40. /// <param name="grahpics">绘图对象</param>
  41. /// <param name="strText">要绘制的文本</param>
  42. /// <param name="rcDisplay">文本要显示的区域</param>
  43. /// <param name="fontSize">文本字号</param>
  44. /// <param name="fontName">文本字体</param>
  45. /// <param name="autoWrap">是否自动换行,如果使用自动换行,则自动缩放不处理</param>
  46. /// <param name="autoScale">字号是否自动缩放,如果自动缩放,则自动换行不处理</param>
  47. /// <param name="showFrame">是否显示边框</param>
  48. /// <param name="nWrapLineSpace">换行时的行间距</param>
  49. public static int DrawCellTextAutroWrap( Graphics grahpics, string strText, Rectangle rcDisplay, Font textFont, bool autoWrap = true, int nWrapLineSpace = 3, bool showFrame = true )
  50. {
  51. // 文本索引;
  52. int nIndex = 0;
  53. // 行数;
  54. int nRowsCount = 0;
  55. // 所有行总高;
  56. int nRowsHeight = 0;
  57. // 显示区域结果;
  58. Rectangle rcResult = rcDisplay;
  59. // 获取指定的 Font 绘制的指定字符串大小;
  60. SizeF textfontSize = grahpics.MeasureString( strText, textFont );
  61. // 换行的文本;
  62. List<string> listText = new List<string>();
  63. // 画刷;
  64. SolidBrush drawBrush = new SolidBrush( Color.Black );
  65. if ( autoWrap )
  66. { // 自动换行;
  67. // 换行符字符串数组;
  68. List<string> listWrapText = Regex.Split( strText, "\r\n", RegexOptions.IgnoreCase ).ToList();
  69. foreach ( string text in listWrapText )
  70. {
  71. strText = text;
  72. nIndex = strText.Length;
  73. textfontSize = grahpics.MeasureString( strText, textFont );
  74. while ( textfontSize.Width > rcDisplay.Width )
  75. {
  76. textfontSize = grahpics.MeasureString( strText.Substring( 0, --nIndex ), textFont );
  77. if ( textfontSize.Width < rcDisplay.Width )
  78. {
  79. // 行数自增;
  80. nRowsCount++;
  81. // 添加新行文本;
  82. listText.Add( strText.Substring( 0, nIndex ) );
  83. // 剩余文本;
  84. strText = strText.Substring( nIndex );
  85. // 重置索引;
  86. nIndex = strText.Length;
  87. // 重新获取剩余行字体宽高;
  88. textfontSize = grahpics.MeasureString( strText, textFont );
  89. }
  90. }
  91. // 是否有剩余文本;
  92. if ( !string.IsNullOrEmpty( strText ) )
  93. {
  94. listText.Add( strText );
  95. nRowsCount++;
  96. }
  97. }
  98. // 计算行总高;
  99. nRowsHeight = (int)Math.Ceiling( nRowsCount * ( textfontSize.Height + nWrapLineSpace ) );
  100. // 设置显示结果区域;
  101. rcResult.Height = nRowsHeight; //向上取整;
  102. // 重新输出所有文本;
  103. for ( int i = 0; i < listText.Count; i++ )
  104. {
  105. grahpics.DrawString( listText[i], textFont, drawBrush, new RectangleF( rcDisplay.Left, rcDisplay.Top + i * textfontSize.Height + ( i + 1 ) * nWrapLineSpace, rcDisplay.Width, textfontSize.Height ) );
  106. }
  107. }
  108. else
  109. { // 不换行,则可能显示不完全;
  110. grahpics.DrawString( strText, textFont, drawBrush, new RectangleF( rcDisplay.Left, rcDisplay.Top + nWrapLineSpace, rcDisplay.Width, textfontSize.Height ) );
  111. }
  112. // 是否画边框;
  113. if ( showFrame )
  114. grahpics.DrawRectangle( new Pen( drawBrush ), rcResult );
  115. // 返回所有行总高;
  116. return nRowsHeight;
  117. }
  118. /// <summary>
  119. /// 绘制文本框;
  120. /// </summary>
  121. /// <param name="grahpics">绘图对象</param>
  122. /// <param name="strText">要绘制的文本</param>
  123. /// <param name="rcDisplay">文本要显示的区域</param>
  124. /// <param name="fontSize">文本字号</param>
  125. /// <param name="fontName">文本字体</param>
  126. /// <param name="autoWrap">是否自动换行,如果使用自动换行,则自动缩放不处理</param>
  127. /// <param name="autoScale">字号是否自动缩放,如果自动缩放,则自动换行不处理</param>
  128. /// <param name="showFrame">是否显示边框</param>
  129. /// <param name="nWrapLineSpace">换行时的行间距</param>
  130. public static int DrawCellTextAutroWrap( Graphics grahpics, string strText, Rectangle rcDisplay, Single fontSize, string fontName, bool autoWrap = true, int nWrapLineSpace = 3, bool showFrame = true )
  131. {
  132. // 文本索引;
  133. int nIndex = 0;
  134. // 行数;
  135. int nRowsCount = 0;
  136. // 所有行总高;
  137. int nRowsHeight = 0;
  138. // 显示区域结果;
  139. Rectangle rcResult = rcDisplay;
  140. // 创建字体;
  141. Font textFont = new Font( fontName, fontSize );
  142. // 获取指定的 Font 绘制的指定字符串大小;
  143. SizeF textfontSize = grahpics.MeasureString( strText, textFont );
  144. // 换行的文本;
  145. List<string> listText = new List<string>();
  146. // 画刷;
  147. SolidBrush drawBrush = new SolidBrush( Color.Black );
  148. if ( autoWrap )
  149. { // 自动换行;
  150. // 换行符字符串数组;
  151. List<string> listWrapText = Regex.Split( strText, "\r\n", RegexOptions.IgnoreCase ).ToList();
  152. foreach ( string text in listWrapText )
  153. {
  154. strText = text;
  155. nIndex = strText.Length;
  156. textfontSize = grahpics.MeasureString( strText, textFont );
  157. while ( textfontSize.Width > rcDisplay.Width )
  158. {
  159. textfontSize = grahpics.MeasureString( strText.Substring( 0, --nIndex ), textFont );
  160. if ( textfontSize.Width < rcDisplay.Width )
  161. {
  162. // 行数自增;
  163. nRowsCount++;
  164. // 添加新行文本;
  165. listText.Add( strText.Substring( 0, nIndex ) );
  166. // 剩余文本;
  167. strText = strText.Substring( nIndex );
  168. // 重置索引;
  169. nIndex = strText.Length;
  170. // 重新获取剩余行字体宽高;
  171. textfontSize = grahpics.MeasureString( strText, textFont );
  172. }
  173. }
  174. // 是否有剩余文本;
  175. if ( !string.IsNullOrEmpty( strText ) )
  176. {
  177. listText.Add( strText );
  178. nRowsCount++;
  179. }
  180. }
  181. // 计算行总高;
  182. nRowsHeight = (int)Math.Ceiling( nRowsCount * ( textfontSize.Height + nWrapLineSpace ) );
  183. // 设置显示结果区域;
  184. rcResult.Height = nRowsHeight; //向上取整;
  185. // 重新输出所有文本;
  186. for ( int i = 0; i < listText.Count; i++ )
  187. {
  188. grahpics.DrawString( listText[i], textFont, drawBrush, new RectangleF( rcDisplay.Left, rcDisplay.Top + i * textfontSize.Height + ( i + 1 ) * nWrapLineSpace, rcDisplay.Width, textfontSize.Height ) );
  189. }
  190. }
  191. else
  192. { // 不换行,则可能显示不完全;
  193. grahpics.DrawString( strText, textFont, drawBrush, new RectangleF( rcDisplay.Left, rcDisplay.Top + nWrapLineSpace, rcDisplay.Width, textfontSize.Height ) );
  194. }
  195. // 是否画边框;
  196. if ( showFrame )
  197. grahpics.DrawRectangle( new Pen( drawBrush ), rcResult );
  198. // 返回所有行总高;
  199. return nRowsHeight;
  200. }
  201. /// <summary>
  202. /// 计算缩放字体;
  203. /// </summary>
  204. /// <param name="grahpics"></param>
  205. /// <param name="strText"></param>
  206. /// <param name="rcDisplay"></param>
  207. /// <param name="textFont"></param>
  208. /// <param name="minFontSize"></param>
  209. /// <returns></returns>
  210. public static Font ComputeAutoScaleFont( Graphics grahpics, string strText, Rectangle rcDisplay, Font textFont, Single minFontSize = 6 )
  211. {
  212. // 字符索引;
  213. int nIndex = 0;
  214. // 行数;
  215. int nRowsCount = 0;
  216. // 所有行总高;
  217. int nRowsHeight = 0;
  218. // 缩放字体;
  219. Font scaleFont = textFont;
  220. // 字体名称;
  221. string fontName = textFont.Name;
  222. // 字体大小;
  223. Single fontSize = textFont.Size;
  224. // 获取指定的 Font 绘制的指定字符串大小;
  225. SizeF textfontSize = grahpics.MeasureString( strText, scaleFont );
  226. // 换行符字符串数组;
  227. List<string> listWrapText = Regex.Split( strText, "\r\n", RegexOptions.IgnoreCase ).ToList();
  228. again:
  229. foreach ( string text in listWrapText )
  230. {
  231. strText = text;
  232. nIndex = strText.Length;
  233. textfontSize = grahpics.MeasureString( strText, scaleFont );
  234. while ( textfontSize.Width > rcDisplay.Width )
  235. {
  236. textfontSize = grahpics.MeasureString( strText.Substring( 0, --nIndex ), scaleFont );
  237. if ( textfontSize.Width < rcDisplay.Width )
  238. {
  239. // 行数自增;
  240. nRowsCount++;
  241. // 剩余文本;
  242. strText = strText.Substring( nIndex );
  243. // 重置字符索引;
  244. nIndex = strText.Length;
  245. // 重新计算剩余文本大小;
  246. textfontSize = grahpics.MeasureString( strText, scaleFont );
  247. }
  248. }
  249. // 是否有剩余文本;
  250. if ( !string.IsNullOrEmpty( strText ) )
  251. {
  252. nRowsCount++;
  253. }
  254. }
  255. // 计算行总高;
  256. nRowsHeight = (int)Math.Ceiling( nRowsCount * textfontSize.Height );
  257. if ( rcDisplay.Height < nRowsHeight && fontSize > minFontSize )
  258. {
  259. // 字号递减;
  260. fontSize--;
  261. // 新建缩放字体;
  262. scaleFont = new Font( fontName, fontSize );
  263. // 重置行数;
  264. nRowsCount = 0;
  265. // 回执goto;
  266. goto again;
  267. }
  268. // 返回缩放字体;
  269. return scaleFont;
  270. }
  271. public static Font ComputeAutoScaleFontEx( Graphics grahpics, string strText, Rectangle rcDisplay, Font textFont, Single minFontSize = 6 )
  272. {
  273. Font scaleFont = textFont;
  274. string fontName = textFont.Name;
  275. Single fontSize = textFont.Size;
  276. Rectangle rcResult = ComputeTextCell( grahpics, strText, rcDisplay, scaleFont );
  277. while ( rcResult.Height > rcDisplay.Height && fontSize > minFontSize )
  278. rcResult = ComputeTextCell( grahpics, strText, rcDisplay, scaleFont = new Font( fontName, --fontSize ) );
  279. // 返回缩放字体;
  280. return scaleFont;
  281. }
  282. /// <summary>
  283. /// 绘制文本框;
  284. /// </summary>
  285. /// <param name="grahpics">绘图对象</param>
  286. /// <param name="strText">要绘制的文本</param>
  287. /// <param name="rcDisplay">文本要显示的区域</param>
  288. /// <param name="fontSize">文本字号</param>
  289. /// <param name="fontName">文本字体</param>
  290. /// <param name="autoWrap">是否自动换行,如果使用自动换行,则自动缩放不处理</param>
  291. /// <param name="autoScale">字号是否自动缩放,如果自动缩放,则自动换行不处理</param>
  292. /// <param name="showFrame">是否显示边框</param>
  293. /// <param name="nWrapLineSpace">换行时的行间距</param>
  294. /// <param name="minFontSize"></param>
  295. public static int DrawCellTextAutroScale( Graphics grahpics, string strText, Rectangle rcDisplay, Font textFont, bool autoScale = true, bool showFrame = true, Single minFontSize = 6 )
  296. {
  297. // 新建缩放字体 ;
  298. Font scaleFont = textFont;
  299. // 字体名称;
  300. string fontName = textFont.Name;
  301. // 字号;
  302. Single fontSize = textFont.Size;
  303. // 获取指定的 Font 绘制的指定字符串大小;
  304. SizeF textfontSize = grahpics.MeasureString( strText, textFont );
  305. // 画刷;
  306. SolidBrush drawBrush = new SolidBrush( Color.Black );
  307. // 字体递减;
  308. if ( autoScale )
  309. {
  310. while ( textfontSize.Width > rcDisplay.Width && fontSize > minFontSize )
  311. {
  312. fontSize--;
  313. textfontSize = grahpics.MeasureString( strText, scaleFont = new Font( fontName, fontSize ) );
  314. }
  315. }
  316. // 上边空白区域值;
  317. int nTopMargin = ( rcDisplay.Height - (int)Math.Ceiling( textfontSize.Height ) ) <= 0 ? 0 : (int)Math.Ceiling( ( rcDisplay.Height - textfontSize.Height ) / 2 );
  318. // 垂直居中显示;
  319. grahpics.DrawString(
  320. strText,
  321. scaleFont,
  322. drawBrush,
  323. new RectangleF( rcDisplay.Left, rcDisplay.Top + ( AutoScaleNotMargion ? nTopMargin : 0 ), rcDisplay.Width, textfontSize.Height ) );
  324. // 是否显示边框;
  325. if ( showFrame )
  326. grahpics.DrawRectangle( new Pen( drawBrush ), rcDisplay );
  327. // 返回新高;
  328. return (int)Math.Ceiling( textfontSize.Height ) + ( AutoScaleNotMargion ? nTopMargin : 0 );
  329. }
  330. /// <summary>
  331. /// 绘制文本框;
  332. /// </summary>
  333. /// <param name="grahpics">绘图对象</param>
  334. /// <param name="strText">要绘制的文本</param>
  335. /// <param name="rcDisplay">文本要显示的区域</param>
  336. /// <param name="fontSize">文本字号</param>
  337. /// <param name="fontName">文本字体</param>
  338. /// <param name="autoWrap">是否自动换行,如果使用自动换行,则自动缩放不处理</param>
  339. /// <param name="autoScale">字号是否自动缩放,如果自动缩放,则自动换行不处理</param>
  340. /// <param name="showFrame">是否显示边框</param>
  341. /// <param name="nWrapLineSpace">换行时的行间距</param>
  342. /// <param name="minFontSize"></param>
  343. public static int DrawCellTextAutroScale( Graphics grahpics, string strText, Rectangle rcDisplay, Single fontSize, string fontName, bool autoScale = true, bool showFrame = true, Single minFontSize = 6 )
  344. {
  345. Font textFont = new Font( fontName, fontSize );
  346. // 获取指定的 Font 绘制的指定字符串大小;
  347. SizeF textfontSize = grahpics.MeasureString( strText, textFont );
  348. // 画刷;
  349. SolidBrush drawBrush = new SolidBrush( Color.Black );
  350. if ( autoScale )
  351. {
  352. while ( textfontSize.Width > rcDisplay.Width && fontSize > minFontSize )
  353. {
  354. fontSize--;
  355. textfontSize = grahpics.MeasureString( strText, textFont = new Font( fontName, fontSize ) );
  356. }
  357. }
  358. // 上边空白区域值;
  359. int nTopMargin = ( rcDisplay.Height - (int)Math.Ceiling( textfontSize.Height ) ) <= 0 ? 0 : (int)Math.Ceiling( ( rcDisplay.Height - textfontSize.Height ) / 2 );
  360. // 垂直居中显示;
  361. grahpics.DrawString(
  362. strText,
  363. textFont,
  364. drawBrush,
  365. new RectangleF( rcDisplay.Left, rcDisplay.Top + ( AutoScaleNotMargion ? nTopMargin : 0 ), rcDisplay.Width, textfontSize.Height ) );
  366. // 是否显示边框;
  367. if ( showFrame )
  368. grahpics.DrawRectangle( new Pen( drawBrush ), rcDisplay );
  369. // 返回新高;
  370. return (int)Math.Ceiling( textfontSize.Height ) + ( AutoScaleNotMargion ? nTopMargin : 0 );
  371. }
  372. /// <summary>
  373. ///
  374. /// </summary>
  375. /// <param name="grahpics"></param>
  376. /// <param name="strText"></param>
  377. /// <param name="rcText"></param>
  378. /// <param name="fText"></param>
  379. /// <param name="brush"></param>
  380. /// <returns></returns>
  381. public static Rectangle ComputeTextCell( Graphics grahpics, string strText, Rectangle rcDisplay, Font textFont )
  382. {
  383. // 字符索引;
  384. int nIndex = 0;
  385. // 换行行数;
  386. int nRowsCount = 0;
  387. // 获取文本宽高;
  388. SizeF textfontSize = grahpics.MeasureString( strText, textFont );
  389. // 换行符字符集;
  390. List<string> listText = Regex.Split( strText, "\r\n", RegexOptions.IgnoreCase ).ToList();
  391. foreach ( string text in listText )
  392. {
  393. strText = text;
  394. nIndex = strText.Length;
  395. textfontSize = grahpics.MeasureString( strText, textFont );
  396. while ( textfontSize.Width > rcDisplay.Width )
  397. {
  398. textfontSize = grahpics.MeasureString( strText.Substring( 0, --nIndex ), textFont );
  399. if ( textfontSize.Width < rcDisplay.Width )
  400. {
  401. // 行数自增;
  402. nRowsCount++;
  403. // 剩余文本;
  404. strText = strText.Substring( nIndex );
  405. // 重置索引;
  406. nIndex = strText.Length;
  407. // 重新获取剩余行字体宽高;
  408. textfontSize = grahpics.MeasureString( strText, textFont );
  409. }
  410. }
  411. // 是否有剩余文本;
  412. if ( !string.IsNullOrEmpty( strText ) )
  413. {
  414. nRowsCount++;
  415. }
  416. }
  417. // 返回新区域;
  418. return new Rectangle( rcDisplay.X, rcDisplay.Y, rcDisplay.Width, (int)Math.Ceiling( textfontSize.Height * nRowsCount ) );
  419. }
  420. /// <summary>
  421. ///
  422. /// </summary>
  423. /// <param name="grahpics"></param>
  424. /// <param name="strText"></param>
  425. /// <param name="rcDisplay"></param>
  426. /// <param name="textFont"></param>
  427. /// <param name="colorBackground"></param>
  428. /// <param name="paddingType"></param>
  429. /// <param name="showFrame"></param>
  430. public static void DrawCell( Graphics grahpics, string strText, Rectangle rcDisplay, Font textFont, Color colorBackground, TextPaddingType paddingType, bool showFrame = true )
  431. {
  432. }
  433. #endregion
  434. }
  435. }