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

打印编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. namespace GoldPrinter
  4. {
  5. /// <summary>
  6. /// 子标题。
  7. /// 
  8. /// 作 者:长江支流(周方勇)
  9. /// Email:flygoldfish@163.com  QQ:150439795
  10. /// 网 址:www.webmis.com.cn
  11. /// ★★★★★您可以免费使用此程序,但是请您完整保留此说明,以维护知识产权★★★★★
  12. /// 
  13. /// </summary>
  14. public class Caption:Printer
  15. {
  16. private string _text; //文本
  17. private int _maxRows; //限制行数
  18. private bool _hasborder; //画边框
  19. /// <summary>
  20. ///  默认构造函数
  21. /// </summary>
  22. public Caption()
  23. {
  24. //初始值
  25. _text = "";
  26. _maxRows = 1;
  27. _hasborder = false;
  28. this.Font = new Font("宋体",13,FontStyle.Italic); //字体
  29. this.IsDrawAllPage = true;
  30. }
  31. /// <summary>
  32. /// 子标题构造函数
  33. /// </summary>
  34. /// <param name="text"></param>
  35. public Caption(string text):this()
  36. {
  37. this._text = text;
  38. }
  39. public string Text
  40. {
  41. get{return this._text;}
  42. set{this._text = value;}
  43. }
  44. public bool HasBorder
  45. {
  46. get{return this._hasborder;}
  47. set{this._hasborder = value;}
  48. }
  49. /// <summary>
  50. /// 限制行数,小于0为不限制
  51. /// </summary>
  52. public int MaxRows
  53. {
  54. get{return this._maxRows;}
  55. set{this._maxRows = value;}
  56. }
  57. public override void Draw()
  58. {
  59. base.Draw();
  60. //绘图起始座标及字符串的宽与高
  61. int x,y;
  62. x = this.PrinterMargins.X1;
  63. y = this.PrinterMargins.Y1;
  64. //相对移动
  65. x += this.MoveX;
  66. y += this.MoveY;
  67. //测量字符串尺寸是否过量
  68. int width = this.PrinterMargins.Width; //用最宽使居中 //this.TextWidth(this._text); //获取文本的宽,最宽不会超过有效打印页的宽
  69. int height = this.TextHeight(this._text); //获取文本的高,测量基宽为有效打印页的宽
  70. //不能超过最高
  71. if (height > this.PrinterMargins.Height)
  72. {
  73. height = this.PrinterMargins.Height;
  74. }
  75. //限制行数
  76. if (this._maxRows > 0)
  77. {
  78. if (height > this.Font.Height * _maxRows)
  79. {
  80. height = this.Font.Height * _maxRows;
  81. }
  82. }
  83.            
  84. //文本的高测量基宽为有效打印页的宽,因此会根据实际字符自动换行
  85. Rectangle rec = new Rectangle(x,y,width,height);
  86.             
  87. StringFormat sf = new StringFormat();
  88. sf.Alignment = StringAlignment.Center; //横向居中
  89. sf.LineAlignment = StringAlignment.Center; //竖向居中
  90. #region 计算实际绘图区
  91. //写绘制时的坐标信息
  92. rec.X = (this.PrinterMargins.Width - this.TextWidth(this.Text))/2  + this.PrinterMargins.Left + this.MoveX;
  93. rec.Y = y;
  94. if (this.TextWidth(this.Text) < this.PrinterMargins.Width)
  95. {
  96. rec.Width = this.TextWidth(this.Text);
  97. }
  98. else
  99. {
  100. rec.Width = this.PrinterMargins.Width;
  101. }
  102. rec.Height = height;
  103. this.Rectangle = rec;
  104. #endregion
  105. //画上打印有效区的线
  106. if (_hasborder)
  107. {
  108. this.Graphics.DrawRectangle(this.Pen,this.Rectangle.X,this.Rectangle.Y,this.Rectangle.Width,this.Rectangle.Height);
  109. }
  110. rec.X = rec.X - 1;
  111. rec.Y = rec.Y - 1;
  112. rec.Width = rec.Width + 2;
  113. rec.Height = rec.Height + 2;
  114. //输出文本
  115. this.Graphics.DrawString(_text,this.Font,this.Brush,rec,sf);
  116. this.Height = height;
  117. }
  118. }//End Class
  119. }//End NameSpace