日期:2014-05-17  浏览次数:21473 次

C# 针式打印机
要用C#控制针式打印机,一行一行的打印,怎么弄呢?

------解决方案--------------------
#region声明
usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.IO;
#endregion
namespaceLongRuan
{
///<summary>
///POSPrinter的摘要说明。
///</summary>
publicclassPOSPrinter
{
constintOPEN_EXISTING=3;
stringprnPort="LPT1";
[DllImport("kernel32.dll",CharSet=CharSet.Auto)]
privatestaticexternIntPtrCreateFile(stringlpFileName,
intdwDesiredAccess,
intdwShareMode,
intlpSecurityAttributes,
intdwCreationDisposition,
intdwFlagsAndAttributes,
inthTemplateFile);
publicPOSPrinter()
{
//
//TODO:在此处添加构造函数逻辑
//
}
publicPOSPrinter(stringprnPort)
{
this.prnPort=prnPort;//打印机端口
}
publicstringPrintLine(stringstr)
{
IntPtriHandle=CreateFile(prnPort,0x40000000,0,0,OPEN_EXISTING,0,0);
if(iHandle.ToInt32()==-1)
{
return"没有连接打印机或者打印机端口不是LPT1";
}
else
{
FileStreamfs=newFileStream(iHandle,FileAccess.ReadWrite);
StreamWritersw=newStreamWriter(fs,System.Text.Encoding.Default);//写数据
sw.WriteLine(str);
//开钱箱
//sw.WriteLine(Chr(&H1B)&Chr(70)&Chr(0)&Chr(20)&Chr(&HA0))
sw.Close();
fs.Close();
return"";
}
}
}
}

--------------------------------------------
调用方法:
LongRuan.POSPrinterprn=newLongRuan.POSPrinter("LPT1");
stringstr=prn.PrintLine("写端口测试!");
if(str!="")
MessageBox.Show(str);
答:>#region声明
usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.IO;
#endregion
namespaceLongRuan
{
///<summary>
///POSPrinter的摘要说明。
///</summary>
publicclassPOSPrinter
{
constintOPEN_EXISTING=3;
stringprnPort="LPT1";
[DllImport("kernel32.dll",CharSet=CharSet.Auto)]
privatestaticexternIntPtrCreateFile(stringlpFileName,
intdwDesiredAccess,
intdwShareMode,
intlpSecurityAttributes,
intdwCreationDisposition,
intdwFlagsAndAttributes,
inthTemplateFile);
publicPOSPrinter()
{
//
//TODO:在此处添加构造函数逻辑
//
}
publicPOSPrinter(stringprnPort)
{
this.prnPort=prnPort;//打印机端口
}
publicstringPrintLine(stringstr)
{
IntPtriHandle=CreateFile(prnPort,0x40000000,0,0,OPEN_EXISTING,0,0);
if(iHandle.ToInt32()==-1)
{
return"没有连接打印机或者打印机端口不是LPT1";
}
else
{
FileStreamfs=newFileStream(iHandle,FileAccess.ReadWrite);
StreamWritersw=newStreamWriter(fs,System.Text.Encoding.Default);//写数据
sw.WriteLine(str);
//开钱箱
//sw.WriteLine(Chr(&H1B)&Chr(70)&Chr(0)&Chr(20)&Chr(&HA0))
sw.Close();
fs.Close();
return"";
}
}
}
}

--------------------------------------------
调用方法:
LongRuan.POSPrinterprn=newLongRuan.POSPrinter("LPT1");
stringstr=prn.PrintLine("写端口测试!");
if(str!="")
MessageBox.Show(str);
------解决方案--------------------
用api createfile 像写文件一样往 lpt1 写

随便找个代码,没有仔细看,思路是对的。
http://bytes.com/topic/c-sharp/answers/942783-write-escape-sequence-epson-printer