WPFWorkMode.xaml.cs 15.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
using BLL;
using ClosedXML.Excel;
using Model;
using System;
using System.Collections.Generic;
using System.IO;
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.Navigation;
using System.Windows.Shapes;
using static System.Windows.Forms.AxHost;

namespace SmartScan.SetControl.WPF
{
    /// <summary>
    /// WPFWorkMode.xaml 的交互逻辑
    /// </summary>
    public partial class WPFWorkMode : System.Windows.Controls.UserControl
    {
        private int clickCount = 0;
        public delegate void PrintDelegate(Dictionary<string, string> content);
        public static event PrintDelegate Printing;
        public class PrinterInfo
        {
            public string Printer { get; set; }
            public string Label { get; set; }
            public string Template { get; set; }
        }
        public WPFWorkMode()
        {
            InitializeComponent();
            LoadInitialSettings();


        }
        private void LoadInitialSettings()
        {
            // 创建表格数据源
            List<PrinterInfo> printerInfoList = new List<PrinterInfo>();
            List<PrinterInfo> printerInfoList1 = new List<PrinterInfo>();
            List<PrinterInfo> printerInfoList2= new List<PrinterInfo>();

            // 添加可用打印机信息
            foreach (string printer in Printer.AllName)
            {
                printerInfoList.Add(new PrinterInfo
                {
                    Printer = printer,
                    Label = "",
                    Template = "",
                });
            }
            foreach (string printer in BLLCommon.labelEdit.Name)
            {
               printerInfoList1.Add(new PrinterInfo
                {
                    Printer = "",
                    Label = printer,
                    Template = "",
                });
            }
            foreach (string printer in BLLCommon.mateEdit.Name)
            {
                printerInfoList2.Add(new PrinterInfo
                {
                    Printer = "",
                    Label = "",
                    Template = printer,
                });
            }




            // 添加标签和模板信息
            foreach (var item in printerInfoList)
            {
                // 尝试匹配默认标签
                if (Printer.AllName.Length > 0)
                {
                    item.Label = BLLCommon.labelEdit.Name[0];
                }

                // 尝试匹配默认模板
                //if (BLLCommon.mateEdit.Name.Length > 0)
                //{
                //    item.Template = BLLCommon.mateEdit.Name[0];
                //}
            }
            foreach (var item in printerInfoList1)
            {
                

                // 尝试匹配默认模板
                if (BLLCommon.labelEdit.Name.Length > 0)
                {
                    if  (BLLCommon.mateEdit.Count>0)
                        item.Template = BLLCommon.mateEdit.Name[0];
                }
            }
            foreach (var item in printerInfoList2)
            {


                // 尝试匹配默认模板
                if (BLLCommon.mateEdit.Name.Length > 0)
                {
                    item.Printer = BLLCommon.mateEdit.Name[0];
                }
            }

            // 设置表格数据源
            PrinterInfoGrid.ItemsSource = printerInfoList;
            PrinterInfoGrid1.ItemsSource = printerInfoList1;
            PrinterInfoGrid2.ItemsSource = printerInfoList2;
            string configPrinterName=  BLLCommon.config.PrinterName;
            string configPrinterName1= BLLCommon.config.DefaultPrintLabel;
            string configPrinterName2= BLLCommon.config.DefaultMaterialName;
            if (!string.IsNullOrEmpty(configPrinterName))
            {
                // 查找匹配项
                PrinterInfo matchingPrinter = printerInfoList.FirstOrDefault(p => p.Printer == configPrinterName);

                if (matchingPrinter != null)
                {
                    // 选中匹配的项
                    PrinterInfoGrid.SelectedItem = matchingPrinter;

                    // 如果需要滚动到选中项
                    PrinterInfoGrid.ScrollIntoView(matchingPrinter);
                }
            }
            if (!string.IsNullOrEmpty(configPrinterName1))
            {
                // 查找匹配项
                PrinterInfo matchingPrinter = printerInfoList1.FirstOrDefault(p => p.Label == configPrinterName1);

                if (matchingPrinter != null)
                {
                    // 选中匹配的项
                    PrinterInfoGrid1.SelectedItem = matchingPrinter;

                    // 如果需要滚动到选中项
                    PrinterInfoGrid1.ScrollIntoView(matchingPrinter);
                }
            }
            if (!string.IsNullOrEmpty(configPrinterName2))
            {
                // 查找匹配项
                PrinterInfo matchingPrinter = printerInfoList2.FirstOrDefault(p => p.Template == configPrinterName2);

                if (matchingPrinter != null)
                {
                    // 选中匹配的项
                    PrinterInfoGrid2.SelectedItem = matchingPrinter;

                    // 如果需要滚动到选中项
                    PrinterInfoGrid2.ScrollIntoView(matchingPrinter);
                }
            }
            //// 选中默认打印机(如果已配置)
            //if (!string.IsNullOrEmpty(BLLCommon.config.PrinterName))
            //{
            //    foreach (PrinterInfo item in printerInfoList)
            //    {
            //        if (item.Printer == BLLCommon.config.PrinterName)
            //        {
            //            PrinterInfoGrid2.SelectedItem = item;
            //            item.Label = BLLCommon.config.DefaultPrintLabel;
            //            item.Template = BLLCommon.config.DefaultMaterialName;
            //            break;
            //        }
            //    }
            //}

            // 打印方向设置
            RdoLandscape.IsChecked = BLLCommon.config.PrintLandscape;
            RdoVertical.IsChecked = !BLLCommon.config.PrintLandscape;

            // 图像保存设置
            switch (BLLCommon.config.HistoryImage)
            {
                case HistoryImage.Original:
                    RdoOriginal.IsChecked = true;
                    break;
                case HistoryImage.Condense:
                    RdoCondense.IsChecked = true;
                    break;
                case HistoryImage.NoImage:
                    RdoNoImage.IsChecked = true;
                    break;
            }

            // 复选框设置
            ChkSelectPN.IsChecked = BLLCommon.config.SelectHttpPN;
            ChkLabelEmptyCheck.IsChecked = BLLCommon.config.LabelEmptyCheck;
            ChkOpenEnterWork.IsChecked = BLLCommon.config.OpenStartWork;
            ChkPrintCompletedClear.IsChecked = BLLCommon.config.PrintCompletedClear;
            ChkOpenMaximize.IsChecked = BLLCommon.config.OpenMaximize;
            ChkTriggerOpenLight.IsChecked = BLLCommon.config.TriggerOpenLight;
            ChkPromptAfterPrinting.IsChecked = BLLCommon.config.PromptAfterPrinting;
            ChkAutoPrint.IsChecked = BLLCommon.config.AutoPrint;
            ChkAllowModifyPrint.IsChecked = Config.AllowModifyPrintInfo;
            ChkCheckFunction.IsChecked = BLLCommon.config.CheckFunction;
        }
        private void PrinterInfoGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            // 可以在这里更新UI或处理选择变化
        }
      
        public void Save()
        {
            // 保存选中的打印机设置
            // 检查是否有选中的行
            if (PrinterInfoGrid.SelectedItem != null)
            {
                // 获取选中的数据对象
                var selectedItem = PrinterInfoGrid.SelectedItem;

                // 如果您知道对象类型,可以进行转换
                // 假设您的数据绑定类型是 PrinterInfo
                var printerInfo = selectedItem as PrinterInfo;

                if (printerInfo != null)
                {
                    // 现在可以访问选中行的数据
                    string printer = printerInfo.Printer;
                    BLLCommon.config.PrinterName = printer;


                }
            }
            if (PrinterInfoGrid1.SelectedItem != null)
            {
                // 获取选中的数据对象
                var selectedItem = PrinterInfoGrid1.SelectedItem;

                // 如果您知道对象类型,可以进行转换
                // 假设您的数据绑定类型是 PrinterInfo
                var printerInfo = selectedItem as PrinterInfo;

                if (printerInfo != null)
                {
                    // 现在可以访问选中行的数据
                    string printer = printerInfo.Label;
                    BLLCommon.config.DefaultPrintLabel = printer;


                }
            }
            if (PrinterInfoGrid2.SelectedItem != null)
            {
                // 获取选中的数据对象
                var selectedItem = PrinterInfoGrid2.SelectedItem;

                // 如果您知道对象类型,可以进行转换
                // 假设您的数据绑定类型是 PrinterInfo
                var printerInfo = selectedItem as PrinterInfo;

                if (printerInfo != null)
                {
                    // 现在可以访问选中行的数据
                    string printer = printerInfo.Template;
                    BLLCommon.config.DefaultMaterialName = printer;


                }
            }



            //if (PrinterInfoGrid.SelectedItem is PrinterInfo selectedInfo)
            //{
            //    BLLCommon.config.PrinterName = selectedInfo.Printer;
            //    BLLCommon.config.DefaultPrintLabel = selectedInfo.Label;
            //    BLLCommon.config.DefaultMaterialName = selectedInfo.Template;
            //}

            // 保存打印方向
            BLLCommon.config.PrintLandscape = RdoLandscape.IsChecked.Value;

            // 保存图像设置
            if (RdoOriginal.IsChecked.Value)
                BLLCommon.config.HistoryImage = HistoryImage.Original;
            else if (RdoCondense.IsChecked.Value)
                BLLCommon.config.HistoryImage = HistoryImage.Condense;
            else if (RdoNoImage.IsChecked.Value)
                BLLCommon.config.HistoryImage = HistoryImage.NoImage;

            // 保存复选框设置
            BLLCommon.config.SelectHttpPN = ChkSelectPN.IsChecked.Value;
            BLLCommon.config.LabelEmptyCheck = ChkLabelEmptyCheck.IsChecked.Value;
            BLLCommon.config.OpenStartWork = ChkOpenEnterWork.IsChecked.Value;
            BLLCommon.config.OpenMaximize = ChkOpenMaximize.IsChecked.Value;
            BLLCommon.config.PrintCompletedClear = ChkPrintCompletedClear.IsChecked.Value;
            BLLCommon.config.TriggerOpenLight = ChkTriggerOpenLight.IsChecked.Value;
            BLLCommon.config.PromptAfterPrinting = ChkPromptAfterPrinting.IsChecked.Value;
            BLLCommon.config.AutoPrint = ChkAutoPrint.IsChecked.Value;
            BLLCommon.config.CheckFunction = ChkCheckFunction.IsChecked.Value;
            Config.AllowModifyPrintInfo = ChkAllowModifyPrint.IsChecked.Value;

            BLLCommon.config.Save();
        }
        private void BtnReadFilePrint_Click(object sender, RoutedEventArgs e)
        {
            clickCount++;
            if (clickCount >= 10)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog
                {
                    Multiselect = false,
                    Title = "请选择文件",
                    Filter = "所有文件(*.*)|*.*"
                };
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string file = openFileDialog.FileName;
                    ContinuousPrinting(file);
                }
                clickCount = 0;
            }
        }
        private void ContinuousPrinting(string filepath)
        {
            Dictionary<int, Dictionary<string, string>> valuePairs = new Dictionary<int, Dictionary<string, string>>();

            if (!File.Exists(filepath))
            {
                //MessageBox.Show("文件不存在");
                return;
            }

            var Titles = ExtraFileData.ParseXLSFileTitle(filepath);

            try
            {
                XLWorkbook wb = new XLWorkbook(filepath);
                var ws = wb.Worksheet(1);
                int emptyrow = 0;
                int currow = 1;

                while (emptyrow < 3)
                {
                    var rowdata = new Dictionary<string, string>();
                    for (int i = 0; i < Titles.Count; i++)
                    {
                        var v = ws.Row(currow + 1).Cell(i + 1).Value.ToString().Trim();
                        rowdata.Add(Titles[i], v);
                    }

                    currow++;

                    if (!valuePairs.ContainsKey(currow))
                    {
                        valuePairs.Add(currow, rowdata);
                    }
                    if (rowdata.Values.Contains(""))
                    {
                        emptyrow++;
                    }
                }
            }
            catch (Exception ex)
            {
               // MessageBox.Show($"数据源加载文件出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                // 可以根据需要添加日志记录
                // LogNet.log.Info("数据源加载文件出错:" + ex.ToString());
            }
            foreach (var item in valuePairs)
            {
                Printing?.Invoke(item.Value);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            string YU = BLLCommon.config.Language;
            if (YU=="English")
            {
                PrinterInfoGridtext.Header = "Printer";
                PrinterInfoGridtext1.Header = "Default Print Template";
                PrinterInfoGridtext2.Header = "Prioritize Match Template\r\n";
            }
            else if (YU == "日语") {
                PrinterInfoGridtext.Header = "プリンター";
                PrinterInfoGridtext1.Header = "デフォルト印刷ラベル";
                PrinterInfoGridtext2.Header = "テンプレート優先マッチ";
            }
            else
            {
                PrinterInfoGridtext.Header = "打印机";
                PrinterInfoGridtext1.Header = "默认打印标签";
                PrinterInfoGridtext2.Header = "优先匹配模板";
            }
            LanguageWwitchover.LoadPath(FilePath.LANGUAGE_DIR);
            LanguageWwitchover.LoadLanguage(YU);
            LanguageWwitchover.SetLanguage(this);
        }
    }
}