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