<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />1-2-2 了解winForm程序的代码结构

1.初识winForm代码

在图1-4的主要窗体控制区域用右键打开后台的C#代码,如图1-6所示。
1-2 Windows编程基础(了解winForm程序的代码结构)
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
1-6  查看winForm程序的代码
    展开的代码如下,具体意义见每行的注释信息:
using System;// 基础核心命名空间

using System.Collections.Generic;

//包含了ArrayList、BitArray、Hashtable、Stack、StringCollection 和 StringTable 类

using System.ComponentModel;

using System.Data;//数据库访问控制

using System.Drawing;//绘图类

using System.Text;//文本类

using System.Windows.Forms; //大量窗体和控件

 

namespace WindowsApplication1   //当前操作的命名控件是WindowsApplication1

{

    public partial class Form011 : Form   // System.Windows.Forms.Form 中派生

    {

        public Form011()

        {

            InitializeComponent();//注意该方法在下面的介绍

        }

    }

}

小知识:

理解Using语句

Using语句通常出现在一个.cs文件中的头部,用于定义引用系统命名空间,具体的操作方法和属性等被定义在该系统的命名控件之中,比如如果不写using System.Drawing,则无法在后期开发之中进行图形图像方面的设计开发。

另一方面,用户可以定义用户自定义类在一个用户自定义的命名空间下,这样在头部通过Using语句声明该用户自定义的命名空间,从而获取该命名空间下的具体类以及该类的属性和方法,达到对于系统软件分层开发的目的。

2.理解InitializeComponent()方法

在每一个窗体生成的时候,都会针对于当前的窗体定义InitializeComponent()方法,该方法实际上是由系统生成的对于窗体界面的定义方法。
//位于.cs文件之中的InitializeComponent()方法

public Form011()

        {

            InitializeComponent();

        }

在每一个Form文件建立后,都会同时产生程序代码文件.CS文件,以及与之相匹配的.Designer.CS文件,业务逻辑以及事件方法等被编写在.CS文件之中,而界面设计规则被封装在.Designer.CS文件里,下面代码为.Designer.CS文件的系统自动生成的脚本代码。
namespace WindowsApplication1

{

    partial class Form011

    {

        /// <summary>

        /// 必需的设计器变量。

        /// </summary>

        private System.ComponentModel.IContainer components = null;

        /// <summary>

        /// 清理所有正在使用的资源。

        /// </summary>

        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

        #region Windows 窗体设计器生成的代码

        /// <summary>

        /// 设计器支持所需的方法 - 不要使用代码编辑器修改此方法的内容。

        /// </summary>

        private void InitializeComponent()

        {

            this.button1 = new System.Windows.Forms.Button();

            this.label1 = new System.Windows.Forms.Label();

            this.SuspendLayout();

            //

            // button1

            //

            this.button1.Location = new System.Drawing.Point(70, 43);

            this.button1.Name = "button1";

            this.button1.Size = new System.Drawing.Size(75, 23);

            this.button1.TabIndex = 0;

            this.button1.Text = "button1";

            this.button1.UseVisualStyleBackColor = true;

            //

            // label1

            //

            this.label1.AutoSize = true;

            this.label1.Location = new System.Drawing.Point(12, 54);

            this.label1.Name = "label1";

            this.label1.Size = new System.Drawing.Size(41, 12);

            this.label1.TabIndex = 1;

            this.label1.Text = "label1";

            //

            // Form011

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(458, 326);

            this.Controls.Add(this.label1);

            this.Controls.Add(this.button1);

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;

            this.Name = "Form011";

            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            this.Text = "Form011";

            this.ResumeLayout(false);

            this.PerFormLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;

        private System.Windows.Forms.Label label1;

    }

}

在代码之中,可以很容易发现InitializeComponent()方法和Dispose()方法,前者为界面设计的变现内容,后者为表单释放系统资源时候执行编码。
小实验:修改Windows 窗体的.Designer.CS文件

请更改InitializeComponent()方法中的相关属性参数,观察界面的显示是否有变化

3.创建WinForm应用程序的入口点

WinForm应用程序的开发设计中,一般会通过多窗体协调一致的处理具体业务流程。这种应用必须由程序员决定那个WinForm的窗体第一个被触发执行,在Windows Forms开发程序设计中由位于根目录下的Program.cs文件决定。展开Program.cs文件,按照下面代码即可决定那个WinForm的表单第一个被触发执行。
小实验:修改WinForm应用程序的入口点

using System;

using System.Collections.Generic;

using System.Windows.Forms;

 

namespace WindowsApplication1

{

    static class Program

    {

        /// <summary>

        /// 应用程序的主入口点。

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Form011());//此处黑体字部分决定那个窗体文件首先被执行。

        }

    }

}