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
|
public static void ApplyCulture(Form form, CultureInfo culture)
{
// Create a resource manager for this Form and determine its fields via reflection.
ComponentResourceManager resources = new ComponentResourceManager(form.GetType());
FieldInfo[] fieldInfos = form.GetType().GetFields(BindingFlags.Instance |
BindingFlags.DeclaredOnly | BindingFlags.NonPublic);
// Call SuspendLayout for Form and all fields derived from Control, so assignment of
// localized text doesn't change layout immediately.
form.SuspendLayout();
for (int index = 0; index < fieldInfos.Length; index++)
{
if (fieldInfos[index].FieldType.IsSubclassOf(typeof(Control)))
{
fieldInfos[index].FieldType.InvokeMember("SuspendLayout",
BindingFlags.InvokeMethod, null,
fieldInfos[index].GetValue(form), null);
}
}
// If available, assign localized text to Form and fields with Text property.
// If available, assign localized Localtion and Size to fields
System.Drawing.Point point;
System.Drawing.Size size;
String text = resources.GetString("$this.Text");
if (text != null)
form.Text = text;
for (int index = 0; index < fieldInfos.Length; index++)
{
if (fieldInfos[index].FieldType.GetProperty("Text", typeof(String)) != null)
{
text = resources.GetString(fieldInfos[index].Name + ".Text");
if (text != null)
{
fieldInfos[index].FieldType.InvokeMember("Text",
BindingFlags.SetProperty, null,
fieldInfos[index].GetValue(form), new object[] { text });
}
}
if (fieldInfos[index].FieldType.GetProperty("Location", typeof(System.Drawing.Point)) != null)
{
point = (System.Drawing.Point)resources.GetObject(fieldInfos[index].Name + ".Location");
if (point != null)
{
fieldInfos[index].FieldType.InvokeMember("Location",
BindingFlags.SetProperty, null,
fieldInfos[index].GetValue(form), new object[] { point });
}
}
if (fieldInfos[index].FieldType.GetProperty("Size", typeof(System.Drawing.Size)) != null)
{
size = (System.Drawing.Size)resources.GetObject(fieldInfos[index].Name + ".Size");
if (size != null)
{
fieldInfos[index].FieldType.InvokeMember("Size",
BindingFlags.SetProperty, null,
fieldInfos[index].GetValue(form), new object[] { size });
}
}
}
// Call ResumeLayout for Form and all fields derived from Control to resume layout logic.
// Call PerformLayout, so layout changes due to assignment of localized text are performed.
for (int index = 0; index < fieldInfos.Length; index++)
{
if (fieldInfos[index].FieldType.IsSubclassOf(typeof(Control)))
{
fieldInfos[index].FieldType.InvokeMember("ResumeLayout",
BindingFlags.InvokeMethod, null,
fieldInfos[index].GetValue(form), new object[] { true });
}
}
form.ResumeLayout(false);
form.PerformLayout();
} |
Partager