Winform+Mygeneration【转】

Interface Code
<%#REFERENCE System.Windows.Forms.dll, System.Drawing.dll %> 
<%#NAMESPACE System.Windows.Forms, System.Drawing %> 
       
public class GeneratedGui : DotNetScriptGui 
       { 

           
public GeneratedGui(ZeusContext context) : base(context) {} 
           
//----------------------------------------- 
           
// 入口函数
           
//----------------------------------------- 

           
public override void Setup() 
       { 

           Form1 form 
= new Form1(MyMeta, input);  
           
if (form.ShowDialog() != DialogResult.OK)  
           { 
               ui.IsCanceled 
= true
           }
       } 
      } 
      
public class Form1:Form 
       { 
           
         
private System.ComponentModel.IContainer components = null
         
protected override void Dispose(bool disposing) 
         { 
         
if (disposing && (components != null)) 
            { 
              components.Dispose(); 
            } 
            
base.Dispose(disposing); 

          } 
        
private void InitializeComponent() 
           { 
               
this.comboBox1 = new System.Windows.Forms.ComboBox(); 
               
this.listBox1 = new System.Windows.Forms.ListBox(); 
               
this.button1 = new System.Windows.Forms.Button(); 
               
this.SuspendLayout(); 
 
               
this.comboBox1.FormattingEnabled = true
               
this.comboBox1.Location = new System.Drawing.Point(2224); 
               
this.comboBox1.Name = "comboBox1"
               
this.comboBox1.Size = new System.Drawing.Size(23320); 
               
this.comboBox1.TabIndex = 0
               
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); 
       
               
this.listBox1.FormattingEnabled = true
               
this.listBox1.ItemHeight = 12
               
this.listBox1.Location = new System.Drawing.Point(2250); 
               
this.listBox1.Name = "listBox1"
               
this.listBox1.Size = new System.Drawing.Size(233196); 
               
this.listBox1.TabIndex = 1
               
this.listBox1.MultiColumn = true;
            
               
this.button1.Location = new System.Drawing.Point(180252); 
               
this.button1.Name = "button1"
               
this.button1.Size = new System.Drawing.Size(7523); 
               
this.button1.TabIndex = 2
               
this.button1.Text = "OK"
               
this.button1.UseVisualStyleBackColor = true
               
this.button1.Click += new System.EventHandler(this.button1_Click); 
           

               
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 
               
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
               
this.ClientSize = new System.Drawing.Size(284293); 
               
this.Controls.Add(this.button1); 
               
this.Controls.Add(this.listBox1); 
               
this.Controls.Add(this.comboBox1); 
               
this.MaximizeBox = false
               
this.MinimizeBox = false
               
this.Name = "Form1"
               
this.Text = "BLL Class"
               
this.Load += new System.EventHandler(this.Form1_Load); 

               
this.ResumeLayout(false); 
           } 
 
           
private System.Windows.Forms.ComboBox comboBox1; 
           
private System.Windows.Forms.ListBox listBox1; 
           
private System.Windows.Forms.Button button1; 
           
private dbRoot myMeta; 
           
private IZeusInput zeusInput; 

           
public Form1(dbRoot myMeta, IZeusInput zeusInput) 
           { 
              
this.myMeta    = myMeta; 
              
this.zeusInput = zeusInput; 
              InitializeComponent(); 
          }    

           
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
          { 
               IDatabase database 
= this.comboBox1.SelectedValue as IDatabase; 
               
if(database != null
               { 
                   
this.listBox1.DataSource = database.Tables; 
                   
this.listBox1.DisplayMember = "Name"
               }    
          } 

          
private void Form1_Load(object sender, EventArgs e) 
           { 
             
//获取数据库,传给ComboBox 
             comboBox1.DataSource    = this.myMeta.Databases; 
             
//ComboBox显示数据库名 
             this.comboBox1.DisplayMember = "Name";   
            
if(this.myMeta.DefaultDatabase != null
              { 
                 
//选中默认的数据库 
                this.comboBox1.SelectedIndex = this.comboBox1.FindStringExact(this.myMeta.DefaultDatabase.Name); 
                
//通过IDatabase 的Tables属性取得数据库里面所有的数据表 
                
//作为数据源传给ListBox 
                this.listBox1.DataSource = this.myMeta.DefaultDatabase.Tables; 
                
//ListBox显示数据表的名字 
                this.listBox1.DisplayMember = "Name"
             }    

           } 

            

        
private void button1_Click(object sender, EventArgs e) 
            { 

              IDatabase database 
= this.comboBox1.SelectedValue as IDatabase; 
              ITable    table    
= this.listBox1.SelectedValue as ITable; 
              
this.zeusInput["databaseName"= database.Name; 
              
this.zeusInput["tableName"]    = table.Name; 
              
this.DialogResult = DialogResult.OK; 
              
this.Close(); 
           } 

       }

 

Template Code
<% 

public class GeneratedTemplate : DotNetScriptTemplate 
    { 
        
public GeneratedTemplate(ZeusContext context) : base(context) {} 
        
public override void Render() 
        { 

         
//设置输出语言... 
         MyMeta.Language = "C#"
         MyMeta.DbTarget 
= "SQLClient"
         
//获得执行完UI后,我们说选中的数据库名,数据表名 
         string databaseName = input["databaseName"].ToString(); 
         
string tableName    = input["tableName"].ToString(); 
                
         IDatabase database 
= MyMeta.Databases[databaseName]; 
         
//获得数据表的接口 
         ITable table = database.Tables[tableName]; 

%> 

namespace BLL 


    
using System; 
    
using System.Data; 
    
using System.Collections;     
    
using System.Collections.Generic; 

<%       
           
//数据表的名称作为BLL类的类名 
           output.writeln("    public partial class " + tableName ); 
           output.writeln(
"    {   "); 
           
//遍历数据表中所有字段     
          foreach (IColumn column in table.Columns) 
           { 
              
//将字段名第一个字母转为小写 
               string tmpColumnName = DnpUtils.SetCamelCase(column.Name); 
               output.writeln(
"        private " + column.LanguageType + " _" + tmpColumnName + ";"); 
               output.writeln(
"        public " + column.LanguageType + " " + column.Name); 
               output.writeln(
"        {"); 
               output.writeln(
"            get { return _" + tmpColumnName + ";    }"); 
               output.writeln(
"            set { _" + tmpColumnName + " = value;   }"); 
               output.writeln(
"        }"); 
               output.writeln(
"");      
          }    
           output.writeln(
"    }   ");      
           output.writeln(
"}"); 
           output.saveEnc( 
"c:\\tem\\1.cs","o","unicode" );//输出后 又保存在文件里
         } 
        
   } 
   
 
%>


 说明:由于 Mygeneration 的Interface很有自我特色,为了顺眼改造成了Winform,另一特色就是Template Code没有用默认的vbscript,而是C#。
         只是告诉自己Mygeneration这个东东可以用C#。有时间翻译一个老师的比较完善的模板。

 原:http://www.cppblog.com/woaidongmao/archive/2010/03/03/108776.aspx

posted on 2010-06-10 13:24  Master zhu  阅读(273)  评论(0编辑  收藏  举报

导航