diff --git a/STranslate/MainWindow.xaml.cs b/STranslate/MainWindow.xaml.cs index 63e66a7..78fcdcf 100644 --- a/STranslate/MainWindow.xaml.cs +++ b/STranslate/MainWindow.xaml.cs @@ -1,6 +1,7 @@ using STranslate.Utils; using STranslate.ViewModel; using System; +using System.Diagnostics; using System.Windows; using System.Windows.Input; using System.Windows.Interop; @@ -15,6 +16,7 @@ namespace STranslate { private MainVM vm; + private Process process = new Process(); public MainWindow() { InitializeComponent(); @@ -23,12 +25,15 @@ namespace STranslate InitialTray(); + DeepLStart(); + //if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict) //{ // MessageBox.Show("全局快捷键有冲突,请您到设置中重新设置"); //} vm = (MainVM)DataContext; } + private void InitialTray() { //notifyIcon.BalloonTipText = "STranslate已运行"; @@ -234,7 +239,9 @@ namespace STranslate /// private void Exit_Click(object sender, EventArgs e) { + notifyIcon.Dispose(); + DeepLStop(); Environment.Exit(0); } private void InitView() @@ -247,5 +254,28 @@ namespace STranslate private BitmapImage LockImgPath = new BitmapImage(new Uri("pack://application:,,,/Images/lock3.png")); private BitmapImage UnLockImgPath = new BitmapImage(new Uri("pack://application:,,,/Images/unlock3.png")); private System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon(); + /// + /// 新进程开启DeepL + /// + private void DeepLStart() + { + string strPathExe = AppDomain.CurrentDomain.BaseDirectory + "\\Static\\deepl-x86_64-pc-windows-gnu.exe"; + process.StartInfo.FileName = strPathExe; + process.StartInfo.Arguments = null;//-s -t 可以用来关机、开机或重启 + process.StartInfo.UseShellExecute = false; + process.StartInfo.RedirectStandardInput = false; //true + process.StartInfo.RedirectStandardOutput = false; //true + process.StartInfo.RedirectStandardError = false; + process.StartInfo.CreateNoWindow = true; + process.Start();//启动 + } + + /// + /// 关闭DeepL进程 + /// + private void DeepLStop() + { + process.Kill();//等待退出。 + } } } \ No newline at end of file diff --git a/STranslate/STranslate.csproj b/STranslate/STranslate.csproj index 731d359..7178ec4 100644 --- a/STranslate/STranslate.csproj +++ b/STranslate/STranslate.csproj @@ -31,7 +31,7 @@ true - AnyCPU + x64 true full false @@ -39,6 +39,7 @@ DEBUG;TRACE prompt 4 + false AnyCPU @@ -160,6 +161,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/STranslate/Static/deepl-x86_64-pc-windows-gnu.exe b/STranslate/Static/deepl-x86_64-pc-windows-gnu.exe new file mode 100644 index 0000000..a7bb677 Binary files /dev/null and b/STranslate/Static/deepl-x86_64-pc-windows-gnu.exe differ diff --git a/STranslate/Utils/TranslateUtil.cs b/STranslate/Utils/TranslateUtil.cs index b230284..fe429d7 100644 --- a/STranslate/Utils/TranslateUtil.cs +++ b/STranslate/Utils/TranslateUtil.cs @@ -51,11 +51,12 @@ namespace STranslate.Utils var respStr = await HttpUtil.PostAsync(url, reqStr); var resp = JsonConvert.DeserializeObject(respStr); - if (resp.Code == 200) + if (resp == null || resp.Code != 200) { - return resp.Data; + return string.Empty; } - return string.Empty; + + return resp?.Data ?? string.Empty; } /// diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index a21bdd7..7263020 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -11,12 +11,23 @@ namespace STranslate.ViewModel { public class MainVM : BaseVM { + /// + /// 引入Go项目库 + /// + /// + //[System.Runtime.InteropServices.DllImport("deepl.dll", EntryPoint = "run")] + //extern static void run(); + + private static string ConfigPath => $"{AppDomain.CurrentDomain.BaseDirectory}STranslate.yml"; public static ConfigModel config = new ConfigModel(); private static Dictionary LanguageEnumDict { get => TranslateUtil.GetEnumList(); } public MainVM() { + //启动deepl + //Task.Run(() => RunCmdUtil.RunCmd(AppDomain.CurrentDomain.BaseDirectory + "\\deepl-x86_64-pc-windows-gnu.exe")); + //初始化界面参数 InputCombo = LanguageEnumDict.Keys.ToList(); InputComboSelected = LanguageEnum.AUTO.GetDescription(); @@ -79,9 +90,9 @@ namespace STranslate.ViewModel OutputTxt = "翻译中..."; //获取结果 - //var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl.url, InputTxt, LanguageEnum.EN, LanguageEnum.AUTO); + var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl.url, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); - var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); + //var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); if (translateResp == string.Empty) {