Deep C++: And the Tips Just Keep On Coming

More Nifty Visual C++ Hints

Rico Mariani

July 13, 1995

In the May/June 1995 issue, I offered "Nine Great Debugging Tips for Visual C++ 2.0." MSDN_DEEPC4_3 It's summer here in Redmond, the fairways look lush and the greens fast, so let's play another nine. As with the last column, while these tips are geared to the Microsoft Visual C++ development system version 2.0, many apply to version 1.0 as well.

Tip 1: Wrap external makefiles for added goodness

The project facility in Visual C++ is actually quite general. You can make it run any command to do your build (it doesn't have to be the NMAKE utility that does the work). So, no matter how tricky your build is, you can make an external makefile that will do it.

Doing this has lots of advantages. The output will be captured in the output window, and you can walk through your errors using F4 or SHIFT+F4. The debugger will know what .EXE to run when you debug, and the browser will know what browser database to use. Many Visual C++ settings are remembered on a per-project basis, so you can have custom options for the project if you wrap it. To create the external makefile, just use the File menu's Open command on the primary file that does the build (usually a makefile) and select Open As Makefile from the list at the bottom right.

Tip 2: Change compile flags for more than one file at a time

One of the most under-used features in the project system is multiple selection in the tree control of the project settings dialog box. By selecting multiple files you can see what flags they have in common, and you can change flags for them all at once. This works even across groups or targets—selecting a group or target node in the tree is the same as selecting all the files in the group or target. This is a great way to see exactly which flags change from one target to another.

Tip 3: There are hidden goodies in the project system user interface

The project system is for more than just building. You can double-click resource files to open the appropriate resource editor, drag and drop files from File Manager to add them to the project, and even add random non-source files to the project and use the project system to launch the associated application. Very handy for updating design documents (you do have design documents, don't you?).

Tip 4: The text editor is way cool

The much-maligned Visual C++ text editor has actually gotten a whole lot better. Here's the Top 10 things you probably didn't know the Visual C++ editor can do.

10. CTRL+ALT+T makes your tab characters visible.

9. SHIFT+ESC closes the active dockable window (Output, Watch, Locals, and so on).

8. CTRL+F3 finds the next occurrence of the current word.

7. CTRL+M finds the matching parenthesis.

6. CTRL+> and CTRL+< find the next/previous #ifdef, #else, or #endif.

5. CTRL+SHIFT+R starts/stops macro recording.

4. CTRL+SHIFT+P plays the recorded macro.

3. A right-click on #include lines can open the included file if you choose that option.

2. TAB or SHIFT+TAB indents or unindents selected lines (when the selection spans a line).

1. Holding down the ALT key lets you select columns by dragging.

Tip 5: Sometimes resources just aren't the same

You can cause resources to be compiled conditionally by using "magic" alternate IDs for them. For instance, if you want one version of your Foo dialog box for debug and another one for other builds, you would do so by creating resources for IDD_FOO and IDD_FOO$(DEBUG). The resource editors use this trick to provide Macintosh-specific versions of dialog boxes in the cross-platform version of the product, but you can do so whenever you need.

Tip 6: ClassWizard is just a CTRL+DOUBLE-CLICK away

You can do the normal ClassWizard operation on any dialog control just by pressing the CTRL key and double-clicking the dialog control. If the control is a button, a click handler will be added and you'll be in position to type in the code; otherwise, you are set to add a suitable member variable. This is particularly useful for data-aware controls, where you have to bind the control to a field in your data table.

Tip 7: The bitmap editor has a won't-quit keyboard interface

Almost all of the editing features are on some key or another (there's a handy table in the documentation—in fact, there are keyboard mapping tables for just about all of the features you might use a lot). What's really interesting is that some keys are best used while you're in the middle of a drag operation.

For instance, suppose you're trying to draw a line from one point to another. You might zoom in to get a good look at the starting point, but now you're stuck in zoom mode, since you're in the middle of dragging, right? Wrong. You can press the M key (M is for Magnify) in the middle of dragging to unzoom, then get close to the endpoint and press M again for precise placement. Another favorite of mine is CTRL+B, which turns the current selection into a brush you can draw with. This has dozens of useful applications, not the least of which is making copies of those three-dimensional lines that give an engraved look but that are quite tedious to draw over and over. Check out the key bindings; if you use this editor at all, you'll find them very handy.

Tip 8: Easter eggs—AKA handy undocumented features

Everything I've mentioned already is somewhere in the documentation, but here are a couple of things that didn't make it into the official features list.

Start Visual C++ with the msvc -p <pid> command-line arguments, and the integrated development environment (IDE) will attach itself to the specified process ID for debugging. This is another flavor of Just-In-Time debugging, but the program doesn't have to fault before you can use it. Don't forget that the program will still be running when you do this, so you'll probably have to choose Break from the Debug menu (or you can set breakpoints).

Visual C++ also has a hidden command switch, bppassc:yes, that enables the hidden pass count field on the breakpoint dialog. CodeView users will remember that this feature lets you stop after a breakpoint has been hit a certain number of times. A handy trick that leverages this feature is to set the skip count to something very high (such as 10,000), then see how many times your breakpoint was actually hit before the event of interest (usually a crash). Repeat the scenario, setting the breakpoint count one smaller than was reported. This technique is sometimes the only way to see what went wrong in a frequently called function ("Who'da thunk my code would fail on the 7139th invocation?").

The syntax coloring feature in Visual C++ also supports a set of custom keywords. Just put the words you want highlighted in a file called USERTYPE.DAT in the same directory in which you keep MSVC.EXE; these words will be highlighted in the custom color.

Tip 9: Credit where it's due

To see the credits for Visual C++ 2.0, copy the MSVCCRD.DLL file from the product CD to the same directory in which you keep MSVC.EXE. Then CTRL+DOUBLE-CLICK the picture in the About box. It dismisses itself very easily, so you might have to try more than once to get it to stay up.

Rico Mariani has worked on Microsoft development tools since 1988. This article is based on a talk he gave at SD 95. If you couldn't tell, he recently took up golf.