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

打印编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Printing;
  4. namespace GoldPrinter
  5. {
  6. /// <summary>
  7. /// 打印基类,继承于DrawBase,IPrinter。
  8. /// 
  9. /// 作 者:长江支流(周方勇)
  10. /// Email:flygoldfish@163.com  QQ:150439795
  11. /// 网 址:www.webmis.com.cn
  12. /// ★★★★★您可以免费使用此程序,但是请您完整保留此说明,以维护知识产权★★★★★
  13. /// 
  14. /// </summary>
  15. public class Printer:PrinterBase
  16. {        
  17. private Sewing _sewing;         //装订线预留
  18. private Font _font;                             //绘制对象的文本字体
  19.         private bool _isDrawAllPage;     //多页打印时,每页都绘制,默认否
  20.         private int _height;         //对象高
  21. //绘制对象相对移动
  22. private int _MoveX;
  23. private int _MoveY;
  24. #region  ************字段属性************
  25. /// <summary>
  26. /// 获取或设置装订线预留,装订线Sewing不再影响页边距等,影响页边距仅仅是PrintDocument。
  27. /// </summary>
  28. public virtual Sewing Sewing
  29. {
  30. get
  31. {
  32. return this._sewing;
  33. }
  34. set
  35. {
  36. if (value != null)
  37. {
  38. this._sewing = value;
  39. }
  40. }
  41. }
  42.         /// <summary>
  43.         /// 绘制对象文本字体
  44.         /// </summary>
  45.         public virtual Font Font
  46.         {
  47.             get
  48.             {
  49.                 return this._font;
  50.             }
  51.             set
  52.             {
  53.                 if (value != null)
  54.                 {
  55.                     this._font = value;
  56.                 }
  57.             }
  58.         }
  59. /// <summary>
  60. /// 多页打印时,每页是否都绘制,默认否
  61. /// </summary>
  62. public virtual bool IsDrawAllPage
  63. {
  64. get
  65. {
  66. return this._isDrawAllPage;
  67. }
  68. set
  69. {
  70. this._isDrawAllPage = value;
  71. }
  72. }
  73.         /// <summary>
  74.         /// 获取或设置对象的高
  75.         /// </summary>
  76.         public virtual int Height
  77.         {
  78.             get
  79.             {
  80.                 return _height;
  81.             }
  82.             set
  83.             {
  84.                 _height = value;
  85.                 if (_height < 0)
  86.                 {
  87.                     _height = 0;
  88.                 }
  89.             }
  90.         }
  91. /// <summary>
  92. /// 绘图起始点相对移动值
  93. /// </summary>
  94. public int MoveX
  95. {
  96. get{return _MoveX;}
  97. set{_MoveX = value;}
  98. }
  99. /// <summary>
  100. /// 绘图起始点相对移动值
  101. /// </summary>
  102. public int MoveY
  103. {
  104. get{return _MoveY;}
  105. set{_MoveY = value;}
  106. }
  107. #endregion
  108. #region 构造函数
  109. /// <summary>
  110. /// 打印类
  111. /// </summary>
  112. public Printer()
  113. {            
  114. _sewing = new Sewing(); 
  115. _font = new Font("宋体",10);
  116. _isDrawAllPage = false;
  117.             _height = 0;
  118. // //其它初始
  119. _MoveX = 0;
  120. _MoveY = 0;
  121. }
  122. #endregion
  123. /// <summary>
  124. /// 获取文本的宽,最宽不会超过有效打印页的宽
  125. /// </summary>
  126. /// <param name="text"></param>
  127. /// <returns></returns>
  128. public int TextWidth(string text)
  129. {
  130. int width = (int)(Graphics.MeasureString(text,this.Font,this.PrinterMargins.Width).Width);
  131. return width;
  132. }
  133. /// <summary>
  134. /// 获取文本的高,测量基宽为有效打印页的宽
  135. /// </summary>
  136. /// <param name="text"></param>
  137. /// <returns></returns>
  138. public int TextHeight(string text)
  139. {
  140. int height = (int)(Graphics.MeasureString(text,this.Font,this.PrinterMargins.Width).Height);
  141. return height;
  142. }
  143. /// <summary>
  144. /// 获取文本的行距
  145. /// </summary>
  146. /// <returns></returns>
  147. public int TextHeight()
  148. {
  149. return this.Font.Height;
  150. }
  151. /// <summary>
  152. /// 绘制印区域
  153. /// </summary>
  154. public virtual void DrawPrinterMargins()
  155. {
  156. CheckGraphics();
  157. //Rectangle rec = new Rectangle(this.PrinterMargins.Left - ADD_PRINTER_MARGINS,this.PrinterMargins.Top - ADD_PRINTER_MARGINS,this.PrinterMargins.Width + ADD_PRINTER_MARGINS * 2,this.PrinterMargins.Height + ADD_PRINTER_MARGINS * 2);
  158.             Rectangle rec = new Rectangle(this.PrinterMargins.Left,this.PrinterMargins.Top,this.PrinterMargins.Width,this.PrinterMargins.Height);
  159.             Graphics.DrawRectangle(this.Pen,rec);
  160. }
  161. /// <summary>
  162. /// 绘制装订线
  163. /// </summary>
  164. public virtual void DrawSewing()
  165. {
  166. CheckGraphics();
  167. this.Sewing.LineLen = GetSewingLineLength();
  168. this.Sewing.Draw(this.Graphics);
  169. }
  170.         private int GetSewingLineLength()
  171.         {
  172.             int mLineLen = 0;
  173.             if (this.Sewing.SewingDirection == SewingDirectionFlag.Left)
  174.             {
  175.                 mLineLen = this.PageHeight;
  176.             }
  177.             else if(this.Sewing.SewingDirection == SewingDirectionFlag.Top)
  178.             {
  179.                 mLineLen = this.PageWidth;
  180.             }
  181.             return mLineLen;
  182.         
  183.         }
  184. /// <summary>
  185. /// 在绘图表面画图,这里没有实际的绘图,仅用CheckGraphics()检测了Graphics是否为空,实际的绘图过程由继承者去画。可以重写CheckGraphics()。
  186. /// </summary>
  187. public override void Draw()
  188. {
  189. CheckGraphics();
  190. //...
  191. //下面由继承者去画具体的
  192. }
  193. protected virtual void CheckGraphics()
  194. {
  195. if (this.Graphics == null)
  196. {
  197. throw new Exception("绘图表面不能为空,请设置Graphics属性!");
  198. }
  199. }
  200.         public override void Dispose()
  201.         {
  202.             base.Dispose ();
  203. _sewing.Dispose();
  204.             _font.Dispose();
  205.         }
  206. //        //加上装订的非打印区域
  207. //        private void AddSewingNonePrintArea()
  208. //        {
  209. //            if (this.Sewing.SewingDirection == SewingDirectionFlag.Left)
  210. //            {
  211. //                this.PrinterMargins.Left += this.Sewing.Margin;
  212. //                this.PrinterMargins.Width -= this.Sewing.Margin;
  213. //            }
  214. //            else if (this.Sewing.SewingDirection == SewingDirectionFlag.Top)
  215. //            {
  216. //                this.PrinterMargins.Top += this.Sewing.Margin;
  217. //                this.PrinterMargins.Height -= this.Sewing.Margin;
  218. //            }
  219. //        }        
  220. }//End Class
  221. }//End NameSpace