diff --git a/STranslate/App.config b/STranslate/App.config index 193aecc..ca88a2c 100644 --- a/STranslate/App.config +++ b/STranslate/App.config @@ -1,6 +1,6 @@  - - - + + + \ No newline at end of file diff --git a/STranslate/App.xaml b/STranslate/App.xaml index 00c1b25..91f1640 100644 --- a/STranslate/App.xaml +++ b/STranslate/App.xaml @@ -1,14 +1,13 @@  - - + + - + \ No newline at end of file diff --git a/STranslate/App.xaml.cs b/STranslate/App.xaml.cs index ab1d2f7..16fce42 100644 --- a/STranslate/App.xaml.cs +++ b/STranslate/App.xaml.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Data; -using System.Linq; -using System.Threading.Tasks; -using System.Windows; +using System.Windows; namespace STranslate { @@ -14,9 +8,10 @@ namespace STranslate public partial class App : Application { private static System.Threading.Mutex mutex; - //系统能够识别有名称的互斥,因此可以使用它禁止应用程序启动两次 - //第二个参数可以设置为产品的名称:Application.ProductName - // 每次启动应用程序,都会验证名称为OnlyRun的互斥是否存在 + + //系统能够识别有名称的互斥,因此可以使用它禁止应用程序启动两次 + //第二个参数可以设置为产品的名称:Application.ProductName + // 每次启动应用程序,都会验证名称为OnlyRun的互斥是否存在 protected override void OnStartup(StartupEventArgs e) { mutex = new System.Threading.Mutex(true, "CE252DD8-179F-4544-9989-453F5DEA378D"); @@ -31,4 +26,4 @@ namespace STranslate } } } -} +} \ No newline at end of file diff --git a/STranslate/MainWindow.xaml b/STranslate/MainWindow.xaml index 8f44b5f..217cfa9 100644 --- a/STranslate/MainWindow.xaml +++ b/STranslate/MainWindow.xaml @@ -25,9 +25,9 @@ CornerRadius="10"> - - - + + + + Header="主界面" /> + Header="退出" /> @@ -74,7 +74,7 @@ AcceptsReturn="True" Text="{Binding InputTxt,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> - + @@ -85,28 +85,28 @@ Orientation="Horizontal"> - - + \ No newline at end of file diff --git a/STranslate/MainWindow.xaml.cs b/STranslate/MainWindow.xaml.cs index b27d459..fa87fb3 100644 --- a/STranslate/MainWindow.xaml.cs +++ b/STranslate/MainWindow.xaml.cs @@ -1,19 +1,8 @@ using STranslate.Utils; using STranslate.ViewModel; 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; namespace STranslate { @@ -149,4 +138,4 @@ namespace STranslate MainVM.Instance.OutputTxt = string.Empty; } } -} +} \ No newline at end of file diff --git a/STranslate/Model/BaiduModel.cs b/STranslate/Model/BaiduModel.cs index 6b6d310..82908cb 100644 --- a/STranslate/Model/BaiduModel.cs +++ b/STranslate/Model/BaiduModel.cs @@ -1,9 +1,4 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace STranslate.Model { @@ -16,22 +11,25 @@ namespace STranslate.Model public string Salt { get; set; } public string Sign { get; set; } } + public class BaiduResp { [JsonProperty("from")] public string From { get; set; } + [JsonProperty("to")] public string To { get; set; } + [JsonProperty("trans_result")] public TransResult[] TransResult { get; set; } } public class TransResult { - [JsonProperty("src")] public string Src { get; set; } + [JsonProperty("dst")] public string Dst { get; set; } } -} +} \ No newline at end of file diff --git a/STranslate/Model/ConfigModel.cs b/STranslate/Model/ConfigModel.cs index a39182c..71dd477 100644 --- a/STranslate/Model/ConfigModel.cs +++ b/STranslate/Model/ConfigModel.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace STranslate.Model +namespace STranslate.Model { public class ConfigModel { @@ -18,6 +12,7 @@ namespace STranslate.Model public string appid { get; set; } public string secretKey { get; set; } } + public class DeepL { public string url { get; set; } diff --git a/STranslate/Model/DeeplModel.cs b/STranslate/Model/DeeplModel.cs index 598259a..d28b677 100644 --- a/STranslate/Model/DeeplModel.cs +++ b/STranslate/Model/DeeplModel.cs @@ -1,9 +1,4 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace STranslate.Model { @@ -27,4 +22,4 @@ namespace STranslate.Model [JsonProperty("data")] public string Data { get; set; } } -} +} \ No newline at end of file diff --git a/STranslate/Model/LanguageEnum.cs b/STranslate/Model/LanguageEnum.cs index 42f6542..b42913f 100644 --- a/STranslate/Model/LanguageEnum.cs +++ b/STranslate/Model/LanguageEnum.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; using System.ComponentModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace STranslate.Model { @@ -11,59 +7,84 @@ namespace STranslate.Model { [Description("自动")] AUTO, //自动 + [Description("德语")] DE, //德语 + [Description("英语")] EN, //英语 + [Description("西班牙语")] ES, //西班牙语 + [Description("法语")] FR, //法语 + [Description("意大利语")] IT, //意大利语 + [Description("日语")] JA, //日语 + [Description("荷兰语")] NL, //荷兰语 + [Description("波兰语")] PL, //波兰语 + [Description("葡萄牙语")] PT, //葡萄牙语 + [Description("俄语")] RU, //俄语 + [Description("中文")] ZH, //中文 + [Description("保加利亚语")] BG, //保加利亚语 + [Description("捷克语")] CS, //捷克语 + [Description("丹麦语")] DA, //丹麦语 + [Description("希腊语")] EL, //希腊语 + [Description("爱沙尼亚语")] ET, //爱沙尼亚语 + [Description("芬兰语")] FI, //芬兰语 + [Description("匈牙利语")] HU, //匈牙利语 + [Description("立陶宛语")] LT, //立陶宛语 + [Description("拉脱维亚语")] LV, //拉脱维亚语 + [Description("罗马尼亚语")] RO, //罗马尼亚语 + [Description("斯洛伐克语")] SK, //斯洛伐克语 + [Description("斯洛文尼亚语")] SL, //斯洛文尼亚语 + [Description("瑞典语")] SV, //瑞典语 } + /// /// 获取Description /// - static class EnumExtensions + internal static class EnumExtensions { public static string GetDescription(this Enum val) { @@ -72,4 +93,4 @@ namespace STranslate.Model return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description; } } -} +} \ No newline at end of file diff --git a/STranslate/Properties/AssemblyInfo.cs b/STranslate/Properties/AssemblyInfo.cs index deb41f5..3edbad0 100644 --- a/STranslate/Properties/AssemblyInfo.cs +++ b/STranslate/Properties/AssemblyInfo.cs @@ -1,6 +1,4 @@ using System.Reflection; -using System.Resources; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Windows; @@ -30,7 +28,6 @@ using System.Windows; //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] - [assembly: ThemeInfo( ResourceDictionaryLocation.None, //主题特定资源词典所处位置 //(未在页面中找到资源时使用, @@ -40,8 +37,7 @@ using System.Windows; //、应用程序或任何主题专用资源字典中找到时使用) )] - -// 程序集的版本信息由下列四个值组成: +// 程序集的版本信息由下列四个值组成: // // 主版本 // 次版本 @@ -53,4 +49,4 @@ using System.Windows; // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("0.0.0.2")] [assembly: AssemblyFileVersion("0.0.0.2")] -[assembly: Guid("CE252DD8-179F-4544-9989-453F5DEA378D")] +[assembly: Guid("CE252DD8-179F-4544-9989-453F5DEA378D")] \ No newline at end of file diff --git a/STranslate/Utils/ConfigUtil.cs b/STranslate/Utils/ConfigUtil.cs index cfcea57..720a019 100644 --- a/STranslate/Utils/ConfigUtil.cs +++ b/STranslate/Utils/ConfigUtil.cs @@ -1,10 +1,6 @@ using STranslate.Model; using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using YamlDotNet.Serialization; namespace STranslate.Utils @@ -27,7 +23,6 @@ namespace STranslate.Utils throw ex; } } - } public static void WriteConfig(string path, ConfigModel configs) @@ -42,8 +37,6 @@ namespace STranslate.Utils { writer.Write(strWriter.ToString()); } - - } } -} +} \ No newline at end of file diff --git a/STranslate/Utils/HotKeysUtil.cs b/STranslate/Utils/HotKeysUtil.cs index fffdee6..b4cd3c1 100644 --- a/STranslate/Utils/HotKeysUtil.cs +++ b/STranslate/Utils/HotKeysUtil.cs @@ -11,24 +11,28 @@ namespace STranslate.Utils /// /// 引用自 https://blog.csdn.net/weixin_44879611/article/details/103275347 /// - static class HotkeysUtil + internal static class HotkeysUtil { #region 系统api + [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] - static extern bool RegisterHotKey(IntPtr hWnd, int id, HotkeyModifiers fsModifiers, uint vk); + private static extern bool RegisterHotKey(IntPtr hWnd, int id, HotkeyModifiers fsModifiers, uint vk); [DllImport("user32.dll")] - static extern bool UnregisterHotKey(IntPtr hWnd, int id); - #endregion + private static extern bool UnregisterHotKey(IntPtr hWnd, int id); + + #endregion 系统api public static IntPtr hwnd; + public static void InitialHook(Window window) { hwnd = new WindowInteropHelper(window).Handle; var _hwndSource = HwndSource.FromHwnd(hwnd); _hwndSource.AddHook(WndProc); } + /// /// 注册快捷键 /// @@ -49,7 +53,7 @@ namespace STranslate.Utils /// /// 快捷键消息处理 /// - static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) + private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WM_HOTKEY) { @@ -76,19 +80,18 @@ namespace STranslate.Utils } } - - const int WM_HOTKEY = 0x312; - static int keyid = 10; - static Dictionary keymap = new Dictionary(); + private const int WM_HOTKEY = 0x312; + private static int keyid = 10; + private static Dictionary keymap = new Dictionary(); public delegate void HotKeyCallBackHanlder(); } - enum HotkeyModifiers + internal enum HotkeyModifiers { MOD_ALT = 0x1, MOD_CONTROL = 0x2, MOD_SHIFT = 0x4, MOD_WIN = 0x8 } -} +} \ No newline at end of file diff --git a/STranslate/Utils/HttpUtil.cs b/STranslate/Utils/HttpUtil.cs index 9245a8f..b858d0d 100644 --- a/STranslate/Utils/HttpUtil.cs +++ b/STranslate/Utils/HttpUtil.cs @@ -1,9 +1,7 @@ using Newtonsoft.Json; using STranslate.Model; using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using System.Net; using System.Net.Http; using System.Text; @@ -40,7 +38,6 @@ namespace STranslate.Utils /// public static async Task PostAsync(string url, string req) { - using (var client = new HttpClient()) { var content = new StringContent(req, Encoding.UTF8, "application/json"); @@ -48,10 +45,11 @@ namespace STranslate.Utils var respContent = await client.PostAsync(url, content); string respStr = await respContent.Content.ReadAsStringAsync(); -; + ; return respStr; } } + /// /// 异步Get请求 /// @@ -76,4 +74,4 @@ namespace STranslate.Utils } } } -} +} \ No newline at end of file diff --git a/STranslate/Utils/KeyboardUtil.cs b/STranslate/Utils/KeyboardUtil.cs index 66653af..cf4959c 100644 --- a/STranslate/Utils/KeyboardUtil.cs +++ b/STranslate/Utils/KeyboardUtil.cs @@ -1,19 +1,15 @@ using System; -using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.InteropServices; using System.Security; using System.Security.Permissions; -using System.Text; -using System.Threading.Tasks; using System.Windows.Input; namespace STranslate.Utils { - - #region Operate Mouse Keyboard + //引用自: https://www.cnblogs.com/sixty/archive/2009/08/09/1542210.html /// /// Native methods @@ -21,14 +17,18 @@ namespace STranslate.Utils internal static class NativeMethods { //User32 wrappers cover API's used for Mouse input + #region User32 + // Two special bitmasks we define to be able to grab // shift and character information out of a VKey. internal const int VKeyShiftMask = 0x0100; + internal const int VKeyCharMask = 0x00FF; // Various Win32 constants internal const int KeyeventfExtendedkey = 0x0001; + internal const int KeyeventfKeyup = 0x0002; internal const int KeyeventfScancode = 0x0008; @@ -59,6 +59,7 @@ namespace STranslate.Utils { [FieldOffset(0)] internal MOUSEINPUT mouseInput; + [FieldOffset(0)] internal KEYBDINPUT keyboardInput; }; @@ -113,10 +114,9 @@ namespace STranslate.Utils [DllImport("user32.dll", CharSet = CharSet.Auto)] internal static extern short VkKeyScan(char ch); - #endregion + #endregion User32 } - /// /// Exposes a simple interface to common mouse operations, allowing the user to simulate mouse input. /// @@ -126,6 +126,7 @@ namespace STranslate.Utils Mouse.MoveTo(new Point(100, 100)); Mouse.Click(MouseButton.Left); */ + /// /// public static class Mouse @@ -161,18 +162,23 @@ namespace STranslate.Utils case MouseButton.Left: SendMouseInput(0, 0, 0, NativeMethods.SendMouseInputFlags.LeftDown); break; + case MouseButton.Right: SendMouseInput(0, 0, 0, NativeMethods.SendMouseInputFlags.RightDown); break; + case MouseButton.Middle: SendMouseInput(0, 0, 0, NativeMethods.SendMouseInputFlags.MiddleDown); break; + case MouseButton.XButton1: SendMouseInput(0, 0, NativeMethods.XButton1, NativeMethods.SendMouseInputFlags.XDown); break; + case MouseButton.XButton2: SendMouseInput(0, 0, NativeMethods.XButton2, NativeMethods.SendMouseInputFlags.XDown); break; + default: throw new InvalidOperationException("Unsupported MouseButton input."); } @@ -242,18 +248,23 @@ namespace STranslate.Utils case MouseButton.Left: SendMouseInput(0, 0, 0, NativeMethods.SendMouseInputFlags.LeftUp); break; + case MouseButton.Right: SendMouseInput(0, 0, 0, NativeMethods.SendMouseInputFlags.RightUp); break; + case MouseButton.Middle: SendMouseInput(0, 0, 0, NativeMethods.SendMouseInputFlags.MiddleUp); break; + case MouseButton.XButton1: SendMouseInput(0, 0, NativeMethods.XButton1, NativeMethods.SendMouseInputFlags.XUp); break; + case MouseButton.XButton2: SendMouseInput(0, 0, NativeMethods.XButton2, NativeMethods.SendMouseInputFlags.XUp); break; + default: throw new InvalidOperationException("Unsupported MouseButton input."); } @@ -342,6 +353,7 @@ namespace STranslate.Utils Keyboard.Type("hello, capitalized world"); Keyboard.Release(Key.LeftShift); */ + /// /// public static class KeyboardUtil @@ -418,7 +430,7 @@ namespace STranslate.Utils } } - #endregion + #endregion Public Members #region Private Members @@ -493,7 +505,7 @@ namespace STranslate.Utils // CTRL keys on the right-hand side of the keyboard; the INS, DEL, HOME, END, PAGE UP, // PAGE DOWN, and arrow keys in the clusters to the left of the numeric keypad; the NUM LOCK // key; the BREAK (CTRL+PAUSE) key; the PRINT SCRN key; and the divide (/) and ENTER keys in - // the numeric keypad. The extended-key flag is set if the key is an extended key. + // the numeric keypad. The extended-key flag is set if the key is an extended key. // // - docs appear to be incorrect. Use of Spy++ indicates that break is not an extended key. // Also, menu key and windows keys also appear to be extended. @@ -514,11 +526,13 @@ namespace STranslate.Utils Key.Apps, Key.RWin, Key.LWin }; + // Note that there are no distinct values for the following keys: // numpad divide // numpad enter - #endregion + #endregion Private Members } - #endregion -} + + #endregion Operate Mouse Keyboard +} \ No newline at end of file diff --git a/STranslate/Utils/MVVMUtil.cs b/STranslate/Utils/MVVMUtil.cs index 4cb3f94..35d5690 100644 --- a/STranslate/Utils/MVVMUtil.cs +++ b/STranslate/Utils/MVVMUtil.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; using System.ComponentModel; -using System.Linq; using System.Runtime.CompilerServices; -using System.Text; -using System.Threading.Tasks; using System.Windows.Input; namespace STranslate.Utils @@ -15,6 +11,7 @@ namespace STranslate.Utils public class BaseVM : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; + protected void UpdateProperty(ref T properValue, T newValue, [CallerMemberName] string properName = "") { if (object.Equals(properValue, newValue)) @@ -28,6 +25,7 @@ namespace STranslate.Utils this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } + /// /// Command /// @@ -65,6 +63,7 @@ namespace STranslate.Utils public abstract class SingletonMode where T : class { public static T _Instance; + public static T Instance() { Type type = typeof(T); @@ -78,4 +77,4 @@ namespace STranslate.Utils } } } -} +} \ No newline at end of file diff --git a/STranslate/Utils/TranslateUtil.cs b/STranslate/Utils/TranslateUtil.cs index 5e1f35d..b230284 100644 --- a/STranslate/Utils/TranslateUtil.cs +++ b/STranslate/Utils/TranslateUtil.cs @@ -2,9 +2,7 @@ using STranslate.Model; using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Net; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; @@ -15,6 +13,7 @@ namespace STranslate.Utils public class TranslateUtil { private static readonly string _url = "http://127.0.0.1:8000/translate"; + public static string Translate(string input, LanguageEnum source, LanguageEnum target) { var req = new DeeplReq() @@ -109,6 +108,7 @@ namespace STranslate.Utils throw ex; } } + // 计算MD5值 public static string EncryptString(string str) { @@ -127,6 +127,7 @@ namespace STranslate.Utils // 返回加密的字符串 return sb.ToString(); } + /// /// 枚举信息 /// @@ -143,4 +144,4 @@ namespace STranslate.Utils return dict; } } -} +} \ No newline at end of file diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index a5f94ef..70a1fc7 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -3,8 +3,6 @@ using STranslate.Utils; using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Input; @@ -17,6 +15,7 @@ namespace STranslate.ViewModel private static Dictionary LanguageEnumDict { get => TranslateUtil.GetEnumList(); } private string Text; + public MainVM() { //初始化界面参数 @@ -38,7 +37,6 @@ namespace STranslate.ViewModel Clipboard.SetText(OutputTxt); }); - TranslateCmd = new RelayCommand((_) => { return string.IsNullOrEmpty(InputTxt) ? false : true; @@ -95,6 +93,5 @@ namespace STranslate.ViewModel private static readonly MainVM _Instance = new MainVM(); public static MainVM Instance { get => _Instance; } - } -} +} \ No newline at end of file diff --git a/STranslate/packages.config b/STranslate/packages.config index baae49f..78498af 100644 --- a/STranslate/packages.config +++ b/STranslate/packages.config @@ -1,6 +1,6 @@  - - - + + + \ No newline at end of file