WPF_Rule.xaml.cs 6.0 KB
using BLL;
using Model;
using SmartScan.SetControl.WPF.Model;
using System;
using System.Collections.Generic;
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.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static SmartScan.SetControl.WPF.Model.NeoAlertBox;

namespace SmartScan.SetControl.WPF
{
    /// <summary>
    /// WPF_Rule.xaml 的交互逻辑
    /// </summary>
    public partial class WPF_Rule : Window
    {
        // 新增事件,用于返回生成的关键字
        public event EventHandler<string> KeywordGenerated;

        // 添加数据结构保存配置
        private int digitPlaces;
        private bool fillZero;
        private int resetStrategy;

        public WPF_Rule()
        {
            InitializeComponent();
            this.Loaded += (s, e) => {
                if (logos is Image img)
                {
                    // 将WinForms资源转换为WPF可用的BitmapSource
                    System.Drawing.Bitmap winFormsBitmap = global::SmartScan.Properties.Resources.App;

                    if (winFormsBitmap != null)
                    {
                        var bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                            winFormsBitmap.GetHbitmap(),
                            IntPtr.Zero,
                            Int32Rect.Empty,
                            BitmapSizeOptions.FromEmptyOptions());

                        img.Source = bitmapSource;
                    }
                }
            };
            // 从配置文件加载现有设置
            LoadConfigSettings();
        }

        private void LoadConfigSettings()
        {
            // 加载配置到控件
            NumReelIDPlaces.Text = BLLCommon.config.ReelIDPlaces.ToString();
            ChkReelIDFillZero.IsChecked = BLLCommon.config.ReelIDFillZero;
            CmbResetidBy.SelectedIndex = Config.REEL_ID_AutoResetStrategy;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string YU = BLLCommon.config.Language;
            bool result = false;
            if (YU == "English")
            {
                result = NeoAlertBox.Show("", "Save changes?", AlertType.Success, "NEO SCAN", true);
            }else if (YU == "日语")
            {
                result = NeoAlertBox.Show("", "保存しますか?", AlertType.Success, "NEO SCAN", true);
            }
            else
            {
                 result = NeoAlertBox.Show("", "是否保存", AlertType.Success, "NEO SCAN", true);
            }
            if (result)
            {
                // 保存配置
                SaveSettings();

                // 关闭窗口
                DialogResult = true;
            }
         
        }

        private void SaveSettings()
        {
            try
            {
                // 保存数字位数
                if (int.TryParse(NumReelIDPlaces.Text, out digitPlaces))
                {
                    BLLCommon.config.ReelIDPlaces = digitPlaces;
                }

                // 保存补零设置
                BLLCommon.config.ReelIDFillZero = ChkReelIDFillZero.IsChecked ?? false;

                // 保存重置策略
                Config.REEL_ID_AutoResetStrategy = CmbResetidBy.SelectedIndex;

                // 保存配置到文件
                BLLCommon.config.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存配置时出错:" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Generate_keywords_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(keyname.Text))
            {
                MessageBox.Show("请输入关键字内容", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            // 触发事件,传递关键字回主窗口
            KeywordGenerated?.Invoke(this, keyname.Text);

            // 清空输入
            keyname.Clear();
            keyname.Focus();
        }

        private void UsrMacro_btn_1_Click(object sender, RoutedEventArgs e)
        {
            int a = int.Parse(NumReelIDPlaces.Text.ToString());
            if (a <= 0)
            {
                return;
            }
            else
            {
                a = a - 1;
                NumReelIDPlaces.Text = a.ToString();
            }
        }
        private void UsrMacro_btn_2_Click(object sender, RoutedEventArgs e)
        {
            int a = int.Parse(NumReelIDPlaces.Text.ToString());

            a = a + 1;
            NumReelIDPlaces.Text = a.ToString();
        }

        private void MinimizeButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
        private void MaximizeRestoreButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
                this.WindowState = WindowState.Normal;
            else
                this.WindowState = WindowState.Maximized;
        }
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        // 如果您想要支持通过点击标题栏拖动窗口
        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
                this.DragMove();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string YU = BLLCommon.config.Language;
            LanguageWwitchover.LoadPath(FilePath.LANGUAGE_DIR);
            LanguageWwitchover.LoadLanguage(YU);
            LanguageWwitchover.SetLanguage(this);
        }
    }
}