现在的位置: 首页 > 综合 > 正文

C# 窗体生成器

2013年10月06日 ⁄ 综合 ⁄ 共 6193字 ⁄ 字号 评论关闭

       要测试 C#windowns 应用程序,当其 Form 很多时, exe 文件运行效率会不会变慢,机器内存会不会暴涨?为此,需要新建很多个 Form 。一种方法则是通过 vs2005 ide 不断地新建 Form 修改其 Name Text ,另外则可以以某个 Form 作为模板,通过设置产生个数、新窗体的命名规则等自动产生 Forms ,然后将这些 Forms 加入到工程中编译即可,本文即阐述了如何自动产生窗体。

       通常 C# 的一个 Form 其有 3 个文件组成: *.cs, *.Designer.cs, *.resx ,其中 .cs 文件为核心代码文件,而 Designer.cs 文件则为窗体的设计文件, .resx 为资源文件。架设我们以 Form1 作为模板产生新的窗体,则只需修改 Form1.cs 文件中的 class Form1 class 目标名称, *.Designer.cs class Form1 class 目标名称, *.Designer.cs this.name= this.name= 新名称 ;// *.Designer.cs this.text= this.text= text;// ,同时复制资源文件即可。

       新建一窗体,取名为 FrmGenerate ,然后注意下述的核心组件:

1)   Button simpleButton1设置保存路径;

2)  Button simpleButton2 ,生成窗体;

3)  Button simpleButton3 选择窗体模板;

       ...

 

    public partial class FrmGenerate : DevExpress.XtraEditors.XtraForm

    {

        public FrmGenerate()

        {

            InitializeComponent();

         }

 

        // 加载窗体模板

        private String loadFormCodes(String fileName)

        {

            //

            String content = "" ;

            try

            {

                using (StreamReader st = new StreamReader (fileName, System.Text.Encoding .GetEncoding("GBK" )))

                {

                    content = st.ReadToEnd();

                    st.Close();

                }

            }

            catch (Exception exp)

            {

                MessageBox .Show(exp.Message);

            }

             return content;

        }

 

        // 设置保存路径

        private void simpleButton1_Click(object sender, EventArgs e)

        {

            using (FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog ())

            {

                if (folderBrowserDialog1.ShowDialog() == DialogResult .OK)

                {

                    this .txtSaveDic.Text = folderBrowserDialog1.SelectedPath;

                }

            }

        }

 

        // 生成窗体

        private void generateForm(String frmBase, int index, String frmName, String frmTitle)

         {

            // 替换class 名称

            String cs = this .memoCS.Text.Replace(frmBase, frmName);

            String designerCs = this .memoDesigner.Text.Replace(frmBase, frmName);

            String csFileName = Path .Combine(this .txtSaveDic.Text, frmName + ".cs" );

            String designerFileName = Path .Combine(this .txtSaveDic.Text, frmName + ".Designer.cs" );

            // 替换nametext 属性

            designerCs = designerCs.Replace("this.Name" , "this.Name = /"" + frmName + "/";//" );   //this.Name = "FrmGrid";

            designerCs = designerCs.Replace("this.Text" , "this.Text = /"" + frmTitle + "/";//" );//this.Text = " 表格演示...";

            //

            writeFile(cs, csFileName);

            writeFile(designerCs, designerFileName);

            try

             {

                File .Copy(this .txtFormName.Text + ".resx" , Path .Combine(this .txtSaveDic.Text, frmName + ".resx" ));

            }

            catch (Exception exp)

            {

            }

        }

 

        // 保存cs,resx 文件

        private void writeFile(String context, String fileName)

        {

            try

            {

                FileStream fs = new FileStream (fileName, FileMode .Create);

                // 获得字节数组

                byte [] data = System.Text.Encoding .GetEncoding("GBK" ).GetBytes(context);

                // 开始写入

                fs.Write(data, 0, data.Length);

                // 清空缓冲区、关闭流

                fs.Flush();

                fs.Close();

            }

            catch (Exception exp)

            {

            }

        }

 

        // 生成窗体

        private void simpleButton2_Click(object sender, EventArgs e)

        {

            if (this .txtClassName.Text.Trim().Length < 1)

            {

                MessageBox .Show(" 请输入ClassName!" );

                return ;

            }

             if (this .txtSaveDic.Text.Trim().Length < 1)

            {

                MessageBox .Show(" 请选择保存路径!" );

                return ;

            }

            if (this .txtFormTitle.Text.Trim().Length < 1)

            {

                MessageBox .Show(" 请设置窗体标题!" );

                return ;

            }

            if (this .txtFormNameAs.Text.Trim().Length < 1)

            {

                MessageBox .Show(" 请设置窗体Name!" );

                return ;

            }

            if (this .spinEdit2.Value < this .spinEdit1.Value)

            {

                MessageBox .Show(" 窗体数设置有误!" );

                return ;

            }

            if (this .txtFormName.Text.LastIndexOf("//" ) < 0)

            {

                MessageBox .Show(" 请选择窗体模板!" );

                return ;

             }

            //

            String frmBase = this .txtFormName.Text.Substring(this .txtFormName.Text.LastIndexOf("//" ) + 1);

            // frmBase.

            for (int i = (int )this .spinEdit1.Value; i < (int )this .spinEdit2.Value; i++)

             {

                generateForm(this .txtClassName.Text, i, this .txtFormNameAs.Text + i.ToString(), this .txtFormTitle.Text + i.ToString());

            }

            MessageBox .Show(" 窗体生成成功! 详见目录:" + this .txtSaveDic.Text);

 

        }

 

        // 选择窗体模板

         private void simpleButton3_Click(object sender, EventArgs e)

        {

            using (OpenFileDialog dialog = new OpenFileDialog ())

            {

                dialog.Filter = "C# 代码文件和资源文件(*.cs,*.resx)|*.cs;*.resx" ;

                if (dialog.ShowDialog() == DialogResult .OK)

                {

                    //

                    String fileName = dialog.FileName;

 

                    if (!String .IsNullOrEmpty(fileName))

                    {

                        fileName = fileName.ToLower();

                        if (fileName.EndsWith("Designer.cs" ))

                        {

                            MessageBox .Show(" 请选择Form 窗体的核心类文件,Form1.cs" );

                            return ;

                        }

                        fileName = fileName.Substring(0, fileName.LastIndexOf("." ));

                        this .txtFormName.Text = fileName;

                        String formName = fileName.Substring(fileName.LastIndexOf("//" ) + 1);

                        //formName = formName.Substring(0, formName.LastIndexOf("."));

                        //

                        String fileCs = this .txtFormName.Text + ".cs" ;

                        String fileDesigner = this .txtFormName.Text + ".Designer.cs" ;

                        String fileResx = this .txtFormName.Text + ".resx" ;

                        tabCS.Text = formName + ".cs" ;

                        tabDesigner.Text = formName + ".Designer.cs" ;

                        tabResource.Text = formName + ".resx" ;

                        memoCS.Text = loadFormCodes(fileCs);

                        memoDesigner.Text = loadFormCodes(fileDesigner);

                        memoRes.Text = loadFormCodes(fileResx);

                        //

                    }

                }

            }

         }

    }

 

 

【上篇】
【下篇】

抱歉!评论已关闭.