C# TSC打印二维码和条形码的实现方法
程序员文章站
2023-12-20 09:39:34
效果图
开发、使用环境说明
安装tsc_7.3.8_m-3.exe打印机驱动,安装时选择对应的ttp 244 pro
将tsclib.dll复制到c:\win...
效果图
开发、使用环境说明
安装tsc_7.3.8_m-3.exe打印机驱动,安装时选择对应的ttp 244 pro
将tsclib.dll复制到c:\windows\system
驱动安装说明
选择下一步
选择安装路径,默认即可,选择下一步
选择安装打印机,选择下一步
选择其他,点击下一步
选择对应的打印机型号,点击下一步
选择usb端口,点击下一步
直接默认即可,点击下一步
驱动安装完成!
tsclib.cs代码:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.runtime.interopservices; namespace windowsformsprint { public class tsclib_dll { [dllimport("tsclib.dll", entrypoint = "about")] public static extern int about(); [dllimport("tsclib.dll", entrypoint = "openport")] public static extern int openport(string printername); [dllimport("tsclib.dll", entrypoint = "barcode")] public static extern int barcode(string x, string y, string type, string height, string readable, string rotation, string narrow, string wide, string code); [dllimport("tsclib.dll", entrypoint = "clearbuffer")] public static extern int clearbuffer(); [dllimport("tsclib.dll", entrypoint = "closeport")] public static extern int closeport(); [dllimport("tsclib.dll", entrypoint = "downloadpcx")] public static extern int downloadpcx(string filename, string image_name); [dllimport("tsclib.dll", entrypoint = "formfeed")] public static extern int formfeed(); [dllimport("tsclib.dll", entrypoint = "nobackfeed")] public static extern int nobackfeed(); [dllimport("tsclib.dll", entrypoint = "printerfont")] public static extern int printerfont(string x, string y, string fonttype, string rotation, string xmul, string ymul, string text); [dllimport("tsclib.dll", entrypoint = "printlabel")] public static extern int printlabel(string set, string copy); [dllimport("tsclib.dll", entrypoint = "sendcommand")] public static extern int sendcommand(string printercommand); [dllimport("tsclib.dll", entrypoint = "setup")] public static extern int setup(string width, string height, string speed, string density, string sensor, string vertical, string offset); [dllimport("tsclib.dll", entrypoint = "windowsfont")] public static extern int windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, string szfacename, string content); //打开打印机端口,并进行相关设置 public static void openportext() { tsclib_dll.openport("tsc ttp-244 pro");//找打打印机端口 tsclib_dll.sendcommand("size 60 mm,30 mm");//设置条码大小 tsclib_dll.sendcommand("gap 2 mm,0");//设置条码间隙 tsclib_dll.sendcommand("speed 4");//设置打印速度 tsclib_dll.sendcommand("density 7");//设置墨汁浓度 tsclib_dll.sendcommand("derection 1");//设置相对起点 tsclib_dll.sendcommand("reference 3 mm,3 mm");//设置偏移边框 tsclib_dll.sendcommand("cls");//清除记忆(每次打印新的条码时先清除上一次的打印记忆) } //打印在用车档案二维码 public static void printvehiclecode(string str_hetong, string str_zhengjian, string str_name) { char space = (char)(32); string codevalue = str_hetong + space + str_zhengjian; tsclib_dll.sendcommand("cls");//需要清除上一次的打印记忆 tsclib_dll.sendcommand("qrcode 260,20,h,7,a,0,m2,s7,\"" + codevalue + "\""); tsclib_dll.windowsfont(240, 100, 24, 180, 0, 0, "黑体", str_hetong); tsclib_dll.windowsfont(240, 140, 24, 180, 0, 0, "黑体", str_zhengjian); tsclib_dll.windowsfont(240, 180, 24, 180, 0, 0, "黑体", str_name); tsclib_dll.printlabel("1", "1"); } //打印财务条形码 public static void printfinancecode(string str_date, string str_siteno, string str_sitename, string str_num, string str_name, int count) { for (int i = 0; i < count; i++) { char num = (char)(i + 65); char space = (char)(32); string value = str_date + space + str_siteno + space + str_num + space + num; string txt = str_date + space + str_sitename + space + str_num + space + str_name + space + num; tsclib_dll.sendcommand("cls");//需要清除上一次的打印记忆 tsclib_dll.barcode("40", "50", "128", "160", "0", "0", "2", "2", value); tsclib_dll.windowsfont(440, 35, 24, 180, 0, 0, "黑体", txt); tsclib_dll.printlabel("1", "1"); } } //关闭打印机端口 public static void closeportext() { tsclib_dll.closeport(); } } }
打印二维码:
private void print_click(object sender, eventargs e) { try { print.enabled = false; base.dowork(doprint); print.enabled = true; } catch (exception ex) { // fileloghelper.writeinfolog(ex.tostring()); messagebox.show(ex.tostring()); } } private void doprint(object sender, eventargs e) { datagridviewselectedrowcollection rows = archivesview.selectedrows; if (rows.count <= 0) { messagebox.show("请先选择数据项!"); return; } tsclib_dll.openportext(); foreach (datagridviewrow dr in rows) { tsclib_dll.printvehiclecode(dr.cells[1].value.tostring(), dr.cells[2].value.tostring(), dr.cells[3].value.tostring()); } tsclib_dll.closeportext(); }
程序在32位操作系统上运行正常,在64位操作系统上运行读卡功能提示”试图加载格式不正确“。
程序在64位操作系统上运行正常,在64位操作系统上运行读卡功能提示”试图加载格式不正确“。
--------------------------------------------------------------------------------------------
点击项目属性,把目标平台any cpu 设置为x86(32位操作系统)或者x64(64位操作系统)
按照上面解决方案可能会有下面的问题:
c# form继承问题 : 提示 无法加载基类 ;请确保已引用该程序集并已生成所有项目。
把 生成 - 目标平台 改成 x86或者any cpu ,重新生成一次,form可正常编辑。
所以新的解决方案是:选择any cpu 把下面打勾的去掉
以上这篇c# tsc打印二维码和条形码的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。