.NET Windows Form开发心得

第一次用VS.NET 2003做Windows Form 开发, 总结一下

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

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 从资源DLL中取得需要的资源
ExpandedBlockEnd.gif        
/// </summary>

None.gif          public   static  Stream GetResource( string  fileName)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Stream stream 
= null;
InBlock.gif            Type resourceType 
= typeof(ResourceManager); //ResourceManager是资源DLL中定义的一个DummyClass
InBlock.gif
            string resourceName = resourceType.Namespace + "." + fileName.Replace("\\"".");
InBlock.gif            System.Reflection.Assembly assembly 
= System.Reflection.Assembly.GetAssembly(resourceType);
InBlock.gif            
if (assembly == null)
InBlock.gif                
throw new Exception("无法装载资源文件: " + resourceType.Namespace + ".dll");
InBlock.gif            stream 
= System.Reflection.Assembly.GetAssembly(resourceType).GetManifestResourceStream(resourceName);
InBlock.gif            
if (stream == null)
InBlock.gif                
throw new Exception("无法取得资源: " + fileName);
InBlock.gif            
return stream;
ExpandedBlockEnd.gif        }

None.gif

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

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

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

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

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 取得指定容器上的全部子控件
ExpandedBlockEnd.gif        
/// </summary>

None.gif          public   static  Control[] GetAllControls(Control ctrl) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            ArrayList allControls 
= new ArrayList();
InBlock.gif            Queue queue 
= new Queue();
InBlock.gif            queue.Enqueue(ctrl.Controls);
InBlock.gif
InBlock.gif            
while( queue.Count > 0 ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Control.ControlCollection
InBlock.gif                    controls 
= (Control.ControlCollection)queue.Dequeue();
InBlock.gif                
if( controls == null || controls.Count == 0 ) 
InBlock.gif                    
continue;
InBlock.gif                
foreach( Control control in controls ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    allControls.Add(control);
InBlock.gif                    queue.Enqueue(control.Controls);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return (Control[])allControls.ToArray(typeof(Control));
ExpandedBlockEnd.gif        }

None.gif

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

None.gif          protected   override   void  DefWndProc( ref  System.Windows.Forms.Message m)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
switch(m.Msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case CM_MYMESSAGE:
InBlock.gif                    
//Do My Business
InBlock.gif
                    break;
InBlock.gif                
default:
InBlock.gif                    
base.DefWndProc(ref m);
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif

None.gif          private   const   int  CM_CUSTOMMESSAGE  =   0x1600 ;
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 自定义消息
ExpandedBlockEnd.gif        
/// </summary>

None.gif          public   const   int  CM_MYMESSAGE  =  CM_CUSTOMMESSAGE  +   1 ;
None.gif
None.gif        [DllImport(
" User32.dll " ,EntryPoint = " SendMessage " )]
None.gif        
public   static   extern   int  SendMessage(IntPtr hWnd,  int  Msg,  int  wParam,  int  lParam);
None.gif
None.gif        [DllImport(
" User32.dll " ,EntryPoint = " PostMessage " )]
None.gif        
public   static   extern   int  PostMessage(IntPtr hWnd,  int  Msg,  int  wParam,  int  lParam);

6. 调用Jet Engine压缩Access数据库

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 压缩数据库
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="fileName">文件名</param>
ExpandedBlockEnd.gif        
/// <param name="newFileName">文件名</param>

None.gif          public   static   bool  CompactDatabase( string  fileName,  string  newFileName)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
bool result = false;
InBlock.gif            
//如果要压缩的数据库打开的话,先关闭之
InBlock.gif

InBlock.gif            
string TempFile;
InBlock.gif            
if (newFileName == String.Empty)
InBlock.gif                TempFile 
= Path.GetTempFileName();
InBlock.gif            
else
InBlock.gif                TempFile 
= newFileName;
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object[] oParams;
InBlock.gif                
object objJRO = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
ExpandedSubBlockStart.gifContractedSubBlock.gif                oParams 
= new object[] dot.gif{TConnection.ConnectionString(fileName), "Provider=Microsoft.Jet.OLEDB.4.0;Data" + " Source=" + TempFile + ";Jet OLEDB:Engine Type=5"};
InBlock.gif                objJRO.GetType().InvokeMember(
"CompactDatabase", System.Reflection.BindingFlags.InvokeMethod, null, objJRO, oParams);
InBlock.gif                
if (newFileName == String.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    File.Delete(fileName);
InBlock.gif                    File.Move(TempFile, fileName);
ExpandedSubBlockEnd.gif                }

InBlock.gif                System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);
InBlock.gif                objJRO 
= null;
InBlock.gif                result 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//如果有必要的话,恢复数据库连接
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
return result;
ExpandedBlockEnd.gif        }

None.gif

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出品
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值