首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#USB九针打印机解决思路

2013-01-06 
C#USB九针打印机打印机是这个。http://auction1.paipai.com/F300590E0000000004010000113BD1AA。我就想写一

C#USB九针打印机
打印机是这个。http://auction1.paipai.com/F300590E0000000004010000113BD1AA。我就想写一个调用它打印的程序。求助啊,马上要交。
[解决办法]
参考:http://blog.csdn.net/sz_bdqn/article/details/5917427
[解决办法]

引用:
打印机是这个。http://auction1.paipai.com/F300590E0000000004010000113BD1AA。我就想写一个调用它打印的程序。求助啊,马上要交。

问厂家要开发文档吧,我之前做个一个蓝牙的,用的是Win32Api
CreateFile
WriteFile
[解决办法]
如果要调用打印机内置指令、字体等,需要向打印机厂家要文档,各家的指令不同。除非你用printDocument。

USB口的话,还需要向打印机厂家咨询使用的哪个的是哪个接口厂家的U口解决方案,再去索要接口的相关资料(如果安装系统驱动,就不需要了)。

给你个参考,这个当时是可用的,使用的是printDocument方法。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        //PrintDocument类是实现打印功能的核心,它封装了打印有关的属性、事件、和方法
        PrintDocument printDocument = new PrintDocument();


        void printDocument_BeginPrint(object sender, PrintEventArgs e)
        {
            //也可以把一些打印的参数放在此处设置
        }

        void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            //打印啥东东就在这写了
            Graphics g = e.Graphics;
            Brush b = new SolidBrush(Color.Black);
            Font titleFont = new Font("幼圆", 10);
            string title = "0123456789.-";
            g.DrawString(title, titleFont, b, new PointF((e.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 2, 20));
            

            //e.Cancel//获取或设置是否取消打印
            //e.HasMorePages    //为true时,该函数执行完毕后还会重新执行一遍(可用于动态分页)
        }

        void printDocument_EndPrint(object sender, PrintEventArgs e)
        {
            //打印结束后相关操作
        }


        private void btnPrint_Click(object sender, EventArgs e)
        {
            //printDocument.PrinterSettings可以获取或设置计算机默认打印相关属性或参数,如:printDocument.PrinterSettings.PrinterName获得默认打印机打印机名称
            //printDocument.DefaultPageSettings   //可以获取或设置打印页面参数信息、如是纸张大小,是否横向打印等

            //设置文档名
            printDocument.DocumentName = "处方笺";//设置完后可在打印对话框及队列中显示(默认显示document)

            //设置纸张大小(可以不设置取,取默认设置)
            PaperSize ps = new PaperSize("Your Paper Name", 100, 70);
            ps.RawKind = 150; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx)
            printDocument.DefaultPageSettings.PaperSize = ps;

            //打印开始前
            printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint);
            //打印输出(过程)
            printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
            //打印结束
            printDocument.EndPrint += new PrintEventHandler(printDocument_EndPrint);

            //跳出打印对话框,提供打印参数可视化设置,如选择哪个打印机打印此文档等
            PrintDialog pd = new PrintDialog();
            pd.Document = printDocument;
            if (DialogResult.OK == pd.ShowDialog()) //如果确认,将会覆盖所有的打印参数设置
            {
                //页面设置对话框(可以不使用,其实PrintDialog对话框已提供页面设置)
                PageSetupDialog psd = new PageSetupDialog();
                psd.Document = printDocument;
                if (DialogResult.OK == psd.ShowDialog())
                {
                    //打印预览
                    PrintPreviewDialog ppd = new PrintPreviewDialog();
                    ppd.Document = printDocument;
                    if (DialogResult.OK == ppd.ShowDialog())


                        printDocument.Print();          //打印
                }
            }

        }


    }
}


热点排行