您的位置:首页 > 其它

.NET Windows Form开发心得

2006-05-29 11:40 162 查看
第一次用VS.NET 2003做Windows Form 开发, 总结一下

1. 把所有的资源放在一个或多个DLL里, 就象游戏软件把所有的图片做一个资源包, 所有的音效做一个资源包的做法类似, 好处是可以减少主程序的大小, 另外把业务代码根据需要放在一个或多个DLL里, 最终是能够提升软件自动升级的效率, 避免无意义的下载流量, 当然, 为了避免出现DLL Hell, 最好是把所有的DLL加上Strong Name

public static Stream GetResource(string fileName)

2. 把应用程序的主入口点的代码放到一个新单元的启动类里, 好处是与主窗体的代码分开, 流程更加清晰

3. 用执行文件的版本号作为系统的版本号,避免出现多个定义的地方

Assembly assembly = Assembly.GetExecutingAssembly();
object[] assemblyTitleAttributes = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if (assemblyTitleAttributes.Length > 0)
title = (assemblyTitleAttributes[0] as AssemblyTitleAttribute).Title;
version = assembly.GetName().Version.ToString();

4.有时需要统一设置窗体上控件的属性,作界面上的运行期绑定,下面提供一个返回所有控件的函数

public static Control[] GetAllControls(Control ctrl)

5.消息机制,写Windows程序,很有可能需要自定义消息加以处理

protected override void DefWndProc(ref System.Windows.Forms.Message m)

private const int CM_CUSTOMMESSAGE = 0x1600;
public const int CM_MYMESSAGE = CM_CUSTOMMESSAGE + 1;

[DllImport("User32.dll",EntryPoint="SendMessage")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

[DllImport("User32.dll",EntryPoint="PostMessage")]
public static extern int PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
6. 调用Jet Engine压缩Access数据库

public static bool CompactDatabase(string fileName, string newFileName)

7. 程序中嵌入WebBrowser, .NET封装的AxWebBrowser的功能实在是够弱的,下面2篇文章基本上包括了设置WebBrowser的方方面面

http://icebird.cnblogs.com/articles/403056.html
http://icebird.cnblogs.com/articles/403031.html

by the way, CodeRush for VS.NET 和 Refactor! Pro for VS.NET真的很好用,不愧是DevExpress出品
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: