Form1.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using LabelManager2;
  2. using Seagull.BarTender.Print;
  3. using System;
  4. using System.Drawing.Printing;
  5. using System.Windows.Forms;
  6. namespace MaterialPrint
  7. {
  8. public partial class Form1 : Form
  9. {
  10. Engine engine = new Engine(true);
  11. LabelFormatDocument format = null;
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. StartPosition = FormStartPosition.CenterScreen;
  16. }
  17. private void Print_Click(object sender, EventArgs e)
  18. {
  19. try
  20. {
  21. //打印的最后一个流水
  22. int LastNum = (int.Parse(Rn.Text) + int.Parse(Num.Text));
  23. //打印的总数
  24. int PrintNum = int.Parse(Num.Text);
  25. for (int i = int.Parse(Rn.Text); i < LastNum; i = i + 8)
  26. {
  27. int temp = i;
  28. for (int j = 0; j < ((LastNum - i) / 8 > 0 ? 8 : (LastNum - i)); j++)
  29. {
  30. switch (format.SubStrings[j].Name)
  31. {
  32. case "RN1":
  33. format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp).ToString()));
  34. break;
  35. case "RN2":
  36. format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 1).ToString()));
  37. break;
  38. case "RN3":
  39. format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 2).ToString()));
  40. break;
  41. case "RN4":
  42. format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 3).ToString()));
  43. break;
  44. case "RN5":
  45. format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 4).ToString()));
  46. break;
  47. case "RN6":
  48. format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 5).ToString()));
  49. break;
  50. case "RN7":
  51. format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 6).ToString()));
  52. break;
  53. case "RN8":
  54. format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 7).ToString()));
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. format.PrintSetup.PrinterName = PrinterList.Text;
  61. format.PrintSetup.IdenticalCopiesOfLabel = 1;
  62. format.Print();
  63. //doc.Printer.SwitchTo(PrinterList.Text);
  64. //doc.PrintDocument(1);
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. MessageBox.Show(ex.Message);
  70. }
  71. }
  72. private static string lpad(int length, string number)
  73. {
  74. while (number.Length < length)
  75. {
  76. number = "0" + number;
  77. }
  78. number = number.Substring(number.Length - length, length);
  79. return number;
  80. }
  81. private void Form1_Load(object sender, EventArgs e)
  82. {
  83. Console.WriteLine("4181231".Substring(0,3));
  84. return;
  85. try
  86. {
  87. //lbl = new ApplicationClass();
  88. format = engine.Documents.Open(System.Windows.Forms.Application.StartupPath + @"\SN.btw");
  89. //doc = lbl.Documents.Open(System.Windows.Forms.Application.StartupPath + @"\SN.btw");
  90. PrintDocument print = new PrintDocument();
  91. string sDefault = print.PrinterSettings.PrinterName;//默认打印机名
  92. foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
  93. {
  94. PrinterList.Items.Add(sPrint);
  95. if (sPrint == sDefault)
  96. PrinterList.SelectedIndex = PrinterList.Items.IndexOf(sPrint);
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. MessageBox.Show(ex.Message);
  102. }
  103. }
  104. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  105. {
  106. System.Diagnostics.Process[] pro = System.Diagnostics.Process.GetProcessesByName("lppa");
  107. for (int i = 0; i < pro.Length; i++)
  108. {
  109. pro[i].Kill();
  110. }
  111. }
  112. }
  113. }