用Visual C#开发WinForm的应用程序
点击次数:25 次 发布日期:2008-11-26 23:56:08 作者:源代码网
|
源代码网推荐 源代码网推荐 一.程序开发和运行环境及概括介绍 源代码网推荐 本问的所有调试程序都基于微软视窗2000专业版本和.Net FrameWork Beta 2版。 源代码网推荐 源代码网推荐 二.第一个WinForm 源代码网推荐 如果你的机器已经达到了我们程序要求的运行环境,那就打开一个文本编辑器把下面的 源代码网推荐 程序代码拷贝到编辑器里面,然后另存为first.cs文件。通过下面的编译语句: 源代码网推荐 csc /t:winexe /r:system.dll first.cs 源代码网推荐 编译完成后。运行程序就可以看到以下界面: 源代码网推荐 源代码网推荐 程序源代码:first.cs 源代码网推荐 using System ; 源代码网推荐 //导入 WinForms 名称空间 源代码网推荐 using System.Windows.Forms ; 源代码网推荐 //Class Form1 继承并扩展 System.Windows.Forms名称空间中class Form 源代码网推荐 public class Form1 : Form 源代码网推荐 { 源代码网推荐 public static void Main() 源代码网推荐 { 源代码网推荐 //运行程序 源代码网推荐 Application.Run(new Form1()); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 小结: 源代码网推荐 1).首选要使用"using System.Windows.Forms"语句导入WinForm的名称空间 源代码网推荐 2).声明Form1 类,此类是继承、扩展了using System.Windows.Forms 名称空间中的Form 类 源代码网推荐 3)."Application"类,此类也被定义在using System.Windows.Forms名称空间中,由于此类封闭的,所有我们不能继承。"Application"类主要作用是运行和退出Windows的应用程序,还可以处理Windows的消息。调用"Application"类的"Run"方法表明将要开始运行应用程序。 源代码网推荐 源代码网推荐 三.做一个透明的WinForm 源代码网推荐 当我第一次在视窗2000中看到透明的窗体,就想做出这样一个窗体应该是非常难的。肯定要调用很多的API函数。当接触了.Net以后,才发现用VisualC#做出一个透明的窗体是多么简单,只要设定一个值就可以了。下面还是让我们来看看通过以下代码生成的透明窗体到底是什么样。 源代码网推荐 源代码网推荐 透明窗体的源代码:second.cs 源代码网推荐 using System ; 源代码网推荐 using System.Windows.Forms ; 源代码网推荐 using System.Drawing ; 源代码网推荐 public class Form2 : Form 源代码网推荐 { 源代码网推荐 public static void Main( ) 源代码网推荐 { 源代码网推荐 Application.Run( new Form2( ) ); 源代码网推荐 } 源代码网推荐 public Form2( ) 源代码网推荐 { 源代码网推荐 this.Location = new System.Drawing.Point( 100 , 100 ) ; 源代码网推荐 this.Cursor = System.Windows.Forms.Cursors.Hand; 源代码网推荐 // 定义在窗体上,光标显示为手形 源代码网推荐 this.Text = "透明的WinForm窗体!"; 源代码网推荐 // 定义窗体的标题名称 源代码网推荐 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 源代码网推荐 // 定义窗体的开始显示位置是屏幕的中间 源代码网推荐 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; 源代码网推荐 // 窗体的边界是Fixed3D类型 源代码网推荐 this.ForeColor = System.Drawing.SystemColors.Desktop; 源代码网推荐 //以桌面的前景色作为窗体的前景色 源代码网推荐 this.Font = new System.Drawing.Font ( "宋体", 9 ) ; 源代码网推荐 // 定义字体类型,大小 源代码网推荐 this.BackColor = System.Drawing.Color.Blue; 源代码网推荐 // 定义背景色为蓝色 源代码网推荐 this.ClientSize = new System.Drawing.Size( 440 , 170 ) ; 源代码网推荐 // 设置窗体的大小 源代码网推荐 // Opacity属性设立窗体的透明程度,只对于视窗2000有效 源代码网推荐 this.Opacity = 0.60 ; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 小结: 源代码网推荐 以上的代码其实和第一个例子的代码有很多相似,只是在Form2 Class中多定义了一些属性。 源代码网推荐 1)."this"关键字,我想大家都注意到了这个关键字,那么到底该如何理解他。举例如下:当我在自我介绍的时候(其实就是在定义我的属性),我会说"我的名字叫xx","我的年龄是xx","我的邮箱是xx"……你可能注意到"我的"这二个字,他就是指我本人--王天。同样的道理在程序设计中,"this"关键字就是指向一个对象的实例。所有在上面代码中"this.Font"、"this.Text"就是在设定当前或者正在运行的Form2实例的属性。 源代码网推荐 2).再看看上面的代码,在程序中又导入了一名称空间--System.Drawing。通过这个名称空间定义的类,就可以更好的设计对象,处理颜色和大小。 源代码网推荐 3).下面通过下表来具体说明一下在上面程序中设立的属性的具体含义。 源代码网推荐 属性 描述 源代码网推荐 Location 初始化WinForm的位置,就是当应用程序运行的时候,显示WinFrom的固定位置 源代码网推荐 Cursor 当光标在WinForm上面的时候显示的光标状态 源代码网推荐 Text 设定WinForm的标题 源代码网推荐 StartPosition 这个属性有点类似于"Location"属性,"Location"属性定义的是WinForm的绝对位置,而本属性定义的是WinForm的相对属性。本属性的值定义为"CenterScreen"、"Manual"、"WindoowsDefaultLocation"、"WindowsDefaultBounds"、"CenterParent" 源代码网推荐 FormBorderStyle 定义窗体的边界的样式。通常设定为,"FixedSingle"、"Sizable"、"FixedDialog"、"FixedToolWindow"、"SizableToolWindow" 源代码网推荐 ForeColor 定义窗体的前景色彩 源代码网推荐 Font 定义放在WinForm上的字体的类型和大小 源代码网推荐 BackColor 定义窗体的背景色彩 源代码网推荐 ClientSize 定义WinForm的大小 源代码网推荐 Opacity 这个属性是定义WinForm的透明程度,并且这个属性只能用在视窗2000。属性的区值0-1,代表从完全透明到不透明。 源代码网推荐 源代码网推荐 四.做一个带一个组件的WinForm 源代码网推荐 本例子主要是介绍如何在WinForm中加入一个组件。如果你想在窗体中加入任何组件,首先,你必须要初始化这个组件(见下面程序中初始化Label一样)。并且使用"Controls.Add"方法加入到窗体中,以下是程序运行的界面和源代码。 源代码网推荐 源代码网推荐 源程序:three.cs 源代码网推荐 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(类)。本文只能通过介绍一个基本的类--Form来了解一下Visual C#的强大功能和.Net Class Library(类库)丰富的开发资源。通过本文也可以看出,要想充分利用Visual C#的强大功能,就必须了解并掌握.Net Class Library。也只有掌握了.Net Class Library,你所开发的.Net程序功能才会强大,生命力才更强。 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网供稿. |
