Update to Code Template add-in for Visual C++


Darren Richard’s code template add-in for Visual C++ is great. It provides a
way to insert commonly used code fragments into a file merely by clicking a
menu item. Furthermore, code fragments can be modified by simply editing a
simple text file. After I used it for a while I found some features lacking that I
really needed in order to use the add-in fully. So I modified it. I have
added three new features to the add-in : submenus, keywords, and format specifiers.

This article does not describe how to use the original features of the add-in.
Refer to Darren’s article Code Template add-in for Visual C++ 5.0 for information
on how to use the original add-in.


The add-in now supports submenus. The submenus may be nested arbitrarily deep
although I have not tested more than 2 levels. To ‘declare’ a submenu within the
code fragment file, use the delimiter ‘#[‘ followed by the name of the submenu. You
may then proceed to declare code fragments as you normally would using the ‘#{‘
delimiter. To close the submenu, use the ‘]#’ delimiter.

For example :

#{Header Files

#{Source Files


The preceding example will create a submenu off the main menu of the add-in. The
submenu will be called ‘Comments’. The submenu will contain 2 menu items,
‘Header Files’ and ‘Source Files’. You can use separators within the submenus as
well. I have not tested the submenus beyond 3 levels.


Whenever I write the comment header for my files, I like to specify the filename,
date, and the project name. In order to support this, the add-in has been modified
to support keywords. Keywords are substituted for their actual value when the add-in
is copying the code fragment from the template file to the target file. Keywords may
appear anywhere that code can appear. Keywords are delimited by ‘#%’ and ‘%#’. The
following keywords are supported :

DATE – The current date.

EXT_ONLY – The current file’s extension.

FILE – The current file, including extension but not path.

FILENAME_ONLY – The current filename, without extension.

PATH – The path to the current file.

PROJECT – The current project.

TIME – The current time.

For example :

#{Header File
* File : #%FILE%#
* Project : #%PROJECT%#

The preceding example, when inserted into the target file, will
expand to a comment block with the filename of the target file and the
project it is contained in.

Format Specifiers

The final enhancement that has been made is format specifiers. Format
specifiers work similar to those in ‘C’. They modify the formatting of
the enclosed text. Format specifiers are delimited by ‘#<‘ and ‘#>’.
Each beginning delimiter must be followed by the format specifier, without any
spaces. After the format specifier, and before the ending delimiter, should be
the text to format. The following format specifiers are supported :

U – Uppercases the text.

L – Lowercases the text.

W – Forces the text to be a given width. The width should be specified after the
‘W’ specifier, separated by a space.

The following example should help clear things up :

#{Header File
* #<W 40 This string will be padded to 40 characters>#
* #<U This string will be uppercased>#
* #<L This string will be lowercased>#

Format specifiers are processed after the keywords have been expanded. Therefore,
format specifiers can be applied to the results of a keyword expansion.

For example :

#if !defined(#<U #%FILENAME_ONLY%#>#_H_INCLUDED)
#endif // Sentry

The preceding example will take the filename of the current file and
uppercase it and then insert it. If the file is called Addin.h, for
example, the code will expand to

#if !defined(ADDIN_H_INCLUDED)
#endif // Sentry

Future Work

One feature that is still missing, but that I would like to add when time permits
is the ability to be prompted for information when the code fragment is being
inserted. Then, based on my response, the code fragment will be modified. One
immediate use I see for this is when defining a class. One of the things I put
in my class comment header is the name of the base class. Currently there is no
mechanism to detect this. It would be nice if, instead, the add-in prompted me
for the base class when it was inserting the code fragment. Perhaps I’ll get around
to it soon…


To install the add-in, do the following :

1. Copy codetempl.dll to any directory you like. If you insert the DLL into the Addins directory (under the directory where
MSDEV.EXE resides, then Visual Studio will automatically load it the next time it starts.

2. Start Visual Studio and go to the ‘Tools/Customize’ menu item. Click on the menu. The Customize dialog will appear.

3. In the Customize dialog, go to the ‘Add-ins and Macro Files’ tab. If you placed the DLL in the Addins directory, then
it will show up here, otherwise use the ‘Browse’ button to locate the DLL.

4. Once the DLL is registered in Visual Studio, click the add-in from within the list to show its buttons.

5. Installation is complete. You can now use the add-ins toolbar buttons.


I hope these extensions prove helpful to those of you using the add-in
already. Any feedback is appreciated. If you want to add your own keywords
or format specifiers then you will need to modify the source code. Look in
Format.h and Format.cpp for the approriate code. Keywords are far easier
to add then format specifiers. If you do add any neat new keywords or format
specifiers then please pass them along.

I have provided a sample template file. It uses most of the keywords and
specifiers. It also uses multiple submenus. You can use the template
file as an example to help you create your own.

Download source and add-in – 45 KB

Date Last Updated: March 6, 1999

More by Author

Must Read