C#工具提示语言在运行时更改

时间:2019-07-16 11:08:22

标签: c# winforms

我试图在运行时本地化工具提示。我随该类更改的所有其他控件都非常适合标准控件和工具栏/状态栏:

public static class RuntimeLocalizer
{
    public static void ChangeCulture(Form frm, string cultureCode)
    {
        CultureInfo culture = CultureInfo.GetCultureInfo(cultureCode);

        Thread.CurrentThread.CurrentUICulture = culture;

        ComponentResourceManager resources = new ComponentResourceManager(frm.GetType());

        ApplyResourceToControl(resources, frm, culture);
        resources.ApplyResources(frm, "$this", culture);
    }

    private static void ApplyResourceToControl(ComponentResourceManager res, Control control, CultureInfo lang)
    {
        if (control.GetType() == typeof(MenuStrip))  // See if this is a menuStrip
        {
            MenuStrip strip = (MenuStrip)control;

            ApplyResourceToToolStripItemCollection(strip.Items, res, lang);
        }

        if (control.GetType() == typeof(StatusStrip))  // See if this is a menuStrip
        {
            StatusStrip strip = (StatusStrip)control;

            ApplyResourceToStatusStripItemCollection(strip.Items, res, lang);
        }

        foreach (Control c in control.Controls) // Apply to all sub-controls
        {
            ApplyResourceToControl(res, c, lang);
            res.ApplyResources(c, c.Name, lang);
        }

        // Apply to self
        res.ApplyResources(control, control.Name, lang);
    }

    private static void ApplyResourceToToolStripItemCollection(ToolStripItemCollection col, ComponentResourceManager res, CultureInfo lang)
    {
        for (int i = 0; i < col.Count; i++)     // Apply to all sub items
        {
            ToolStripItem item = (ToolStripMenuItem)col[i];

            if (item.GetType() == typeof(ToolStripMenuItem))
            {
                ToolStripMenuItem menuitem = (ToolStripMenuItem)item;
                ApplyResourceToToolStripItemCollection(menuitem.DropDownItems, res, lang);
            }

            res.ApplyResources(item, item.Name, lang);
        }
    }

    private static void ApplyResourceToStatusStripItemCollection(ToolStripItemCollection col, ComponentResourceManager res, CultureInfo lang)
    {
        for (int i = 0; i < col.Count; i++)     // Apply to all sub items
        {
            ToolStripItem item = (ToolStripItem)col[i];

            if (item.GetType() == typeof(ToolStripMenuItem))
            {
                ToolStripMenuItem menuitem = (ToolStripMenuItem)item;
                ApplyResourceToToolStripItemCollection(menuitem.DropDownItems, res, lang);
            }

            res.ApplyResources(item, item.Name, lang);
        }
    }
}

但是工具提示不会更改。我在可视设计器中添加了两种语言。但是工具提示中的语言始终是默认语言。

我不想使用支持多语言的字符串表。

0 个答案:

没有答案