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.
274 lines
12 KiB
274 lines
12 KiB
using FastReport;
|
|
using formula_manage.UserClass;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing.Printing;
|
|
using System.IO;
|
|
using System.Windows;
|
|
using System.Windows.Forms;
|
|
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(); //打印机搜索
|
|
cbxPrinter2.ItemsSource = this.GetLocalPrinters();
|
|
cbxPrinter3.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;
|
|
bool Printer2;
|
|
bool Printer3;
|
|
|
|
|
|
string Temp_M, Temp_S;
|
|
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"); //读配置文件
|
|
Print_path.Text = Configini.IniReadvalue("PRINTER", "P3");
|
|
Printer2 = Boolean.Parse(Configini.IniReadvalue("PRINTER", "P4"));
|
|
Printer3 = Boolean.Parse(Configini.IniReadvalue("PRINTER", "P5"));
|
|
cbxPrinter2.SelectedValue = Configini.IniReadvalue("PRINTER", "P8"); //读配置文件
|
|
cbxPrinter3.SelectedValue = Configini.IniReadvalue("PRINTER", "P9"); //读配置文件
|
|
Print_path2.Text = Configini.IniReadvalue("PRINTER", "P12");
|
|
Temp_M = Configini.IniReadvalue("PRINTER", "P13");
|
|
Temp_S = Configini.IniReadvalue("PRINTER", "P14");
|
|
|
|
TEXT_Print.IsChecked = Printer;
|
|
cbxPrinter.IsEnabled = Printer;
|
|
|
|
TEXT_Print2.IsChecked = Printer2;
|
|
cbxPrinter2.IsEnabled = Printer2;
|
|
|
|
TEXT_Print3.IsChecked = Printer3;
|
|
cbxPrinter3.IsEnabled = Printer3;
|
|
|
|
PRINT_file();
|
|
}
|
|
|
|
private void PRINT_file()//建立模板列表
|
|
{
|
|
DirectoryInfo di_M = new DirectoryInfo(Print_path.Text); //new文件夹
|
|
FileInfo[] files_M = di_M.GetFiles("*.frx");//生成文件夹中的文件名合集
|
|
Template_M.ItemsSource = files_M;//加载合集
|
|
Template_M.DisplayMemberPath = "Name"; // 显示文件名
|
|
DirectoryInfo di_S = new DirectoryInfo(Print_path2.Text);
|
|
FileInfo[] files_S = di_S.GetFiles("*.frx");
|
|
Template_S.ItemsSource = files_S;
|
|
Template_S.DisplayMemberPath = "Name";
|
|
Template_M.Text = Temp_M;//传入保存的默认模板
|
|
Template_S.Text = Temp_S;//
|
|
|
|
PrintTemplate.ItemsSource = files_M;
|
|
PrintTemplate.DisplayMemberPath = "Name";
|
|
}
|
|
|
|
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) //窗口关闭事件
|
|
{
|
|
MessageBoxResult result = System.Windows.MessageBox.Show("设置完成后请重启程序", "提示", MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.Cancel);
|
|
if (result == MessageBoxResult.Cancel) //判断是否确认
|
|
{
|
|
e.Cancel = true; // 中断点击事件
|
|
}
|
|
|
|
UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(INIPath);
|
|
Configini.IniWritevalue("PRINTER", " P1", Printer.ToString());
|
|
Configini.IniWritevalue("PRINTER", " P2", cbxPrinter.SelectedValue.ToString());
|
|
Configini.IniWritevalue("PRINTER", " P3", Print_path.Text.ToString());
|
|
Configini.IniWritevalue("PRINTER", " P4", Printer2.ToString());
|
|
Configini.IniWritevalue("PRINTER", " P5", Printer3.ToString());
|
|
Configini.IniWritevalue("PRINTER", " P8", cbxPrinter2.SelectedValue.ToString());
|
|
Configini.IniWritevalue("PRINTER", " P9", cbxPrinter3.SelectedValue.ToString());
|
|
Configini.IniWritevalue("PRINTER", " P12", Print_path2.Text.ToString());
|
|
Configini.IniWritevalue("PRINTER", " P13", Template_M.Text);
|
|
Configini.IniWritevalue("PRINTER", " P14", Template_S.Text);
|
|
|
|
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_Click1(object sender, RoutedEventArgs e) //路径选择按钮
|
|
{
|
|
FolderBrowserDialog Dilog = new FolderBrowserDialog();
|
|
Dilog.SelectedPath = Print_path2.Text; //打开目录
|
|
Dilog.ShowNewFolderButton = false; //不显示新建文件夹按钮
|
|
//
|
|
if (Dilog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
|
|
{
|
|
Print_path2.Text = Dilog.SelectedPath; //返回选择的字符串
|
|
}
|
|
}
|
|
|
|
private void Button_Click_1(object sender, RoutedEventArgs e) //编辑模板
|
|
{
|
|
if (string.IsNullOrEmpty(Template_M.Text))
|
|
{
|
|
System.Windows.MessageBox.Show("模板不能为空");
|
|
return;
|
|
}
|
|
|
|
var tuple = PrintHelper.Design(Print_path.Text + "/" + Template_M.Text);
|
|
if (!tuple.Item1)
|
|
{
|
|
System.Windows.MessageBox.Show($"打开设计器失败:{tuple.Item2}");
|
|
}
|
|
}
|
|
private void Button_Click_12(object sender, RoutedEventArgs e) //编辑模板
|
|
{
|
|
if (string.IsNullOrEmpty(Template_S.Text))
|
|
{
|
|
System.Windows.MessageBox.Show("模板不能为空");
|
|
return;
|
|
}
|
|
|
|
var tuple = PrintHelper.Design(Print_path2.Text + "/" + Template_S.Text);
|
|
if (!tuple.Item1)
|
|
{
|
|
System.Windows.MessageBox.Show($"打开设计器失败:{tuple.Item2}");
|
|
}
|
|
}
|
|
|
|
private void Button_Click_2(object sender, RoutedEventArgs e) //预览模板
|
|
{
|
|
Report report = new Report();
|
|
//加载标签模版
|
|
report.Load(Print_path.Text + "/" + Template_M.Text);
|
|
//打印预览
|
|
report.Prepare();//准备
|
|
report.ShowPrepared();//预览
|
|
}
|
|
private void Button_Click_22(object sender, RoutedEventArgs e) //预览模板
|
|
{
|
|
Report report = new Report();
|
|
//加载标签模版
|
|
report.Load(Print_path2.Text + "/" + Template_S.Text);
|
|
//打印预览
|
|
report.Prepare();//准备
|
|
report.ShowPrepared();//预览
|
|
}
|
|
|
|
private void Print_Checked(object sender, RoutedEventArgs e) //打印机是否生效
|
|
{
|
|
Printer = TEXT_Print.IsChecked.GetValueOrDefault(); //打印机是否生效
|
|
cbxPrinter.IsEnabled = Printer; //
|
|
}
|
|
private void Print_Checked2(object sender, RoutedEventArgs e) //打印机是否生效
|
|
{
|
|
Printer2 = TEXT_Print2.IsChecked.GetValueOrDefault(); //打印机是否生效
|
|
cbxPrinter2.IsEnabled = Printer2; //
|
|
}
|
|
private void Print_Checked3(object sender, RoutedEventArgs e) //打印机是否生效
|
|
{
|
|
Printer3 = TEXT_Print3.IsChecked.GetValueOrDefault(); //打印机是否生效
|
|
cbxPrinter3.IsEnabled = Printer3; //
|
|
}
|
|
|
|
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)
|
|
{
|
|
|
|
}
|
|
return list;
|
|
}
|
|
|
|
private void Button_Click_3(object sender, RoutedEventArgs e)//新建模板文件
|
|
{
|
|
|
|
Prompt.Visibility = Visibility.Visible; //开启页面遮罩
|
|
|
|
string Template_path = Print_path.Text + "/" + PrintTemplate.Text;
|
|
string Template_DATA = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Report ScriptLanguage=\"CSharp\" ReportInfo.Created=\"12/12/2023 23:59:59\" ReportInfo.Modified=\"12/12/2023 23:59:59\" ReportInfo.CreatorVersion=\"2023.12.12.0\">\r\n <Dictionary/>\r\n <ReportPage Name=\"Page1\" Watermark.Font=\"宋体, 60pt\">\r\n <ReportTitleBand Name=\"ReportTitle1\" Width=\"718.2\" Height=\"37.8\"/>\r\n <PageHeaderBand Name=\"PageHeader1\" Top=\"41.8\" Width=\"718.2\" Height=\"28.35\"/>\r\n <DataBand Name=\"Data1\" Top=\"74.15\" Width=\"718.2\" Height=\"75.6\"/>\r\n <PageFooterBand Name=\"PageFooter1\" Top=\"153.75\" Width=\"718.2\" Height=\"18.9\"/>\r\n </ReportPage>\r\n</Report>";
|
|
|
|
if (File.Exists(Template_path)) //判断文件是否存在
|
|
{
|
|
System.Windows.MessageBox.Show("模板已存在");
|
|
Prompt.Visibility = Visibility.Hidden;//去页面遮罩
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(PrintTemplate.Text))
|
|
{
|
|
System.Windows.MessageBox.Show("模板名不能为空");
|
|
Prompt.Visibility = Visibility.Hidden;//去页面遮罩
|
|
return;
|
|
}
|
|
|
|
File.WriteAllText(Template_path + ".frx", Template_DATA);
|
|
var tuple = PrintHelper.Design(Template_path + ".frx");
|
|
if (!tuple.Item1)
|
|
{
|
|
System.Windows.MessageBox.Show($"打开设计器失败:{tuple.Item2}");
|
|
Prompt.Visibility = Visibility.Hidden;
|
|
return;
|
|
}
|
|
|
|
PRINT_file(); ;//刷新模板列表
|
|
PrintTemplate.Text = null;//清空输入
|
|
|
|
Prompt.Visibility = Visibility.Hidden;//去页面遮罩
|
|
}
|
|
|
|
private void Button_Click_4(object sender, RoutedEventArgs e)//删除指定模板
|
|
{
|
|
try
|
|
{
|
|
string Template_path = Print_path.Text + "/" + PrintTemplate.Text;
|
|
File.Delete(Template_path);
|
|
}
|
|
catch
|
|
{
|
|
System.Windows.MessageBox.Show("删除失败");
|
|
}
|
|
|
|
PrintTemplate.Text = null;
|
|
DirectoryInfo di_M = new DirectoryInfo(Print_path.Text); //new文件夹
|
|
FileInfo[] files_M = di_M.GetFiles("*.frx");//生成文件夹中的文件名合集
|
|
PrintTemplate.ItemsSource = files_M;//加载合集
|
|
PrintTemplate.DisplayMemberPath = "Name"; // 显示文件名
|
|
}
|
|
}
|
|
}
|
|
|