C# winform打印总结

本文介绍了如何使用C# Winform进行打印操作,包括基本的打印方法、设置打印机字体和内容,以及USB打印的实现步骤。通过示例代码展示了创建PrintPage事件处理程序、调用PrintDialog进行打印,并提供了处理USB打印机和执行批处理命令的参考链接。还涉及到特定品牌打印机BEIYANG BTP-R580的打印参数和走纸、切纸控制。
摘要由CSDN通过智能技术生成

 针对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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值