PrinterTabStops.cs
上传用户:jx_fiona
上传日期:2014-03-08
资源大小:1387k
文件大小:2k
源码类别:

打印编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. namespace GoldPrinter
  4. {
  5. /// <summary>
  6. /// 绘制表符分隔的字符串,以|分隔列,以n换行,列对齐均为居左。合适于列少每列字符少的几行几列的打印。
  7. /// 
  8. /// 作 者:长江支流(周方勇)
  9. /// Email:flygoldfish@163.com  QQ:150439795
  10. /// 网 址:www.webmis.com.cn
  11. /// ★★★★★您可以免费使用此程序,但是请您完整保留此说明,以维护知识产权★★★★★
  12. /// 
  13. /// </summary>
  14. public class PrinterTabStops:IDisposable
  15. {
  16. private int _cols; //列数
  17. public Font Font; //字体
  18. public Rectangle Rectangle; //在此区域画
  19. private string _text; //原始打印文本
  20. private string _textConverted; //转换后打印文本  
  21. public PrinterTabStops()
  22. {
  23. Font = new Font("宋体",10);
  24. Rectangle = new Rectangle(0,0,this.Font.Height,100);
  25. }
  26. public PrinterTabStops(string text):this()
  27. {
  28. this.Text = text;
  29. }
  30. /// <summary>
  31. /// 或取或设置最大列数
  32. /// </summary>
  33. public int Cols
  34. {
  35. get{return this._cols;}
  36. set{this._cols = value;}
  37. }
  38. /// <summary>
  39. /// 或取或设置打印文本
  40. /// </summary>
  41. public string Text
  42. {
  43. get{return _text;}
  44. set
  45. {
  46. _text = value;
  47. string txt = _text;
  48. _textConverted = ConvertText(txt);
  49. }
  50. }
  51. public void Draw(Graphics g)
  52. {
  53. //文本格式
  54. StringFormat sf = new StringFormat();
  55. sf.FormatFlags = StringFormatFlags.NoWrap;
  56. float colWidth;
  57. colWidth = Rectangle.Width / _cols;
  58. float[] arrcolWidth = new float[_cols];
  59. for(int i = 0 ; i < _cols ; i++)
  60. {
  61. arrcolWidth[i] = colWidth;
  62. }
  63. //起用制表位
  64. sf.SetTabStops(0.0f,arrcolWidth);
  65. g.DrawString(this._textConverted,this.Font,Brushes.Black,this.Rectangle,sf);
  66. }
  67. /// <summary>
  68. /// 将文本以|分隔的文本转换为制表文本
  69. /// </summary>
  70. /// <param name="text"></param>
  71. /// <returns></returns>
  72. private string ConvertText(string text)
  73. {
  74. string txt = text;
  75. txt = txt.Replace("|","t");
  76. return txt;
  77.         }
  78.         #region IDisposable 成员
  79.         public void Dispose()
  80.         {
  81.             this.Font.Dispose();
  82.         }
  83.         #endregion
  84.     }//End Class
  85. }//End NameSpace