隨筆 - 6  文章 - 129  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(14)

          隨筆檔案(6)

          文章分類(467)

          文章檔案(423)

          相冊

          收藏夾(18)

          JAVA

          搜索

          •  

          積分與排名

          • 積分 - 827256
          • 排名 - 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 閱讀(2184) 評論(0)  編輯  收藏 所屬分類: C#
          主站蜘蛛池模板: 汝南县| 萍乡市| 车险| 建水县| 分宜县| 裕民县| 措勤县| 环江| 枣庄市| 福清市| 静宁县| 喀喇沁旗| 黑龙江省| 大同县| 新绛县| 鄄城县| 安仁县| 尉犁县| 扶风县| 米泉市| 成都市| 富顺县| 福建省| 涿州市| 青海省| 江门市| 颍上县| 隆林| 无为县| 呼图壁县| 监利县| 阳城县| 天全县| 华安县| 马尔康县| 孟津县| 八宿县| 太谷县| 大宁县| 寿宁县| 界首市|