·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> c#与winform界面开发

c#与winform界面开发

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

在 windows 下使用 vs2010 开发,未深入研究。

 

c# 与 .net 开发,一堆又一堆的新名词,头晕目眩,比如 CLR / apartments / STA / MTA / COM

吐槽无力,只一个问题:微软真的是软件公司,而不是文学公司?

1. 工程代码结构

创建 Windows Forms application 工程后,自动生成如下代码:

Form1.cs 与 Form1.Designer.cs  是 2 个文件,一起定义了一个 form 的行为/样式等。在 vs2010 中会折叠在一起。

其中,Designer 中定义样式。事件监听、事件处理都在 form.cs 中定义。

双击 form.cs 会打开 UI 效果界面,可以直接拖拽完成界面布局。右键 view code 可以查看 form1.cs 源码。

 

PRogram.cs 内定义了 main 函数,是启动文件。

用一个 App run 一个 form。

[STAThread]/[MTAThead] 指定单/多线程运行模式。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace AppDemo
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

2. Form.cs 与 Form.Designer.cs

form1.cs 与 designer.cs 2个文件定义的是同一个 namespace 下的 同一个 class

每一个文件定义时,都使用了 partial class

界面布局的代码,一般自动生成,在 Designer 中。

手写的代码主要是事件处理,一般放在 form.cs 中

 

form.cs 的构造函数中,一般会先调用 Designer.cs 中定义的 InitializeComponent() 完成界面初始化。

在 InitializeComponent 中会声明每个控件的索引   private System.Windows.Forms.Button button1; 

在 form.cs 中可以直接用过变量名 button1 操作该控件。

所以,form.cs 的构造函数中,一般先调用 InitializeComponent。

Designer.cs 中,InitializeComponent 初始化界面。Dispose 方法释放资源。释放时需要注意不要造成资源泄露。

// --------------- Form1.cs -----------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

// ------------------- Form1.Designer.cs -------------------------
namespace AppDemo
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
    }
}

3. 资源文件 与 resx

exe 或者 dll 文件需要依赖其它文件得以正常运行,

最简单的做法是:把依赖文件拷贝到客户端,程序内部用相对路径读取。

这一般是可以的,但若客户删除了这些资源,则会导致不可预期的效果。

通过资源文件,可以把这些文件嵌入到 exe 或者 dll 中。

 

资源文件可分为 2 类:.resx 和 .resources。前者是 xml 格式,后者是二进制。

只有当前 solution 存在同名的 .cs 文件时,.resx 文件才能正常工作。resources 则无此限制。

 

通过 System.Resources 下的 ResourceWriter 可以生成资源文件,Generate 产生文件,Close 关闭文件。

创建实例后,即可通过 AddResource 方法添加资源。第一个参数是标示符,可以在代码中通过标示符使用资源。

资源文件中一般存三种类型的数据:byte流(byte[])、对象(object)和字符串(string)。

ResourceWriter rw = new ResourceWriter ( "filename.resources" ) ;
rw.Generate ( ) ;  // 产生文件
rw.Close ( ) ;

// 添加资源
public void AddResource ( str_identifier , byte [ ] ) ;
public void AddResource ( str_identifier , object );
public void AddResource ( str_identifier , str_value ) ;
resources.ApplyResources( this.myButton, str_identifier ); // 为 this.myButton 使用资源 str_identifier
this.ApplyResource();

4. 多国语言与本地化

4.1 添加多国语言支持

在界面上添加组件后,会生成 .resx 文件,vs2010 中折叠在对应的 form.cs 下。

这是默认语言的资源文件,文件名为 form1.resx

若要开发其他语言版本,在对应 form 右侧的属性菜单中,将 Localizable 设为 True,并将 language 设为所需语言即可。

设置新的显示文本后保存,会生成对应语言的 .resx 文件。文件名格式为 form1.language-Location.resx,例如:简体中文为 form1.zh-CN.resx

  1. 仅当 Language 为 default 时才可以添加或删除界面中的组件。
  2. 仅当设定为新 language,且修改过显示内容后,才会创建对应的资源文件。
  3. 仅当选定 form 时,才能在右侧属性菜单中设置语言。若选中 form 中的 button、text 等组件时,无法设置本地化属性。
  4. 添加多国语言支持后,默认语言的 form1.resx 中不再包含显示文字 Text、控件大小 Size、显示位置 Location 等,而是放在了对应语言的资源文件中,通过 ApplyResources 取用。

4.2 获取与设定运行时语言

两个关键概念:

  • CurrentCulture:默认值是操作系统的用户区域设置,它在“区域选项”控制面板中设置。
  • CurrentUICulture: 默认值是操作系统的用户界面 (UI) 语言,即操作系统用户界面所使用的语言。
System.Globalization.CultureInfo.InstalledUICulture.Name;  // 获取当前运行语言
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo( "zh-CHS" );  // 设置当前运行语言

5. 页面布局与样式设定

SuspendLayout() 调用后控件的布局逻辑被挂起,直到调用 ResumeLayout() 方法为止。

当调整控件的多个属性时,将先调用 SuspendLayout 方法,然后设置控件的 Size、Location、Anchor 或 Dock 属性,

最后调用 ResumeLayout 方法以使更改生效。