using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Cjwdev;
using Cjwdev.WindowsApi;
using System.Runtime.InteropServices;
using System.Configuration;
namespace QM.Assist
{
public class PrintUtil
{
///
/// 条码打印队列1
///
public static List LabelList1 = new List();
///
/// 条码打印队列2(PDF虚拟打印机)
///
public static List LabelList2 = new List();
int count1 = 0;
///
/// 截取字符串函数每行放20个字符
///
///
///
private string[] strSplit(string str)
{
//计数如果大于20则放弃
string[] tempStr = new string[3];
int strNo = 0;
//分解的次数
int number = 0;
string strTemp = string.Empty;
for (int i = 0; i < str.Length; i++)
{
strTemp = str.Substring(i, 1);
byte[] strByte = Encoding.Default.GetBytes(strTemp);
if (strNo + strByte.Length <= 20)
{
strNo += strByte.Length;
tempStr[number] = tempStr[number] + strTemp;
}
else
{
strNo = strByte.Length;
number += 1;
tempStr[number] = tempStr[number] + strTemp;
}
}
return tempStr;
}
///
/// 打印条码2(PDF)
///
public void PrintLabel2(string proPath, string temPath, string dataPath, string charr)
{
/*************************************测试用***********************************************************************************************************/
#region
while (true)
{
count1 = LabelList2.Count;
System.Diagnostics.Process[] thisproc = System.Diagnostics.Process.GetProcessesByName("Lmwprint");
if (thisproc.Length == 0)
{
if (count1 > 0)
{
while (true)
{
try
{
string head = System.IO.File.ReadAllLines(dataPath, Encoding.ASCII)[0];
string[] lines = new string[count1 + 1];
lines[0] = head;
for (int i = 1; i <= count1; i++)
{
string hb = LabelList2[i - 1].BarCode.Substring(12, 1);
//ll.BarCode = item + ",*" + item + "*" + "," + (hb == "H" ? "高" : "");
lines[i] = LabelList2[i - 1].BarCode + "," + charr + LabelList2[i - 1].BarCode + charr + "," + (hb == "H" ? "高" : "");
//QM.Assist.ExceptionUtil.WriteTrace(lines[i]);
}
System.IO.File.WriteAllLines(dataPath, lines, Encoding.Default);
lines = null;
break;
}
catch (Exception ex)
{
throw ex;
}
}
try
{
//System.Diagnostics.Process proc = new System.Diagnostics.Process();
//proc.StartInfo.FileName = "\"" + proPath + "\"";
//proc.StartInfo.Arguments = " \"" + temPath + "\"";
//proc.Start();
string appStartPath = proPath;
IntPtr userTokenHandle = IntPtr.Zero;
ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle);
ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION();
ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO();
startInfo.cb = (uint)Marshal.SizeOf(startInfo);
ApiDefinitions.CreateProcessAsUser(
userTokenHandle,
appStartPath,
" " + temPath,
IntPtr.Zero,
IntPtr.Zero,
false,
0,
IntPtr.Zero,
null,
ref startInfo,
out procInfo);
if (userTokenHandle != IntPtr.Zero)
ApiDefinitions.CloseHandle(userTokenHandle);
for (int i = 0; i < count1; i++)
{
LabelList2.RemoveAt(0);
}
break;
}
catch (Exception ex)
{
throw ex;
}
}
}
else
{
thisproc[0].Kill();
}
System.Threading.Thread.Sleep(1000);
}
#endregion
}
///
/// 修改2号条码数据文件(PDF)
///
private void ModifyDataBase2(string dataPath)
{
#region
#endregion
}
///
/// 打印条码
///
public void PrintLabel()
{
#region
while (true)
{
count1 = LabelList2.Count;
System.Diagnostics.Process[] thisproc = System.Diagnostics.Process.GetProcessesByName("Lmwprint");
if (thisproc.Length == 0)
{
if (count1 > 0)
{
ModifyDataBase();
try
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "\"" + "程序文件" + "\"";
proc.StartInfo.Arguments = " \"" + "模板文件" + "\"";
proc.Start();
for (int i = 0; i < count1; i++)
{
LabelList2.RemoveAt(0);
}
break;
}
catch (Exception ex)
{
throw ex;
}
}
}
else
{
thisproc[0].Kill();
}
System.Threading.Thread.Sleep(1000);
}
#endregion
}
///
/// 修改2号条码数据文件(PDF)
///
private void ModifyDataBase()
{
#region
while (true)
{
try
{
string head = System.IO.File.ReadAllLines("数据文件", Encoding.ASCII)[0];
string[] lines = new string[count1 + 1];
lines[0] = head;
for (int i = 1; i <= count1; i++)
{
lines[i] = LabelList2[i - 1].LabelData.Rows[0]["LABELID"].ToString()
+ "," + "零件号 " + LabelList2[i - 1].LabelData.Rows[0]["MATNO"].ToString()
+ "," + LabelList2[i - 1].LabelData.Rows[0]["ITEMNAME"].ToString()
+ "," + LabelList2[i - 1].LabelData.Rows[0]["MATNAME"].ToString()
+ "," + LabelList2[i - 1].LabelData.Rows[0]["PACKAAGEQUAN"].ToString()
+ "," + LabelList2[i - 1].LabelData.Rows[0]["COLOR"].ToString()
+ "," + LabelList2[i - 1].LabelData.Rows[0]["SIZE"].ToString()
+ "," + LabelList2[i - 1].LabelData.Rows[0]["WEIGHT"].ToString()
+ "," + LabelList2[i - 1].LabelData.Rows[0]["BATCH"].ToString()
+ "," + "供应商编号 " + LabelList2[i - 1].LabelData.Rows[0]["VENDORID"].ToString()
+ "," + LabelList2[i - 1].LabelData.Rows[0]["VENDORNAME"].ToString();
//QM.Assist.ExceptionUtil.WriteTrace(lines[i]);
}
System.IO.File.WriteAllLines("数据文件", lines, Encoding.Default);
lines = null;
break;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
public static class BarCode
{
public static string Get128CodeString(string inputData)
{
string result;
int checksum = 104;
for (int ii = 0; ii < inputData.Length; ii++)
{
if (inputData[ii] >= 32)
{
checksum += (inputData[ii] - 32) * (ii + 1);
}
else
{
checksum += (inputData[ii] + 64) * (ii + 1);
}
}
checksum = checksum % 103;
if (checksum < 95)
{
checksum += 32;
}
else
{
checksum += 100;
}
result = Convert.ToChar(204) + inputData + Convert.ToChar(checksum) + Convert.ToChar(206);
return result;
}
}
}
}