windows应用程序过程

来源:互联网 发布:商场女装品牌 知乎 编辑:程序博客网 时间:2024/06/05 11:30

过程/winforms 编辑

  一.程序开发和运行环境及概括介绍 

  本文的所有调试程序都基于微软视窗2000专业版本和.Net FrameWork Beta 2版。 

  二.第一个WinForms 

  如果你的机器已经达到了我们程序要求的运行环境,那就打开一个文本编辑器把下面的 

  程序代码Copy到记事本里面,然后另存为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"方法表明将要开始运行应用程序。 

  三.做一个透明的WinForms 

  当我第一次在视窗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 定义窗体的前景色彩 

  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 ) ; 

  } 

  }四.做一个带一个组件的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;五.总结 

  以上是对用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程序功能才会强大,生命力才更强。 

  Font 定义放在WinForm上的字体的类型和大小 

  BackColor 定义窗体的背景色彩 

  ClientSize 定义WinForm的大小 

  Opacity 这个属性是定义WinForm的透明程度,并且这个属性只能用在视窗2000。属性的区值0-1,代表从完全透明到不透明。

原创粉丝点击