2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > c#调用codesoft实现打印

c#调用codesoft实现打印

时间:2022-03-25 05:36:51

相关推荐

c#调用codesoft实现打印

相对于图形打印,codesoft算是一种简单易行的方法。

1,codesoft7下载安装

下载地址: /download/sknwsknw/10713559

下载地址二: /download/qq_37326058/11265990

下载,安装,替换文件,破解成功。 注意这时候 在 c:/Program Files / 会生成两个文件夹,codesoft7 和 Tki 文件夹。

2,条码设计保存

codesoft图文教程: /view/7825d13104a1b0717ed5dd9c.html

任意变量的设置,打印机的设置,尺寸的设置等等吧.(打印机设置很重要啊) ,制作完成,命名为barJapLab.lab,放在c#程序根目录下。

如下,点击打印机图标,点击添加,就可以添加对应打印机的驱动了。(DPI:像素点数,DPI越高,打印精度越高)

3,c#代码设计

codesoft官网c#代码示例:https://www.codesoft.hk/archives/6559

c#,vb调用codesoft :/lv_fu/article/details/51702257

程序下载地址:/download/qq_37326058/11266043

参照codesoft官网 教程,引用dll。

1,visual studio 中,添加引用>com>浏览> 在 C:\Program Files\Tki\7\Common 中搜索 Lppx2.tlb,点击添加,引用中会出现labelmanager2.dll的引用(注意了,要右键 labelmanager2.dll,嵌入互操作类型设置为false)

2, vs 中创建 BaseForm基类(作用就是让窗体控件随窗体大小等比例变化),代码如下:

using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApplication2{public partial class BaseForm : Form{public BaseForm(){InitializeComponent();}private float X;//当前窗体的宽度private float Y;//当前窗体的高度/// <summary>/// 将控件的宽,高,左边距,顶边距和字体大小暂存到tag属性中/// </summary>/// <param name="cons">递归控件中的控件</param>private void setTag(Control cons){foreach (Control con in cons.Controls){con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;if (con.Controls.Count > 0)setTag(con);}}//根据窗体大小调整控件大小private void setControls(float newx, float newy, Control cons){//遍历窗体中的控件,重新设置控件的值foreach (Control con in cons.Controls){string[] mytag = con.Tag.ToString().Split(new char[] { ':' });//获取控件的Tag属性值,并分割后存储字符串数组float a = System.Convert.ToSingle(mytag[0]) * newx;//根据窗体缩放比例确定控件的值,宽度con.Width = (int)a;//宽度a = System.Convert.ToSingle(mytag[1]) * newy;//高度con.Height = (int)(a);a = System.Convert.ToSingle(mytag[2]) * newx;//左边距离con.Left = (int)(a);a = System.Convert.ToSingle(mytag[3]) * newy;//上边缘距离con.Top = (int)(a);Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);if (con.Controls.Count > 0){setControls(newx, newy, con);}}}//private void BaseForm_Load(object sender, EventArgs e)//{// X = this.Width;//获取窗体的宽度// Y = this.Height;//获取窗体的高度// setTag(this);//调用方法//}private void BaseForm_Resize(object sender, EventArgs e){float newx = (this.Width) / X; //窗体宽度缩放比例float newy = (this.Height) / Y;//窗体高度缩放比例setControls(newx, newy, this);//随窗体改变控件大小}protected override void OnLoad(EventArgs e){base.OnLoad(e);this.Resize += new EventHandler(BaseForm_Resize);X = this.Width;Y = this.Height;setTag(this);}}}

3,创建打印条码的窗体Form2, progressBar控件命名为 progressBar1,richtextbox控件命名为 lbStatus,button控件命名为 buttonX1,dataGridView控件命名为 dataGridView1。

using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace WindowsFormsApplication2{public partial class Form2 : BaseForm {public int ilableLx = 0 ;//打印模板类型,目前这里仅一个模板//public DataGridView dv;//定义公共变量,调用这个窗口时把datagridview传进来,public Form2(){InitializeComponent();InitializeDataGridView();}//barcode_noprivate System.Windows.Forms.DataGridViewTextBoxColumn barcode_no;//style_noprivate System.Windows.Forms.DataGridViewTextBoxColumn style_no;//upp_jpprivate System.Windows.Forms.DataGridViewTextBoxColumn upp_jp;//outsole_jpprivate System.Windows.Forms.DataGridViewTextBoxColumn outsole_jp;//col_jpprivate System.Windows.Forms.DataGridViewTextBoxColumn col_jp;//size_widthprivate System.Windows.Forms.DataGridViewTextBoxColumn size_width;//priceprivate System.Windows.Forms.DataGridViewTextBoxColumn price;//comp_jpprivate System.Windows.Forms.DataGridViewTextBoxColumn comp_jp;//address_jpprivate System.Windows.Forms.DataGridViewTextBoxColumn address_jp;//urlprivate System.Windows.Forms.DataGridViewTextBoxColumn url;//qtyprivate System.Windows.Forms.DataGridViewTextBoxColumn qty;public void InitializeDataGridView(){//barcode_nothis.barcode_no = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.barcode_no});this.barcode_no.HeaderText = "barcode_no";this.barcode_no.Name = "barcode_no";//style_nothis.style_no = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.style_no});this.style_no.HeaderText = "style_no";this.style_no.Name = "style_no";//upp_jpthis.upp_jp = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.upp_jp});this.upp_jp.HeaderText = "upp_jp";this.upp_jp.Name = "upp_jp";//outsole_jpthis.outsole_jp = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.outsole_jp});this.outsole_jp.HeaderText = "outsole_jp";this.outsole_jp.Name = "outsole_jp";//col_jpthis.col_jp = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.col_jp});this.col_jp.HeaderText = "col_jp";this.col_jp.Name = "col_jp";//size_widththis.size_width = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.size_width});this.size_width.HeaderText = "size_width";this.size_width.Name = "size_width";//pricethis.price = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.price});this.price.HeaderText = "price";this.price.Name = "price";//p_jp = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {p_jp});p_jp.HeaderText = "comp_jp";p_jp.Name = "comp_jp";//address_jpthis.address_jp = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.address_jp});this.address_jp.HeaderText = "address_jp";this.address_jp.Name = "address_jp";//urlthis.url = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.url});this.url.HeaderText = "url";this.url.Name = "url";//qtythis.qty = new System.Windows.Forms.DataGridViewTextBoxColumn();this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.qty});this.qty.HeaderText = "qty";this.qty.Name = "qty";dataGridView1.Columns[0].HeaderText = "barcode_no";dataGridView1.Columns[1].HeaderText = "style_no";dataGridView1.Columns[2].HeaderText = "upp_jp";dataGridView1.Columns[3].HeaderText = "outsole_jp";dataGridView1.Columns[4].HeaderText = "col_jp";dataGridView1.Columns[5].HeaderText = "size_width";dataGridView1.Columns[6].HeaderText = "price";dataGridView1.Columns[7].HeaderText = "comp_jp";dataGridView1.Columns[8].HeaderText = "address_jp";dataGridView1.Columns[9].HeaderText = "url";dataGridView1.Columns[10].HeaderText = "qty";this.dataGridView1.Rows.Add();this.dataGridView1.Rows[0].Cells[0].Value = "1";this.dataGridView1.Rows[0].Cells[1].Value = "Baqar";this.dataGridView1.Rows[0].Cells[2].Value = "1";this.dataGridView1.Rows[0].Cells[3].Value = "Baqar";this.dataGridView1.Rows[0].Cells[4].Value = "1";this.dataGridView1.Rows[0].Cells[5].Value = "Baqar";this.dataGridView1.Rows[0].Cells[6].Value = "1";this.dataGridView1.Rows[0].Cells[7].Value = "Baqar";this.dataGridView1.Rows[0].Cells[8].Value = "1";this.dataGridView1.Rows[0].Cells[9].Value = "Baqar";this.dataGridView1.Rows[0].Cells[10].Value = "1";this.dataGridView1.Rows.Add();this.dataGridView1.Rows[1].Cells[0].Value = "2";this.dataGridView1.Rows[1].Cells[1].Value = "Baqar";this.dataGridView1.Rows[1].Cells[2].Value = "1";this.dataGridView1.Rows[1].Cells[3].Value = "Baqar";this.dataGridView1.Rows[1].Cells[4].Value = "1";this.dataGridView1.Rows[1].Cells[5].Value = "Baqar";this.dataGridView1.Rows[1].Cells[6].Value = "1";this.dataGridView1.Rows[1].Cells[7].Value = "Baqar";this.dataGridView1.Rows[1].Cells[8].Value = "1";this.dataGridView1.Rows[1].Cells[9].Value = "Baqar";this.dataGridView1.Rows[1].Cells[10].Value = "1";this.dataGridView1.Rows.Add();this.dataGridView1.Rows[2].Cells[0].Value = "3";this.dataGridView1.Rows[2].Cells[1].Value = "Baqar";this.dataGridView1.Rows[2].Cells[2].Value = "1";this.dataGridView1.Rows[2].Cells[3].Value = "Baqar";this.dataGridView1.Rows[2].Cells[4].Value = "1";this.dataGridView1.Rows[2].Cells[5].Value = "Baqar";this.dataGridView1.Rows[2].Cells[6].Value = "1";this.dataGridView1.Rows[2].Cells[7].Value = "Baqar";this.dataGridView1.Rows[2].Cells[8].Value = "1";this.dataGridView1.Rows[2].Cells[9].Value = "Baqar";this.dataGridView1.Rows[2].Cells[10].Value = "1";}protected override bool ProcessCmdKey(ref Message msg, Keys keyData){if (keyData == Keys.Escape){Close();}else if (keyData == Keys.Enter){SendKeys.Send("{tab}");return true;}return base.ProcessCmdKey(ref msg, keyData);}private void btnSet_Click(object sender, EventArgs e){}private void btnPrint_Click(object sender, EventArgs e){if (ilableLx == 0)//模板一{LabelManager2.ApplicationClass labApp = null;LabelManager2.Document doc = null;string labFileName = System.Windows.Forms.Application.StartupPath + @"\barJapLab.Lab";//try//{if (!File.Exists(labFileName)){MessageBox.Show("沒有找到標簽模板文件:barJapLab.Lab,請聯系系統管理員", "溫馨提示");return;}labApp = new LabelManager2.ApplicationClass();labApp.Documents.Open(labFileName, false);// 调用设计好的label文件doc = labApp.ActiveDocument;if (dataGridView1.SelectedRows.Count > 0){progressBar1.Maximum = dataGridView1.SelectedRows.Count;progressBar1.Value = 0;for (int i = 0; i < dataGridView1.SelectedRows.Count; i++) //选中多行打印{//下面这些是给模板的变量传值进去doc.Variables.FormVariables.Item("barcode_no").Value = dataGridView1.SelectedRows[i].Cells["barcode_no"].Value.ToString();doc.Variables.FormVariables.Item("style_no").Value = dataGridView1.SelectedRows[i].Cells["style_no"].Value.ToString();doc.Variables.FormVariables.Item("upp_jp").Value = dataGridView1.SelectedRows[i].Cells["upp_jp"].Value.ToString();doc.Variables.FormVariables.Item("outsole_jp").Value = dataGridView1.SelectedRows[i].Cells["outsole_jp"].Value.ToString();doc.Variables.FormVariables.Item("col_jp").Value = dataGridView1.SelectedRows[i].Cells["col_jp"].Value.ToString();doc.Variables.FormVariables.Item("size_width").Value = dataGridView1.SelectedRows[i].Cells["size_width"].Value.ToString();doc.Variables.FormVariables.Item("price").Value = dataGridView1.SelectedRows[i].Cells["price"].Value.ToString();doc.Variables.FormVariables.Item("comp_jp").Value = dataGridView1.SelectedRows[i].Cells["comp_jp"].Value.ToString();doc.Variables.FormVariables.Item("address_jp").Value = dataGridView1.SelectedRows[i].Cells["address_jp"].Value.ToString();doc.Variables.FormVariables.Item("url").Value = dataGridView1.SelectedRows[i].Cells["url"].Value.ToString();//下面这行是打印份数的定义doc.PrintDocument(Convert.ToInt16(dataGridView1.SelectedRows[i].Cells["qty"].Value)); //+ Convert.ToInt16(dataGridView1.SelectedRows[i].Cells["att_qty"].Value));progressBar1.Value++;progressBar1.Refresh();lbStatus.Text = "總共" + dataGridView1.SelectedRows.Count.ToString() + "行需要列印,已送出" + Convert.ToString(i + 1) + "行";lbStatus.Refresh();this.Refresh();}}else //光标所在行打印{//doc.Variables.FormVariables.Item("barcode_no").Value = dv.CurrentRow.Cells["barcode_no"].Value.ToString();//doc.Variables.FormVariables.Item("style_no").Value = dv.CurrentRow.Cells["style_no"].Value.ToString();//doc.Variables.FormVariables.Item("upp_jp").Value = dv.CurrentRow.Cells["upp_jp"].Value.ToString();//doc.Variables.FormVariables.Item("outsole_jp").Value = dv.CurrentRow.Cells["outsole_jp"].Value.ToString();//doc.Variables.FormVariables.Item("col_jp").Value = dv.CurrentRow.Cells["col_jp"].Value.ToString();//doc.Variables.FormVariables.Item("size_width").Value = dv.CurrentRow.Cells["size_width"].Value.ToString();//doc.Variables.FormVariables.Item("price").Value = dv.CurrentRow.Cells["price"].Value.ToString();//doc.Variables.FormVariables.Item("comp_jp").Value = dv.CurrentRow.Cells["comp_jp"].Value.ToString();//doc.Variables.FormVariables.Item("address_jp").Value = dv.CurrentRow.Cells["address_jp"].Value.ToString();//doc.Variables.FormVariables.Item("url").Value = dv.CurrentRow.Cells["url"].Value.ToString();//doc.PrintDocument(Convert.ToInt16(dv.CurrentRow.Cells["qty"].Value));doc.Variables.FreeVariables.Item("变量0").Value = dataGridView1.CurrentRow.Cells["barcode_no"].Value.ToString();doc.Variables.FreeVariables.Item("变量1").Value = dataGridView1.CurrentRow.Cells["style_no"].Value.ToString();doc.Variables.FreeVariables.Item("变量2").Value = dataGridView1.CurrentRow.Cells["upp_jp"].Value.ToString();doc.Variables.FreeVariables.Item("变量3").Value = dataGridView1.CurrentRow.Cells["outsole_jp"].Value.ToString();doc.Variables.FreeVariables.Item("变量4").Value = dataGridView1.CurrentRow.Cells["col_jp"].Value.ToString();doc.Variables.FreeVariables.Item("变量5").Value = dataGridView1.CurrentRow.Cells["size_width"].Value.ToString();doc.Variables.FreeVariables.Item("变量6").Value = dataGridView1.CurrentRow.Cells["price"].Value.ToString();doc.Variables.FreeVariables.Item("变量8").Value = dataGridView1.CurrentRow.Cells["comp_jp"].Value.ToString();doc.Variables.FreeVariables.Item("变量9").Value = dataGridView1.CurrentRow.Cells["address_jp"].Value.ToString();doc.Variables.FreeVariables.Item("变量10").Value = dataGridView1.CurrentRow.Cells["url"].Value.ToString();doc.PrintDocument(Convert.ToInt16(dataGridView1.CurrentRow.Cells["qty"].Value));}//}//catch (Exception ex)//{// MessageBox.Show("出錯啦,原因如下:\n\r" + ex.Message, "出錯啦");//}//finally//{// labApp.Documents.CloseAll(true);// labApp.Quit();//退出// labApp = null;// doc = null;// GC.Collect(0);//}}}private void frmJP_CODESOFT_PT_Load(object sender, EventArgs e){if (dataGridView1.SelectedRows.Count > 0)lbStatus.Text = "總共" + dataGridView1.SelectedRows.Count.ToString() + "行需要列印";elselbStatus.Text = "總共1行需要列印";}private void btnClose_Click(object sender, EventArgs e){Close();}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。