- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Chapter 22. Using OLE DB
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
The SourceInfo Add-In
As an example of a Developer Studio add-in, the CD-ROM that accompanies this book includes SourceInfo—an add-in that displays information about source files currently open in Developer studio.
The following pseudocode shows the algorithm used by the add-in:
For each open file
if the file is a text file
increase the file counter
count the number of lines in the file
add the number of lines to the line counter
count the number of semicolons
add the number of semicolons to the semicolon counter
end if
end for
Report the number of files, lines, and semicolons
The SourceInfo add-in was created using the Developer Studio Add-In Wizard. Figure 17.4 shows the wizard page used to create the SourceInfo add-in.
Figure 17.4 The Developer Studio Add-In Wizard used to create the SourceInfo add-in.
The SourceInfo add-in was created using the following options:
- Name: SourceInfo Add-in
- Description: Displays information about source files
- Provides a Toolbar: Checked
- Responds to Developer Studio Events: Unchecked
As with other wizard-created projects, the SourceInfo project has basic functionality right away. To make SourceInfo really useful, you need to perform three steps:
- Create a dialog box to be used to display source information to the user.
- Modify the command-handling code to perform the steps outlined in the pseudocode earlier in this section.
- Modify the toolbar icon associated with the add-in.
Adding the Source Information Dialog Box to the Project
The IDD_SOURCEINFO dialog box was added to the project to display information about open source files. Figure 17.5 shows the IDD_SOURCEINFO dialog box.
Figure 17.5 The IDD_SOURCEINFO dialog box resource.
In addition to the OK button, which is added automatically to the dialog box, the IDD_SOURCEINFO dialog box has six controls. Three of the new controls are static text controls, and three controls are edit controls. Table 17.1 lists the properties of the edit controls. Each of the controls also has the read-only property.
Table 17.1. Edit Control Properties
| Name | Resource ID |
| Open Files | IDC_FILES |
| Lines | IDC_LINES |
| Semicolons | IDC_SEMICOLONS |
The IDD_SOURCEINFO dialog box is managed by the CDlgSourceInfo class. Each of the edit controls is associated with a member variable in the CDlgSourceInfo class, as Table 17.2 shows.
Table 17.2. Edit Control Properties
| Variable[sr] | ||||
| Resource ID | Class | Category | Type | Name |
| IDC_FILES | CDlgSourceInfo | Value | DWORD | m_dwFiles |
| IDC_LINES | CDlgSourceInfo | Value | DWORD | m_dwLines |
| IDC_SEMICOLONS | CDlgSourceInfo | Value | DWORD | m_dwSemiColons |
Adding Command-Handling Code to the Project
As discussed earlier in this chapter, command-handling code is located in the CCommands class. The implementation file for this class is commands.cpp. The SourceInfo project's command code is handled by the CCommands::SourceInfoCommandMethod shown in Listing 17.5.
Example 17.5. The Command-Handling Code for the SourceInfo Project
STDMETHODIMP CCommands::SourceInfoCommandMethod()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
CDlgSourceInfo dlg;
CComPtr<IDispatch> pDocsDisp;
VERIFY_OK(m_pApplication->get_Documents(&pDocsDisp));
CComQIPtr<IDocuments, &IID_IDocuments> pDocs(pDocsDisp);
long totalDocsRead = 0;
long totalLineCount = 0;
long totalSemiCount = 0;
// The number of documents in the collection includes
// non-text documents.
long docCount;
VERIFY_OK(pDocs->get_Count(&docCount));
// The documents collection is 1-based, rather than the
// traditional 0-based.
for(long ndxDoc = 1 ; ndxDoc <= docCount ; ndxDoc++)
{
CComVariant varItemNum = ndxDoc;
CComPtr<IDispatch> pDocDisp;
VERIFY_OK(pDocs->Item(varItemNum, &pDocDisp));
CComQIPtr<ITextDocument, &IID_ITextDocument> pTextDoc(pDocDisp);
if(!pTextDoc) continue;
CComVariant varMove = dsMove;
CComVariant varSearchOpt = dsMatchForward;
CComBSTR bstrSearch("");
VARIANT_BOOL vbool = VARIANT_TRUE;
long lineCount = 0;
CComPtr<IDispatch> pSelDisp;
VERIFY_OK(pTextDoc->get_Selection(&pSelDisp));
CComQIPtr<ITextSelection, &IID_ITextSelection> pSel(pSelDisp);
totalDocsRead++;
// Count lines in file...
VERIFY_OK(pSel->EndOfDocument(varMove));
VERIFY_OK(pSel->get_BottomLine(&lineCount));
totalLineCount += lineCount;
// Move the selection to the top of the file
VERIFY_OK(pSel->StartOfDocument(varMove));
// Count the number of semicolons in the file...
long prevLine = -1;
long prevCol = -1;
while(1)
{
pSel->FindText(bstrSearch, varSearchOpt, &vbool);
if(vbool != VARIANT_FALSE)
{
// If the new selection hasn't moved down the file
// break out of the search.
long currentLine;
long currentCol;
pSel->get_CurrentLine(¤tLine);
pSel->get_CurrentColumn(¤tCol);
if(currentLine < prevLine) break;
if((currentLine == prevLine)&&(currentCol <= prevCol)) break;
prevLine = currentLine;
prevCol = currentCol;
totalSemiCount++;
}
else
{
break;
}
}
}
dlg.m_dwFiles = totalDocsRead;
dlg.m_dwLines = totalLineCount;
dlg.m_dwSemiColons = totalSemiCount;
dlg.DoModal();
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
return S_OK;
}
The code provided in Listing 17.5 uses the CDlgProjInfo class to display project information. To compile the commands.cpp file, the following line must be added just after the existing #include directives at the top of the file:
#include "dlgprojinfo.h"
The source code in Listing 17.5 follows the pseudocode presented earlier in this chapter. The Developer Studio IDocuments interface is used to iterate over the collection of files opened in Developer Studio. Next, the ITextDocument interface is queried for. If this interface cannot be retrieved for an open document, the document isn't a source file; for example, open resource files are not capable of returning ITextDocument interface pointers. Next, the number of lines and semicolons in the document is calculated. Finally, after all documents have tested, the totals for all open source files are displayed to the user.
Modifying the Toolbar
The SourceInfo add-in toolbar icon is modified to make it more easily recognizable among other Developer Studio toolbar icons. Figure 17.6 shows the bitmap for the new toolbar icons.
Figure 17.6 The toolbar icons used for the SourceInfo Developer Studio add-in.
Note that two bitmaps are used for the toolbar—one for large 32x32–pixel buttons and one for 16x16–pixel buttons.
Using the SourceInfo Add-in
To begin using the SourceInfo add-in, you must compile the SourceInfo project and then copy the add-in to the Common\MSDev98\AddIns subdirectory under the Visual Studio installation directory.
Like the macro files discussed in the first half of this chapter, add-ins are not loaded automatically. You must specify that the add-in is to be loaded by following these steps:
- Choose Customize from the Developer Studio Tools menu. The Customize dialog box appears.
- Select the Add-Ins and Macro Files tab. One of the items displayed on this property page is a list of the currently loaded add-ins and macro files.
- Add-ins and macros that are loaded by Developer Studio have a check mark next to their icon in the list box. Click on the check box next to the SourceInfo icon to mark the add-in to be loaded.
- Click the Close button to exit the Customize dialog box. The SourceInfo toolbar appears inside Developer Studio.
Open one or more source files and click the SourceInfo icon. The SourceInfo dialog box appears, along with information about open source files. Figure 17.7 shows an example of the Source Information dialog box.
Figure 17.7 The SourceInfo Developer Studio add-in.
Summary | Next Section

Account Sign In
View your cart