2
2
using System . Collections . Generic ;
3
3
using System . ComponentModel ;
4
4
using System . Data ;
5
+ using System . Diagnostics ;
5
6
using System . Drawing ;
6
7
using System . IO ;
7
8
using System . IO . Compression ;
14
15
namespace EasyTransaction
15
16
{
16
17
public partial class FrmMain : Form
17
- {
18
- Uri googleCNURL = new Uri ( "http://translate.google.cn/" ) ;
18
+ {
19
19
TranslateTool tool ;
20
20
BatchTran tran ;
21
+ Stopwatch workTimeWatch ;
22
+
21
23
public FrmMain ( )
22
24
{
23
25
InitializeComponent ( ) ;
24
- tool = new TranslateTool ( wb ) ;
25
- tran = new BatchTran ( tool ) ;
26
+ tool = new TranslateTool ( wb , "http://translate.google.cn" ) ;
27
+ tran = new BatchTran ( tool )
28
+ {
29
+ FromLanguage = "en" ,
30
+ ToLanguage = "zh-Cn" ,
31
+ } ;
26
32
tran . BatchMessagePushed += Tran_BatchMessagePushed ;
33
+ tran . ProcessChanged += Tran_ProcessChanged ;
34
+ tran . Completed += Tran_Completed ;
27
35
IsReady ( false ) ;
36
+ txtResult . MaxLength = int . MaxValue ;
37
+ statusStrip1 . Visible = false ;
28
38
29
39
}
30
40
41
+ private void Tran_Completed ( WorkProcessEventArgs args )
42
+ {
43
+ this . Invoke ( new Action ( ( ) =>
44
+ {
45
+ DoWork ( false ) ;
46
+ } ) ) ;
47
+ }
48
+
49
+ /// <summary>
50
+ /// 翻译进度变化
51
+ /// </summary>
52
+ /// <param name="args"></param>
53
+ private void Tran_ProcessChanged ( WorkProcessEventArgs args )
54
+ {
55
+ if ( workTimeWatch == null )
56
+ {
57
+ workTimeWatch = new Stopwatch ( ) ;
58
+ workTimeWatch . Start ( ) ;
59
+ }
60
+ if ( args . SumCount == ( args . CompletedCount + args . FailCount ) )
61
+ {
62
+ workTimeWatch . Stop ( ) ;
63
+ }
64
+ else if ( args . CompletedCount == 0 && args . FailCount == 0 )
65
+ {
66
+ workTimeWatch . Restart ( ) ;
67
+ }
68
+ this . Invoke ( new Action ( ( ) =>
69
+ {
70
+ toolProBar . Minimum = 0 ;
71
+ toolProBar . Maximum = args . SumCount ;
72
+ toolProBar . Value = args . CompletedCount ;
73
+ lblProcess . Text = string . Format ( "{0:P0}" , args . CompletionRatio ) ;
74
+ var info = string . Format ( "翻译进度:已完成{0:P2},{1:N0}/{2:N0},失败数:{3}." , args . CompletionRatio , args . CompletedCount , args . SumCount , args . FailCount ) ;
75
+ if ( args . CompletedCount > 0 )
76
+ {
77
+ info += string . Format ( "速度:{0:N2}ms/行." , workTimeWatch . ElapsedMilliseconds / args . CompletedCount ) ;
78
+ }
79
+ if ( ! string . IsNullOrWhiteSpace ( args . Message ) )
80
+ {
81
+ info += "消息:" + args . Message ;
82
+ }
83
+ toolLableStatus . Text = info ;
84
+ if ( args . CompletedCount == 0 )
85
+ {
86
+ lkSaveFile . Text = "Open:" + Path . GetFileName ( tran . GetSaveFileName ( ) ) ;
87
+ }
88
+ } ) ) ;
89
+ }
90
+
91
+ /// <summary>
92
+ /// 翻译出来消息接收
93
+ /// </summary>
94
+ /// <param name="args"></param>
31
95
private void Tran_BatchMessagePushed ( MessageEventArgs args )
32
96
{
33
- lblBatchResult . Invoke ( new Action ( ( ) =>
97
+ this . Invoke ( new Action ( ( ) =>
34
98
{
35
- lblBatchResult . Text = "翻译进度:" + args . Message ;
99
+ showLog ( args . Message ) ;
36
100
} ) ) ;
37
101
}
102
+ /// <summary>
103
+ /// 显示进度信息
104
+ /// </summary>
105
+ /// <param name="msg"></param>
106
+ private void showLog ( string msg )
107
+ {
108
+ txtResult . AppendText ( string . Format ( "{0:HH:mm:ss}:{1}\r \n " , DateTime . Now , msg ) ) ;
109
+ }
38
110
39
111
private void Form1_Load ( object sender , EventArgs e )
40
112
{
@@ -43,6 +115,11 @@ private void Form1_Load(object sender, EventArgs e)
43
115
private void wb_Navigated ( object sender , WebBrowserNavigatedEventArgs e )
44
116
{
45
117
gpWeb . Text = wb . DocumentTitle ;
118
+ showLog ( "页面地址:" + wb . Document . Url . ToString ( ) ) ;
119
+ if ( txtUrl . Focused == false )
120
+ {
121
+ txtUrl . Text = wb . Document . Url . ToString ( ) ;
122
+ }
46
123
}
47
124
private void wb_DocumentCompleted ( object sender , WebBrowserDocumentCompletedEventArgs e )
48
125
{
@@ -64,7 +141,8 @@ private void IsReady(bool yes)
64
141
private void btnRunTest_Click ( object sender , EventArgs e )
65
142
{
66
143
var text = txtTestText . Text . Trim ( ) ;
67
- txtTestResult . Text = tool . Translate ( text ) ;
144
+ var result = tool . Translate ( text , txtFromLan . Text . Trim ( ) , txtToLan . Text . Trim ( ) ) ;
145
+ showLog ( string . Format ( "测试结果:\r \n 成功:{0}\r \n ,结果:{1}\r \n 错误信息:{2} " , result . IsSuccess , result . Result , result . ErrorMessage ) ) ;
68
146
}
69
147
70
148
private void btnSelectFile_Click ( object sender , EventArgs e )
@@ -81,11 +159,20 @@ private void btnSelectFile_Click(object sender, EventArgs e)
81
159
82
160
private void btnAction_Click ( object sender , EventArgs e )
83
161
{
84
- if ( ( btnAction . Tag as string ) == "runing" )
162
+ DoWork ( ( btnAction . Tag as string ) != "runing" ) ;
163
+ }
164
+
165
+ private void DoWork ( bool needStart )
166
+ {
167
+ if ( ! needStart )
85
168
{
86
169
tran . Stop ( ) ;
87
170
btnAction . Tag = "stoped" ;
88
171
btnAction . Text = "开始" ;
172
+ statusStrip1 . Visible = true ;
173
+ btnSelectFile . Enabled = true ;
174
+ txtFromLan . Enabled = true ;
175
+ txtToLan . Enabled = true ;
89
176
}
90
177
else
91
178
{
@@ -96,15 +183,101 @@ private void btnAction_Click(object sender, EventArgs e)
96
183
}
97
184
try
98
185
{
186
+ tran . FromLanguage = txtFromLan . Text . Trim ( ) ;
187
+ tran . ToLanguage = txtToLan . Text . Trim ( ) ;
188
+ tran . Interval = ( int ) numTranslateInv . Value ;
99
189
tran . Start ( openFileDialog1 . FileName ) ;
100
190
btnAction . Tag = "runing" ;
101
191
btnAction . Text = "停止" ;
192
+ statusStrip1 . Visible = true ;
193
+ lblProcess . Text = "0.00%" ;
194
+ btnSelectFile . Enabled = false ;
195
+ lblProcess . Visible = true ;
196
+ txtFromLan . Enabled = false ;
197
+ txtToLan . Enabled = false ;
102
198
}
103
199
catch ( Exception ex )
104
200
{
105
201
MessageBox . Show ( ex . Message ) ;
106
202
}
107
203
}
108
204
}
205
+
206
+ private void numTranslateInv_ValueChanged ( object sender , EventArgs e )
207
+ {
208
+ this . tran . Interval = ( int ) numTranslateInv . Value ;
209
+ }
210
+
211
+ private void lkSaveFile_LinkClicked ( object sender , LinkLabelLinkClickedEventArgs e )
212
+ {
213
+ var filename = tran . GetSaveFileName ( ) ;
214
+ if ( string . IsNullOrWhiteSpace ( filename ) )
215
+ {
216
+ return ;
217
+ }
218
+ Process . Start ( filename ) ;
219
+ }
220
+
221
+
222
+ private void txtResultSaveDir_Leave ( object sender , EventArgs e )
223
+ {
224
+ if ( checkSavePath ( ) )
225
+ {
226
+ tran . SaveDir = txtResultSaveDir . Text . Trim ( ) ;
227
+ } else
228
+ {
229
+ txtResultSaveDir . Focus ( ) ;
230
+ txtResultSaveDir . SelectAll ( ) ;
231
+ }
232
+ }
233
+
234
+ /// <summary>
235
+ /// 检测存储位置是否存在
236
+ /// </summary>
237
+ private bool checkSavePath ( )
238
+ {
239
+ var dir = txtResultSaveDir . Text . Trim ( ) ;
240
+ if ( Directory . Exists ( dir ) )
241
+ {
242
+ return true ;
243
+ }
244
+ if ( DialogResult . No == MessageBox . Show ( "翻译结果存储目录不存在,是否自动创建" , "提示" , MessageBoxButtons . YesNo ) )
245
+ {
246
+ return false ;
247
+ }
248
+ try
249
+ {
250
+ Directory . CreateDirectory ( dir ) ;
251
+ }
252
+ catch ( Exception ex )
253
+ {
254
+ MessageBox . Show ( "创建目录失败," + ex . Message + ",可手工新建目录" ) ;
255
+ return false ;
256
+ }
257
+
258
+ return true ;
259
+ }
260
+
261
+ private void btnChangeURL_Click ( object sender , EventArgs e )
262
+ {
263
+ var url = txtUrl . Text . Trim ( ) ;
264
+ if ( url == "" )
265
+ {
266
+ return ;
267
+ }
268
+ if ( ! url . StartsWith ( "https://" ) && ! url . StartsWith ( "http://" ) )
269
+ {
270
+ url = "http://" + url ;
271
+ }
272
+ try
273
+ {
274
+ var u = new Uri ( url ) ;
275
+ tool . GoogleTranslateWeb = u ;
276
+ }
277
+ catch ( Exception ex )
278
+ {
279
+ MessageBox . Show ( ex . Message ) ;
280
+ }
281
+ }
109
282
}
110
283
}
0 commit comments