急求,C#,打印程序中LPT1可以打印,而USB001无法打印。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;
using System.Data;
using System.IO;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
using Microsoft.Win32.SafeHandles;
using System.Data.OleDb;
using Business;
//using Database;
namespace CBG_Grille
{
class PrintCBG
{
ArrayList arr = new ArrayList();
IncomeBLL income = new IncomeBLL();
// CBGDataInfo cbgserver = new CBGDataInfo();
PrintCBGBLL pCBGbll = new PrintCBGBLL();
private FileStream fs = null;
private const int GENERIC_READ = - 2147483648;
private const int GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
// Font f = new Font("宋体", 16,FontStyle.Bold);
// StringFormat sf = new StringFormat(StringFormatFlags.LineLimit);
[DllImport("kernel32", EntryPoint = "CreateFileA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
private ArrayList DBTableToString(DataTable tb)
{
StringBuilder sb = new StringBuilder();
ArrayList list = new ArrayList();
StringBuilder builder = new StringBuilder();
list.Add("商品名称 |单 价|数量| 金 额");
int num = arr.Count;
int length = 0;
string gridid = arr[length].ToString();
for (int i = 0; i < tb.Rows.Count; i++)
{
builder.Append(tb.Rows[i]["MCD_NAME"].ToString().PadRight(10));
builder.Append(tb.Rows[i]["STO_PRICE"].ToString().PadRight(7));
builder.Append(tb.Rows[i]["STO_STOCK_NUM"].ToString().PadRight(5));
builder.Append(tb.Rows[i]["sumPrice"].ToString().PadRight(8));
list.Add(builder.ToString());
builder.Remove(0, builder.Length);
builder.Append(tb.Rows[i]["GRID_CARD"].ToString() + " 格 ");
builder.Append("厂家:" + tb.Rows[i]["GRG_NAME"].ToString() + "\r\n");
if (tb.Rows[i]["GRG_TEL"].ToString() != "" && tb.Rows[i]["GRG_PHONE"].ToString() != "")
{
builder.Append(" 电话:" + tb.Rows[i]["GRG_PHONE"].ToString() + "\r\n");
}
else
{
if (tb.Rows[i]["GRG_TEL"].ToString() != "" && tb.Rows[i]["GRG_PHONE"].ToString() == "")
{
builder.Append(" 电话:" + tb.Rows[i]["GRG_TEL"].ToString() + "\r\n");
}
if (tb.Rows[i]["GRG_TEL"].ToString() == "" && tb.Rows[i]["GRG_PHONE"].ToString() != "")
{
builder.Append(" 电话:" + tb.Rows[i]["GRG_PHONE"].ToString() + "\r\n");
}
}
builder.Append("-------------------------------");
list.Add(builder.ToString());
builder.Remove(0, builder.Length);
}
arr.Clear();
return list;
}
public void PrintTicket(ArrayList array,DataTable MerTb, double yins, double fk,string cbgid)
//public void PrintTicket(ArrayList array, DataTable MerTb, double yins, double fk, string cbgid, System.Drawing.Printing.PrintPageEventArgs pe)
{
this.arr = array;
try
{
IntPtr handle = CreateFile("LPT1", -1073741824, 0, 0, 3, 0, 0);
// System.Drawing.Printing.PrintPageEventArgs g = pe;
if (handle.ToInt32() != -1)
{
fs = new FileStream(handle, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(fs);
StreamWriter writer = new StreamWriter(fs, Encoding.Default);
ArrayList list = DBTableToString(MerTb);
ShowTotalBLL show = new ShowTotalBLL();
DataSet ds = show.getSPname(frmLogin.shopId.ToString());
string sshopname = ds.Tables[0].Rows[0][2].ToString();
//string sopid = " select CBG_USERS.USER_NUMBER from CBG_USERS ,CBG_SELL where CBG_USERS.USER_ID = CBG_SELL.USER_ID and CBG_SELL.USER_ID = " + "'frmLogin.opId'";
//DataSet rs = cbgserver.GetDataSet(sopid);
string sopid = pCBGbll.getOPNumber(frmLogin.opId);
//g.Graphics.DrawString(sshopname, f, Brushes.Black, 10,20,sf);
writer.WriteLine("-----------------------------");
writer.WriteLine();
writer.WriteLine(" " + sshopname);
writer.WriteLine("单据号:" + cbgid);
writer.WriteLine("操作员:" + sopid + " 单位(元)");
// writer.WriteLine("操作员:" + frmLogin.opId + "");
writer.WriteLine("********************************");
writer.WriteLine();
for (int i = 0; i < list.Count; i++)
{
writer.Write(list[i]);
writer.WriteLine();
}
double num2 = fk - yins;
writer.WriteLine("应收:" + yins.ToString("F2") + " 付款:" + fk.ToString("F2"));
writer.WriteLine("找零:" + num2.ToString("F2"));
writer.WriteLine();
writer.WriteLine("********************************");
writer.WriteLine("GSP兽药王超市连锁,您的管理好帮手");
writer.WriteLine(" 此票为购物凭证!");
writer.WriteLine(" 谢谢惠顾!");
writer.WriteLine();
writer.WriteLine(" 店家电话:" + ds.Tables[0].Rows[0][8].ToString());
writer.WriteLine();
writer.WriteLine(" " + DateTime.Now.ToString());
writer.WriteLine("-------------------------------");
writer.WriteLine();
writer.WriteLine();
writer.WriteLine();
writer.Flush();
fs.Flush();
writer.Close();
fs.Close();
}
else
MessageBox.Show("没有打印机!");
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
public void PrintTicketBack(ArrayList array, DataTable MerTb, double yins, string cbgid, string oldcbgid)
{
this.arr = array;
try
{
IntPtr handle = CreateFile("LPT1", -1073741824, 0, 0, 3, 0, 0);
if (handle.ToInt32() != -1)
{
fs = new FileStream(handle, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(fs);
StreamWriter writer = new StreamWriter(fs, Encoding.Default);
ArrayList list = DBTableToString(MerTb);
//frmShowTotal frmshowtota = new frmShowTotal();
ShowTotalBLL show = new ShowTotalBLL();
DataSet ds = show.getSPname(frmLogin.shopId.ToString());
//string sopid = " select CBG_USERS.USER_NUMBER from CBG_USERS ,CBG_SELL where CBG_USERS.USER_ID = CBG_SELL.USER_ID and CBG_SELL.USER_ID = " + "'frmLogin.opId'";
//DataSet rs = cbgserver.GetDataSet(sopid);
string sopid = pCBGbll.getOPNumber(frmLogin.opId);
writer.WriteLine("-------------------------------");
writer.WriteLine();
writer.WriteLine(" " + ds.Tables[0].Rows[0][2].ToString());
writer.WriteLine("单号:" + cbgid + "");
writer.WriteLine("操作员:" +sopid);
writer.WriteLine("********************************");
writer.WriteLine();
for (int i = 0; i < list.Count; i++)
{
writer.Write(list[i]);
writer.WriteLine();
}
writer.WriteLine("总计:" + yins.ToString("F2"));
writer.WriteLine("原票号:" + oldcbgid + "作废 转本票");
writer.WriteLine();
writer.WriteLine("********************************");
writer.WriteLine("GSP兽药王超市连锁,您的管理好帮手");
writer.WriteLine(" 此票为购物凭证!");
writer.WriteLine(" 谢谢惠顾!");
writer.WriteLine();
writer.WriteLine(" 店家电话:" + ds.Tables[0].Rows[0][8].ToString());
writer.WriteLine();
writer.WriteLine(" " + DateTime.Now.ToString());
writer.WriteLine("-------------------------------");
writer.WriteLine();
writer.WriteLine();
writer.WriteLine();
writer.Flush();
fs.Flush();
writer.Close();
fs.Close();
}
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
}
}