码迷,mamicode.com
首页 > Windows程序 > 详细

C# winform打印总结 z

时间:2014-12-21 19:20:00      阅读:413      评论:0      收藏:0      [点我收藏+]

标签:

http://blog.csdn.net/jing_xin/article/details/41444063

针对BEIYANG收据打印机 BTP-R580测试通过。

操作说明:http://www.docin.com/p-395110672.html

1、一般的打印

static Font printFont; //打印使用的字体
        public static void PrintEventPage(object sender, PrintPageEventArgs e)
        {

            float yPos = 0;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            //string line = null;
            //string subs = " ";
            string context = null;


            //打印字体
            printFont = new Font("宋体", 14F, FontStyle.Bold | FontStyle.Underline);
            //打印内容

            context = "收银小票";
            //打印的位置
            yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
            e.Graphics.DrawString(context, printFont, Brushes.Black,
            50, 10, new StringFormat());
            //换行
            count++;


            e.HasMorePages = false;
        }

 

调用处:

            System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
            System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();
            docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintEventPage);

            PrintDialog1.AllowSomePages = true;
            PrintDialog1.ShowHelp = true;


            PrintDialog1.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
            PrintController printController = new StandardPrintController();
            docToPrint.PrintController = printController;


            DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框

            //If the result is OK then print the document.
            if (result == DialogResult.OK)
            {
                docToPrint.Print();//开始打印
            }

效果:直接调出设置打印机的窗口。

2、USB打印

思路:
1、先找到USB 打印机。SetupDiGetClassDevs、SetupDiEnumDeviceInfo、SetupDiGetDeviceRegistryProperty,找到“USB 支持设备”;
2、然后就是老路,获得该USB打印机的路径,SetupDiEnumDeviceInterfaces、SetupDiGetInterfaceDeviceDetail,获得路径。(SetupDiGetClassDevs不用了,因为1中已经获得句柄集)
3、再根据路径CreateFile,后面的内容网上一搜一大堆。

步骤:
1、CreateFile
2、SetupComm
3、SetCommTimeouts
4、GetCommState
5、SetCommState
6、PurgeComm
7、WriteFile


不知道是不是绕远路了,呵呵。

参考资料:http://www.cnblogs.com/SunYu/archive/2010/04/29/1723977.html
参考代码:http://www.codeproject.com/Articles/14500/Detecting-Hardware-Insertion-and-or-Removal

 

USB下直接用程序访问不太方便,于是利用下面这个连接里的方法进行LPT并口转换:

http://www.yt-pos.com/ask/question.php?id=263

然后就可以连接上打印机了。

参考其他网站:

http://zhidao.baidu.com/link?url=3hhQOUVhcJdaV9tNgxNnD_SqEUgA-cesSciNvhV42a4G6Pu8u5r17m1J3HmHTud_6ZAtyQquX9gHzVoIYgaLqa

http://bbs.csdn.net/topics/390085304?page=1#post-398581498

http://www.cnblogs.com/zbo/archive/2009/07/11/1521454.html

 

打印输出并切纸操作:

第一点: 

const uint GENERIC_READ =0x80000000;
        const uint GENERIC_WRITE = 0x40000000;     
        const int OPEN_EXISTING = 3;
        private FileStream fs=null;
        IntPtr iHandle;
        [DllImport("kernel32.dll")]
        public static extern IntPtr CreateFileA(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);

        [DllImport("kernel32.dll")]
        private static extern bool WriteFile(
        int hFile,
        byte[] lpBuffer,
        int nNumberOfBytesToWrite,
        ref   int lpNumberOfBytesWritten,
        ref   int i
        );

        [DllImport("kernel32.dll")]
        private static extern bool CloseHandle(IntPtr hObject);

第二点:

 protected void imageButton1_Click(object sender, ImageClickEventArgs e)
        {
          
            ShopCart mycart = CartCtroller.GetCart();         
           iHandle = CreateFileA("LPT1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
           if (iHandle.ToInt32() == -1)
           {
               Jscript.AjaxRunJs(this.Page, "alert(‘没有连接打印机或者打印机端口不是LPT1!‘)");
               return;
           }
           else
           {             
               fs = new FileStream(iHandle, FileAccess.ReadWrite);
             
               //for (int i = 0; i < 5; i++)
               //{
               //    fs.WriteByte(0x0A); //走纸(忘记了可能是这个)
               //}
               //StreamReader sr = new StreamReader(fs);   //读数据;  
               //StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);  //些数据;  
            
               if (mycart.Count == 0)
               {
                   Jscript.AjaxRunJs(this.Page, "alert(‘请先购买产品!‘)");
                   return;
               }   

               byte[] buf = new byte[500];
               char ESC = (char)(27);
               char GS = (char)(29);                                                                                                                 
               char LF = (char)(10);
           

     //读出数据
               StringBuilder SEND = new StringBuilder();
               //SEND.Append(LF.ToString());    
               char c1 = (char)60;
               char c2 = (char)0;
               SEND.Append(GS.ToString() + "L" + c1.ToString() + c2.ToString());
               SEND.Append(ESC + "D" + Convert.ToChar(25) + Convert.ToChar(30) + Convert.ToChar(0));
               string dt = DateTime.Now.ToString();
               string mingcheng = "产品名称:";
               string miaoshu = "产品描述:";
               string shuliang = "购买数量:";
               string zhi = "支";
               SEND.Append(LF.ToString());   
               SEND.Append("欢 迎 光 临 - 上海金福");
               SEND.Append("\r\n");
               SEND.Append("-------------------------------");
               SEND.Append(LF.ToString());
               SEND.Append("购买时间:"+dt);
               SEND.Append("\r\n");
               SEND.Append("-------------------------------");
               SEND.Append(LF.ToString());
               SEND.Append(LF.ToString());
               for (int i = 0; i < mycart.Count; i++)
               {
                   modelArrayProduct temp = (modelArrayProduct)mycart[i];                
                   SEND.Append(mingcheng + temp.T_name);
                   SEND.Append("\r\n");
                   SEND.Append(miaoshu +   temp.T_miaoshu);
                   SEND.Append("\r\n");  
                   SEND.Append(shuliang + temp.T_shuliang.ToString() + zhi);
                   SEND.Append("\r\n");  
                   SEND.Append("-------------------------------");
                   SEND.Append(LF.ToString());                   
               }            

               //走纸
               SEND.Append(LF.ToString());
               SEND.Append(LF.ToString());             
               SEND.Append(LF.ToString());
               SEND.Append(LF.ToString());
               SEND.Append(GS + "V" + Convert.ToChar(1));
             
                //字符类型转换
               Encoding utf8 = Encoding.GetEncoding(65001);
               Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936
               byte[] tempa = utf8.GetBytes(SEND.ToString());

               buf = Encoding.Convert(utf8, gb2312, tempa);
                                       
              fs.Write(buf, 0, buf.Length);
                          fs.Close();                       
           }
                 CloseHandle(iHandle);     

        }

 

 

C#执行批处理命令

using System.Diagnostics ;

using System.IO;

 

private void btnRun_Click(object sender, EventArgs e)
        {
            txtResult.Text = "";
            processCommand("Ping.exe", this.txtAddress.Text);
            processCommand("Ping.exe", this.txtAddress.Text);
        }

 

        public void processCommand(string commandName, string argument)
        {
            ProcessStartInfo start = new ProcessStartInfo(commandName);//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
            //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
            start.WorkingDirectory = "d:\\360Downloads\\";
            start.Arguments = argument;//设置命令参数
            start.CreateNoWindow = true;//不显示dos命令行窗口
            start.RedirectStandardOutput = true;//
            start.RedirectStandardInput = true;//
            start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
            txtResult.AppendText(start.WorkingDirectory  + "\n");
            Process p = Process.Start(start);
            StreamReader reader = p.StandardOutput;//截取输出流
            string line = reader.ReadLine();//每次读取一行
            while (!reader.EndOfStream)
            {
                txtResult.AppendText(line + "\n");
                line = reader.ReadLine();
            }
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();//关闭进程
            reader.Close();//关闭流
        }

以上均测试成功。

 

补充BEIYANG BTP-R580相关参数:

 

主要参数  
打印方式 热敏打印 分辨率 203×203dpi
最高打印速度 230mm/s 打印宽度(mm) 80
基本参数  
打印方式 热敏打印 分辨率 203×203dpi
最高打印速度 230mm/s    
高级参数  
打印宽度(mm) 80 打印方向 双向打印
纸张种类 连续纸, 标记纸, 标签纸 纸张厚度 0.06mm-0.1mm
字体 ASCII(12×24), 压缩 ASCII(9×17), 国标宋体I, Ⅱ级(24×24), 国际字符 供纸方式 自动
接口类型 并行接口(IEEE 1284), RS-232, RS-485/422串行接口, USB接口, Ethernet接口, WLAN接口可选    
电源参数  
电源电压(V) 220±10% 电源频率(Hz) 50-60
外观参数  
颜色 黑色 重量(Kg) 1.3
高度(mm) 147 宽度(mm) 147
长度(mm) 205    

测试打印机二:

ESPON TM TM58 票据打印机

 

爱普生 T58 基本参数
产品类型: 微型热敏打印机
打印方式: 热敏式
打印宽度: 32/16
缓冲区: 2KB
接口类型: RS-232,Bi-directionalparallel
爱普生 T58 打印性能
打印速度: 最大100mm/sec
打印分辨率: 360dpi
字符集: 95Alphanumeric,37 International,128×1Graphic
字体: 12×24(ANK)/24×24(Chinese)
爱普生 T58 介质规格
介质宽度: 57.5±0.5mm
介质厚度: 0.06-0.07mm
爱普生 T58 其它参数
平均无故障时间: 160000小时
产品尺寸: 116×204×137mm
产品重量: 1.3kg
系统平台: Windows2000/XP
电源电压: DC24V(±7%),1.1A
其它特性: 字符尺寸:1.25×3.0mm/2.5×3.0mm
每英寸字符数:16.9cpi/10.2cpi
D.K.D.功能:2drives
外壳颜色:ECW
安全标准:CCC
电源:Exclusive AC adapter(included)
配件:AC adapter,I/F cable,User’s manual
安全标准:UL/CSA/T+V(E N60950)

另参考:

 http://blog.csdn.net/guoyong4321/article/details/7079333

C# winform打印总结 z

标签:

原文地址:http://www.cnblogs.com/zeroone/p/4176847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!