1-2 Windows编程基础(了解winForm程序的代码结构)

简介:

1-2-2 了解winForm程序的代码结构

1.初识winForm代码

在图 1-4 的主要窗体控制区域用右键打开后台的 C# 代码,如图 1-6 所示。
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());//此处黑体字部分决定那个窗体文件首先被执行。
        }
    }
}




本文转自 qianshao 51CTO博客,原文链接:http://blog.51cto.com/qianshao/201501,如需转载请自行联系原作者

目录
相关文章
|
16天前
|
消息中间件 编译器 API
Windows窗口程序
Windows窗口程序
|
1月前
|
Windows
实现Windows程序的数据更新
实现Windows程序的数据更新
12 0
|
1月前
|
Windows
构建布局良好的Windows程序
构建布局良好的Windows程序
11 0
|
1月前
|
C# Windows
初识Windows程序
初识Windows程序
10 0
|
1月前
|
数据可视化 Python Windows
使用 Python 代码在 windows 控制台打印正弦三角函数
使用 Python 代码在 windows 控制台打印正弦三角函数
19 0
|
1天前
|
API C++ Windows
windows编程入门_链接错误的配置
windows编程入门_链接错误的配置
6 0
|
2月前
|
Windows
火山中文编程 -- 第一个windows程序
火山中文编程 -- 第一个windows程序
12 0
|
2月前
|
编译器 API Windows
windows编程基础
windows编程基础
13 0
|
1月前
|
安全 数据安全/隐私保护 Windows
解锁安全之门,Windows Server 2019密码修改攻略大揭秘
解锁安全之门,Windows Server 2019密码修改攻略大揭秘
|
1月前
|
存储 安全 网络安全
铁壁如墙-WINDOWS SERVER 2019勒索病毒终极防御指南
铁壁如墙-WINDOWS SERVER 2019勒索病毒终极防御指南