Exercise 3: Custom Visual C++ Item and Project Templates

Visual Studio project and item templates provide reusable and customizable project and item stubs that accelerate the development process because users do not have to create new projects and items from scratch. Visual Studio templates can be extended to create custom project templates and project item templates and have these templates appear in the New Project and Add New Item dialog boxes.

In previous versions of Visual Studio, you could create C++ project templates using an old .vsz/.vsdir format. This format is quite old and does not support the functionality of publishing the templates in the Visual Studio Gallery for others to download and consume. Visual Studio 11 now supports the ”vstemplates” format for authoring your C++ custom project and item templates. This will enable you to publish the templates online.

You can learn more about Visual Studio templates in the following MSDN article: https://msdn.microsoft.com/en-us/library/ms247121(VS.110).aspx

In this exercise, you will learn the basics of how to create item and project templates for Visual C++.

Task 1 – Authoring Item Templates

In this task, you will create a custom item template that will contain a license. You will add a text file to a new project, and export the file as a template item to reuse it later.

  1. Select File | New | Project. In the New Project dialog, select the Visual C++ | CLR Console Application template in the left pane tree. Name the project CustomTemplate. Use any location in your file system or leave the default location.
  2. In the Solution Explorer, right-click the project node and select Add |New Item. In the dialog box, select the Text File (.txt) file template, name it License.txt, and click Add.
  3. In License.txt, type some text, for instance “Copyright 2012 by Fabrikam.
  4. Select File|Save All or press CTRL+SHIFT+S to save all the changes before exporting the template.
  5. Select File | Export Template to open the Export Template Wizard.
  6. In the Choose Template Type step, select the Item Template option and click Next.

    Figure 24

    Generating an item template

  7. In the Select Item to Export step, select the License.txt item only, and click Next. Make sure the rest of the items are unchecked.

    Figure 25

    Selecting items to export

  8. In the Select Item References step, leave all the items unchecked and click Next.
  9. In the Select Template Options step, type Custom License (.txt) as the Template name and write a template description. In the Icon Image field, select the license.png icon located under the Assets/Images folder of this lab. Click Finish.

    Figure 26

    Exporting an item template

  10. Notice the Template Wizard generates a zip file with your template. Visual Studio will open the folder where the template was created. If you open Custom License (.txt).zip, you can verify that the content of the template contains the items you have included.

    Figure 27

    Exporting an item template

  11. In the Solution Explorer, right-click the project node and select Add | New Item.
  12. Select Custom License (.txt). Notice the default name suggested for the new file is the same as the template name: Custom License (.txt).txt.

    Figure 28

    Selecting a new item based on the template created

  13. Click Cancel.
  14. Now you will change the default name of the item template. Using Windows Explorer, browse to the folder from which the custom templates are loaded: Documents\Visual Studio 11\Templates\ItemTemplates.
  15. Double-click the Custom License (.txt).zip file to open it.
  16. Copy the MyTemplate.vstemplate file outside the .zip file. Open the file with a text editor and change the DefaultName element to License.txt.

    Figure 29

    Changing the item template default name

  17. Copy and replace the MyTemplate.vstemplate file back into the My Custom License (.txt).zip file.
  18. Now you will verify that the item template is updated. In the Solution Explorer, right-click the project node and select Add | New Item.
  19. Select Custom License. Notice the new suggested file name. You already have a file named License.txt in your project, so Visual Studio suggests the name License1.txt. Click Add to add the file.

    Figure 30

    Selecting a new item based on the template created

Task 2 – Authoring Project Templates

In this task, you will customize and export a C++ project as a template. Then, you will use your template to create projects with authoring information and licenses.

  1. Select File | New | Project. In the New Project dialog, select the Visual C++ | CLR Consoleapplication template in the left pane tree. Name the project CustomProjectTemplate. Use any location in your file system or leave the default location.
  2. In the Solution Explorer, right-click the project node and select Add | New Item.
  3. Select Custom License and click Add.
  4. Open CustomProjectTemplate.cpp and add a reference to iostream library.

    C++

    #include <iostream>

  5. Locate the main method and add the following line to write a Copyright message.

    C++

    std::cout << "Copyright 2012 by Fabrikam" << std::endl;

  6. In CustomProjectTemplate.cpp and AssemblyInfo.cpp, add a comment in the first line with the Copyright information. The copyright comment will be copied in the template project, so you won’t need to add it again.

    C++

    // Copyright 2012 by Fabrikam

  7. Select Save All from the File menu.
  8. Select File | Export Template to open the Export Template Wizard. In the Export Template Wizard, select Project template and click Next.

    Figure 31

    Generating an item template

  9. In the Select Template Options step, set the Template name to Custom Project and write a description. In the Icon Image box, browse to the Assets folder located in the Source\Assets\Images folder of this lab and select project.png file. Leave the options located at the bottom of the form checked, and click Finish.

    Figure 32

    Selecting template options

  10. Visual Studio will open the folder where the template was created. If you open Custom Project.zip, you can verify that the template contains the project files.

    Figure 33

    Inside a custom project template

  11. You will now verify that the project template you have created works in Visual Studio. Select File | New | Project. In the New Project dialog, find and select Custom Project from the project templates list. Name the project MyCustomProject and click OK.

    Figure 34

    Using your template

  12. Notice that the project contains the License.txt file.

    Figure 35

    Project created from a custom template

  13. Open the AssemblyInfo.cpp and CustomProjectTemplate.cpp files. Notice that they include the copyright comment.

    Figure 36

    Project template files

    Note:
    Once you have created a template you can also upload it to the Visual Studio Gallery by creating a VSIX. Instructions on how to create a VSIX can be found here.