FrmBase.cs
18.3 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Reflection;
namespace ServiceManager
{
using Comm;
using System.IO;
public partial class FrmBase : Form
{
protected TextBox RTBMessage = null;
public FrmBase()
{
InitializeComponent();
}
private void FrmBase_Load(object sender, EventArgs e)
{
AddHelpInfo(this);
AddF1Event(this);
}
private void FrmBase_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.RTBMessage != null)
this.RTBMessage.Text = String.Empty;
}
private void AddHelpInfo(Control ctl)
{
for (int i = 0; i < ctl.Controls.Count; i++)
{
switch (ctl.Controls[i].GetType().FullName)
{
case "System.Windows.Forms.Panel":
AddHelpInfo(ctl.Controls[i]);
break;
case "System.Windows.Forms.TabControl":
AddHelpInfo(ctl.Controls[i]);
break;
case "System.Windows.Forms.TabPage":
AddHelpInfo(ctl.Controls[i]);
break;
default:
ctl.Controls[i].Enter += new System.EventHandler(this.Ctl_Enter);
break;
}
}
}
/// <summary>
/// 控件Enter 事件处理函数,用来指定控件对应的帮助信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void Ctl_Enter(object sender, System.EventArgs e)
{
}
/// <summary>
/// 设置Buggit帮助文档的F1事件
/// </summary>
/// <param name="cc"></param>
public void AddF1Event(Control ctl_Param)
{
ctl_Param.KeyDown += new KeyEventHandler(ctl_Param_KeyDown);
foreach (Control ctl_Tmp in ctl_Param.Controls)
{
if (!(ctl_Tmp is TabPage))
this.AddF1Event(ctl_Tmp);
}
}
/// <summary>
/// 提取Buggit子窗口属性
/// </summary>
/// <param name="cc"></param>
private void ctl_Param_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
Control o_Tmp = (Control)sender;
if (o_Tmp is Form)
{
//str_Location = o_Tmp.Name;
}
else
{
if ((o_Tmp is TreeView) && (o_Tmp.Name == "TreMenu"))
{
TreeView o_Tvw = (TreeView)sender;
string str_BaseLocation = o_Tvw.SelectedNode.Text;
ShowChm(str_BaseLocation);
}
else
{
while (!(o_Tmp is Form))
{
o_Tmp = o_Tmp.Parent;
}
string str_Location = o_Tmp.Text;
ShowChm(str_Location);
}
}
}
}
/// <summary>
/// 索引Buggit中文使用手册
/// </summary>
private void ShowChm(string indexName)
{
string helpfile = Application.StartupPath + @"\\MyCMMI中文使用手册.chm";
HelpNavigator navigator = HelpNavigator.KeywordIndex;
Help.ShowHelp(this, helpfile, navigator, indexName);
}
/// <summary>
/// 设置子控件的只读属性
/// </summary>
/// <param name="cc">控件</param>
public void SetFormReadOnly(Control cc)
{
for (int i = 0; i < cc.Controls.Count; i++)
{
switch (cc.Controls[i].GetType().Name)
{
case "TabControl":
SetFormReadOnly(cc.Controls[i]);
break;
case "Panel":
SetFormReadOnly(cc.Controls[i]);
break;
case "RadioButton":
((RadioButton)cc.Controls[i]).Enabled = false;
break;
case "TabPage":
SetFormReadOnly(cc.Controls[i]);
break;
case "TextBox":
((TextBox)cc.Controls[i]).ReadOnly = true;
break;
case "CheckBox":
((CheckBox)cc.Controls[i]).Enabled = false;
break;
case "ComboBox":
((ComboBox)cc.Controls[i]).Enabled = false;
break;
case "DateTimePicker":
((DateTimePicker)cc.Controls[i]).Enabled = false;
break;
case "GroupBox":
SetFormReadOnly(cc.Controls[i]);
break;
default:
//cc.Controls[i].Enabled = false;
break;
}
}
}
#region 换语言
private string formName;
string str_LResourse = "zh-CN"; //默认为中文
public void SelectLanguage(string kkk)
{
}
/// <summary>
/// 获取字符串相关的资源文件
/// </summary>
public ResourceManager GetCurrentCulture()
{
ResourceManager rm = new ResourceManager("MyCMMI.App.Properties.LResource", Assembly.GetExecutingAssembly());//从指定的资源文件中获取资源
return rm;
}
/// <summary>
/// 获取图像文件相关的资源文件
/// </summary>
public ResourceManager GetCurrentCultureForBitmap()
{
ResourceManager rm = new ResourceManager("MyCMMI.App.Properties.Resources", Assembly.GetExecutingAssembly());
return rm;
}
/// <summary>
///根据传来的图像控件的名字返回该控件在指定语言区域的图像
/// </summary>
/// <param name="strObjectId"></param>
/// <returns></returns>
public System.Drawing.Bitmap GetImage(string strObjectId)
{
ResourceManager rm = GetCurrentCultureForBitmap();//获取指定图像资源文件
object obj = rm.GetObject(strObjectId);
return (System.Drawing.Bitmap)obj;
}
/// <summary>
/// 根据传入的控件名字返回该控件在指定语言区域的TEXT
/// </summary>
/// <param name="strId"></param>
/// <returns></returns>
public string getMsg(string strId)
{
string currentLanguage = "";
try
{
ResourceManager rm = GetCurrentCulture();//得到当前语言区域的指定资源
//
//通过语言种类变量设置当前线程的语言区域,若是注释该行,则根据操作系统自身区域语言设置显示值
//
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(str_LResourse);
CultureInfo ci = Thread.CurrentThread.CurrentCulture;//获取当前线程所设置的语言区域
currentLanguage = rm.GetString(strId, ci);//通过所选语言区域匹配资源文件,根据传入控件名称返回该控件相应的TEXT
}
catch
{
currentLanguage = "";//未找到匹配字符时返回默认的字符
}
return currentLanguage;
}
/// <summary>
/// 用于MessageBox根据传入ID值获取MessageBox显示的字符串
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
public string MBSelectLang(String ID)
{
ID = getMsg(ID);
return ID;
}
/// <summary>
/// 用于事件触发TextBox显示相应内容
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
public string TBSelectLang(String ID)
{
ID = getMsg(formName + ID);
return ID;
}
/// <summary>
/// 根据传入的控件设置该控件内的所有控件TEXT属性,若AppGlobal为null,则默认显示中文
/// </summary>
/// <param name="control"></param>
public void SetFormLanguage(System.Windows.Forms.Control control)
{
if (control.GetType().BaseType.Name == "Form" || control.GetType().BaseType.Name == "FrmBase")//判断控件是否为Form类型
{
formName = control.Name;
control.Text = getMsg(control.Name);//根据传入控件名字返回该控件TEXT
}
//对所有控件做循环,并判断,符合条件的控件能够将名字返回为该控件特定区域的TEXT
for (int i = 0; i < control.Controls.Count; i++)
{
//MessageBox.Show(control.Controls[i].GetType().ToString());
switch (control.Controls[i].GetType().Name)//根据控件的类型判断能否实现功能
{
case "Label":
case "Button":
case "CheckBox":
case "LinkLabel":
case "RadioButton":
//根据传入的控件名字返回该控件在制定语言区域的TEXT属性
control.Controls[i].Text = getMsg(formName + control.Controls[i].Name);
break;
case "Panel":
SetFormLanguage(control.Controls[i]);
break;
case "GroupBox":
control.Controls[i].Text = getMsg(formName + control.Controls[i].Name);
SetFormLanguage(control.Controls[i]);
break;
case "TabControl":
TabControl tbc = (TabControl)control.Controls[i];
for (int j = 0; j < tbc.TabCount; j++)
{
tbc.TabPages[j].Text = getMsg(formName + tbc.TabPages[j].Name);
SetFormLanguage(tbc.TabPages[j]);//返回该页所拥有控件,并设置其语言
}
break;
case "DataGridView":
DataGridView dgv = (DataGridView)control.Controls[i];
for (int j = 0; j < dgv.ColumnCount; j++)
{
dgv.Columns[j].HeaderText = getMsg(formName + dgv.Columns[j].Name);
}
break;
case "MenuStrip":
MenuStrip ms = (MenuStrip)control.Controls[i];
ToolStripMenuItem menu = new ToolStripMenuItem();
for (int k = 0; k < ms.Items.Count; k++)
{
ms.Items[k].Text = getMsg(formName + ms.Items[k].Name);
menu = (ToolStripMenuItem)ms.Items[k];
for (int l = 0; l < menu.DropDownItems.Count; l++)
{
menu.DropDownItems[l].Text = getMsg(formName + menu.DropDownItems[l].Name);
}
}
break;
//case "TreeView":
// TreeView tv = (TreeView)control.Controls[i];
// for (int m = 0; m < tv.Nodes.Count; m++)
// {
// tv.Nodes[m].Text = getMsg(formName + tv.Nodes[m].Name);
// for (int n = 0; n < tv.Nodes[m].Nodes.Count; n++)
// {
// tv.Nodes[m].Nodes[n].Text = getMsg(formName + tv.Nodes[m].Nodes[n].Name);
// }
// }
// break;
case "ComboBox":
ComboBox cb = (ComboBox)control.Controls[i];
if (cb.Items.Count > 0 || cb.DataSource != null)
{
break;
}
else
{
string cbName = getMsg(formName + cb.Name);
char[] delimiterChars = { '|' };//将' '去处,修改 by 倪豪 2007-08-14
string[] strarr_cbName = cbName.Split(delimiterChars);
cb.Items.AddRange(strarr_cbName);
break;
}
case "StatusStrip":
StatusStrip ss = (StatusStrip)control.Controls[i];
for (int x = 0; x < ss.Items.Count; x++)
{
ss.Items[x].Text = getMsg(formName + ss.Items[x].Name);
}
break;
case "ToolStrip":
ToolStrip ts = (ToolStrip)control.Controls[i];
for (int w = 0; w < ts.Items.Count; w++)
{
ts.Items[w].Text = getMsg(formName + ts.Items[w].Name);
ts.Items[w].ToolTipText = getMsg(formName + ts.Items[w].Name + "Tip");
//以下内容暂时不需要 By wsl
//if (str_LResourse == "en-US")
// ts.Items[w].Image = GetImage(formName + ts.Items[w].Name + "EN");
//if (str_LResourse == "zh-CN")
// ts.Items[w].Image = GetImage(formName + ts.Items[w].Name + "CH");
}
break;
case "PictureBox":
PictureBox pb = (PictureBox)control.Controls[i];
if (str_LResourse == "en-US")
pb.Image = GetImage(formName + pb.Name + "EN");
if (str_LResourse == "zh-CN")
pb.Image = GetImage(formName + pb.Name + "CH");
break;
case "ListView":
ListView lv = (ListView)control.Controls[i];
for (int v = 0; v < lv.Columns.Count; v++)
{
lv.Columns[v].Text = getMsg(formName + lv.Columns[v].Name);
}
break;
default:
break;
}
}
}
#region 尚未登录时调用
/// </summary>
/// 根据语言种类变量设置str_LResourse
/// </summary>
public void GetCurrLanguageWithoutAppGlobal(string str_Language)
{
if (str_Language == Const.LANGUAGE_ENGLISH)
{
str_LResourse = "en-US";
}
if (str_Language == Const.LANGUAGE_CHINESE)
{
str_LResourse = "zh-CN";
}
if (str_Language == Const.LANGUAGE_JAPANESE)
{
str_LResourse = "ja-JP";
}
}
/// <summary>
/// 根据传入的语言设置该控件内的所有控件TEXT属性,当AppGlobal为null时使用
/// </summary>
/// <param name="str_Language">当前语言</param>
public void SetLanguageWithoutAppGlobal(string str_Language, System.Windows.Forms.Control control)
{
GetCurrLanguageWithoutAppGlobal(str_Language);
SetFormLanguage(control);
}
#endregion
#region 登录时调用
/// </summary>
/// 根据语言种类变量设置str_LResourse
/// </summary>
public void GetCurrLanguage()
{
//if (ServiceManagerGlobal.g_personalInfo.Language == Const.LANGUAGE_ENGLISH)
//{
// str_LResourse = "en-US";
//}
//if (ServiceManagerGlobal.g_personalInfo.Language == Const.LANGUAGE_CHINESE)
//{
// str_LResourse = "zh-CN";
//}
}
/// <summary>
/// 根据传入的控件设置该控件内的所有控件TEXT属性,若AppGlobal为null,则默认显示中文
/// </summary>
/// <param name="control"></param>
public void SetLanguage(System.Windows.Forms.Control control)
{
GetCurrLanguage();
SetFormLanguage(control);
}
#endregion
#endregion
#region 导出常用方法
/// <summary>
/// 检查当前给定路径是否存在文件
/// </summary>
/// <param name="fname">路径</param>
public bool ValidFile(string fname)
{
if (!String.IsNullOrEmpty(fname.Trim()))
{
if (File.Exists(fname))
{
return true;
}
}
return false;
}
/// <summary>
/// 根据给定VSS路径,二进制流在本地新建文档,并返回本地路径
/// </summary>
/// <param name="vssPath"></param>
/// <param name="docByte"></param>
public string CreateLocalFiles(string vssPath, byte[] docByte)
{
string temSystemPath = Path.GetTempPath();
string[] file = vssPath.Split('\\');
int length = file.Length;
string fileName = file[length - 1].ToString();
string temPath = temSystemPath + "R" + fileName;
FileStream myStream = new FileStream(temPath, FileMode.Create, FileAccess.Write);
myStream.Write(docByte, 0, (int)docByte.Length);
myStream.Close();
return temPath;
}
#endregion
}
}