LanguageWwitchover.cs
37.1 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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
using Asa.FaceControl;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
using Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using System.Xml;
using Control = System.Windows.Controls.Control;
using Font = System.Drawing.Font;
using FontStyle = System.Drawing.FontStyle;
namespace SmartScan.SetControl.WPF
{
public enum FontSizeOption
{
Small,
Medium,
Large,
ExtraLarge
}
public class LanguageWwitchover
{
// 全局字体大小缩放因子
private static double _fontSizeScaleFactor = 1.0;
private class ClsLangText
{
public string Name = "";
public string Text = "";
// 存储原始字体字符串,便于后续解析
public string FontString { get; set; } = "";
// WinForms字体
private Font _winFormsFont;
public Font Font
{
get
{
if (_winFormsFont == null && !string.IsNullOrEmpty(FontString))
{
_winFormsFont = ConvFont(FontString);
}
return _winFormsFont;
}
set { _winFormsFont = value; }
}
// WPF字体属性 - 懒加载
private (string Family, double Size, System.Windows.FontWeight Weight, System.Windows.FontStyle Style)? _wpfFontProps;
public (string Family, double Size, System.Windows.FontWeight Weight, System.Windows.FontStyle Style) WpfFontProperties
{
get
{
if (!_wpfFontProps.HasValue && !string.IsNullOrEmpty(FontString))
{
_wpfFontProps = ConvFontForWpf(FontString);
}
return _wpfFontProps ?? ("Arial", 12, System.Windows.FontWeights.Normal, System.Windows.FontStyles.Normal);
}
}
// 应用字体到WPF元素的辅助方法
public void ApplyFontToWpfElement(System.Windows.FrameworkElement element)
{
var props = WpfFontProperties;
if (element is System.Windows.Controls.TextBlock textBlock)
{
textBlock.FontFamily = new System.Windows.Media.FontFamily(props.Family);
textBlock.FontSize = props.Size * _fontSizeScaleFactor;
textBlock.FontWeight = props.Weight;
textBlock.FontStyle = props.Style;
}
else if (element is System.Windows.Controls.Control control)
{
control.FontFamily = new System.Windows.Media.FontFamily(props.Family);
control.FontSize = props.Size * _fontSizeScaleFactor;
control.FontWeight = props.Weight;
control.FontStyle = props.Style;
}
}
public override string ToString()
{
return Text ?? "";
}
}
public static List<string> Name { get; private set; } = new List<string>();
private static readonly Dictionary<string, Dictionary<string, ClsLangText>> langDict = new Dictionary<string, Dictionary<string, ClsLangText>>();
public static string CurrentLng { get; private set; } = "zh-CN";
private static string filePath;
// 设置全局字体大小缩放因子
public static void SetGlobalFontSizeScale(double scaleFactor)
{
if (scaleFactor <= 0) throw new ArgumentException("Scale factor must be positive");
_fontSizeScaleFactor = scaleFactor;
}
public static void ApplyFontSize(DependencyObject root, FontSizeOption option)
{
double sizeFactor;
switch (option)
{
case FontSizeOption.Small:
sizeFactor = 0.85;
break;
case FontSizeOption.Medium:
sizeFactor = 1.0;
break;
case FontSizeOption.Large:
sizeFactor = 1.2;
break;
case FontSizeOption.ExtraLarge:
sizeFactor = 1.4;
break;
default:
sizeFactor = 1.0;
break;
}
SetGlobalFontSizeScale(sizeFactor);
SetLanguage(root); // 重新应用语言和字体
// 额外的保证,在UI更新后再次执行按钮模板更新
System.Windows.Threading.Dispatcher.CurrentDispatcher.InvokeAsync(() =>
{
AdjustButtonTemplateTextBlocks(root);
}, System.Windows.Threading.DispatcherPriority.Render);
}
private static string lastPath = "";
public static void LoadPath(string path)
{
if(lastPath!=""&&lastPath == path&&langDict.Count>0)
{
return;
}
langDict.Clear();
filePath = path;
string[] files = Directory.GetFiles(path, "*.lngres");
string[] array = files;
foreach (var file in array)
{
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
if (fileNameWithoutExtension.ToLower() == "en-US".ToLower())
{
Name.Add("English");
}
else if (fileNameWithoutExtension.ToLower() == "zh-CN".ToLower())
{
Name.Add("简体中文");
}
else if (fileNameWithoutExtension.ToLower() == "ja-JP".ToLower())
{
Name.Add("日语");
}
langDict.Add(fileNameWithoutExtension, new Dictionary<string, ClsLangText>());
var lines = File.ReadAllLines(file);
string[] array3 = lines;
foreach (var line in array3)
{
string[] parts = line.Replace("\t\t","\t").Split('\t');
if (parts.Length >= 3)
{
var key = parts[0];
var text = parts[2];
string s = ((parts.Length > 3) ? parts[3] : "");
ClsLangText clsLangText = new ClsLangText
{
Name = key,
Text = text,
FontString = s // 保存原始字体字符串
};
if (!langDict[fileNameWithoutExtension].ContainsKey(key))
{
langDict[fileNameWithoutExtension].Add(key, clsLangText);
}
}
}
}
}
private static Font ConvFont(string s)
{
if (string.IsNullOrWhiteSpace(s))
{
return null;
}
string[] array = s.Split(',');
if (array.Length < 2) return null;
float emSize = Convert.ToSingle(array[1]);
FontStyle fontStyle = FontStyle.Regular;
if (array.Length > 2 && array[2] == "B")
{
fontStyle |= FontStyle.Bold;
}
if (array.Length > 3 && array[3] == "I")
{
fontStyle |= FontStyle.Italic;
}
return new Font(array[0], emSize, fontStyle);
}
private static (string FontFamily, double Size, System.Windows.FontWeight Weight, System.Windows.FontStyle Style)
ConvFontForWpf(string s)
{
if (string.IsNullOrWhiteSpace(s))
{
return ("Arial", 12, System.Windows.FontWeights.Normal, System.Windows.FontStyles.Normal);
}
string[] array = s.Split(',');
if (array.Length < 2)
return ("Arial", 12, System.Windows.FontWeights.Normal, System.Windows.FontStyles.Normal);
string fontFamily = array[0];
double emSize = Convert.ToDouble(array[1]);
var weight = System.Windows.FontWeights.Normal;
var style = System.Windows.FontStyles.Normal;
if (array.Length > 2 && array[2] == "B")
{
weight = System.Windows.FontWeights.Bold;
}
if (array.Length > 3 && array[3] == "I")
{
style = System.Windows.FontStyles.Italic;
}
return (fontFamily, emSize, weight, style);
}
public static void LoadLanguage(string name)
{
if (name == "English")
{
CurrentLng = "en-US";
}
else if (name == "简体中文")
{
CurrentLng = "zh-CN";
}
else if (name == "日语")
{
CurrentLng = "ja-JP";
}
}
public static string GetText(string key)
{
if (langDict.ContainsKey(CurrentLng) && langDict[CurrentLng].ContainsKey(key))
{
return langDict[CurrentLng][key].Text;
}
return key; // Return key if text not found
}
// 获取文本和字体信息的方法
public static (string Text, FontProperties FontProps) GetTextAndFont(string key)
{
if (langDict.ContainsKey(CurrentLng) && langDict[CurrentLng].ContainsKey(key))
{
var langText = langDict[CurrentLng][key];
var fontProps = langText.WpfFontProperties;
var fontProperties = new FontProperties
{
Family = fontProps.Family,
Size = fontProps.Size * _fontSizeScaleFactor,
Weight = fontProps.Weight,
Style = fontProps.Style
};
return (langText.Text, fontProperties);
}
return (key, new FontProperties
{
Family = "Arial",
Size = 12 * _fontSizeScaleFactor,
Weight = System.Windows.FontWeights.Normal,
Style = System.Windows.FontStyles.Normal
});
}
public class FontProperties
{
public string Family { get; set; }
public double Size { get; set; }
public System.Windows.FontWeight Weight { get; set; }
public System.Windows.FontStyle Style { get; set; }
}
public static void SetLanguage(DependencyObject parent)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
// 1. 处理 TextBlock 控件
if (child is System.Windows.Controls.TextBlock textBlock)
{
if (!string.IsNullOrEmpty(textBlock.Name))
{
try
{
var key = textBlock.Name;
if (langDict[CurrentLng].ContainsKey(key))
{
var langText = langDict[CurrentLng][key];
textBlock.Text = langText.Text;
// 应用字体设置
langText.ApplyFontToWpfElement(textBlock);
}
else
{
// 尝试获取和应用文本的替代逻辑
try
{
var textKey = langDict[CurrentLng][key].Text;
if (!string.IsNullOrEmpty(textKey))
{
textBlock.Text = textKey;
}
}
catch { }
}
}
catch { }
}
}
// 2. 处理 Control 控件 (如 Button 等)
else if (child is Control control)
{
if (!string.IsNullOrEmpty(control.Name))
{
var key = control.Name;
// 处理 Button 控件
if (control is System.Windows.Controls.Button button)
{
try
{
if (langDict[CurrentLng].ContainsKey(key))
{
var langText = langDict[CurrentLng][key];
button.Content = langText.Text;
// 应用字体设置
langText.ApplyFontToWpfElement(button);
}
else
{
try
{
var textKey = key;
if (langDict[CurrentLng].ContainsKey(textKey))
{
var langText = langDict[CurrentLng][textKey];
var text = langText.Text;
button.Content = text;
langText.ApplyFontToWpfElement(button);
}
}
catch { }
}
}
catch { }
}
// 处理 ComboBox 控件
else if (control is System.Windows.Controls.ComboBox comboBox)
{
try
{
// 应用ComboBox本身的字体
if (langDict[CurrentLng].ContainsKey(key))
{
langDict[CurrentLng][key].ApplyFontToWpfElement(comboBox);
}
// 特殊处理 CmbResetidBy 这个特定的 ComboBox
if (comboBox.Name == "CmbResetidBy")
{
// 为 ComboBox 中的每个 ComboBoxItem 设置翻译文本和字体
foreach (ComboBoxItem item in comboBox.Items)
{
string itemKey = "";
// 根据原始内容确定对应的键
string originalContent = item.Content.ToString();
if (originalContent == "序号不重置" || originalContent == "No Reset")
itemKey = "Dialog_ResetidByNone";
else if (originalContent == "序号每日重置" || originalContent == "Reset Daily")
itemKey = "Dialog_ResetidByDay";
else if (originalContent == "序号每月重置" || originalContent == "Reset Monthly")
itemKey = "Dialog_ResetidByMonth";
// 如果找到了对应的键,获取翻译后的文本和字体
if (!string.IsNullOrEmpty(itemKey) && langDict[CurrentLng].ContainsKey(itemKey))
{
var langText = langDict[CurrentLng][itemKey];
item.Content = langText.Text;
langText.ApplyFontToWpfElement(item);
}
}
}
// 处理其他通用ComboBox
else
{
// 通用处理ComboBox项的文本和字体
foreach (ComboBoxItem item in comboBox.Items)
{
if (item.Name != null && !string.IsNullOrEmpty(item.Name))
{
string itemKey = item.Name;
if (langDict[CurrentLng].ContainsKey(itemKey))
{
var langText = langDict[CurrentLng][itemKey];
item.Content = langText.Text;
langText.ApplyFontToWpfElement(item);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error processing ComboBox: {ex.Message}");
}
}
// 处理CheckBox
else if (control is System.Windows.Controls.CheckBox checkBox)
{
try
{
if (langDict[CurrentLng].ContainsKey(key))
{
var langText = langDict[CurrentLng][key];
checkBox.Content = langText.Text;
langText.ApplyFontToWpfElement(checkBox);
}
else
{
try
{
var text = GetText(key);
if (!string.IsNullOrEmpty(text))
{
checkBox.Content = text;
}
}
catch { }
}
}
catch { }
}
// 处理 RadioButton 控件
else if (control is System.Windows.Controls.RadioButton radioButton)
{
try
{
if (langDict[CurrentLng].ContainsKey(key))
{
var langText = langDict[CurrentLng][key];
radioButton.Content = langText.Text;
langText.ApplyFontToWpfElement(radioButton);
}
else
{
try
{
var text = GetText(key);
if (!string.IsNullOrEmpty(text))
{
radioButton.Content = text;
}
}
catch { }
}
}
catch { }
}
// 处理 Label 控件
else if (control is System.Windows.Controls.Label label)
{
try
{
if (langDict[CurrentLng].ContainsKey(key))
{
var langText = langDict[CurrentLng][key];
label.Content = langText.Text;
langText.ApplyFontToWpfElement(label);
}
else
{
try
{
var text = GetText(key);
if (!string.IsNullOrEmpty(text))
{
label.Content = text;
}
}
catch { }
}
}
catch { }
}
// 处理 GroupBox 控件
else if (control is System.Windows.Controls.GroupBox groupBox)
{
try
{
if (langDict[CurrentLng].ContainsKey(key))
{
var langText = langDict[CurrentLng][key];
groupBox.Header = langText.Text;
langText.ApplyFontToWpfElement(groupBox);
}
else
{
try
{
var text = GetText(key);
if (!string.IsNullOrEmpty(text))
{
groupBox.Header = text;
}
}
catch { }
}
}
catch { }
}
}
}
// 3. 处理 DataGrid 控件
else if (child is System.Windows.Controls.DataGrid dataGrid)
{
if (!string.IsNullOrEmpty(dataGrid.Name))
{
// 应用DataGrid本身的字体
if (langDict[CurrentLng].ContainsKey(dataGrid.Name))
{
langDict[CurrentLng][dataGrid.Name].ApplyFontToWpfElement(dataGrid);
}
// 处理 DataGrid 列的 Header
foreach (var column in dataGrid.Columns)
{
// 仅处理具有 Header 的列
if (!string.IsNullOrEmpty(column.Header?.ToString()))
{
// 尝试使用列 Header 作为键
string headerKey = $"{dataGrid.Name}_{column.Header}";
try
{
// 首先尝试使用定义好的映射
if (langDict[CurrentLng].ContainsKey(headerKey))
{
var langText = langDict[CurrentLng][headerKey];
column.Header = langText.Text;
// 对于HeaderedContentControl类型的Header,可以设置字体
if (column.HeaderTemplate != null &&
column.HeaderTemplate.LoadContent() is FrameworkElement headerElement)
{
langText.ApplyFontToWpfElement(headerElement);
}
}
else
{
// 直接尝试使用复合键
var text = GetText(headerKey);
if (!string.IsNullOrEmpty(text))
{
column.Header = text;
}
}
}
catch { }
// 如果上面的尝试失败,可以尝试直接使用列标头
try
{
var columnHeaderKey = column.Header.ToString();
if (langDict[CurrentLng].ContainsKey(columnHeaderKey))
{
var langText = langDict[CurrentLng][columnHeaderKey];
column.Header = langText.Text;
// 对于HeaderedContentControl类型的Header,尝试设置字体
if (column.HeaderTemplate != null &&
column.HeaderTemplate.LoadContent() is FrameworkElement headerElement)
{
langText.ApplyFontToWpfElement(headerElement);
}
}
else
{
var text = GetText(columnHeaderKey);
if (!string.IsNullOrEmpty(text))
{
column.Header = text;
}
}
}
catch { }
}
}
}
}
// 4. 处理TabControl和TabItem
else if (child is System.Windows.Controls.TabControl tabControl)
{
if (!string.IsNullOrEmpty(tabControl.Name) && langDict[CurrentLng].ContainsKey(tabControl.Name))
{
langDict[CurrentLng][tabControl.Name].ApplyFontToWpfElement(tabControl);
}
foreach (System.Windows.Controls.TabItem tabItem in tabControl.Items)
{
if (!string.IsNullOrEmpty(tabItem.Name) && langDict[CurrentLng].ContainsKey(tabItem.Name))
{
var langText = langDict[CurrentLng][tabItem.Name];
tabItem.Header = langText.Text;
langText.ApplyFontToWpfElement(tabItem);
}
}
}
// 5. 处理MenuItem
else if (child is System.Windows.Controls.MenuItem menuItem)
{
if (!string.IsNullOrEmpty(menuItem.Name) && langDict[CurrentLng].ContainsKey(menuItem.Name))
{
var langText = langDict[CurrentLng][menuItem.Name];
menuItem.Header = langText.Text;
langText.ApplyFontToWpfElement(menuItem);
}
}
// 递归遍历子控件
SetLanguage(child);
}
if (VisualTreeHelper.GetParent(parent) == null) // 只在根元素上调用一次
{
// 等待UI更新完成后调用
System.Windows.Threading.Dispatcher.CurrentDispatcher.InvokeAsync(() =>
{
AdjustButtonTemplateTextBlocks(parent);
}, System.Windows.Threading.DispatcherPriority.Background);
}
}
private static void SetLang(string frmname, System.Windows.Forms.Control ctl)
{
if (!langDict.ContainsKey(CurrentLng))
{
return;
}
foreach (System.Windows.Forms.Control control in ctl.Controls)
{
if (control.Name == "")
{
continue;
}
string key = frmname + "_" + control.Name;
if (control.HasChildren)
{
SetLang(frmname, control);
}
}
}
private static bool HasChinese(string txt)
{
if (Regex.IsMatch(txt.ToString(), "[\\u4E00-\\u9FA5]+"))
{
return true;
}
return false;
}
public static void UpdateUI(UIElement element, string key)
{
if (element is FrameworkElement fe)
{
fe.Dispatcher.Invoke(() =>
{
if (langDict[CurrentLng].ContainsKey(key))
{
var langText = langDict[CurrentLng][key];
if (fe is TextBlock tb)
{
tb.Text = langText.Text;
langText.ApplyFontToWpfElement(tb);
}
else if (fe is System.Windows.Controls.Button btn)
{
btn.Content = langText.Text;
langText.ApplyFontToWpfElement(btn);
}
else if (fe is System.Windows.Controls.Label lbl)
{
lbl.Content = langText.Text;
langText.ApplyFontToWpfElement(lbl);
}
// 添加更多控件类型支持...
}
else
{
var text = GetText(key);
if (fe is TextBlock tb)
{
tb.Text = text;
}
else if (fe is System.Windows.Controls.Button btn)
{
btn.Content = text;
}
else if (fe is System.Windows.Controls.Label lbl)
{
lbl.Content = text;
}
}
});
}
}
public static string Dialog(string key, string defaulttxt = "")
{
if (!langDict.ContainsKey(CurrentLng))
{
return "";
}
string key2 = key.StartsWith("Dialog_") ? key : ("Dialog_" + key);
if (langDict[CurrentLng].TryGetValue(key2, out var value))
{
return value.Text;
}
SetNoLangXML("Dialog", key, defaulttxt);
return defaulttxt;
}
public static bool IsSameText(string key, string text)
{
string key2 = key.StartsWith("Dialog_") ? key : ("Dialog_" + key);
if (langDict[CurrentLng].TryGetValue(key2, out var value))
{
if (value.Text .Trim()== text.Trim())
{
return true;
}
}
string[] langs = new string[] { "en-US", "zh-CN", "ja-JP" };
foreach (string lan in langs)
{
if (langDict[lan].TryGetValue(key2, out var newValue))
{
if (newValue.Text.Trim() == text.Trim())
{
return true;
}
}
}
return false;
}
// 在 LanguageWwitchover 类中添加此方法
private static void AdjustButtonTemplateTextBlocks(DependencyObject root)
{
// 首先查找所有按钮
var buttons = FindVisualChildren<System.Windows.Controls.Button>(root);
foreach (var button in buttons)
{
if (!string.IsNullOrEmpty(button.Name))
{
string key = button.Name;
bool hasValidFont = false;
// 检查按钮是否使用了 MenuButtonStyle 样式
if (button.Style != null && button.Style.TargetType == typeof(System.Windows.Controls.Button))
{
// 尝试获取按钮的语言和字体设置
if (langDict.ContainsKey(CurrentLng) && langDict[CurrentLng].ContainsKey(key))
{
var langText = langDict[CurrentLng][key];
var fontProps = langText.WpfFontProperties;
// 查找按钮模板内的 TextBlock
var textBlocks = FindVisualChildren<TextBlock>(button);
foreach (var textBlock in textBlocks)
{
// 确保这是按钮模板内部用于显示内容的 TextBlock
if (textBlock.Parent is Grid || Grid.GetRow(textBlock) == 1)
{
// 应用字体设置
textBlock.FontFamily = new System.Windows.Media.FontFamily(fontProps.Family);
textBlock.FontSize = fontProps.Size * _fontSizeScaleFactor;
textBlock.FontWeight = fontProps.Weight;
textBlock.FontStyle = fontProps.Style;
hasValidFont = true;
Console.WriteLine($"Updated template TextBlock for button: {button.Name}, FontSize: {textBlock.FontSize}");
}
}
}
// 如果没有从语言资源找到字体设置,使用按钮自身的字体设置
if (!hasValidFont)
{
var textBlocks = FindVisualChildren<TextBlock>(button);
foreach (var textBlock in textBlocks)
{
if (textBlock.Parent is Grid || Grid.GetRow(textBlock) == 1)
{
// 使用按钮的默认字体大小,但应用缩放比例
textBlock.FontSize = button.FontSize * _fontSizeScaleFactor;
Console.WriteLine($"Applied default font size to button: {button.Name}, FontSize: {textBlock.FontSize}");
}
}
}
}
}
}
}
// 添加辅助方法用于查找视觉子元素(如果类中还没有)
private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
private static void SetNoLangXML(string formname, string controlname, string text)
{
string fontString = (CurrentLng == "zh-CN") ? "微软雅黑,12,," : "Arial,12,,";
File.AppendAllText(
path: Path.Combine(filePath, CurrentLng + ".lngres"),
contents: (formname == "Dialog")
? $"{formname}_{controlname}\t{text}\t{text}\t\r\n"
: $"{formname}_{controlname}\t{text}\t{text}\t{fontString}\r\n"
);
}
}
}