c#与winform界面开发

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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

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

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

1. 工程代码结构创建 Windows Formsapplication工程后,自动生成如下代码:

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] 指定单/多线程运行模式。

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Windows.Forms;namespaceAppDemo

{staticclassProgram

{///<summary>///The main entry point for the application.///</summary>[STAThread]staticvoidMain()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(newForm1());

}

}

}

2. Form.cs 与 Form.Designer.csform1.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 -----------------------------------usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceAppDemo

{publicpartialclassForm1 : Form

{publicForm1()

{

InitializeComponent();

}

}

}//------------------- Form1.Designer.cs -------------------------namespaceAppDemo

{partialclassForm1

{///<summary>///Required designer variable.///</summary>privateSystem.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>protectedoverridevoidDispose(booldisposing)

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

{

components.Dispose();

}base.Dispose(disposing);

}#regionWindows Form Designer generated code}

}

3. 资源文件 与 resxexe 或者 dll 文件需要依赖其它文件得以正常运行,

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

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

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

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

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

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

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

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

ResourceWriter rw =newResourceWriter ("filename.resources") ;

rw.Generate ( ) ;//产生文件rw.Close ( ) ;//添加资源publicvoidAddResource ( str_identifier ,byte[ ] ) ;publicvoidAddResource ( str_identifier ,object);publicvoidAddResource ( 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

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

4.2 获取与设定运行时语言两个关键概念:

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

5. 页面布局与样式设定SuspendLayout() 调用后控件的布局逻辑被挂起,直到调用 ResumeLayout() 方法为止。

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

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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航