快乐生活 轻松工作

在B/S架构下实现对位打印

现在我们实现打印类的基类

using System;
using System.Drawing;
using System.Drawing.Printing;

namespace WGSys.DataExchange
{
    
public class PrinterBase
    
{
        
protected static readonly Font memFont = new Font("宋体"9);        //普通字体
        protected static readonly Font memBarCodeFont=new Font("C39HrP24DhTt",36); //条形码字体

        
protected PrintPageEventHandler memHandler;
        
string memDocName = null;

        
public PrinterBase()
        
{
            memHandler 
= new PrintPageEventHandler(this.PageHandler);//表示将要处理 PrintDocument 的 PrintPage 事件的方法
        }


        
public string DocName
        
{
            
get
            
{
                
return memDocName;
            }

            
set
            
{
                memDocName 
= value;
            }

        }


        
public virtual void Print(PrintDocument tempPrnDoc,bool isPreview)
        
{
                tempPrnDoc.PrintPage 
+= memHandler;
                
if ((memDocName!=null)&&(memDocName.Length>0))
                    tempPrnDoc.DocumentName 
= memDocName;//打印文档时要显示的文档名。默认情况下为“文档”
                if(isPreview)
                
{
                    System.Windows.Forms.PrintPreviewDialog pd 
= new System.Windows.Forms.PrintPreviewDialog();
                    pd.Document 
= tempPrnDoc;
                    pd.ShowDialog();
                }

                
else
                
{
                    tempPrnDoc.Print();
                }

        }

        
public virtual void OnPrintPage(PrintPageEventArgs ev)
        
{
            ev.Graphics.DrawString(
                
"Output",
                memFont,
                Brushes.Black,
                ev.MarginBounds.Left,
                ev.MarginBounds.Top,
                
new StringFormat()
                );
            
//=================
            
            ev.HasMorePages 
= false;
        }


        
protected void PageHandler(object sender, PrintPageEventArgs ev)
        
{
            OnPrintPage(ev);
        }

    }

}

打印所需的数据我们用一个数据类来表示,每个属性我们应用[XmlAttribute]特性,为此我们可是使用XML的序列化和反序列化以便把数据转换成XML格式通过WEB SERVER的方式传递给客户端的控件处理,同时客户端控件接受到XML数据以后也可以进行反序列化还原出数据类实例。
/*
 *                账单打印数据类
 * Create:2005-8-11    Version:WGWATER1.1   Author:maojun
 * 
 * 
*/

using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace WGSys.DataExchange
{
    
/// <summary>
    
/// Summary description for BillPrintData.
    
/// </summary>

    public class BillPrintData
    
{
        
private static XmlSerializer memBillPrnDataSerializer;
        
static BillPrintData()
        
{
            memBillPrnDataSerializer 
= new XmlSerializer(typeof(BillPrintData)); 
        }


        
public string ToXml()
        
{
            StringWriter sw 
= new StringWriter(); 
            XmlTextWriter writer 
= new XmlTextWriter(sw);
            memBillPrnDataSerializer.Serialize(writer,
this);
            writer.Close();
            sw.Close();
            
return sw.ToString();
        }


        
public static BillPrintData FromXml(string xml)
        
{
            StringReader r 
= new StringReader(xml);
            XmlTextReader reader 
= new XmlTextReader(r);
            
return memBillPrnDataSerializer.Deserialize(reader) as BillPrintData;
        }

 
        
//抄表员
        private string mynotename;
        [XmlAttribute]
        
public string NoteName{
            
get{return mynotename;}
            
set{mynotename=value;}
        }

        
//统册号
        private string mytcnumber;
        [XmlAttribute]
        
public string TCNumber{
            
get{return mytcnumber;}
            
set{mytcnumber=value;}
        }

        ..
        ..
    }

}

posted on 2006-02-22 15:07  思考 学习 总结  阅读(308)  评论(0)    收藏  举报