using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Beiyang;
using ASAP.Extend.UIControl.Common;
using ASAP.Extend.UIControl.Dialog;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace LableClassLibrary
{
public class LableClass
{
protected AsapExlib.Base.BaseForm MainForm = null; //主窗体实例
/// <summary>
///
/// </summary>
/// <param name="m_DriverName"></param>
/// <param name="outmode">默认2 int类型,0-3 切刀,剥离,撕离,回卷。</param>
/// <param name="papermode">默认0 int类型,0-1 非连续纸,连续纸</param>
/// <param name="printmode">默认1 int类型,0-1 热敏打印,热转印打印</param>
/// <param name="position">设置停止位 默认290 ,position: int类型,0-999, 0:存在传感器和按键起作用,非0:打印机不判别是否撕掉或剥掉,直接出纸到停止位。</param>
/// <param name="printwidth"> 默认 820 便签宽度度</param>
/// <param name="Outcode"></param>
public static string OpenPrintPoint(string m_DriverName, int outmode, int papermode, int printmode, int position, int printwidth)
{
int m_Result = 0;
string m_info;
string Outcode = "";
m_Result = PrinterDll.BPLA_OpenPrinterDriver(m_DriverName);
if (m_Result == PrinterDll.BPLA_OK)
{
//"驱动打开正确";
}
else
{
Outcode = "驱动打开错误,错误值--";
Outcode += m_Result.ToString();
}
m_Result = PrinterDll.BPLA_StartDoc();
if (m_Result != PrinterDll.BPLA_OK)
{
m_info = "驱动启动打印失败,错误值 -- ";
m_info += m_Result.ToString();
Outcode = m_info;
}
m_Result = PrinterDll.BPLA_Set(outmode, papermode, printmode);
if (m_Result != PrinterDll.BPLA_OK)
{
m_info = "设置出纸位,错误值 -- ";
m_info += m_Result.ToString();
Outcode = m_info;
}
m_Result = PrinterDll.BPLA_SetEnd(position);
if (m_Result != PrinterDll.BPLA_OK)
{
m_info = "设置停止位,错误值 -- ";
m_info += m_Result.ToString();
Outcode = m_info;
}
m_Result = PrinterDll.BPLA_StartArea(0, printwidth, 0, 0, 0, 0, 0, 0);
if (m_Result != PrinterDll.BPLA_OK)
{
m_info = "设置标签宽度,错误值 -- ";
m_info += m_Result.ToString();
Outcode = m_info;
}
return Outcode;
}
public static string ClosePrintPoint(string m_DriverName, int pieces)
{
string Outcode = "";
int m_Result = 0;
string m_info;
m_Result = PrinterDll.BPLA_Print(pieces, 0, 1);
if (m_Result != PrinterDll.BPLA_OK)
{
m_info = "出纸,错误值 -- ";
m_info += m_Result.ToString();
Outcode = m_info;
}
m_Result = PrinterDll.BPLA_EndDoc();
if (m_Result != PrinterDll.BPLA_OK)
{
m_info = "驱动关闭打印失败,错误值 -- ";
m_info += m_Result.ToString();
Outcode = m_info;
}
return Outcode;
}
/// <summary>
/// 打印字体输出
/// </summary>
/// <param name="txt">文字</param>
/// <param name="startX"></param>
/// <param name="startY"></param>
/// <param name="txtType">字体</param>
/// <param name="txtSize">大小</param>
/// <param name="rowrotate">整行文字的逆时针旋转方向 1- 0度;2-90度;3-180度;4-270度</param>
public static string PrintTruetypeEx(string txt, int startX, int startY, string txtType, int txtSize, int rowrotate)
{
string Outcode = "";
int m_Result = 0;
string m_info;
m_Result = PrinterDll.BPLA_PrintTruetypeEx(txt, startX, startY, txtType, txtSize, 0, rowrotate);
if (m_Result != PrinterDll.BPLA_OK)
{
m_info = "输出命令,错误值 -- ";
m_info += m_Result.ToString();
Outcode = m_info;
}
return Outcode;
}
public static string GetControlValue(AsapExlib.Base.BaseForm bf,string ControlName)
{
string retrunStr;
ControlName=ControlName.Replace("{","");
ControlName=ControlName.Replace("}","");
retrunStr = ASAPGlobe.GetControlValue(bf, ControlName);
if (string.IsNullOrEmpty(retrunStr))
retrunStr = "";
return retrunStr;
}
public static string ReplaceControlName(AsapExlib.Base.BaseForm bf,string Txt)
{
foreach (Match m in Regex.Matches(Txt, @"/{(.+?)/}"))
{
Txt = Txt.Replace(m.Groups[0].Value, GetControlValue(bf, m.Value));
}
return Txt;
//System.Text.RegularExpressions.Regex.Replace(Txt, @"/{(.+?)/}", GetControlValue(bf,"$1"), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
}
/// <summary>
///
/// </summary>
/// <param name="xmlFile"></param>
/// <param name="pieces">打印分数</param>
/// <returns></returns>
public static string GetConfig(AsapExlib.Base.BaseForm bf,string xmlFile,int pieces)
{
try
{
int RightWidth=0;
//如果pieces是奇数 and 标签为一行两列 ,则需要补打一列
bool isNeedPrint=false;
DataSet dsConfig = new DataSet();
string Outcode = string.Empty;
dsConfig.ReadXml(xmlFile);
DataRow drOpenPrintPoint=dsConfig.Tables["OpenPrintPoint"].Rows[0];
DataRow drRightWidth = dsConfig.Tables["RightWidth"].Rows[0];
//如果RightWidth有数值大于0,则为一行两列标签
if(pieces>1)
RightWidth=Convert.ToInt32(drRightWidth["Width"]);
if(RightWidth>0 && pieces>1)
{
if(pieces % 2 == 0)
{
pieces=pieces/2;
}
else
{
pieces=pieces-1;
pieces=pieces/2;
isNeedPrint=true;
}
}
int x=0;
do
{
Outcode += OpenPrintPoint(Convert.ToString(drOpenPrintPoint["m_DriverName"]), Convert.ToInt32(drOpenPrintPoint["outmode"]), Convert.ToInt32(drOpenPrintPoint["papermode"]),
Convert.ToInt32(drOpenPrintPoint["printmode"]), Convert.ToInt32(drOpenPrintPoint["position"]), Convert.ToInt32(drOpenPrintPoint["printwidth"]));
if (!string.IsNullOrEmpty(Outcode))
{
Outcode += "/r/n请确保 打印机驱动程序已安装,并把打印机名字填在xml中";
return Outcode;
}
for (int i = 0; i < dsConfig.Tables["PrintTruetypeEx"].Rows.Count; i++)
{
Outcode += PrintTruetypeEx(ReplaceControlName(bf,Convert.ToString(dsConfig.Tables["PrintTruetypeEx"].Rows[i]["txt"])), Convert.ToInt32(dsConfig.Tables["PrintTruetypeEx"].Rows[i]["startX"]),
Convert.ToInt32(dsConfig.Tables["PrintTruetypeEx"].Rows[i]["startY"]), Convert.ToString(dsConfig.Tables["PrintTruetypeEx"].Rows[i]["txtType"]),
Convert.ToInt32(dsConfig.Tables["PrintTruetypeEx"].Rows[i]["txtSize"]), Convert.ToInt32(dsConfig.Tables["PrintTruetypeEx"].Rows[i]["rowrotate"]));
//但打印数量大于1 需要 输出第二列标签
if (RightWidth > 0)
{
Outcode += PrintTruetypeEx(Convert.ToString(dsConfig.Tables["PrintTruetypeEx"].Rows[i]["txt"]), Convert.ToInt32(dsConfig.Tables["PrintTruetypeEx"].Rows[i]["startX"]) + RightWidth,
Convert.ToInt32(dsConfig.Tables["PrintTruetypeEx"].Rows[i]["startY"]), Convert.ToString(dsConfig.Tables["PrintTruetypeEx"].Rows[i]["txtType"]),
Convert.ToInt32(dsConfig.Tables["PrintTruetypeEx"].Rows[i]["txtSize"]), Convert.ToInt32(dsConfig.Tables["PrintTruetypeEx"].Rows[i]["rowrotate"]));
}
}
if (!string.IsNullOrEmpty(Outcode))
{
return Outcode;
}
Outcode += ClosePrintPoint(Convert.ToString(drOpenPrintPoint["m_DriverName"]), pieces);
RightWidth = 0;
pieces = 1;
if(isNeedPrint)
{
x++;
}
else
{
x=2;
}
} while (x < 2);
if (string.IsNullOrEmpty(Outcode))
{
Outcode = "打印成功!!";
}
return Outcode;
}
catch(Exception ex)
{
string Outcode;
Outcode=ex+"/r/n xml格式不正确或 dll 加载失败";
return Outcode;
}
}
}
}