针对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
然后就可以连接上打印机了。
参考其他网站: