在運行時切換 WinForm 程式的介面語言 System.ComponentModel.ComponentResourceManager .ApplyResources

來源:互聯網
上載者:User

標籤:

Download the code for this article: WinForm-Multilanguages-2.rar (11 KB).方法二:

下面介紹一種只需對現有代碼做較小改動的方法。

在 Visual Studio 的設計檢視中,如果在 Properties 視窗中改變了程式的預設介面語言(Language),我們會注意到無論是工程還是表單對應的 .Designer.cs 檔案都會有顯著的改變。比如,我們建立一個叫 MyForm 的 form,並且添加一個叫 MyButton 的按鈕。

在改變表單 Properties 中的 Language 屬性之前, .Designer.cs 代碼檔案中的 InitializeComponent 方法的內容大致如下:

  • private void InitializeComponent()
  • {
  • this.myButton = new System.Windows.Forms.Button();
  • this.SuspendLayout();
  • //
  • // myButton
  • //
  • this.myButton.Location = new System.Drawing.Point(100, 200);
  • this.myButton.Name = "myButton";
  • this.myButton.Size = new System.Drawing.Size(75, 23);
  • this.myButton.TabIndex = 0;
  • this.myButton.Text = "My Button";
  • this.myButton.UseVisualStyleBackColor = true;
  • //
  • // myForm
  • //
  • this.ClientSize = new System.Drawing.Size(292, 273);
  • this.Controls.Add(this.myButton);
  • this.Name = "MyForm";
  • this.Text = "My Form";
  • this.ResumeLayout(false);
  • }

 

而在改變了表單 Properties 中的 Language 屬性之後,工程中除了預設的 .resx 檔案之外,還會自動添加一個 .zh-CHS.resx 檔案(假設我們將 Language 改變為 Chinese (Simplified))。另外,.Designer.cs 檔案中的 InitializeComponent 方法也會改變成:

  • private void InitializeComponent()
  • {
  • System.ComponentModel.ComponentResourceManager resources
  • = new System.ComponentModel.ComponentResourceManager(typeof(MyForm));
  • this.myButton = new System.Windows.Forms.Button();
  • this.SuspendLayout();
  • //
  • // myButton
  • //
  • this.myButton.AccessibleDescription = null;
  • this.myButton.AccessibleName = null;
  • resources.ApplyResources(this.myButton, "myButton");
  • this.myButton.BackgroundImage = null;
  • this.myButton.Font = null;
  • this.myButton.Name = "myButton";
  • this.myButton.UseVisualStyleBackColor = true;
  • //
  • // myForm
  • //
  • this.AccessibleDescription = null;
  • this.AccessibleName = null;
  • resources.ApplyResources(this, "$this");
  • this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  • this.BackgroundImage = null;
  • this.Controls.Add(this.myButton);
  • this.Font = null;
  • this.Icon = null;
  • this.Name = "myForm";
  • this.ResumeLayout(false);
  • }

 

我們注意到改變 Language 屬性之後,代碼的主要變化有:

  • ComponentResourceManager resources = new ComponentResourceManager(typeof(MyForm));
  •     resources.ApplyResources(this.myButton, "myButton");     resources.ApplyResources(this, "$this"); 

另外,設定控制項屬性(比如顯示文字 Text,控制項大小 Size,顯示位置 Location 等)的代碼都沒有了。也就是說設定控制項屬性的代碼都是由 resources.ApplyResource 方法來完成的。那麼在我們想改變 WinForm 程式的介面顯示語言的時候,能不能直接調用 ApplyResources 方法呢?答案是肯定的。

 

為 myButton 添加 Click 事件的事件處理函數:

  • private void myButton_Click(object sender, EventArgs e)
  • {
  • int currentLcid = Thread.CurrentThread.CurrentUICulture.LCID;
  • currentLcid = (currentLcid == 2052) ? 1033 : 2052;
  • // Changes the CurrentUICulture property before changing the resources that are loaded for the win-form.
  • Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentLcid);
  • // Reapplies resources.
  • ComponentResourceManager resources = new ComponentResourceManager(typeof(MyForm));
  • resources.ApplyResources(myButton, "myButton");
  • resources.ApplyResources(this, "$this");
  • }

 

當程式啟動並執行時候,點擊表單上的 myButton 按鈕,表單的介面顯示語言就會在英語和簡體中文之間互相切換。

在運行時切換 WinForm 程式的介面語言 System.ComponentModel.ComponentResourceManager .ApplyResources

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.