Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1228118
  • 博文数量: 699
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 4970
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-15 13:45
文章分类

全部博文(699)

文章存档

2011年(1)

2008年(698)

我的朋友

分类:

2008-10-15 13:52:56

    在控制台应用程序中Main函数是程序的入口点。同样地,在窗体应用程序中,Main函数也是程序入口点。这可以通过调试看出来(关于调试的深入介绍请参考第7章),方法如下所示。

    (1)打开或新建一个窗体应用程序,如前面的FormsTest应用程序。

    (2)单击“调试”|“逐句调试”命令,也可以按快捷键F11。可以看到,程序会跳转到Program.cs文件。指示运行的黄色箭头指向Main函数的起始位置。

    (3)继续按F11键,直到运行箭头移动到函数最后一句,代码如下所示。

    Application.Run(new Form1());

    该语句表示,开始应用程序消息循环。其参数new Form1()用于实例化Form1类,这个类就是窗体类Form的一个派生类。对界面设计和事件的处理代码都放在了Form1类中。

    (4)再按F11键,程序运行至Form1.Designer.cs文件。该文件中存放了有关界面设计的初始化内容,如控件、控件布局、控件事件(或者叫委托)的处理程序等,代码如下。

 /* Form1.Designer.cs文件 */

namespace FormsTest

{

partial class Form1

{

///

/// 必需的设计器变量

///


private System.ComponentModel.IContainer components = null;

///

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

///


/// 如果应释放托管资源,为 true;否则为 false。



protected override void Dispose(bool disposing)

{

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

{

components.Dispose();

}

base.Dispose(disposing);

}

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

///

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容

///


private void InitializeComponent()

{

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

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

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(35, 211);

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;

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// button2

//

this.button2.Location = new System.Drawing.Point(167, 211);

this.button2.Name = "button2";

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

this.button2.TabIndex = 1;

this.button2.Text = "button2";

this.button2.UseVisualStyleBackColor = true;

this.button2.Click += new System.EventHandler(this.button2_Click);

//

// Form1

//

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

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

this.ClientSize = new System.Drawing.Size(292, 273);

this.Controls.Add(this.button2);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;

}

}


    一般来说,程序员不必直接编写上面的代码。因为Form类会自动处理这些事情。前面讲过,这些事情包括了控件、控件布局、控件事件的处理程序等。程序员也不必关心事件何时会被调用,何时结束。程序员所需要做的就是编写响应事件的具体代码。

[1]  

【责编:huangchunmei】

--------------------next---------------------

阅读(345) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~