44
Hands-On Lab What’s New in Visual Studio 11 for C+ + Developers Lab version: 1.1.0.0 Last updated: 3/2/2012

az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

  • Upload
    ngohanh

  • View
    220

  • Download
    3

Embed Size (px)

Citation preview

Page 1: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Hands-On LabWhat’s New in Visual Studio 11 for C++ DevelopersLab version: 1.1.0.0

Last updated: 3/2/2012

Page 2: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

CONTENTS

OVERVIEW................................................................................................................................................. 3

EXERCISE 1: NEW EDITOR FEATURES FOR VISUAL C++....................................................................4Task 1 – Project Compatibility.............................................................................................................5

Task 2 – IntelliSense...........................................................................................................................10

Task 3 – Code Snippets......................................................................................................................15

Task 4 – Reference Highlighting.........................................................................................................18

Task 5 – Semantic Colorization..........................................................................................................19

EXERCISE 2: AUTHORING CODE SNIPPETS FOR VISUAL C++.........................................................22Task 1 – Authoring Code Snippets.....................................................................................................22

Task 2 – Using Your Code Snippet......................................................................................................24

EXERCISE 3: CUSTOM VISUAL C++ ITEM AND PROJECT TEMPLATES...........................................27Task 1 – Authoring Item Templates...................................................................................................27

Task 2 – Authoring Project Templates...............................................................................................33

To give feedback please write to [email protected]. Copyright © 2011 by Microsoft Corporation. All rights reserved.

Page 3: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Overview

In Visual Studio 11, the integrated development environment (IDE) for Visual C++ ships with several new features and improvements that will make the C++ developer more productive with daily code-focused tasks.

First, the Solution Explorer has been greatly empowered in the new version of Visual Studio. It has been enhanced to combine Class View, Object Browser, Find References, Navigate To, and other code-browsing features into a single tool window. Now Solution Explorer enables you to not only browse the files of a solution but also the content in the code files, including their types and members, without having to switch between multiple tool windows.

In the previous version of Visual Studio, C++ code was displayed in black, except for reserved words, preprocessor directives or code comments. In this new version, code colorization and styling have been enhanced. This makes it easier to scan code and infer more semantic meaning. Additionally, when you place your text cursor on a symbol, all the instances of that symbol in the file are highlighted, which helps you better understand larger pieces of code.

In previous releases of Visual Studio, the IntelliSense member drop-down list had to be explicitly invoked. In Visual Studio 11, the member drop-down list now automatically displays and is filtered to show only relevant members. In addition, IntelliSense features now work with C++/CLI.

In the customization area, Visual Studio 11 supports the Visual Studio Templates (vstemplates) format for authoring C++ project and item templates. Any custom C++ templates can now leverage the infrastructure for online publishing in the extension gallery.

Throughout this lab, you will learn how to use and benefit from each of these new features and more!

Note: This lab covers only IDE features that are unique to Visual C++. To review features that are shared by all Visual Basic, Visual C#, Visual C++ and Visual F#, see A Lap Around the Visual Studio 11 Development Environment lab. The online version of this lab is at http://msdn.microsoft.com/en-us/VS11TrainingCourse_LapAroundDev11.

Objectives

In this hands-on lab, you will learn how to:

Take advantage of the new C++ features in the Visual Studio 11 development environment

Create, deploy and use your own code snippets for Visual C++

Create and use your own Item and Project templates for Visual C++

Page 4: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Prerequisites

Microsoft Visual Studio 11 Beta

Microsoft Visual Studio 2010 with Service Pack 1 (for Exercise 1, Task 1)

Exercises

This hands-on lab includes the following exercises:

1. New Editor Features for Visual C++

2. Authoring Code Snippets for Visual C++

3. Item and Project Templates for Visual C++

Estimated time to complete this lab: 60 minutes.

Exercise 1: New Editor Features for Visual C++

In this exercise, you will discover the new C++ features of Visual Studio that will improve your productivity. You will work with a console application with boilerplate code already in place and insert simple code logic, mainly to showcase some of these new features:

Compatibility with Visual Studio 2010, with no conversion wizard required

Code colorization and highlighting

Code snippets for C++

IntelliSense improvements

Note: Before starting this exercise, make sure you have the Visual Studio text editor configured properly. Select the Tools | Options item in the menu to open the configuration dialog. In the left pane tree, open the Text editor | C / C++ | Advanced leaf to enter the IntelliSense options and verify the following attribute values:

- Disable IntelliSense: False

- Disable Member List Code Snippets: False

- Disable Semantic Colorization: False

Page 5: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

- Member List Filter Mode: Fuzzy

Task 1 – Project Compatibility

One of the pillars of this release is project compatibility from one version to the next. Now, you can open Visual Studio 2010 projects in Visual Studio 11 and continue working from Visual Studio 2010 without any problems since the project files are the same.

In this task, you will see how easy it is to migrate your solutions to Visual Studio 11 while maintaining compatibility with Visual Studio 2010. To learn about this feature, you will open a Visual Studio 2010 solution in Visual Studio 11, modify its contents and then reopen it in Visual Studio 2010.

1. Open Visual Studio 2010 if you have it installed in your environment; otherwise continue to step 3. Open the WhatsNewCpp-Ex1-Begin.sln solution located under the Source\Ex1-NewFeatures\Begin folder of this lab.

This solution was created using Visual Studio 2010; therefore, you can open it without any problem in that version of the IDE. If you want, you can also build and run the solution.

2. Close Visual Studio 2010.

3. Now open Visual Studio 11.

4. Select File | Open | Project/Solution. In the Open Project dialog, navigate to the Source\Ex1-NewFeatures\Begin folder of this lab and open WhatsNewCpp-Ex1-Begin.sln.

Page 6: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Notice that the solution was created using Visual Studio 2010, and Visual Studio 11 opened this solution without requiring any conversion.

Note: As project files don’t require upgrading, you won’t see the Project Updater Wizard from the previous editions. If you have Visual Studio 2010 SP1, you can still open projects in Visual Studio 2010 after you’ve opened and saved them with Visual Studio 11.

You can build your project using Visual Studio 2010 tools in Visual Studio 11 using the multi targeting feature (requires Visual Studio 2010 with Visual C++ be installed side-by-side with Visual Studio 11). If you have installed Visual Studio 2010 with Visual C++ you can build your project with the Visual Studio 2010 tools, or you can change the targeting toolset to Visual Studio 11. This means that while you adapt to the new compiler and your 3rd party vendors start providing you with binaries compatible with the Visual Studio 11 compiler, you can still leverage the new Visual Studio 11 IDE without disrupting your ship cycle.

The Solution Explorer shows you that this project was built using the Visual Studio 2010 tools.

Figure 1This project was built using the Visual Studio 2010 tools

5. In the Solution Explorer, right-click the WhatsNewCpp project and select Properties.

Page 7: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

6. In the General section of the property pages, change the Platform Toolset to Visual Studio 11 (v110) and click OK. This will configure the solution to build your project using the Visual Studio 11 toolset.

The Platform Toolset enables the project to target a different version of the Visual C++ libraries and compiler. This enables you to take advantage of the IDE enhancements in Visual Studio 11 while you continue to use an older version of the Visual C++ libraries and compiler.

Figure 2Changing the platform toolset

Note: Since there is no project upgrade, you can continue loading the project/solution in Visual Studio 2010 as long as you do not change the Visual Studio 2010 tools version (v100) used to build the project.

The Solution Explorer no longer displays (Visual Studio 2010) after the project name.

Page 8: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 3The project now uses the Visual Studio 11 toolset

7. Press CTRL+SHIFT+B to build the solution. The project now builds using the Visual Studio 11 tools (v110).

Note: If you do not have Visual Studio 2010 with Visual C++ installed on your system, you may notice you cannot build the project with the original toolset (v100). Remember to switch to the appropriate toolset depending on which version of Visual Studio your project will be opened with.

8. You can also update the target toolset to use v110 (and .NET Framework 4.5) for all the Visual C++ projects in your solution at once. In the Solution Explorer, right-click on the solution node and select Upgrade VC++ projects.

Page 9: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 4Upgrading all the Visual C++ projects to the new toolset

9. Click Yes to confirm the operation. The Output Window displays the results of upgrading the solution.

Page 10: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 5Confirm upgrade operation

Figure 6The solution has been upgraded

Task 2 – IntelliSense

In this task, you will discover some of the new enhancements included in C++ IntelliSense by completing the solution you have been working on.

1. In the Solution Explorer, expand the Source Files folder and open Greetings.cpp. Locate the constructor of the class, Greetings::Greetings, which is now empty.

2. Implement the constructor by assigning the local variable greetingsType to the values passed in the arguments. To do this, start by typing this-> to access the class members inside the Greetings constructor.

Notice that Visual Studio automatically shows the member drop-down list while you are typing without having to invoke it explicitly.

Page 11: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 7Members list is shown automatically

Note: You have probably already noticed that the code colorization has been improved in this version. In addition to the keywords, strings and comments, now other tokens like types, enumerations and macros are colorized; the parameters are in italics and so on. You will learn more about this later on.

3. Continue typing the greetingsType member name and pay attention to the members list displayed.

Notice that the list is being filtered while you are typing in order to show only the relevant members. In addition, you may notice a tooltip to the right of the list, which displays additional information on the selected member, such as the definition, its location and a brief description.

Page 12: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 8List filtered to show only relevant members

4. Complete the line by assigning the value from the greetingsType argument.

C++

Greetings::Greetings(GreetingsType greetingsType, std::string userName){ this->greetingsType = greetingsType;}

5. Continue to assign the value for the userName member. Type this->un and press TAB.

Notice that un is not a prefix or even a substring of the members in the list. The filtering uses fuzzy logic to quickly find the relevant members.

Page 13: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 9Camel casing based filtering

6. Complete the line by assigning the value of the userName member as shown below.

C++

Greetings::Greetings(GreetingsType greetingsType, std::string userName){ this->greetingsType = greetingsType; this->userName = userName;}

7. Open WhatsNewCpp.cpp.

8. In the main function, create an instance of the Greetings class. To do this, type “Greetings greetings(”. Once you have opened the bracket, notice the tooltip showing the constructor reference. You can also switch to any of the available overloads by pressing the up and down arrow keys.

Page 14: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 10Displaying help for the Greetings constructor

9. Complete the constructor by using any GreetingsType item and your name.

C++

Greetings greetings(GreetingsType::WELCOME, "User");

10. Invoke the Show function from the greetings object. While listing the class members, notice the tooltip showing help for each of the members of the class.

Page 15: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 11Displaying help for the class members

Task 3 – Code Snippets

Code snippets help you quickly type boilerplate code with minimal keystrokes. This feature was not available for C++ developers in previous versions of Visual Studio. Now, as you start typing, the IntelliSense member list also includes code snippets, which can be selected by pressing tab.

In this task, you will learn how to use Visual C++ code snippets while completing your application.

1. Open Greetings.cpp.

2. Locate the Show function and place the cursor in the first line. Start typing switch, while paying attention to the IntelliSense list.

Notice that IntelliSense is displaying a code snippet to assist you in creating the switch structure.

Page 16: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 12Switch code snippet in the members list

3. Press TAB to insert the code snippet.

Notice that an empty switch structure has been inserted, and the cursor is now placed inside the switch expression.

Figure 13Inserting the switch code snippet

4. Inside the switch expression, type greetingsType (or press TAB to autocomplete) and press ENTER.

Notice the switch structure is completed with the possible values for the greetingsType enum.

Page 17: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 14Completing the switch expression

Note: In addition to the switch statement, there are other snippets for basic code constructs available to you – like if-else, for loop, etc. Each these snippets saves you from unnecessary typing and lets you focus more on your logic, adding up to significant productivity gains over time.

5. On each of the conditions (HELLO, HI, WELCOME), add a cout instruction that prints the message to the console.

C++

void Greetings::Show(){ switch (greetingsType) { case HELLO: std::cout << "Hello"; break; case HI: std::cout << "Hi"; break; case WELCOME: std::cout << "Welcome"; break; default: break; }}

Page 18: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

6. Below the greetingsType switch, use another cout instruction to print the username value as shown below.

C++

void Greetings::Show(){ switch (greetingsType) { case HELLO: std::cout << "Hello"; break; case HI: std::cout << "Hi"; break; case WELCOME: std::cout << "Welcome"; break; default: break; }

std::cout << " " << userName << "!" << std::endl;}

Task 4 – Reference Highlighting

This new feature improves the readability of code. If you place the text cursor on a symbol, all its instances in the file will be highlighted. Moreover, if two symbols with the same name that belong to different scopes – one local and one global – appear in the same file, the IDE will recognize which one needs to be highlighted. This means that you no longer have to invoke Find All References if you are simply looking for symbols within a file.

1. In the Greetings.cpp file, click on the greetingsType argument of the Greetings constructor.

Notice that greetingsType is highlighted either in the method arguments or in the method body; also notice that the greetingsType class member is not highlighted.

Figure 15Reference highlighting

Page 19: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

2. In the Show function, click any occurrence of cout. You can use CTRL+SHIFT+DOWN and CTRL+SHIFT+UP to move between the highlighted references.

Figure 16Navigating between the highlighted references

Task 5 – Semantic Colorization

Semantic colorization helps you quickly scan code and infer more semantic meaning through enhanced visual feedback in the editor. In addition to the keywords, strings and comments, other tokens like types, enumerations and macros are colorized; the parameters are in italics and so on. However, there are even more colorization options available for each of the language elements that you will now explore.

1. In the new Quick Launch box in the top-right corner of the Visual Studio Window, type font to find the options to change the fonts and colors. Select Environment->Fonts and Colors to open the Visual Studio options dialog box.

The quick launch box is a new Visual Studio feature that can be used to search and execute any command you need. You do not need to look through all the menus or memorize shortcuts to find them.

Page 20: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 17Use the Quick Launch to quickly find and execute commands

2. In the Display Items list, select C++ Fields and set the Item foreground to Purple.

There are only a few tokens that are colorized differently by default. However, notice that around twenty different semantic tokens are available to users as shown in the screenshot below.

Figure 18Customizing the fonts and colors

Note: You can also select different options in the Show settings for list. For instance, you can customize the printed code style, reducing the font size and removing the colorization. If you want to restore the original styles, click Use Defaults.

Page 21: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

3. Select C++ Member Functions under Display Items and select Bold.

4. Select C++ Namespaces under Display Items and set the Item foreground to Gray.

5. Click OK and switch back to the code. Notice how the fields, functions and namespaces stand out to facilitate code readability.

Figure 19Semantic colorization

6. You can go back to the default values by re-opening the Fonts and Colors options page and clicking Use Defaults.

Note: You can customize your IDE to colorize the tokens differently. For example, you can choose to colorize local and global variables differently, which could be a handy source when differentiating between variables that are identically named but defined in different scopes.

Page 22: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Exercise 2: Authoring Code Snippets for Visual C++

Code snippets provide a way for you to insert ready-made snippets of code into your projects with minimal keystrokes. As you start typing code in Visual Studio, the IntelliSense member list shows you the relevant code snippet that you can use.

Code snippets are extensible so you can build your own custom code snippets by creating XML files with a .snippet file extension that adhere to the Code Snippet XML schema. One of the advantages of authoring your own snippets is that you can insert the code structures you frequently use, such as class scaffolds or repeated functions, without doing copy-paste and removing the previous code.

In this exercise, you will learn how to create your own custom code snippet, install it in your environment and use it from the Code Snippets Manager or the IntelliSense members list.

For more information on Code snippets check out the following link at MSDN: http://msdn.microsoft.com/en-us/library/ms165392(VS.110).aspx

Task 1 – Authoring Code Snippets

In this task, you will create your own code snippet starting from a simple snippet template.

1. Open Visual Studio 11 if it is not already opened.

2. Select File | New | File. In the New File dialog, select the XML File template and click Open.

3. Right-click on the text editor surface, select Insert Snippet and then Snippet. This is the basic XML structure for creating a code snippet, which you will now use as the skeleton.

Figure 20Creating a custom code snippet

4. Replace the placeholders of the Header fields with the following values. You can press the TAB key to switch between each placeholder.

Page 23: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

a. Title: pause

b. Author: your name

c. Shortcut: pause

d. Description: Show a message and wait for the user to press a key

Additionally, remove the SurroundsWith SnippetType element. The Header element should look similar to the following code.

XML

<Header> <Title>Pause</Title> <Author>[your name]</Author> <Description>Show a message and wait for the user to press a key</Description> <Shortcut>pause</Shortcut> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes></Header>

Note: The text value of a Shortcut element can only contain alphanumeric characters, hyphens ( - ), and underscores ( _ ).

The SnippetType element supports the following values:

- SurroundsWith: enables the code snippet to be placed around a selected piece of code.

- Expansion: enables the code snippet to be inserted at the cursor position.

- Refactoring: specifies that the code snippet is used during Visual C# refactoring. Refactoring cannot be used in custom code snippets.

5. Complete the Declarations element to define a literal replacement for the message placeholder with the following values:

a. ID: message

b. Default: Press any key to exit

c. ToolTip (add this element): Replace with a message

The Declarations element should look similar to the following code.

XML

</Code> <Declarations> <Literal> <ID>message</ID>

Page 24: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

<Default>Press any key to exit</Default> <ToolTip>Replace with a message</ToolTip> </Literal> </Declarations></Snippet>

6. Now you will fill the code snippet body. Notice that the code will be written inside the <![CDATA{}]> field, while header fields are aimed at providing additional information for the IDE. Locate the Snippet tag and complete the code to use a placeholder for the message. Additionally, change the Language to cpp.

XML

<Code Language="cpp"> <![CDATA[std::cout << "$message$" << std::endl;Console::ReadKey(true);]]></Code>

7. Save the snippet file in the My Code Snippets folder.

To do this, select File | Save XMLFile1.xml As. In the Save File As dialog, browse to Documents\Visual Studio 11\Code Snippets\Visual C++\My Code Snippets, set the file name to pause.snippet and click Save.

Task 2 – Using Your Code Snippet

In this task, you will use the code snippet you have just created to complete the solution from the previous exercise.

1. In Visual Studio, select Tools | Code Snippets Manager. In the Code Snippets Manager dialog, select the Visual C++ language and expand My Code Snippets item. Notice Visual Studio now recognizes your code snippet and shows its header information automatically.

Page 25: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 21The new code snippet in the Code Snippets Manager

2. Close the Code Snippets Manager.

3. Open the WhatsNewCpp-Ex2-Begin.sln solution located in the Source\Ex2-CodeSnippets\Begin of this lab. Alternatively, you can continue using the solution you created in Exercise 1.

4. Open WhastNewCpp.cpp class and locate the main method. Insert a new line after greetings.Show() instruction and start typing pause. Notice your code snippet appears in the list and you can see its title and description in the tooltip message.

Page 26: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 22Inserting your code snippet

5. Press TAB to insert the code snippet. Notice that the cursor is positioned within the message to enable you to replace the text. In addition, if you hover over the text you can see its tooltip message.

Figure 23Replacing the literal replacement

6. Press ENTER once you have finished writing the message, or otherwise use the default.

7. Press CTRL+SHIFT+B to build the solution. Then, press F5 to run the console application and see the results.

8. In the console window, press any key to exit.

Page 27: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

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: http://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.

Page 28: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 24Generating 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.

Page 29: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 25Selecting 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.

Page 30: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 26Exporting 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 27Exporting an item template

Page 31: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

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 28Selecting 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.

Page 32: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 29Changing 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.

Page 33: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 30Selecting 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 Console application 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>

Page 34: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

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 31Generating an item template

Page 35: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

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 32Selecting 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.

Page 36: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 33Inside 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.

Page 37: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 34Using your template

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

Page 38: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

Figure 35Project created from a custom template

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

Figure 36

Page 39: az12722.vo.msecnd.netaz12722.vo.msecnd.net/.../Lab.docx · Web viewCreated Date: 12/07/2011 12:39:00 : Title: What's New in Visual Studio 11 for C++ Developers : Description: In Visual

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.