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.
 
 

141 lines
4.9 KiB

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ThoughtWorks.QRCode.Codec;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using formula_manage.UserClass;
using System.Xml.Linq;
namespace formula_manage.Windows
{
/// <summary>
/// Print.xaml 的交互逻辑
/// </summary>
public partial class Print : Window
{
public Print()
{
WindowStartupLocation = WindowStartupLocation.CenterScreen;
InitializeComponent();
cbxPrinter.ItemsSource = this.GetLocalPrinters();
}
public string INIPath = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "formula.ini";
bool Printer;
string Printer_NAME;
private void Window_Print(object sender, RoutedEventArgs e)
{
UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(INIPath);
Printer = Boolean.Parse(Configini.IniReadvalue("PRINTER", "P1"));
Printer_NAME = Configini.IniReadvalue("PRINTER", "P2"); //读配置文件
}
private void Button_Click(object sender, RoutedEventArgs e) //SQL_DISPEN_bak路径选择按钮
{
FolderBrowserDialog Dilog = new FolderBrowserDialog();
Dilog.SelectedPath = Print_path.Text; //打开目录
Dilog.ShowNewFolderButton = false; //不显示新建文件夹按钮
//
if (Dilog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Print_path.Text = Dilog.SelectedPath; //返回选择的字符串
}
}
private void Button_Click_1(object sender, RoutedEventArgs e) //编辑模板
{
/* if (string.IsNullOrEmpty(Print_path.Text))
{
System.Windows.MessageBox.Show("模板不能为空");
return;
}*/
var data = this.CreateBillData();
if (!data.Item1)
{
System.Windows.MessageBox.Show("模拟数据生成错误");
return;
}
var tuple = PrintHelper.Design(Print_path.Text, data.Item2, data.Item3);
if (!tuple.Item1)
{
System.Windows.MessageBox.Show($"打开设计器失败:{tuple.Item2}");
}
}
private void Button_Click_2(object sender, RoutedEventArgs e) //预览模板
{
}
private Tuple<bool, Dictionary<string, object>, DataSet> CreateBillData()
{
//注意事项
//小票打印和门票一样,主要的区别是小票动态数据会变化,小票的长度也会动态改变
//这里主要演示下 动态数据源 为了动态拉伸,除了传入数据源,在模板上面code部分需要加代码
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("billNo", "2018111100002222");
dic.Add("optorName", "管理员");
//组装dataset数据源
DataTable dtDetail = new DataTable("dtDetail");
dtDetail.Columns.Add("GOODSCODE");
dtDetail.Columns.Add("GOODSNAME");
dtDetail.Columns.Add("GOODSPRICE");
dtDetail.Columns.Add("GOODSCOUNT");
dtDetail.Columns.Add("PAYSUM");
//加10种商品
for (int i = 1; i <= 10; i++)
{
dtDetail.Rows.Add("10000" + 1, "测试商品" + i, 10.00m, 5, 50.00m);
}
DataSet dsFrx = new DataSet();
dsFrx.Tables.Add(dtDetail);
return new Tuple<bool, Dictionary<string, object>, DataSet>(true, dic, dsFrx);
}
private void Print_Checked(object sender, RoutedEventArgs e) //打印机是否生效
{
Printer = TEXT_Print.IsChecked.GetValueOrDefault(); //打印机是否生效
cbxPrinter.IsEnabled = Printer; //
}
private List<XElement> GetLocalPrinters() //查找打印机
{
List<XElement> list = new List<XElement>();
try
{
foreach (string fPrinterName in PrinterSettings.InstalledPrinters)
{
var xePrintName = new XElement("Item", new XElement("OPTIONNAME", fPrinterName.Trim()), new XElement("OPTIONCODE", fPrinterName.Trim()));
if (!list.Contains(xePrintName))
list.Add(xePrintName);
}
}
catch (Exception ex)
{
}
return list;
}
}
}