You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
6.8 KiB
220 lines
6.8 KiB
using BBWY.Client.Helpers;
|
|
using BBWY.Client.Models;
|
|
using BBWY.Controls;
|
|
using System;
|
|
using System.Drawing;
|
|
using System.Drawing.Printing;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Collections.ObjectModel;
|
|
using BBWY.Client.Extensions;
|
|
|
|
namespace BBWY.Client.Views.PackTask
|
|
{
|
|
/// <summary>
|
|
/// PrintWindow.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class PrintWindow : BWindow
|
|
{
|
|
public PrintWindow()
|
|
{
|
|
InitializeComponent();
|
|
this.Loaded += PrintWindow_Loaded;
|
|
|
|
|
|
}
|
|
|
|
private void PrintWindow_Loaded(object sender, RoutedEventArgs e)
|
|
{
|
|
LoadPrints();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取打印机名称
|
|
/// </summary>
|
|
private void LoadPrints()
|
|
{
|
|
var printingNames = PrinterSettings.InstalledPrinters;//获取本机的打印机数据
|
|
int index = -1;
|
|
int selectIndex = 0;
|
|
foreach (string name in printingNames)
|
|
{
|
|
if (name == "Microsoft XPS Document Writer" || name == "Microsoft Print to PDF" || name == "Fax")
|
|
{
|
|
continue;
|
|
}
|
|
index++;
|
|
if (name.Contains("Deli"))
|
|
{
|
|
selectIndex = index;
|
|
}
|
|
cbPrints.Items.Add(name);
|
|
}
|
|
if (cbPrints.Items.Count > selectIndex)
|
|
{
|
|
cbPrints.SelectedIndex = selectIndex;
|
|
}
|
|
}
|
|
|
|
|
|
public ObservableCollection<GoodsNumberCer> GoodsNumberCerList { get; set; }
|
|
/// <summary>
|
|
/// 加载预览打印数据
|
|
/// </summary>
|
|
public void LoadData()
|
|
{
|
|
// PrintData data = new PrintData();
|
|
if (CertificateModel != null)
|
|
{
|
|
GoodsNumberCerList = new ObservableCollection<GoodsNumberCer>();
|
|
foreach (CertificateModel certificateModel in CertificateModel)
|
|
GoodsNumberCerList.Add(new GoodsNumberCer
|
|
{
|
|
CertificateModel = certificateModel.Copy(),
|
|
});
|
|
bar.Visibility = Visibility.Collapsed;
|
|
cer.Visibility = Visibility.Visible;
|
|
}
|
|
if (BarCodeModel != null)
|
|
{
|
|
cer.Visibility = Visibility.Collapsed;
|
|
bar.Visibility = Visibility.Visible;
|
|
}
|
|
this.DataContext = this;
|
|
}
|
|
|
|
public CertificateModel[] CertificateModel { get; set; }
|
|
|
|
public BarCodeModel BarCodeModel { get; set; }
|
|
|
|
private void BButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
int printCount = 0;
|
|
try
|
|
{
|
|
printCount = Convert.ToInt32(tbCount.Text);
|
|
}
|
|
catch
|
|
{
|
|
new TipsWindow("请输入打印份数", 1).ShowDialog();
|
|
return;
|
|
}
|
|
|
|
string printName = cbPrints.Text;//选择的要打印的打印机名称
|
|
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
if (BarCodeModel != null)
|
|
{
|
|
PrintData(printCount, printName,BarCodeModel);
|
|
//MyPrintHelper.PrintBarcode(ref args, BarCodeModel, font);
|
|
}
|
|
if (CertificateModel != null&& CertificateModel.Count()>0)
|
|
{
|
|
foreach (var cer in CertificateModel)
|
|
{
|
|
PrintData(printCount, printName,null,cer);
|
|
System.Threading.Thread.Sleep(100);
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
private void PrintData(int printCount, string printName, BarCodeModel barCode = null, CertificateModel certificateModel = null)
|
|
{
|
|
try
|
|
{
|
|
|
|
PrintDocument document = new PrintDocument();
|
|
document.PrinterSettings.PrinterName = printName;//使用打印机名称,指定特定的打印机进行打印。
|
|
//设置打印页面
|
|
//document.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custom", 236, 157);
|
|
document.PrintPage += (s, a) =>
|
|
{
|
|
Font font = new Font("宋体", 6, System.Drawing.FontStyle.Regular);
|
|
if (barCode != null)
|
|
{
|
|
MyPrintHelper.PrintBarcode(ref a, barCode, font);
|
|
}
|
|
if (certificateModel != null)
|
|
{
|
|
MyPrintHelper.PrintCertificate(ref a, certificateModel, font);
|
|
}
|
|
|
|
};
|
|
document.PrinterSettings.Copies = (short)printCount;//打印份数
|
|
document.Print();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
new TipsWindow($"打印失败,{ex.Message}").Show();
|
|
});
|
|
|
|
}
|
|
}
|
|
|
|
private void Document_PrintPage(object sender, PrintPageEventArgs args)
|
|
{
|
|
//RectangleF bounds = new RectangleF(0, 0, 236, 157);
|
|
//args.Graphics.SetClip(bounds);//设置打印区域
|
|
|
|
|
|
|
|
|
|
//设置 打印纸张类型
|
|
//var paperSizes = args.PageSettings.PrinterSettings.PaperSizes;
|
|
|
|
|
|
//foreach (System.Drawing.Printing.PaperSize item in paperSizes)
|
|
//{
|
|
// if (item.Height==157)
|
|
// {
|
|
// args.PageSettings.PaperSize=item;
|
|
// break;
|
|
// }
|
|
//}
|
|
//var sss = args.PageSettings.PaperSize;
|
|
//默认设置 打印类型
|
|
|
|
|
|
|
|
#if DEBUG
|
|
//打印 纵向打印 会模糊
|
|
//args.PageSettings.PrinterSettings.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custom", 236, 157);
|
|
//args.PageSettings.PrinterSettings.DefaultPageSettings.Landscape = true;
|
|
|
|
//Bitmap bitmap = new Bitmap(236,157);
|
|
#endif
|
|
|
|
Font font = new Font("宋体", 6, System.Drawing.FontStyle.Regular);
|
|
if (BarCodeModel != null)
|
|
{
|
|
MyPrintHelper.PrintBarcode(ref args, BarCodeModel, font);
|
|
}
|
|
if (CertificateModel != null)
|
|
{
|
|
foreach (var cer in CertificateModel)
|
|
{
|
|
MyPrintHelper.PrintCertificate(ref args, cer, font);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public class PrintData
|
|
{
|
|
public CertificateModel CertificateModel { get; set; }
|
|
|
|
public BarCodeModel BarCodeModel { get; set; }
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|