C#バックグラウンドコールLPT 1ポートはレシート印刷方法を実現する.
4710 ワード
public class POSPrinter
{
const int OPEN_EXISTING = 3;
string prnPort = "LPT1";
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr CreateFile(string lpFileName,
int dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile);
public POSPrinter()
{
}
public POSPrinter(string prnPort)
{
this.prnPort = prnPort;//
}
public string PrintLine(string str)
{
IntPtr iHandle = CreateFile(prnPort, 0x50000000, 0, 0, OPEN_EXISTING, 0, 0);
if (iHandle.ToInt32() == -1)
{
Console.WriteLine(iHandle.ToString());
return " LPT1";
}
else
{
Console.WriteLine(iHandle.ToString());
FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
sw.WriteLine(" ");
sw.WriteLine();
sw.WriteLine(str);
sw.WriteLine(" ");
sw.WriteLine("---------------------------");
sw.Close();
fs.Close();
return " !";
}
}
}
PrintLine()を直接呼び出します.方法印刷の具体的なパラメータと印刷フォーマットはみんなで自分で調整します.