淺談C#開發WinForm
本例子主要是介紹如何在 C#開發WinForm中加入一個組件,如果你想在窗體中加入任何組件,首先,你必須要初始化這個組件(見下面程序中初始化Label一樣)。并且使用"Controls.Add"方法加入到窗體中,以下是程序運行的界面和源代碼。
C#開發WinForm源程序:
- using System ;
- using System.Windows.Forms ;
- using System.Drawing ;
- public class Form3 : Form
- {
- //定義一個標簽
- private Label label1 ;
- public static void Main( )
- {
- Application.Run( new Form3( ) ) ;
- }
- // 構造
- public Form3( )
- {
- // 建立標簽并且初始化
- this.label1 = new System.Windows.Forms.Label( ) ;
- //先繼承一個Label 類
- label1.Location = new System.Drawing.Point( 24 , 16 ) ;
- //設定 Label的顯示位置
- label1.Text = "這是一個WinForm中的標簽";
- label1.Size = new System.Drawing.Size ( 200, 50 );
- //設定標簽的大小
- label1.TabIndex = 0 ;
- label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
- // 設定標簽的對齊方式
- this.Text = "在WinForm中加入一個標簽!";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
- this.AutoScaleBaseSize = new System.Drawing.Size( 8 , 16 ) ;
- this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
- // 設定窗體的邊界類型
- this.ForeColor = System.Drawing.SystemColors.Desktop;
- this.Font = new System.Drawing.Font ("宋體", 10 , System.Drawing.FontStyle.Bold ) ;
- // 設定字體、大小就字體的式樣
- this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
- this.BackColor = System.Drawing.Color.Blue;
- // 設定背景色
- this.ClientSize = new System.Drawing.Size( 250 , 250 ) ;
- //把標簽加到窗體中
- this.Controls.Add ( this.label1 ) ;
- }
- }
以上是對用Visual C#開發WinForm應用程序進行了著重的介紹,由于在.Net FrameWork Sdk中的System.Windows.Froms名稱空間中封裝了許多關于界面設計的Class(類)。本文只能了解一下Visual C#的強大功能和豐富的開發資源。要想充分利用Visual C#的強大功能,就必須了解并掌握.Net Class Library。也只有掌握了.Net Class Library,你所開發的.Net程序功能才會強大,生命力才更強。
【編輯推薦】