C#与C++项目模板开发

l  制作模板流程

1.     如何:使用向导来处理项目模板

 http://msdn.microsoft.com/zh-cn/library/ms185301(v=VS.100).aspx 

 中详细讲述了一个例子,可以借鉴。可以做出项目模板,但有些问题不解决,还是无法成功。如下面提到的一些问题。


l  全局程序集缓存(Global Assembly Cache)的程序集

1. 首先制作程序集

添加接口文件:IWizardImplementation.cs

添加程序集引用:.NET 标签下

                                     EnvDTE;

              Microsoft.VisualStudio.TemplateWizard;

接口文件内容如下:

using System;

using System.Collections.Generic;

using Microsoft.VisualStudio.TemplateWizard;

using System.Windows.Forms;

using EnvDTE;

 

namespace RDSWizard

{

    public class IWizardImplementation:IWizard

    {

        private CFile content;

 

        // This method is called before opening any item that

        // has the OpenInEditor attribute.

        public void BeforeOpeningFile(ProjectItem projectItem)

        {

        }

        public void ProjectFinishedGenerating(Project project)

        {

}

       

        // This method is only called for item templates,

        // not for project templates.

        public void ProjectItemFinishedGenerating(ProjectItem

            projectItem)

        {

        }

 

        // This method is called after the project is created.

        public void RunFinished()

        {

        }

 

        public void RunStarted(object automationObject,

            Dictionary<string, string> replacementsDictionary,

            WizardRunKind runKind, object[] customParams)

        {

            try

            {

                // Display a form to the user. The form collects

                // input for the custom message.

                Form1 inputForm = new Form1();

                inputForm.ShowDialog();

 

                content = inputForm.GetWizardCFile();

                // Add custom parameters.

                replacementsDictionary.Add("$head$",

                                           content.ProduceHeadFileContent());

                // Add custom parameters.

                replacementsDictionary.Add("$source$",

                                           content.ProduceSourceFileContent());

           }

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

        }

 

        // This method is only called for item templates,

        // not for project templates.

        public bool ShouldAddProjectItem(string filePath)

        {

            return true;

        }       

    }

}

2.     其次就是制作强名称:(引自网名为zgh2002007的博客)

实际操作如下: 
1.打开SDK Command Prompt或者VS2005中的Visual Studio 2005 Command Prompt.而不是windows的cmd 
2.输入sn -k SampleKey.snk,得到的文件在命令提示符的当前文件夹下(微软叫文件夹很少叫目录,最正点是folder) 
3.输入sn -p SampleKey.snk SampleKeyPublic.snk 
4.输入sn -t SampleKeyPublic.snk 得到public key token

打开VS2005,找到项目,然后右击"项目名"找到"属性","属性"里面有个"Signing"的选项卡,然后钩上
"Sign the assembly",再Choose a strong name key file.做完了操作,然后记得保存,编译
这样你的DLL才能拖到GAC,GAC就是"全局程序集缓存",这就是为什么你的你的项目的bin文件夹下没有System.Data,你都能引用的原因,因为程序会自动去找GAC,GAC的位置在win 2003与xp 中是"c:/windows/assembly".

如果你的assembly在GAC中,那么你的东西就不用每次用的时候都添加引用到项目中了。

3.     然后是把程序集添加到GAC中

从51CTO中学习到,http://book.51cto.com/art/200811/99267.htm

在命令提示符中,输入gacutil.exe /i "<程序集路径>" 即可提示您好,成功!


l  模板与GAC结合,共同组成带向导的项目模板;

1.         生成项目模板的路径文件夹:C:\Documents and Settings\FreedomLive\My Documents\Visual Studio 2008\Templates\ItemTemplates   

2.         向文件MyTemplate.vstemplate中添加相应的XML标签;

<WizardExtension>

    <Assembly>RDSWizard, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=788303fbc60c2ffc</Assembly>

    <FullClassName>RDSWizard.IWizardImplementation</FullClassName>

</WizardExtension>

3.         把修改后的文件再压缩回zip文件即可全部完成带有自定义向导的项目模板的开发工作;


l  带有gac程序集的项目模板的部署

1.     http://msdn.microsoft.com/zh-cn/library/ms246580(v=VS.90).aspx  

如何:打包社区组件以使用 Visual Studio 内容安装程序

2.     需要三加一个文件:

·RDSWizard.vscontent

<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">

  <Content>

    <FileName>RDSWizardItem.zip</FileName>

    <DisplayName>RDSWizardItem</DisplayName>

    <Description>与远程设计系统对接的自动生成接口文件向导</Description>

    <FileContentType>VSTemplate</FileContentType>

    <ContentVersion>1.0</ContentVersion>

         <Attributes>

            <Attribute name="ProjectType" value="Visual C#"/>

            <Attribute name="ProjectSubType" value=""/>

            <Attribute name="TemplateType" value="Item"/>

        </Attributes>

  </Content>

</VSContent>

.Vscontent 文件使用基于 Visual Studio 内容安装程序架构参考的 XML 来描述所创建并打算共享的组件。至少,.vscontent 文件可以识别组件的种类以及应与组件一起使用的 Visual Studio 的版本。

·RDSWizardItem.zip

                    项模板

·RDSWizard.dll

                    gac 程序集

最后:即可将组件文件和 .vscontent 文件压缩为一个 .zip 文件,并将文件扩展名从 .zip 更改为 .vsi。

3.     Register.bat

http://www.cnblogs.com/VisualStudioDesigner/archive/2010/08/23/1806613.html    如何让程序集在编译时自动添加到GAC

@ECHO OFF

SET PATH=%PATH%;C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin;

set solutionPath=C:\Users\cyljxu\Documents\Visual Studio 2008\Projects\WindowsFormsApplication17\

gacutil /if "\RDSWizard.dll"

EXIT 0

                   以上为批处理文件,并且要求GAC的DLL放到与bat同目录下即可。 XP是v6.0A,而Vista和Win7则是v7.0A。



l  VC++项目模板制作

1.  新创建Custom Wizard选项的项目;

2.  保存后,即可在Wizards中看到刚才建立的VC++模板;只能存储在 C:\Documents and Settings\FreedomLive\My Documents\Visual Studio 2008\Projects  如果存储在其它文件夹目录下会提示:

 

3.  前后台的参数自定义

在前台default.htm

·<HEAD>里增加<SYMBOL NAME='RADIO_EMPTY' TYPE=checkbox VALUE=true></SYMBOL> 标签;

·在<Body>里增加相应的html标签;

在后台default.js

·OnFinish函数,利用wizard.AddSymbol('HEAD',ReadFile(HeadFilePath));即可实现向导对前台标签的内容替换;

4.       增加向导源文件

·  Template Files 文件夹中放置实际向导中需要的源文件;

·  在Templates.inf中列出需要增加的文件列表;

·  在函数GetTargetName中对文件名进行自定义和参数化;

5.       注意的一些问题

·   Js打开文件的路径中有空格怎么办?     

//var rootFilePath = "file:///C:\\Documents%20and%20Settings\\FreedomLive\\My%20Documents\\Visual%20Studio%202008\\Projects\\RDSWizardPro\\WizardFiles\\";上面等同于下面: //var rootFilePath2 = "C:\\Documents and Settings\\FreedomLive\\My Documents\\Visual Studio 2008\\Projects\\RDSWizardPro\\WizardFiles\\";

·  编码问题,有原始的H与CPP文件必须用txt保存为UTF-8的字符格式才能够被此向导生成的项目中产生正常的源文件,否则中文乱码;

·  用Js读取并写入的文件,源码问题如何解决?

l  VC++项目模板带向导的部署方案

1.  项目模板调试方法

A、 新打开一个VS并确保里面加载进一个项目;

B、 打开模板所在VS中的Debug菜单——Attach  to Process ;

进行两个选择: 一个是Avalilable Processes中选择A中所指的Vs;另一个是Attche to 点击Select按扭,选择Script Code;

C、 调试过程中,不需要重新启动,直接修改项目源码,进行rebuild后,即可在下一次利用此项目模板新建的项目中得到体现;

2.  部署

A.      Thank you !  the  writer          http://newbgirl.blog.sohu.com/83050673.html  

B.      工程项目文件拷贝到文件夹            {VS2005InstallationFolder} \Microsoft Visual Studio 9.0\VC\VCWizards\AppWiz 安装路径;

C.      个引导性文件:ICo,vsdir,vsz拷贝到文件           {VS2005InstallationFolder}\VC\VCProjects 中;

D.      修改VSZ文件为:   Param="RELATIVE_PATH = VCWizards\AppWiz"

E.       批处理文件:

@echo off

      for /f "tokens=1,2,3,4,*" %%i in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0" ^| find /I "InstallDir"') do SET "pURL=%%k"

echo Microsoft Visual Studio 2008  安装路径为:%pURL%

                                    

xcopy        RDSWizardPro\*.*                   "%pURL% Files\Microsoft Visual Studio 9.0\VC\VCWizards\AppWiz\RDSWizardPro\"        /s /e         

xcopy        RDSWizardPro\RDSWizardPro.ico                  "%pURL% Files\Microsoft Visual Studio 9.0\VC\vcprojects"          /s /e                  

xcopy        RDSWizardPro\RDSWizardPro.vsdir               "%pURL% Files\Microsoft Visual Studio 9.0\VC\vcprojects"          /s /e                  

xcopy        RDSWizardPro\RDSWizardPro.vsz                  "%pURL% Files\Microsoft Visual Studio 9.0\VC\vcprojects"          /s /e                  

pause>nul

                  

                  

世界是你们的,也是我们的,但归根结底是他们的。
原文地址:https://www.cnblogs.com/mqgh/p/3063140.html