admin管理员组文章数量:1122852
C#,VB.Net,VB6调用Codesoft打印标签
做工控软件不免要打印标签,打印方式很多:打印图片方式,命令方式……
最简单的要数Codesoft了
Codesoft可以编辑要要打印的标签,需要改变的文字或条码,二维码置为变量,打印时将参数传入即可
首先需要安装Codesoft,并编辑好标签备用
然后下载需要使用的库文件里面有dll文件和tlb文件,本文只介绍dll文件用法
.rar
新建工程,简单界面如下
C#代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using LabelManager2;
using System.Threading;namespace PrintTestByCodesoft
{public partial class Form1 : Form{public Form1(){InitializeComponent();}LabelManager2.ApplicationClass lbl = new LabelManager2.ApplicationClass();private void btPrint_Click(object sender, EventArgs e){Document doc = lbl.ActiveDocument;doc.Printer.SwitchTo("ZDesigner ZT410-300dpi ZPL", "USB001", true);//选择打印机doc.Variables.FreeVariables.Item("变量0").Value = tbSn.Text;//给lab文件中设置的变量传值doc.PrintLabel(1, 1, 1, 1, 1, "");//打印doc.FormFeed();}private void Form1_Load(object sender, EventArgs e){openFile();} void openFile() { string strFile = System.Windows.Forms.Application.StartupPath.ToString() + @"\S703.lab";//lab文件路径 lbl.Documents.Open(strFile, false);//比较费时间 } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { lbl.Documents.CloseAll(false); lbl.Quit(); } catch { } } }
}
VB.Net代码如下与C#差不多
Imports LabelManager2Public Class Form1Public lbl As LabelManager2.ApplicationDim strFile As StringDim doc As DocumentDim portNames As String()Private Sub btPrint_Click(sender As Object, e As EventArgs) Handles btPrint.Clickdoc.Variables.FreeVariables.Item("变量0").Value = tbSn.Textdoc.PrintLabel(1, 1, 1, 1, 1, "")doc.FormFeed()End SubPrivate Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Loadlbl = New LabelManager2.Application()strFile = System.Windows.Forms.Application.StartupPath.ToString() & "\S703.lab"lbl.Documents.Open(strFile, False)doc = lbl.ActiveDocumentdoc.Printer.SwitchTo("ZDesigner ZT410-300dpi ZPL", "USB001", True)End SubPrivate Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosingTrylbl.Documents.CloseAll(False)lbl.Quit()CatchEnd TryEnd SubEnd Class
VB不能直接调用的dll,需要自己封装一下
新建一个VB的类库,然后新建一个COM类
Imports LabelManager2
<ComClass(ComCodeSoftPrint.ClassId, ComCodeSoftPrint.InterfaceId, ComCodeSoftPrint.EventsId)> _
Public Class ComCodeSoftPrint#Region "COM GUID"' 这些 GUID 提供此类的 COM 标识 ' 及其 COM 接口。若更改它们,则现有的' 客户端将不再能访问此类。Public Const ClassId As String = "81498e68-3899-4b7b-ae9f-c9e301480d69"Public Const InterfaceId As String = "724ab222-2448-4440-a9e0-d06b5aa14f17"Public Const EventsId As String = "49c8e387-a0e9-4f25-acaf-cd2cd028b2dc"
#End Region' 可创建的 COM 类必须具有一个不带参数的 Public Sub New() ' 否则, 将不会在 ' COM 注册表中注册此类,且无法通过' CreateObject 创建此类。Public Sub New()MyBase.New()End SubPublic lbl As LabelManager2.ApplicationDim strFile As StringDim doc As DocumentPublic Sub openLabFile(filePath As String)lbl = New LabelManager2.Application()lbl.Documents.Open(filePath, False)doc = lbl.ActiveDocumentdoc.Printer.SwitchTo("ZDesigner ZT410-300dpi ZPL", "USB001", True)End SubPublic Sub closeLabFile()Trylbl.Documents.CloseAll(False)lbl.Quit()CatchEnd TryEnd SubPublic Sub printLabel(var0 As String)doc.Variables.FreeVariables.Item("变量0").Value = var0doc.PrintLabel(1, 1, 1, 1, 1, "")doc.FormFeed()End SubEnd Class
在目录里会生成tlb文件,把dll和tlb复制到VB6目录中,添加tlb的引用即可
本文标签: cVBNETVB6调用Codesoft打印标签
版权声明:本文标题:C#,VB.Net,VB6调用Codesoft打印标签 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1697351878a267367.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论