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; using FastReport.Format; using Svg; using System.IO; using System.Windows.Media.Media3D; namespace formula_manage.Windows { /// /// Print.xaml 的交互逻辑 /// public partial class Print : Window { public Print() { WindowStartupLocation = WindowStartupLocation.CenterScreen; InitializeComponent(); cbxPrinter.ItemsSource = this.GetLocalPrinters(); //打印机搜索 this.Closing += Window_Closing; //添加窗口关闭事件 } public string INIPath = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "formula.ini"; string logpath = System.Environment.CurrentDirectory + "\\Log";//日志文件目录 string logPath = "" + System.Environment.CurrentDirectory + "\\Log\\Log.txt";//日志文件 string Log_time = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]:"; bool Printer; private void Window_Print(object sender, RoutedEventArgs e) { UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(INIPath); Printer = Boolean.Parse(Configini.IniReadvalue("PRINTER", "P1")); cbxPrinter.SelectedValue = Configini.IniReadvalue("PRINTER", "P2"); //读配置文件 TEXT_Print.IsChecked = Printer; cbxPrinter.IsEnabled = Printer; } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) //窗口关闭事件 { MessageBoxResult result = System.Windows.MessageBox.Show("设置完成后请重启程序", "提示", MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.Cancel); UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(INIPath); Configini.IniWritevalue("PRINTER", " P1", Printer.ToString()); Configini.IniWritevalue("PRINTER", " P2", cbxPrinter.SelectedValue.ToString()); System.IO.DirectoryInfo log = new System.IO.DirectoryInfo(@logpath);//生成日志文件目录 FileStream fs = new FileStream(logPath, FileMode.Append, FileAccess.Write); StreamWriter wr = new StreamWriter(fs);//创建文件 wr.WriteLine(Log_time + "Print:Print_SET"); wr.Close(); } private void Button_Click(object sender, RoutedEventArgs e) //路径选择按钮 { 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, DataSet> CreateBillData() { //注意事项 //小票打印和门票一样,主要的区别是小票动态数据会变化,小票的长度也会动态改变 //这里主要演示下 动态数据源 为了动态拉伸,除了传入数据源,在模板上面code部分需要加代码 Dictionary dic = new Dictionary(); 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, DataSet>(true, dic, dsFrx); } private void Print_Checked(object sender, RoutedEventArgs e) //打印机是否生效 { Printer = TEXT_Print.IsChecked.GetValueOrDefault(); //打印机是否生效 cbxPrinter.IsEnabled = Printer; // } private List GetLocalPrinters() //查找打印机 { List list = new List(); 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; } } }