This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainWindow.xaml.cs
341 lines (304 loc) · 11.7 KB
/
MainWindow.xaml.cs
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
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Web;
using System.Windows;
using System.Windows.Input;
using ToggleSwitch;
using WK.Libraries.SharpClipboardNS;
namespace CopyPlusPlus
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//Is the translate API being changed or not
#pragma warning disable CA2211 // Non-constant fields should not be visible
public static bool changeStatus = false;
#pragma warning restore CA2211 // Non-constant fields should not be visible
private bool switch1Check;
private bool switch2Check;
private bool switch3Check;
private bool switch4Check;
public SharpClipboard clipboard;
public MainWindow()
{
InitializeComponent();
InitializeClipboardMonitor();
switch1Check = Properties.Settings.Default.Switch1Check;
switch2Check = Properties.Settings.Default.Switch2Check;
switch3Check = Properties.Settings.Default.Switch3Check;
switch4Check = Properties.Settings.Default.Switch4Check;
if (switch1Check == true)
{
switch1.IsChecked = true;
}
if (switch2Check == true)
{
switch2.IsChecked = true;
}
if (switch3Check == true)
{
switch3.IsChecked = true;
}
if (switch4Check == true)
{
switch4.IsChecked = true;
}
}
//Initializes a new instance of SharpClipboard
public void InitializeClipboardMonitor()
{
clipboard = new SharpClipboard();
//Attach your code to the ClipboardChanged event to listen to cuts/copies
clipboard.ClipboardChanged += ClipboardChanged;
//disable issueing ClipboardChanged event when start
clipboard.ObserveLastEntry = false;
//disable monitoring files
clipboard.ObservableFormats.Files = false;
//disable monitoring images
clipboard.ObservableFormats.Images = false;
}
private void ClipboardChanged(Object sender, SharpClipboard.ClipboardChangedEventArgs e)
{
// Is the content copied of text type?
if (e.ContentType == SharpClipboard.ContentTypes.Text)
{
// Get the cut/copied text.
string text = e.Content.ToString();
if (switch1Check == true || switch2Check == true)
{
for (int counter = 0; counter < text.Length - 1; counter++)
{
if (switch1Check == true)
{
if (text[counter + 1].ToString() == "\r")
{
if (text[counter].ToString() == ".")
{
continue;
}
if (text[counter].ToString() == "。")
{
continue;
}
text = text.Remove(counter + 1, 2);
}
}
if (switch2Check == true)
{
if (text[counter].ToString() == " ")
{
text = text.Remove(counter, 1);
}
}
}
}
if (switch3Check == true)
{
if (changeStatus == false)
{
string appId = Properties.Settings.Default.AppID;
string secretKey = Properties.Settings.Default.SecretKey;
if (appId == "none" || secretKey == "none")
{
//MessageBox.Show("请先设置翻译接口", "Copy++");
Show_InputAPIWindow();
}
else
{
text = BaiduTrans(appId, secretKey, text);
if (switch4Check == true)
{
//MessageBox.Show(text);
TranslateResult translateResult = new TranslateResult();
translateResult.textBox.Text = text;
//translateResult.WindowStartupLocation = WindowStartupLocation.Manual;
//translateResult.Left = System.Windows.Forms.Control.MousePosition.X;
//translateResult.Top = System.Windows.Forms.Control.MousePosition.Y;
translateResult.Show();
//var left = translateResult.Left;
//var top = translateResult.Top;
}
}
}
}
//Prevent loop
clipboard.StopMonitoring();
Clipboard.SetText(text);
Clipboard.Flush();
InitializeClipboardMonitor();
}
// If the cut/copied content is complex, use 'Other'.
else if (e.ContentType == SharpClipboard.ContentTypes.Other)
{
//do nothing
// Do something with 'clipboard.ClipboardObject' or 'e.Content' here...
}
}
private void Todolist_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show("欢迎赞助我,加快开发进度!");
}
private void Github_Click(object sender, RoutedEventArgs e)
{
Process.Start("explorer.exe", "https://github.com/CopyPlusPlus/CopyPlusPlus");
}
private static string BaiduTrans(string appId, string secretKey, string q = "apple")
{
//q为原文
// 源语言
string from = "en";
// 目标语言
string to = "zh";
// 改成您的APP ID
//appId = NoAPI.baidu_id;
// 改成您的密钥
//secretKey = NoAPI.baidu_secretKey;
Random rd = new Random();
string salt = rd.Next(100000).ToString();
string sign = EncryptString(appId + q + salt + secretKey);
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
url += "q=" + HttpUtility.UrlEncode(q);
url += "&from=" + from;
url += "&to=" + to;
url += "&appid=" + appId;
url += "&salt=" + salt;
url += "&sign=" + sign;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = null;
request.Timeout = 6000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
//read json(retString) as a object
var result = JsonSerializer.Deserialize<Rootobject>(retString);
return result.trans_result[0].dst;
}
// 计算MD5值
public static string EncryptString(string str)
{
MD5 md5 = MD5.Create();
// 将字符串转换成字节数组
byte[] byteOld = Encoding.UTF8.GetBytes(str);
// 调用加密方法
byte[] byteNew = md5.ComputeHash(byteOld);
// 将加密结果转换为字符串
StringBuilder sb = new StringBuilder();
foreach (byte b in byteNew)
{
// 将字节转换成16进制表示的字符串,
sb.Append(b.ToString("x2"));
}
// 返回加密的字符串
return sb.ToString();
}
//打开翻译按钮
private void TranslateSwitch_Check(object sender, RoutedEventArgs e)
{
string appId = Properties.Settings.Default.AppID;
string secretKey = Properties.Settings.Default.SecretKey;
if (appId == "none" || secretKey == "none")
{
//MessageBox.Show("请先设置翻译接口", "Copy++");
Show_InputAPIWindow();
}
switch3Check = true;
}
//点击"翻译"文字
private void TranslateText_Clicked(object sender, MouseButtonEventArgs e)
{
Show_InputAPIWindow(false);
}
private void Show_InputAPIWindow(bool showMessage = true)
{
KeyInput keyinput = new KeyInput
{
Owner = this
};
keyinput.Show();
if (showMessage == true)
{
MessageBox.Show(keyinput, "请先设置翻译接口", "Copy++");
}
changeStatus = true;
}
private void SwitchUncheck(object sender, RoutedEventArgs e)
{
HorizontalToggleSwitch switchButton = sender as HorizontalToggleSwitch;
string switchName = switchButton.Name;
if (switchName == "switch1")
{
switch1Check = false;
}
if (switchName == "switch2")
{
switch2Check = false;
}
if (switchName == "switch3")
{
switch3Check = false;
}
if (switchName == "switch4")
{
switch4Check = false;
}
}
private void SwitchCheck(object sender, RoutedEventArgs e)
{
HorizontalToggleSwitch switchButton = sender as HorizontalToggleSwitch;
string switchName = switchButton.Name;
if (switchName == "switch1")
{
switch1Check = true;
}
if (switchName == "switch2")
{
switch2Check = true;
}
if (switchName == "switch3")
{
switch3Check = true;
}
if (switchName == "switch4")
{
switch4Check = true;
}
}
public static void Switch3Uncheck()
{
//switch3.IsChecked = false;
}
private void ChangeSwitch()
{
if (switch1Check == true)
{
switch1.IsChecked = true;
}
}
private void MainWindow_Closed(object sender, EventArgs e)
{
//记录每个Switch的状态,以便下次打开恢复
Properties.Settings.Default.Switch1Check = switch1Check;
Properties.Settings.Default.Switch2Check = switch2Check;
Properties.Settings.Default.Switch3Check = switch3Check;
Properties.Settings.Default.Switch4Check = switch4Check;
//判断Swith3状态,避免bug
if (Properties.Settings.Default.AppID == "none" || Properties.Settings.Default.SecretKey == "none")
{
Properties.Settings.Default.Switch3Check = false;
}
Properties.Settings.Default.Save();
}
}
}