条码打印系统的实现
一、如何控制条码打印机
通过向条码打印机发送ZPLII指令来实现。(指令参考后面的介绍)
二、条码打印出中文
只要是通过调用com组建kernel32.dll来实现
/// <summary>
/// 試打中文
/// </summary>
private void PrintChn()
{
string sChn = txtExBarcode.Text;
if (sChn.Trim().Equals(""))
{
txtExBarcode.Focus();
MessageBox.Show("請輸入中文字");
return;
}
StringBuilder sResult = new StringBuilder();
sResult.Append("^XA");
int nWidth = 0, nHeight = 0;
int nStartX = 50, nStartY = 0;
for (int i =10; i <= 100; i += 10)
{
StringBuilder hexbuf = new StringBuilder(21 * 1024);
int count = GETFONTHEX(sChn, fontDialog1.Font.Name, 0, nHeight + i, nWidth + i, 1, 0, hexbuf);
if (count > 0)
{
nStartY += nHeight + i + 35;
string sEnd = "^FO" + nStartX.ToString() + "," + nStartY.ToString() + "^XGOUTSTR" + i.ToString() + ",1,2^FS ";
sResult.Append(hexbuf.ToString().Replace("OUTSTR01", "OUTSTR" + i.ToString()) + sEnd);
}
}
sResult.Append("^XZ");
CreateLPTFile(sResult.ToString());
}
#region //Create LPT File
private void CreateLPTFile(string str)
{
try
{
string LPT = "LPT1";
int hPort = CreateFile(LPT, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
System.IntPtr hPortP = new IntPtr(hPort);
FileStream fs;
fs = new FileStream(hPortP, FileAccess.Write, false);
StreamWriter writer = new StreamWriter(fs);
writer.AutoFlush = false;
writer.WriteLine(str);
writer.Flush();
writer.Close();
fs.Close();
CloseHandle(hPort);
}
catch
{
//statusBar1.Panels[0].Text = "I/O錯誤:請檢查打印機是否打開連接!" + eIO.Message;
}
}
#endregion
#region //API GETFONTHEX
[DllImport("fnthex32.dll", CharSet = CharSet.Ansi)]
public static extern int GETFONTHEX(
string chnstr,
string fontname,
int orient,
int height,
int width,
int bold,
int italic,
StringBuilder cBuf);
#endregion
#region //API Create file
[DllImport("kernel32.dll")]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);
#endregion
#region //API CloseHandle
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(int hObject);
#endregion