C#实现多语言切换

2023-11-03

代码:https://github.com/tangbb1/C-shop/tree/master
思路描述:
①窗体的language属性修改为自己需要设定语言 localizable属性改为true。在窗体上进行英文编辑,即可生成对应的资源文件在这里插入图片描述
在这里插入图片描述
②根据组件名称读取资源文件内容
在这里插入图片描述
③其中除窗口对应的字段之外,还有类似MessageBox的固定字段,及代码中默认设定值。须在资源文件中自定义该类字段
在这里插入图片描述
使用代码读取:
public static ResourceManager res = new ResourceManager(typeof(nonUIresx)); //自定义资源字段
res.GetString(“saveFault”)

在这里插入图片描述
注:主要代码如下:(设置默认语言;遍历组件;重写messagebox;

using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BingSolhalcon
{
     class MultiLanguage
    {
        //当前默认语言
        public static string DefaultLanguage = "zh";


        /// <summary>
        /// 修改默认语言
        /// </summary>
        /// <param name="lang">待设置默认语言</param>
        public  void SetDefaultLanguage(string lang)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
            DefaultLanguage = lang;
            Properties.Settings.Default.DefaultLanguage = lang;
            Properties.Settings.Default.Save();
        }

        public void LoadDefaultLanguage(Form form, Type formType)
        {
            string language = Properties.Settings.Default.DefaultLanguage;
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);
            MultiLanguage multiLanguage = new MultiLanguage();
            //加载默认语言
            multiLanguage.LoadLanguage(form, formType);
        }

        /// <summary>
        /// 加载语言
        /// </summary>
        /// <param name="form">加载语言的窗口</param>
        /// <param name="formType">窗口的类型</param>
        public  void LoadLanguage(Form form, Type formType)
        {
            if (form != null)
            {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType);
                resources.ApplyResources(form, "$this");
                Loading(form, resources);
            }
        }

        /// <summary>
        /// 加载语言
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="resources">语言资源</param>
        private static void Loading(Control control, System.ComponentModel.ComponentResourceManager resources)
        {
            if (control is RibbonControl)
            {
                //将资源与控件对应
                resources.ApplyResources(control, control.Name);
                RibbonControl ms = (RibbonControl)control;
                if (ms.Pages.Count > 0)
                {
                    foreach (RibbonPage c in ms.Pages)
                    {
                        resources.ApplyResources(c, c.Name);
                    }
                }
                if (ms.Items.Count > 0)
                {
                    foreach (BarItem c in ms.Items)
                    {
                        //将资源与控件对应
                        Loading(c, resources);
                    }
                }
            }

            if (control is ComboBox)
            {
                //将资源与控件对应
                //登陆界面中的combox暂时处理
                resources.ApplyResources(control, control.Name);
                ComboBox cb = (ComboBox)control;
                if (cb.FindForm().Name == "LogIn")
                {
                    cb.Items.Clear();
                    cb.Items.Add("Operator");
                    cb.Items.Add("Technician");
                    cb.Items.Add("Expert");
                }  
            }


            foreach (Control c in control.Controls)
            {
                resources.ApplyResources(c, c.Name);
                Loading(c, resources);
            }
        }

        private static void Loading(BarItem control, System.ComponentModel.ComponentResourceManager resources)
        {
            resources.ApplyResources(control, control.Name);
        }

    }

    class MessageBoxEX
    {
        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, string[] buttonTitles)
        {
            MessageForm frm = new MessageForm(buttons, buttonTitles);
            frm.Show();
            frm.WatchForActivate = true;
            DialogResult result = MessageBox.Show(frm, text, caption, buttons);
            frm.Close();
            return result;
        }

        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons,
            MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, string[] buttonTitles)
        {
            MessageForm frm = new MessageForm(buttons, buttonTitles);
            frm.Show();
            frm.WatchForActivate = true;
            DialogResult result = MessageBox.Show(frm, text, caption, buttons, icon, defaultButton);
            frm.Close();
            return result;
        }

        class MessageForm : Form
        {
            IntPtr _handle;
            MessageBoxButtons _buttons;
            string[] _buttonTitles = null;

            bool _watchForActivate = false;

            public bool WatchForActivate
            {
                get { return _watchForActivate; }
                set { _watchForActivate = value; }
            }

            public MessageForm(MessageBoxButtons buttons, string[] buttonTitles)
            {
                _buttons = buttons;
                _buttonTitles = buttonTitles;

                // Hide self form, and don't show self form in task bar.
                this.Text = "";
                this.StartPosition = FormStartPosition.CenterScreen;
                this.Location = new Point(-32000, -32000);
                this.ShowInTaskbar = false;
                this.TopMost = true;
            }

            protected override void OnShown(EventArgs e)
            {
                base.OnShown(e);
                // Hide self form, don't show self form even in task list.
                NativeWin32API.SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, 0, 0, 659);
            }

            protected override void WndProc(ref System.Windows.Forms.Message m)
            {
                if (_watchForActivate && m.Msg == 0x0006)
                {
                    _watchForActivate = false;
                    _handle = m.LParam;
                    CheckMsgbox();
                }
                base.WndProc(ref m);
            }

            private void CheckMsgbox()
            {
                if (_buttonTitles == null || _buttonTitles.Length == 0)
                    return;

                // Button title index
                int buttonTitleIndex = 0;
                // Get the handle of control in current window.
                IntPtr h = NativeWin32API.GetWindow(_handle, GW_CHILD);

                // Set those custom titles to the three buttons(Default title are: Yes, No and Cancle).
                while (h != IntPtr.Zero)
                {
                    if (NativeWin32API.GetWindowClassName(h).Equals("Button"))
                    {
                        if (_buttonTitles.Length > buttonTitleIndex)
                        {
                            // Changes the text of the specified window's title bar (if it has one). 
                            // If the specified window is a control, the text of the control is changed. 
                            // However, SetWindowText cannot change the text of a control in another application.
                            NativeWin32API.SetWindowText(h, _buttonTitles[buttonTitleIndex]);

                            buttonTitleIndex++;
                        }
                    }

                    // Get the handle of next control in current window.
                    h = NativeWin32API.GetWindow(h, GW_HWNDNEXT);
                }
            }
        }


        public const int GW_CHILD = 5;
        public const int GW_HWNDNEXT = 2;

        public class NativeWin32API
        {
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, int flags);
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindow(IntPtr hWnd, Int32 wCmd);
            [DllImport("user32.dll")]
            public static extern bool SetWindowText(IntPtr hWnd, string lpString);
            [DllImport("user32.dll")]
            public static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);

            public static string GetWindowClassName(IntPtr handle)
            {
                StringBuilder sb = new StringBuilder(256);

                // Retrieves the name of the class to which the specified window belongs
                GetClassNameW(handle, sb, sb.Capacity);
                return sb.ToString();
            }
        }

    }

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C#实现多语言切换 的相关文章

  • 为什么 F# 的默认集合是排序的,而 C# 的不是?

    当从 C 世界迁移到 F 最惯用的可能 思维方式时 我发现了这个有趣的差异 在 C 的 OOP mutable 世界中 默认的集合集合似乎是HashSet https learn microsoft com en us dotnet api
  • 删除是如何工作的? [复制]

    这个问题在这里已经有答案了 可能的重复 C 编程 free 如何知道要释放多少 https stackoverflow com questions 1518711 c programming how does free know how m
  • 有没有办法在 xcode 上使用 c++0x ?我想使用 gcc 4.4 或更高版本

    我想使用 gcc 4 4 或更高版本进行 iphone 开发 有人知道怎么做吗 不 你不知道 相信我 你不会 Apple 仍保留 gcc 4 2 1 因为 4 2 2 及更高版本使用 GPLv3 这意味着他们必须放弃对其平台的控制 对于 i
  • linq 中使用字符串数组 c# 的 'orderby'

    假设我有一个这样的方法定义 public CustomerOrderData GetCustomerOrderData string CustomerIDs var query from a in db Customer join b in
  • 从代码中,如何创建对存储在附加属性中的对象的属性的绑定?

    我们有一个继承的附加属性来存储一个对象 在可视化树的更下方 我们希望从代码绑定到该对象的属性 通常我们像这样构建绑定的路径部分 var someBinding new Binding Path new PropertyPath Attach
  • 在 C# 中调用 C++ 库 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我有很多用 C 编写的库 我想从 C 调用这些库 但是 我遇到了很多问题 我想知道是否有书籍或指南告诉我如何做到这一点 Dll导入 htt
  • 在开关中使用“goto”?

    我看到了一个建议的编码标准 内容如下Never use goto unless in a switch statement fall through 我不跟 这个 例外 案例到底是什么样的 这证明了goto 此构造在 C 中是非法的 swi
  • Gwan C#,如何获取HTTP标头?

    我需要它来重写 url 以了解我正在处理哪个友好的 url 用于用户代理和其他东西 EDIT public class Gwan MethodImplAttribute MethodImplOptions InternalCall exte
  • C# 5 async/await 线程机制感觉不对?

    为什么让调用线程进入异步方法直到内部 等待 一旦调用异步方法就生成一个线程 这不是更干净吗 这样您就可以确定异步方法会立即返回 您不必担心在异步方法的早期阶段没有做任何昂贵的事情 我倾向于知道某个方法是否要在 我的 线程上执行代码 不管是堵
  • 将表(行)与 OpenXML SDK 2.5 保持在一起

    我想在 Word 文档中生成多个表 每行 2 行 但我想将这两行保留在一起 如果可能的话 new KeepNext 第一行不起作用 new KeepNext 第一行的最后一段不起作用 new CantSplit 放在桌子上不起作用 在所有情
  • 访问 ascx 文件中的母版页控件

    我有一个母版页文件 其中包含 2 个面板控件中的 2 个菜单 我还使用控件来检查用户是否登录并获取用户类型 根据我想要显示 隐藏面板的类型 控件本身不在母版页中引用 而是通过 CMS 系统动态引用 我想在用户控件中使用findcontrol
  • 根据对象变量搜索对象列表

    我有一个对象列表 这些对象具有三个变量 ID 名称和值 这个列表中可能有很多对象 我需要根据ID或Name找到一个对象 并更改值 例子 class objec public string Name public int UID public
  • 使用 C# 和 wpf 创建类似 Dock 的应用程序

    我需要创建一个与我们购买笔记本电脑时获得的应用程序类似的应用程序 仅当鼠标指针到达窗口顶部时它才可见 那么我怎样才能使用 C 4 0 来做到这一点呢 http www notebookcheck net uploads pics win2
  • 搜索实体的所有字段

    我正在尝试在客户数据库上实现 多功能框 类型的搜索 其中单个查询应尝试匹配客户的任何属性 这是一些示例数据来说明我想要实现的目标 FirstName LastName PhoneNumber ZipCode Mary Jane 12345
  • 引用/指针失效到底是什么?

    我找不到任何定义指针 引用无效在标准中 我问这个问题是因为我刚刚发现 C 11 禁止字符串的写时复制 COW 据我了解 如果应用了 COW 那么p仍然是一个有效的指针并且r以下命令后的有效参考 std string s abc std st
  • Project Euler #8,我不明白我哪里出了问题[关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我正在做项目欧拉第八题 https projecteuler net problem 8 其中我得到了这个大得离谱的数字 7316
  • 使用 GCC 生成可读的程序集?

    我想知道如何使用GCC http en wikipedia org wiki GNU Compiler Collection在我的 C 源文件中转储机器代码的助记符版本 这样我就可以看到我的代码被编译成什么 你可以使用 Java 来做到这一
  • CUDA 8 编译错误 -std=gnu++11

    我正在尝试转换一些代码以使用 CUDA 并且我认为我遇到了兼容性问题 我们使用CMake 这些是我使用的 gcc 和 CUDA 版本 gcc version gcc Ubuntu 5 4 0 6ubuntu1 16 04 5 5 4 0 2
  • 如何调试 .NET 运行时中的内部错误?

    我正在尝试调试一些处理大文件的工作 代码本身works 但 NET 运行时本身会报告零星错误 对于上下文 这里的处理是一个 1 5GB 文件 仅加载到内存中一次 在循环中处理和释放 故意尝试重现此否则不可预测的错误 我的测试片段基本上是 t
  • 为什么以下 C 程序会出现总线错误?

    我认为这是第一个失败的 strtok 调用 好久没写C了 有点不知所措 非常感谢 include

随机推荐

  • matlab 矩阵列乘系数,matlab 给某一列乘上一个系数

    矩阵M是一个 mxn 的矩阵 现在要给M矩阵的第一列都要乘上10 使其第一列扩大10倍 那肿么做呢 我第一时间用的是 M 1 M 1 10 错误的 但是这个错了 结果是不对的 这里要用点乘才行 所以正确的写法是 M 1 M 1 10 正确写
  • Qt 实现简易串口助手

    界面预览 代码如下 h文件 pragma once include
  • 重磅更新!YoloV4最新论文!解读yolov4框架

    论文地址和代码 https arxiv org abs 2004 10934v1 代码 https github com AlexeyAB darknet 本篇博文是对YOLOv4论文的翻译和框架解读 并且有PDF版本可供下载 YOLOv4
  • 如何删除EFI分区

    当我们想重装一下Ubuntu时 需要删除之前的系统以腾出空间 这时会发现之前Ubuntu系统的EFI分区用磁盘管理删除不掉 这里有两个解决方法 1 使用大白菜或者类似的U盘启动工具进入PE系统 使用自带的磁盘管理工具来进行删除 2 直接在W
  • 如何设置计算机自动连接宽带,宽带连接怎么设置,怎么设置宽带自动连接

    处于信息时代的我们 电脑 智能机早已不再是陌生的产品 宽带的连接是我们通过电脑与外部沟通 发布信息的重要渠道 如果没有宽带的连接 那么就算有电脑与无法实现上网的功能 通常会有人疑惑的是 宽带要怎么样才能够自动连接 实现上网的方便程度呢 如果
  • 帝国CMS手机APP服务器端接口API

    帝国CMS手机APP服务器端接口API 100个左右接口详细请看 https www guiboweb com appapi html 使用说明 使用示例 demo 安全验证 security 新闻模型 新闻列表与搜索 list 新闻内容
  • Nginx Windows下编译和安装

    参照官网http nginx org en docs howto build on win32 html提前下载好编译所需软件 Microsoft Visual C compiler Microsoft Visual Studio 8 an
  • 华为OD机试 -最长回文子串(C++ & Java & JS & Python)

    描述 给定一个仅包含小写字母的字符串 求它的最长回文子串的长度 所谓回文串 指左右对称的字符串 所谓子串 指一个字符串删掉其部分前缀和后缀 也可以不删 的字符串 数据范围 字符串长度1 350 1 s 350 进阶 时间复杂度 O n 空间
  • c++ windows下基于TCP的socket编程 入门

    服务器端 socket 创建1个socket bind 绑定IP地址 端口号等信息到socket上 listen 监听 设置允许最大连接数 accept 接受客户端的请求连接 send 和 recv read 和 write 收发数据 cl
  • 定时拷贝删除文件命令

    拷贝文件夹 会把这个文件夹下的文件拷贝到oss的img文件夹下要加 不然会重命名为img的文件 而不拷贝iiimg文件夹本身 ossutil64 cp home leite iiimg oss elatemall img rf ossuti
  • 660 40

    题干 初次思路 1 ln 1 x 1 y gt ln 1 x ln 1 y 对数除法的变形 2 考察了高次求导与泰勒公式理解 泰勒公式可以展开成常数以及从1阶到n阶的无穷小 题目告知是零点 函数值为零的点 的n阶导数 故展开为麦克劳林公式
  • hdu 1024 Max Sum Plus Plus

    Problem acm hdu edu cn showproblem php pid 1024 题意 给一个长为 n 的序列 有从中挑 m 个相互不重合的子序列求总和 让总和最大 分析 没能看懂百度的前几份题解 好像都跟 kuangbin
  • UE4 适用于多人游戏的简单小地图制作探索

    网络上大部分使用Capture2D直接映射小地图方式不仅没有扩展性 且性能要求直接翻倍 实际效果极差 不可支持多人 多物件显示麻烦 纯浪费时间的sb方案 除了初学者超小demo制作 建议不学 学了不使用 该博客仅用于多次制作时快速翻阅 要详
  • 重定向http://www.domain.com到http://domain.com

    Make your site Class B Sep 3 2003 Class B means that all of the traffic to http www yourdomain com is politely and silen
  • java-使用BufferedWriter离线下载csv/Excel文件,使用response在线下载csv/Excel文件

    离线下载 Scheduled cron dmp task download task cron public void isRun throws IOException if null isDownload isDownload equal
  • 盘点 2012 年没落科技巨头

    当我们安然度过2012年12月21日 世界末日 的谣言已不攻自破时 在家电 IT 通信 互联网等科技领域 一些企业却正在经受着 末日征兆 的考验甚至正在走向 末日终结 开篇残酷的选择 与金融能源领域的企业不同 即使扩大到全球范围 也没有一家
  • Python中字符串切片

    在Python中 可以对字符串按自己需要切片 注意 1 第一个字符串排序为0 最后一个字符串为 1 2 切片时 从小切到大 3 切片时 不包含最后一个字符 举例 str 0123456789 print str 0 3 截取第一位到第三位的
  • Android面试必刷Framewrok面试题(附答案),打破面试难点(2023年最新版)

    最近收到身边很多人反馈 现在的android面试 大多数企业除了对求职者的语言和编码等基础能力提出要求外 越来越强调对于 Framework 层的理解和 UI 框架的掌控能力 而完整的项目经历和多端知识也成了重要的加分项 于是小编收拾了一下
  • 【跨模态】【对比学习】CLIP:文本监督CV的预训练(2021)

    文章目录 前言 一 整体架构 1 训练 2 测试 迁移学习zero shot 3 prompt engineering and ensembling 二 实验 1 few shot与zero shot的对比 2 Representation
  • C#实现多语言切换

    代码 https github com tangbb1 C shop tree master 思路描述 窗体的language属性修改为自己需要设定语言 localizable属性改为true 在窗体上进行英文编辑 即可生成对应的资源文件