COM学习--2个地址

http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=320

http://www.cnblogs.com/gossip/archive/2009/06/16/1504655.html

 

C#打印原理其实就是生成MDI文件,那么什么是MDI文件呢?MDI是虚拟打印的文档,系统碰到MDI的时候会自动以打印的方式处理。所以,不管用什么模板,什么方式;能在PrintPage事件处理中,生成一张要打印内容的图片就OK!

C#打印原理应用实例:

1.           #region 打印  
2.            
3.           private void btnPrint_Click(object sender, EventArgs e)  
4.            
5.           {  
6.           //C#打印原理之打印预览  
7.           //PrintPreviewDialog ppd = new PrintPreviewDialog();  
8.            
9.           PrintDocument pd = new PrintDocument();  
10.        
11.       //C#打印原理之设置边距  
12.        
13.       Margins margin = new Margins(20, 20, 20, 20);  
14.        
15.       pd.DefaultPageSettings.Margins = margin;  
16.        
17.       ////C#打印原理之纸张设置默认  
18.        
19.       //PaperSize pageSize = new PaperSize("First custom size", 800, 600);  
20.        
21.       //pd.DefaultPageSettings.PaperSize = pageSize;  
22.        
23.       //C#打印原理之打印事件设置  
24.        
25.       pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);  
26.        
27.       //ppd.Document = pd;  
28.        
29.       //ppd.ShowDialog();  
30.        
31.       try 
32.        
33.       {  
34.        
35.       pd.Print();  
36.        
37.       }  
38.        
39.       catch (Exception ex)  
40.        
41.       {  
42.        
43.       MessageBox.Show(ex.Message, "打印出错",  
44.        
45.        MessageBoxButtons.OK, MessageBoxIcon.Error);  
46.        
47.       pd.PrintController.OnEndPrint(pd, new PrintEventArgs());  
48.        
49.       }  
50.        
51.       }  
52.        
53.       //C#打印原理之打印事件处理  
54.        
55.       private void pd_PrintPage(object sender, PrintPageEventArgs e)  
56.        
57.       {  
58.        
59.       string date = lblDate.Text; //当前日期  
60.        
61.       string flowId = lblFlowId.Text; //流水号  
62.        
63.       string payDate = PayDate.Year.ToString() + "" +   
64.        
65.       PayDate.Month.ToString() + ""//应收年月  
66.        
67.       string adminId = lblAdminId.Text;   //操作员编号  
68.        
69.       string baseExpense = lblBaseExpense.Text; //应交基本费用  
70.        
71.       string fine = lblFine.Text;   //罚款数目  
72.        
73.       string upExpense = lblUpExpense.Text;   //上月上余  
74.        
75.       string actualExpense = txtActualExpense.Text;   //实际应交费用  
76.        
77.       string chineseExpense = DecimalToChinese.ConvertSum(actualExpense);    
78.        //实际应交费用的中文大写  
79.        
80.       //C#打印原理之读取图片模板  
81.        
82.       Image temp = Image.FromFile(@"Receipts.jpg");  
83.        
84.       GetResultIntoImage(ref temp, UserId, flowId, date, baseExpense,  
85.        
86.        fine, upExpense, actualExpense, chineseExpense, payDate, adminId);  
87.        
88.       int x = e.MarginBounds.X;  
89.        
90.       int y = e.MarginBounds.Y;  
91.        
92.       int width = temp.Width;  
93.        
94.       int height = temp.Height;  
95.        
96.       Rectangle destRect = new Rectangle(x, y, width, height);  
97.        
98.       e.Graphics.DrawImage(temp, destRect, 0, 0, temp.Width,  
99.        
100.    temp.Height, System.Drawing.GraphicsUnit.Pixel);  
101.    
102.   }  
103.    
104.   /// <summary>  
105.    
106.   /// 将收费结果填充到图片模板  
107.   ///C#打印原理  
108.   /// </summary>  
109.    
110.   private void GetResultIntoImage(  
111.    
112.   ref Image temp,  
113.    
114.   string userId,  
115.    
116.   string flowId,  
117.    
118.   string currentDate,  
119.    
120.   string baseExpense, string actualExpense,  
121.    
122.   string chineseExpense,  
123.    
124.   string payDate,  
125.    
126.   string adminName)  
127.    
128.   {  
129.    
130.   //C#打印原理之读取图片模板  
131.    
132.   Graphics g = Graphics.FromImage(temp);  
133.    
134.   Font f = new Font("宋体", 12);  
135.    
136.   Brush b = new SolidBrush(Color.Black);  
137.    
138.   //C#打印原理之填充数据到图片模板(位置要在制作图片模板的时候度量好)  
139.    
140.   g.DrawImage(temp, 0, 0, temp.Width, temp.Height);  
141.    
142.   g.DrawString(userId, f, b, 168, 105);  
143.    
144.   g.DrawString(UserName, f, b, 166, 134);  
145.    
146.   g.DrawString(flowId, f, b, 535, 105);  
147.    
148.   g.DrawString(currentDate, f, b, 535, 134);  
149.    
150.   g.DrawString(baseExpense, f, b, 219, 202);  
151.    
152.   g.DrawString(fine, f, b, 372, 202);  
153.    
154.   g.DrawString(upExpense, f, b, 486, 202);  
155.    
156.   g.DrawString(actualExpense, f, b, 596, 202);  
157.    
158.   g.DrawString(chineseExpense, f, b, 196, 238);  
159.    
160.   g.DrawString(payDate, f, b, 176, 269);  
161.    
162.   g.DrawString(adminName, f, b, 497, 298);  
163.    
164.   g.Dispose();  
165.    
166.   }  

C#打印原理的基本内容以及实例的解析就向你介绍到这里,希望对你了解和学习C#打印原理有所帮助。

 

================================================================================

C#打印条码一般是通过指令或图片方式来打印,图片形式有fastreport,不过未曾找到VS调用它的方式,仅在Delphi 7中成功的使用。而实际上绝大多数的条码打印机厂商都有一套他们自己的打印指令语言,通过这种语言,可以不需要驱动,支持直接打印,并且操作也很简单,仅仅将指令送入打印机中就好。

VS中存在Com口操作的控件,却未有现成的LPT端口控件,而相对COM口来说,LPT的速度要快,所以在打印的时候客户一般选择LPT通讯方式,经过网上的一些查阅,终于实现了LPT口的打印,打印机为Zebra,写出来与大家分享。

C#打印条码操作的实例:

1.           using System;   
2.           using System.Collections.Generic;   
3.           using System.ComponentModel;   
4.           using System.Data;   
5.           using System.Drawing;   
6.           using System.Text;   
7.           using System.Windows.Forms;   
8.           //C#打印条码操作的实例  
9.           namespace PrintDemo   
10.       {   
11.       public partial class Form1 : Form   
12.       {     
13.       public Form1()   
14.       {   
15.       InitializeComponent();   
16.       }   
17.        
18.       private void Form1_Load(object sender, EventArgs e)   
19.       {   
20.       tbBarCode.Focus();   
21.       }   
22.       //C#打印条码操作的实例  
23.       private void tbBarCode_KeyDown(object sender,   
24.        
25.       KeyEventArgs e)   
26.       {   
27.       switch (e.KeyCode)   
28.       {   
29.       case Keys.Enter:   
30.       PrintBarcode(tbBarCode.Text.Trim());   
31.       tbBarCode.Text = "";   
32.       tbBarCode.Focus();   
33.       break;   
34.       default:   
35.       break;   
36.       }   
37.       }   
38.       private void PrintBarcode(string Barcode)   
39.       {   
40.       Barcode = "^XA^FO48,12^BY4^BCN,152,N,N^FD>;" +   
41.       //C#打印条码操作的实例  
42.       Barcode.Trim() + "^FS^FT62,220^CI0^ABN,44,28^FD" +   
43.        
44.       Barcode.Trim() + "^FS^PQ1,0,1,Y^XZ";   
45.       PrintDemo.POSPrinter prn = new   
46.        
47.       PrintDemo.POSPrinter("LPT1");   
48.       string strmsg = prn.PrintLine(Barcode);   
49.       if (strmsg != "")   
50.       {   
51.       MessageBox.Show(strmsg);   
52.       }   
53.       }   
54.       }   
55.       }   

C#打印条码操作之类POSPrinter定义如下

1.           namespace PrintDemo   
2.           {   
3.           class POSPrinter   
4.           {   
5.           const int OPEN_EXISTING = 3;   
6.           string prnPort = "LPT1";   
7.           [DllImport("kernel32.dll", CharSet = CharSet.Auto)]   
8.           private static extern IntPtr CreateFile(string   
9.            
10.       lpFileName,   
11.       int dwDesiredAccess,   
12.       int dwShareMode,   
13.       int lpSecurityAttributes,   
14.       int dwCreationDisposition,   
15.       int dwFlagsAndAttributes,   
16.       int hTemplateFile);   
17.        
18.       public POSPrinter()   
19.       {   
20.       //  
21.       //   TODO:   在此处添加构造函数逻辑  
22.       //  
23.       }   
24.        
25.       public POSPrinter(string prnPort)   
26.       {   
27.       this.prnPort = prnPort;//打印机端口  
28.       }   
29.        
30.       public string PrintLine(string str)   
31.       {   
32.        
33.       IntPtr iHandle = CreateFile(prnPort, 0x40000000,   
34.        
35.       0, 0, OPEN_EXISTING, 0, 0);   
36.       if (iHandle.ToInt32() == -1)   
37.       {   
38.       return "LPT1 Port Open Failed";   
39.       }   
40.       else   
41.       {   
42.        
43.       FileStream fs = new FileStream(iHandle,   
44.        
45.       FileAccess.ReadWrite);   
46.       StreamWriter sw = new StreamWriter(fs,   
47.        
48.       System.Text.Encoding.Default);//C#打印条码操作之写数据  
49.       sw.WriteLine(str);   
50.       sw.Close();   
51.       fs.Close();   
52.       return "";   
53.       }   
54.       }   
55.       }     
56.       }  

C#打印条码操作的实例浅析就向你介绍到这里,希望对你了解和学习C#打印条码操作有所了解。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值