随笔 - 6  文章 - 129  trackbacks - 0
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(14)

随笔档案(6)

文章分类(467)

文章档案(423)

相册

收藏夹(18)

JAVA

搜索

  •  

积分与排名

  • 积分 - 821414
  • 排名 - 49

最新评论

阅读排行榜

评论排行榜

原文:
http://blog.csdn.net/JustLovePro/article/details/2246339
两种打印方式:机器码打印、调用CodeSoft.

调用CodeSoft打印:
 利用第三方软件codesofe进行label设计,然后在程序中调用打印。

 这种方式维护起来比较方便,手动调整label各参数指标即可。

 准备工作:
 1.安装打印机驱动
 2.安装codeSoft
 3.设计label,设置label参数
 
 程序实现:
  注意首先添加引用:Lppx2.tlb (codesoft安装后文件中)

using LabelManager2;         //引入命名空间
private void btnPrint_Click(object sender, EventArgs e)
        
{
            ApplicationClass lbl 
= new ApplicationClass();

            
try
            
{
                lbl.Documents.Open(
@"D:label.Lab"false);// 调用设计好的label文件
                Document doc = lbl.ActiveDocument;
                doc.Variables.FormVariables.Item(
"Var0").Value = txtContent.Text.Trim(); //给参数传值
                doc.Variables.FormVariables.Item("Var1").Value = txtContent2.Text.Trim(); //给参数传值

                
int Num = Convert.ToInt32(txtQuentity.Text);        //打印数量
                doc.PrintDocument(Num);                             //打印
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }

            
finally
            
{
                lbl.Quit();                                         
//退出
            }

        }

机器码打印:
 这种方式直接使用打印机机器指令进行打印,调用系统接口函数实现。
 1.建立接口函数类 LPTControl
 

using   System;   
using   System.Collections.Generic;   
using   System.Text;   
using   System.Runtime.InteropServices;

namespace SMTOffLine
{
    
class LPTControl
    
{
        [StructLayout(LayoutKind.Sequential)]
        
private struct OVERLAPPED
        
{
            
int Internal;
            
int InternalHigh;
            
int Offset;
            
int OffSetHigh;
            
int hEvent;
        }


        [DllImport(
"kernel32.dll")]
        
private static extern int CreateFile(
        
string lpFileName,
        
uint dwDesiredAccess,
        
int dwShareMode,
        
int lpSecurityAttributes,
        
int dwCreationDisposition,
        
int dwFlagsAndAttributes,
        
int hTemplateFile
        );


        [DllImport(
"kernel32.dll")]
        
private static extern bool WriteFile(
        
int hFile,
        
byte[] lpBuffer,
        
int nNumberOfBytesToWrite,
        
out   int lpNumberOfBytesWritten,
        
out   OVERLAPPED lpOverlapped
        );


        [DllImport(
"kernel32.dll")]
        
private static extern bool CloseHandle(
        
int hObject
        );

        
private int iHandle;
        
public bool Open()
        
{
            iHandle 
= CreateFile("lpt1"0x4000000000300);
            
if (iHandle != -1)
            
{
                
return true;
            }

            
else
            
{
                
return false;
            }

        }


        
public bool Write(String Mystring)
        
{
            
if (iHandle != -1)
            
{
                
int i;
                OVERLAPPED x;
                
byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
                
return WriteFile(iHandle, mybyte, mybyte.Length, out  i, out  x);
            }

            
else
            
{
                
throw new Exception("打印機端口未打開!");
            }

        }


        
public bool Close()
        
{
            
return CloseHandle(iHandle);
        }


    }

}

2.程序实现
 

 private void btnPrint_Click(object sender, EventArgs e)
        
{
            
string PtrStr = "";
            
string MaterialNo="";
            
string StartValue="";
            
int PrintNum ;

            LPTControl Print 
= new LPTControl();
            
try
            
{
                Print.Open();
                
for (int i = 0; i < dtPrint.Rows.Count; i++)
                
{
                     MaterialNo 
= dtPrint.Rows[i][0].ToString();
                     StartValue 
= dtPrint.Rows[i][1].ToString();

                    PrintNum 
= Convert.ToInt32(numericUpDown1.Value);

                    
for (int j = 0; j < PrintNum; j++)
                    
{
                        
int FlowNo =(Convert.ToInt32(StartValue) + j+1);
                        
string StrFlowNo = FlowNo.ToString("000000");
                        PtrStr 
= "";
                        PtrStr 
+= "^XA^LH40,20";

                        PtrStr 
+= "^FO10,10^BY3,2,90^B3,N,90,N,N^FD" + MaterialNo + "^FS";
                        PtrStr 
+= "^FO10,105^AE^FD" + MaterialNo + "^FS";

                        PtrStr 
+= "^FO10,145^BY3,2,90^B3,N,90,N,N^FD" + StrFlowNo + "^FS";
                        PtrStr 
+= "^FO10,245^AE^FD" + StrFlowNo + "^FS";

                        PtrStr 
+= "^FO10,280^BY3,2,90^B3,N,90,N,N^FD" + DateTime.Now.ToString("yyyy-MM-dd"+ "^FS";
                        PtrStr 
+= "^FO10,385^AE^FD" + DateTime.Now.ToString("yyyy-MM-dd"+ "^FS";

                        PtrStr 
+= "^XZ";

                        Print.Write(PtrStr);

                    }


                    
//update newest Num to DB  
                    int NewNum = Convert.ToInt32(StartValue) +PrintNum;
                    
string SqlUpdate = "update tablelabel set curren_value=" + NewNum + " where material_id='" + MaterialNo + "'";
                    
try
                    
{
                        DAL DbOp 
= new DAL();
                        DbOp.ExecDBOp(SqlUpdate);
                    }

                    
catch (Exception ex)
                    
{
                        MessageBox.Show(
"Error","information");
                    }
                 
                }
      
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"Information");
            }

            
finally
            
{
                Print.Close();
            }



        }



posted on 2012-09-13 11:26 Ke 阅读(2157) 评论(0)  编辑  收藏 所属分类: C#

只有注册用户登录后才能发表评论。


网站导航: