Browse Source

添加设置默认打印机

AddValidOverTime
506583276@qq.com 2 years ago
parent
commit
4dc5d5ecb1
  1. 2
      BBWY.Client/GlobalContext.cs
  2. 15
      BBWY.Client/Helpers/MyPrintHelper.cs
  3. 11
      BBWY.Client/Models/PackTask/PackTaskModel.cs
  4. 53
      BBWY.Client/ViewModels/QualityTask/QualityViewModel.cs
  5. 100
      BBWY.Client/Views/PackTask/ServiceWindow.xaml.cs

2
BBWY.Client/GlobalContext.cs

@ -9,7 +9,7 @@ namespace BBWY.Client
{
ShopServiceGroupList = new List<string>();
ShopServiceGroupLowerList = new List<string>();
ClientVersion = "10122";
ClientVersion = "10123";
}
private User user;

15
BBWY.Client/Helpers/MyPrintHelper.cs

@ -9,6 +9,7 @@ using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Media.Imaging;
using WebSocketSharp;
@ -618,5 +619,19 @@ namespace BBWY.Client.Helpers
}
}
public static void SetDefaultPrint(string printName)
{
SetDefaultPrinter(printName);
}
/// <summary>
/// 调用win api将指定名称的打印机设置为默认打印机
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(string Name);
}
}

11
BBWY.Client/Models/PackTask/PackTaskModel.cs

@ -103,7 +103,16 @@ namespace BBWY.Client.Models
// qualityViewModel.TaskId = TaskId;
qualityViewModel.OrderId = OrderId;
qualityViewModel.SkuId = SkuId;
qualityViewModel.SearchSku(this);
try
{
qualityViewModel.SearchSku(this);
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
//qualityViewModel.LoadPackDatas();
//qualityViewModel.SetAllFees = new Action<FeesItemResponse, List<string>>((feesItem, packUsers) =>
//{

53
BBWY.Client/ViewModels/QualityTask/QualityViewModel.cs

@ -552,28 +552,43 @@ namespace BBWY.Client.ViewModels
request.PreCompeteTime = competeTime;
var competeRes = packPurchaseTaskService.CompeteQualityTask(request);
if (competeRes == null)
{
MessageBox.Show("网络异常");
return;
}
if (!competeRes.Success)
{
MessageBox.Show(competeRes.Msg);
return;
}
BatchPrintWindow batchPrint = new BatchPrintWindow();
try
{
var competeRes = packPurchaseTaskService.CompeteQualityTask(request);
if (competeRes == null)
{
MessageBox.Show("网络异常");
return;
}
if (!competeRes.Success)
{
MessageBox.Show(competeRes.Msg);
return;
}
BatchPrintWindow batchPrint = new BatchPrintWindow();
batchPrint.SetData(GoodProductQuantity,
PurchaseSkuList.Where(p => p.IsNeedCer && p.CerDTO.Id > 0).Select(p => p.CerDTO).ToArray()
, BarCodeModel);
batchPrint.ShowDialog();
if (ReflashWindow != null) ReflashWindow();
var window = obj as BWindow;
App.Current.Dispatcher.Invoke(new Action(() => {
batchPrint.SetData(GoodProductQuantity,
PurchaseSkuList.Where(p => p.IsNeedCer && p.CerDTO.Id > 0).Select(p => p.CerDTO).ToArray()
, BarCodeModel);
batchPrint.ShowDialog();
window.Close();
}));
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
if (ReflashWindow != null) ReflashWindow();
var window = obj as BWindow;
window.Close();
}

100
BBWY.Client/Views/PackTask/ServiceWindow.xaml.cs

@ -1,5 +1,6 @@
using BarcodeLib;
using BBWY.Client.APIServices;
using BBWY.Client.Helpers;
using BBWY.Client.ViewModels;
using BBWY.Client.ViewModels.PackTask;
using BBWY.Controls;
@ -50,9 +51,8 @@ namespace BBWY.Client.Views.PackTask
MessageBox.Show("打印机处于错误状态");
return;
}
//Print(this.printArea, cbPrintName.Text, "打印任务",1);
MyPrintHelper.SetDefaultPrint(printName);//设置默认打印机
this.printArea.Arrange(new Rect(new Point(0, 0), new Size(printArea.ActualWidth, printArea.ActualHeight)));
PrintDialog printDialog = new PrintDialog();
@ -76,99 +76,7 @@ namespace BBWY.Client.Views.PackTask
/// <summary>
/// 打印
/// </summary>
/// <param name="document">流文档</param>
/// <param name="printer">打印机名称</param>
/// <param name="description">打印描述</param>
/// <param name="copyCount">打印个数</param>
public static void Print(Visual document, string printer, string description, int copyCount)
{
var localPrintServer = new LocalPrintServer();
var printQueue = localPrintServer.GetPrintQueue(printer);
if (printQueue.IsInError)
{
throw new Exception("打印机处于错误状态");
}
var printDialog = new PrintDialog
{
PrintQueue = printQueue, //打印队列
PrintTicket = { CopyCount = copyCount } //打印个数
};
//设置纸张大小
var pageWidth = (int)Math.Ceiling(printDialog.PrintableAreaWidth); //小标签:114
var pageHeight = (int)Math.Ceiling(printDialog.PrintableAreaHeight); //小标签:227
printDialog.PrintTicket.PageMediaSize = new PageMediaSize(pageWidth, pageHeight);
//设置纸张边距
var paperSize = GetPaperSize(printer); //小标签:118*246
//var offsetX = (int)Math.Ceiling((paperSize.Width - pageWidth) / 2f);
//var offsetY = (int)Math.Ceiling((paperSize.Height - pageHeight) / 2f);
//document.PagePadding = new Thickness(offsetX, offsetY, offsetX, offsetY);
//打印
var paginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
printDialog.PrintDocument(paginator, description);
var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string printNames = System.IO.Path.Combine(applicationPath, "printName.init");
File.WriteAllText(printNames, printer);
}
private static object GetPaperSize(string printer)
{
return null;
}
public interface IDocumentRenderer
{
void Render(FlowDocument doc, object data);
}
public class CommonDocumentRenderer : IDocumentRenderer
{
public void Render(FlowDocument doc, object data)
{
var model = data as PrintModel;
if (model == null)
{
throw new ArgumentException("data is not PrintModel");
}
var type = typeof(PrintModel);
var properties = type.GetProperties();
foreach (var property in properties)
{
//文本赋值
if (doc.FindName(property.Name) is TextBlock textBlock)
{
textBlock.Text = property.GetValue(model)?.ToString();
}
}
}
}
public class PrintModel
{
/// <summary>
/// 批号
/// </summary>
public string BatchNumber { get; set; }
/// <summary>
/// 订单号
/// </summary>
public string OrderNumber { get; set; }
/// <summary>
/// 物料代码
/// </summary>
public string MaterialNumber { get; set; }
}
}
}

Loading…
Cancel
Save