zoukankan      html  css  js  c++  java
  • c# 与 winform 界面开发

    在 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 方法,然后设置控件的 SizeLocationAnchor 或 Dock 属性,

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

  • 相关阅读:
    JAVA面向对象继承 及super的学习
    JAVA 封装的学习
    submit和button的区别
    Servlet四大域对象
    转发和重定向的区别
    JSTL标签用法 详解
    JSP中EL表达式的使用
    解决idea的项目启动报404的问题
    intellij idea 创建动态web项目
    解决mysql数据库中文乱码问题
  • 原文地址:https://www.cnblogs.com/misspy/p/3668958.html
Copyright © 2011-2022 走看看