最近要做一个项目,这个项目以前有个版本,我很佩服原来那个程序员,用了很多编程技巧,写了很多代码。看了他的代码很多地方我都有很大的提高,可惜客户不认这些,于是又要我们重写。老版本里很多地方其实没必要的,而且如果用户对数据库字段修改了也会导致程序不能使用。 
    为了避免以上问题出现,我想到了用XML来预先定义窗体,通过运行时动态生成窗体来提高程序的可重用性,这样就可以写很少的代码生成不同需要的窗体。而且当有类似的项目需求时,可以很容易的就做出来。 

好了,让我来说说这个项目大体思路吧(所用代码为这个项目的一个类似工程)。项目工程下载 

首先定义一个XML文件:FormStyle.xml,这个项目所有的程序都是围绕这个XML文档来操作的。 

<!--FormStyle.xml--> 
 <!--该文档用来定义窗体--> 
 <?xml version="1.0" encoding="utf-8" ?>  
 <FormStyles> 
     <Form Name="Add" Width="300" Height="400"> 
         <Button Name="OK" Text="确定" Width="75" Height="23" Left="100" Top="30"> 
         </Button> 
     </Form> 
     <Form Name="Del" Width="350" Height="450"> 
         <Button Name="Cancel" Text="取消"  Width="75" Height="23" Left="100" Top="30"> 
         </Button> 
         <TextBox Name="txtName" Width="100" Height="25" Left="100" Top="60"></TextBox> 
     </Form> 
     <Form Name="Search" Width="500" Height="400"> 
         <Button Name="OK" Text="确定" Width="75" Height="23" Left="150" Top="30"></Button> 
         <Button Name="Cancel" Text="取消" Width="75" Height="23" Left="60" Top="30"></Button> 
         <TextBox Name="txtOK" Width="100" Height="25" Left="100" Top="60"></TextBox> 
     </Form> 
     <Form Name="Modify" Width="500" Height="400"> 
         <Button Name="OK" Text="确定" Width="75" Height="23" Left="150" Top="30"></Button> 
         <Button Name="Cancel" Text="取消" Width="75" Height="23" Left="60" Top="30"></Button> 
     </Form> 
 </FormStyles>


   
文档中用<Form></Form>来定义一个窗体,该节点里的其他元素定义窗体中的控件。 

然后就是在主应用程序(frmMain.cs)中通过读取Xml文档来生成菜单项: 

XmlDocument xmlReader = null;            // 定义XML文档类 
  
 if(xmlReader == null) 
 { 
 xmlReader = new XmlDocument(); 
 xmlReader.Load(Application.StartupPath + "\\..\\..\\FormStyle.xml"); 
 } 
 // 通过遍历Xml文档的子节点来生成菜单项,菜单项的Text属性值为<Form></Form>节点// 中的"Name"属性值。 
 for(int i=0, count=xmlReader.DocumentElement.ChildNodes.Count; i < count; i++) 
 { 
 if(xmlReader.DocumentElement.ChildNodes[i].Name == "Form") 
 { 
 MenuItem mnuChild = new MenuItem(); 
 mnuChild.Text = xmlReader.DocumentElement.ChildNodes[i].Attributes["Name"].Value; 
 mnuEdit.MenuItems.Add(mnuChild); 
 mnuChild.Click += new EventHandler(CreateChildForm);    // 将菜单的Click事件关联到//CreateChildForm方法 
 } 
 } 
  
 // 菜单Click事件的处理方法 
 private void CreateChildForm(object sender, EventArgs e) 
 { 
     frmChild child; 
     string FormStyle; 
  
     FormStyle = (sender as MenuItem).Text; 
     child = new frmChild(FormStyle); 
     child.MdiParent = this; 
     child.Show(); 
 }


 

  
下面是子窗体frmChild.cs的主要代码,具体功能在构造函数中实现: 

private XmlDocument xmlReader = null; 
  
 public frmChild(string FormStyle) 
 { 
     // 
     // Windows 窗体设计器支持所必需的 
     // 
     InitializeComponent(); 
  
     if(xmlReader == null) 
     { 
         xmlReader = new XmlDocument(); 
         xmlReader.Load(Application.StartupPath + "\\..\\..\\FormStyle.xml"); 
     }


 
    XmlNode xnode = xmlReader.DocumentElement.SelectSingleNode(string.Format("descendant::Form[@Name='{0}']", FormStyle));        // 这里用到了XPath来确定要在Xml文档中读取的节点,具体关于// XPath的知识请读者自行查看相关资料吧 

if(xnode != null) 
     { 
         for(int i=0; i < xnode.ChildNodes.Count; i++) 
         { 
             if(xnode.ChildNodes[i].Name == "Button") 
             { 
                 Button btnOk = new Button(); 
                 btnOk.Width = int.Parse(xnode.ChildNodes[i].Attributes["Width"].Value); 
                 btnOk.Height = int.Parse(xnode.ChildNodes[i].Attributes["Height"].Value); 
                 btnOk.Left = int.Parse(xnode.ChildNodes[i].Attributes["Left"].Value); 
                  = int.Parse(xnode.ChildNodes[i].Attributes["Top"].Value); 
                 btnOk.Text = xnode.ChildNodes[i].Attributes["Text"].Value; 
                 this.Controls.Add(btnOk); 
             } 
             else if(xnode.ChildNodes[i].Name == "TextBox") 
             { 
                 TextBox txtAdd = new TextBox(); 
                 txtAdd.Width = int.Parse(xnode.ChildNodes[i].Attributes["Width"].Value); 
                 txtAdd.Height = int.Parse(xnode.ChildNodes[i].Attributes["Height"].Value); 
                 txtAdd.Left = int.Parse(xnode.ChildNodes[i].Attributes["Left"].Value); 
                  = int.Parse(xnode.ChildNodes[i].Attributes["Top"].Value); 
                 this.Controls.Add(txtAdd); 
             } 
         } 
     } 
 }


 不足之处:没有完成动态生成的控件的事件处理函数。有兴趣的朋友自己完成吧