2、用Visual C# 建立Windows應用程式
| 在Visual C#建立一個Windows (GUI) 應用程式要以前版本的VC++ 容易得多。下面將介紹用Visual C#工程檔案嚮導建立Windows應用程式的過程。 |
| 在VS .NET IDE中選擇“建立->工程檔案->Visual C# 工程檔案->Windows 應用程式”: |
| 然後點擊 OK,出現一個表單設計檢視(這與VB或Delphi相同)。在右側我們看到了一個解決方案導航器( Solution Explorer)。嚮導為新表單增加了一個Form1.cs 檔案,其中包括了這個表單及其所有子視窗的的代碼: |
| using System.Collections; |
| using System.ComponentModel; |
| /// Summary description for Form1. |
| public class Form1 : System.WinForms.Form |
| /// Required designer variable. |
| private System.ComponentModel.Container components; |
| // Required for Windows Form Designer support |
| // TODO: Add any constructor code after InitializeComponent call |
| /// Clean up any resources being used. |
| public override void Dispose() |
| /// Required method for Designer support - do not modify |
| /// the contents of this method with the code editor. |
| private void InitializeComponent() |
| this.components = new System.ComponentModel.Container (); |
| //@this.TrayLargeIcon = false; |
| //@this.TrayAutoArrange = true; |
| this.AutoScaleBaseSize = new System.Drawing.Size (5, 13); |
| this.Click += new System.EventHandler (this.Form1_Click); |
| protected void Form1_Click (object sender, System.EventArgs e) |
| /// The main entry point for the application. |
| public static void Main(string[] args) |
| Application.Run(new Form1()); |
| 從以上代碼中,我們看到:嚮導增加了一個預設的名稱空間以及對WinForms 所要求的不同名稱空間的引用;Form1 類是從System.WinForms.Form中派生出來的;InitializeComponent方法負責初始化(建立)表單及其控制項(當在表單中 托放下一些控制項時,可以看到它的更多細節);Dispose方法負責清除所有不再使用的資源。 |
| 要向一個表單中添加控制項或者子視窗,需要開啟 工具箱ToolBox。這個工具箱的概念來自VB。點擊菜單“視圖->工具箱”,啟用工具箱功能: |
| ToolBox(工具箱)視窗的樣子如所示。現在就可以添加控制項了,添加方法與Visual Studio的以前版本一樣,拖放或者雙擊控制項都可以。 |
|
| 首先在表單上托放下一個按鈕和一個編輯框,然後讓我們看看系統向初始組件(InitializeComponent)中增加了什麼東西。 |
|
| 接著在屬性視窗中設定控制項的屬性,這與VB中的操作方式一樣。在控制項上點擊右鍵,並點中“屬性”菜單條就可以調出屬性視窗。 |
| 現在看看InitializeComponent方法,就會發現這些代碼已經增加到其中了。接著手工修改一下這些代碼: |
| this.components = new System.ComponentModel.Container (); |
| this.button1 = new System.WinForms.Button (); |
| this.textBox1 = new System.WinForms.TextBox (); |
| //@this.TrayLargeIcon = false; |
| //@this.TrayAutoArrange = true; |
| button1.Location = new System.Drawing.Point (16, 24); |
| button1.Size = new System.Drawing.Size (88, 32); |
| button1.Click += new System.EventHandler (this.button1_Click); |
| textBox1.Location = new System.Drawing.Point (128, 32); |
| textBox1.Text = "textBox1"; |
| textBox1.Size = new System.Drawing.Size (144, 20); |
| this.AutoScaleBaseSize = new System.Drawing.Size (5, 13); |
| this.Click += new System.EventHandler (this.Form1_Click); |
| this.Controls.Add (this.textBox1); |
| this.Controls.Add (this.button1); |
| 最後,要為按鈕增加一個事件處理器,實現瀏覽檔案的目的。在按鈕上雙擊,開啟Button1_Click事件處理器。同理,使用同樣的方法可以為任何控制項編寫事件處理器。 |
| protected void button1_Click (object sender, System.EventArgs e) |
| OpenFileDialog fdlg = new OpenFileDialog(); |
| fdlg.Title = "C# Corner Open File Dialog" ; |
| fdlg.InitialDirectory = @"c:/" ; |
| fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ; |
| fdlg.RestoreDirectory = true ; |
| if(fdlg.ShowDialog() == DialogResult.OK) |
| textBox1.Text = fdlg.FileName ; |
到此就完成了所有步驟,剩下的就是運行這個程式。它實現了瀏覽一個檔案,然後將選擇的檔案名稱裝進文字框的功能。請下載相關代碼:winFormApp.zip 。