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,如需转载请自行联系原作者

目录
相关文章
|
2月前
|
安全 Ubuntu iOS开发
Nessus Professional 10.10 Auto Installer for Windows - Nessus 自动化安装程序
Nessus Professional 10.10 Auto Installer for Windows - Nessus 自动化安装程序
137 3
Nessus Professional 10.10 Auto Installer for Windows - Nessus 自动化安装程序
|
5月前
|
安全 生物认证 网络安全
windows10无法设置默认保存位置怎么办?显示错误代码0x80070002怎么解决?
Win10系统下载文件时,默认会保存在特定位置,但用户可自行修改。若更改后仍无效,可通过删除目标磁盘中的特定文件夹、修改注册表权限、“干净启动”排除干扰软件或使用第三方修复工具等方式解决此问题。
1110 0
|
4月前
|
Ubuntu Linux Windows
如何在Ubuntu系统中安装Wine,借此来运行Windows程序
熟悉的登录画面出现,在Ubuntu系统中扫描登录微信程序。
|
4月前
|
Unix Linux 编译器
解决在Windows平台上运行Golang程序时出现的syscall.SIGUSR1未定义错误。
通过这种结构,你的代码既可以在支持 SIGUSR1 信号的系统上正常工作,又可以在不支持这些信号的 Windows 系统上编译通过,确保跨平台的兼容性和功能的完整性。
177 0
|
4月前
|
Windows
office出现0xc0000142错误?windows错误代码为0xc0000142?
office出现0xc0000142错误?windows错误代码为0xc0000142?
169 0
|
5月前
|
Windows
Windows下版本控制器(SVN)-验证是否安装成功+配置版本库+启动服务器端程序
Windows下版本控制器(SVN)-验证是否安装成功+配置版本库+启动服务器端程序
143 2
|
6月前
|
Windows
Windows下版本控制器(SVN)-启动服务器端程序
Windows下版本控制器(SVN)-启动服务器端程序
196 4
|
7月前
|
安全 Devops 测试技术
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
146 0
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
|
10月前
|
安全 JavaScript Java
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
165 12
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
|
9月前
|
Windows
Windows程序的数字签名证书怎么申请
Windows程序的数字签名证书申请流程包括:准备企业资料(营业执照、税务登记证等),提交申请表及企业资料。经过初审、实名认证和二审后,等待1-5个工作日审核结果。审核通过后,CA机构颁发证书并通过邮件或邮寄方式发送。收到证书后按指南安装并使用签名工具对程序进行数字签名,确保软件完整性和可信度。注意证书有效期、管理和兼容性问题。