在 windows 下使用 vs2010 開(kāi)發(fā),未深入研究。
c# 與 .net 開(kāi)發(fā),一堆又一堆的新名詞,頭暈目眩,比如 CLR / apartments / STA / MTA / COM
吐槽無(wú)力,只一個(gè)問(wèn)題:微軟真的是軟件公司,而不是文學(xué)公司?
創(chuàng )建 Windows Forms Application 工程后,自動(dòng)生成如下代碼:

Form1.cs 與 Form1.Designer.cs 是 2 個(gè)文件,一起定義了一個(gè) form 的行為/樣式等。在 vs2010 中會(huì )折疊在一起。
其中,Designer 中定義樣式。事件監聽(tīng)、事件處理都在 form.cs 中定義。
雙擊 form.cs 會(huì )打開(kāi) UI 效果界面,可以直接拖拽完成界面布局。右鍵 view code 可以查看 form1.cs 源碼。
Program.cs 內定義了 main 函數,是啟動(dòng)文件。
用一個(gè) App run 一個(gè) form。
[STAThread]/[MTAThead] 指定單/多線(xiàn)程運行模式。
using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace AppDemo{ static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }}form1.cs 與 designer.cs 2個(gè)文件定義的是同一個(gè) namespace 下的 同一個(gè) class
每一個(gè)文件定義時(shí),都使用了 partial class
界面布局的代碼,一般自動(dòng)生成,在 Designer 中。
手寫(xiě)的代碼主要是事件處理,一般放在 form.cs 中
form.cs 的構造函數中,一般會(huì )先調用 Designer.cs 中定義的 InitializeComponent() 完成界面初始化。
在 InitializeComponent 中會(huì )聲明每個(gè)控件的索引 private System.Windows.Forms.Button button1;
在 form.cs 中可以直接用過(guò)變量名 button1 操作該控件。
所以,form.cs 的構造函數中,一般先調用 InitializeComponent。
Designer.cs 中,InitializeComponent 初始化界面。Dispose 方法釋放資源。釋放時(shí)需要注意不要造成資源泄露。
// --------------- Form1.cs -----------------------------------using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace AppDemo{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } }}// ------------------- Form1.Designer.cs -------------------------namespace AppDemo{ partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code }}exe 或者 dll 文件需要依賴(lài)其它文件得以正常運行,
最簡(jiǎn)單的做法是:把依賴(lài)文件拷貝到客戶(hù)端,程序內部用相對路徑讀取。
這一般是可以的,但若客戶(hù)刪除了這些資源,則會(huì )導致不可預期的效果。
通過(guò)資源文件,可以把這些文件嵌入到 exe 或者 dll 中。
資源文件可分為 2 類(lèi):.resx 和 .resources。前者是 xml 格式,后者是二進(jìn)制。
只有當前 solution 存在同名的 .cs 文件時(shí),.resx 文件才能正常工作。resources 則無(wú)此限制。
通過(guò) System.Resources 下的 ResourceWriter 可以生成資源文件,Generate 產(chǎn)生文件,Close 關(guān)閉文件。
創(chuàng )建實(shí)例后,即可通過(guò) AddResource 方法添加資源。第一個(gè)參數是標示符,可以在代碼中通過(guò)標示符使用資源。
資源文件中一般存三種類(lèi)型的數據:byte流(byte[])、對象(object)和字符串(string)。
ResourceWriter rw = new ResourceWriter ( "filename.resources" ) ;rw.Generate ( ) ; // 產(chǎn)生文件rw.Close ( ) ;// 添加資源public void AddResource ( str_identifier , byte [ ] ) ;public void AddResource ( str_identifier , object );public void AddResource ( str_identifier , str_value ) ;
resources.ApplyResources( this.myButton, str_identifier ); // 為 this.myButton 使用資源 str_identifier
this.ApplyResource();
在界面上添加組件后,會(huì )生成 .resx 文件,vs2010 中折疊在對應的 form.cs 下。
這是默認語(yǔ)言的資源文件,文件名為 form1.resx
若要開(kāi)發(fā)其他語(yǔ)言版本,在對應 form 右側的屬性菜單中,將 Localizable 設為 True,并將 language 設為所需語(yǔ)言即可。
設置新的顯示文本后保存,會(huì )生成對應語(yǔ)言的 .resx 文件。文件名格式為 form1.language-Location.resx,例如:簡(jiǎn)體中文為 form1.zh-CN.resx

兩個(gè)關(guān)鍵概念:
System.Globalization.CultureInfo.InstalledUICulture.Name; // 獲取當前運行語(yǔ)言System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo( "zh-CHS" ); // 設置當前運行語(yǔ)言
SuspendLayout() 調用后控件的布局邏輯被掛起,直到調用 ResumeLayout() 方法為止。
當調整控件的多個(gè)屬性時(shí),將先調用 SuspendLayout 方法,然后設置控件的 Size、Location、Anchor 或 Dock 屬性,
最后調用 ResumeLayout 方法以使更改生效。
聯(lián)系客服