Working with Microsoft® Visual Studio® 2005

Transcription

Working with Microsoft® Visual Studio® 2005
Working with Microsoft®
Visual Studio® 2005
Craig Skibo
Marc Young
Brian Johnson
X1146479A01.indd 1
9/27/2005 1:28:18 PM
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2006 by Microsoft Corporation
Information in this document, including URL and other Internet Web site references, is subject to change
without notice. Unless otherwise noted, the example companies, organizations, products, domain names,
e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with
any real company, organization, product, domain named, e-mail address, logo, person, place, or event is
intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the
user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in
or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical,
photocopying, recording, or otherwise), or for any purpose, without express written permission of
Microsoft Corporation.
Microsoft, ActiveX, Authenticode, IntelliSense, Microsoft Press, MSDN, Outlook, Visual Basic, Visual
C#, Visual C++, Visual J#, Visual Studio, Visual Web Developer, Win32, Windows, and Windows Media
are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other
countries.
This book expresses the authors' views and opinions. The information contained in this book is provided
without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its
resellers or distributors will be held liable for any damages caused or alleged to be caused either directly or
indirectly by this book.
6QO[OQOCPFFCF2GRRGTCPF-CVJ[,QJPUQP
6JCPMUHQTGXGT[VJKPI
²$,
6QO[RCTGPVU#NCPF,CPO[DTQVJGT$TKCPCPFCNNVJGHTKGPFU
YJQJCXGUWRRQTVGFOGQXGTVJG[GCTU
²%5
6Q,WNKC/CZCPF$TKIKVVG²MKUUGUJWIUCPFNCF[DWIU
²/;
Contents at a Glance
1
Introducing Visual Studio 2005 ...........................................................................1
2
Project Management in Visual Studio 2005 ..................................................17
3
The Visual Studio Editor.......................................................................................33
4
Community Content and VSTemplates ...........................................................55
5
Using Visual Studio Macros ................................................................................91
6
Extending the IDE with Add-Ins ..................................................................... 107
7
Exploring Commands Programmatically ..................................................... 131
8
Managing Solutions and Projects Programmatically ............................... 153
9
Programming the Visual Studio User Interface ......................................... 197
10
Text-Editing Objects and Events ..................................................................... 241
v
Table of Contents
Acknowledgments .............................................................................................................................xv
Introduction ..................................................................................................................................... xvii
1
Introducing Visual Studio 2005 ...................................................................................................... 1
What Is Visual Studio 2005? ...............................................................................................................1
Developing for .NET.................................................................................................................2
Building Native Applications ................................................................................................3
Visual Studio 2005 Features ................................................................................................................4
Editors, Designers, and Tool Windows ..............................................................................4
Visual Studio 2005 File Paths ............................................................................................. 11
Visual Studio 2005 Extensibility ...................................................................................................... 13
Macros ....................................................................................................................................... 14
Add-Ins ...................................................................................................................................... 15
Wizards ...................................................................................................................................... 15
Starter Kits ................................................................................................................................ 16
The Visual Studio SDK .......................................................................................................... 16
Looking Ahead ..................................................................................................................................... 16
2
Project Management in Visual Studio 2005 ............................................................................. 17
Overview of Solutions and Projects .............................................................................................. 17
Understanding Solutions .................................................................................................................. 18
Solution Items and Miscellaneous Files ......................................................................... 19
Solution Properties ................................................................................................................ 20
Solution and Solution User Options Files ..................................................................... 22
Projects .................................................................................................................................................... 22
Project Items ............................................................................................................................ 23
Project Properties .................................................................................................................. 24
Project Source Files ............................................................................................................... 28
Project Dependencies........................................................................................................... 29
Building Projects and Solutions ...................................................................................................... 30
Looking Ahead ..................................................................................................................................... 31
vii
viii
3
Table of Contents
The Visual Studio Editor.................................................................................................................. 33
Documents in the IDE ........................................................................................................................ 33
It’s All About Text ................................................................................................................... 34
Typing and Shortcuts............................................................................................................ 36
Other Keyboard Schemes ................................................................................................... 40
Understanding Tabs and Code Formatting .................................................................. 40
Other Editing Features in Visual Studio 2005 ........................................................................... 42
Code Snippets ......................................................................................................................... 42
Refactoring ............................................................................................................................... 43
Code Definition Window .................................................................................................... 43
Call Browser ............................................................................................................................. 43
Line Numbering and Outlining ...................................................................................................... 44
Line Numbering ..................................................................................................................... 44
Outlining ................................................................................................................................... 46
Programming Help ............................................................................................................................. 47
IntelliSense................................................................................................................................ 47
Using the Command Window ........................................................................................................ 49
Search, Replace, and Regular Expressions .................................................................................. 50
Incremental Searching ......................................................................................................... 52
Looking Ahead ..................................................................................................................................... 53
4
Community Content and VSTemplates ...................................................................................... 55
Community Content ........................................................................................................................... 55
Installing Content ................................................................................................................................ 56
Security ...................................................................................................................................... 58
Creating Downloadable Content ................................................................................................... 58
The VSContent File Format ................................................................................................ 58
Zipping....................................................................................................................................... 65
Signing Your Content ........................................................................................................... 66
Implementing Your Own Downloadable Types ........................................................................ 67
Creating the Project .............................................................................................................. 67
Interface Implementation ................................................................................................... 67
The Site Interface ................................................................................................................... 70
Registration .............................................................................................................................. 72
Table of Contents
ix
An Example—Samples Installer ........................................................................................ 74
Security Attributes ................................................................................................................. 74
Creating VSTemplates ........................................................................................................................ 75
Using the Export Template Wizard.................................................................................. 75
Creating Templates by Hand ............................................................................................. 78
The VSTemplate Schema ..................................................................................................... 81
Wizard Data ............................................................................................................................. 86
Storing the Template on Disk ............................................................................................ 87
Wizard Extensions .................................................................................................................. 87
Security Attributes ................................................................................................................. 90
Looking Ahead ..................................................................................................................................... 90
5
Using Visual Studio Macros ........................................................................................................... 91
Macros: The Duct Tape of Visual Studio ...................................................................................... 91
Recording Visual Studio Macros ....................................................................................... 92
Macro Commands ................................................................................................................. 94
Editing Macros in the Macros IDE ................................................................................... 95
A Simple Macro ...................................................................................................................... 97
Working with Macros ......................................................................................................................... 98
Manipulating Documents and Text ................................................................................. 98
Moving Windows .................................................................................................................100
Macro Events .........................................................................................................................102
Sharing Macros with Others ..........................................................................................................104
Exporting Modules and Projects ....................................................................................105
Looking Ahead ...................................................................................................................................106
6
Extending the IDE with Add-Ins .................................................................................................107
Running the Add-In Wizard...........................................................................................................107
The Add-In Project ............................................................................................................................109
Loading the Add-In...........................................................................................................................111
Debugging the Add-In ....................................................................................................................113
Add-In Architecture ..........................................................................................................................114
Writing an Add-In from Scratch .....................................................................................114
Add-In Events ........................................................................................................................117
x
Table of Contents
The IDTExtensibility2 Interface ........................................................................................119
The .Addin File ......................................................................................................................126
Looking Ahead ...................................................................................................................................130
7
Exploring Commands Programmatically .................................................................................131
What Is a Command? .......................................................................................................................131
Locating Commands...........................................................................................................132
Command Names ................................................................................................................133
Executing Commands ........................................................................................................134
Creating Macro Commands .............................................................................................135
Creating an Add-In Command .....................................................................................................135
Handling a Command Invocation .................................................................................137
Command State ....................................................................................................................138
How an Add-In Command Handler Is Found ...........................................................144
The Command User Interface .......................................................................................................144
The Command Bar Object Model..................................................................................145
The Primary Command Bar..............................................................................................146
Adding New Command Bar Elements .........................................................................147
Using Custom Bitmaps.......................................................................................................148
Regenerating Commands and Their User Interface .............................................................150
Looking Ahead ...................................................................................................................................151
8
Managing Solutions and Projects Programmatically ...........................................................153
Working with Solutions ...................................................................................................................153
Creating, Loading, and Unloading Solutions .............................................................154
Enumerating Projects .........................................................................................................155
Adding Projects to a Solution .........................................................................................156
Capturing Solution Events ................................................................................................158
Working with Project Items ...........................................................................................................163
Enumerating Project Items ...............................................................................................163
Adding and Removing Project Items............................................................................166
Working with Language-Specific Project Objects .................................................................170
VSProject Projects ................................................................................................................171
Using Visual Studio Utility Project Types...................................................................................176
Miscellaneous Files Project ...............................................................................................176
Table of Contents
xi
Solution Folders....................................................................................................................177
Unmodeled Projects ...........................................................................................................180
Project and Project Item Events ...................................................................................................181
Managing Build Configurations ...................................................................................................183
Manipulating Solution Settings ......................................................................................183
Manipulating Project Settings.........................................................................................189
Build Events ............................................................................................................................193
Persisting Solution and Project Information Across IDE Sessions ....................................194
Looking Ahead ...................................................................................................................................196
9
Programming the Visual Studio User Interface .....................................................................197
Window Basics ....................................................................................................................................197
The Windows Collection ...................................................................................................197
Using the Object Property ................................................................................................200
Shortcuts to Common Tool Windows ..........................................................................201
The Main Window ...............................................................................................................202
Explorer Windows and the UIHierarchy Object......................................................................203
The UIHierarchy Object Tree ............................................................................................203
The UIHierarchy Object .....................................................................................................205
The UIHierarchyItems Object...........................................................................................206
The UIHierarchyItem Object ............................................................................................207
The Toolbox Window .......................................................................................................................207
Tabs and Items ......................................................................................................................207
Adding Items to the Toolbox ...........................................................................................209
The Task List Window .......................................................................................................................210
Task List Items........................................................................................................................211
Adding New Tasks ...............................................................................................................211
The TaskItem Object ...........................................................................................................215
Task List Events ......................................................................................................................216
Comment Tokens .................................................................................................................218
The Error List Window......................................................................................................................221
The Output Window .........................................................................................................................221
Output Window Panes .......................................................................................................222
xii
Table of Contents
The Forms Designer Window ........................................................................................................224
The IDesignerHost Interface .............................................................................................224
Marshaling ..............................................................................................................................224
Adding Controls to a Form ..............................................................................................225
Finding Existing Controls ..................................................................................................225
A Form Layout Sample.......................................................................................................226
Creating Custom Tool Windows ...................................................................................................227
Setting the Tab Picture of a Custom Tool Window..................................................231
Setting the Selection Object.............................................................................................232
The Options Dialog Box ..................................................................................................................233
Changing Existing Settings ...............................................................................................233
Creating Custom Settings .................................................................................................237
Looking Ahead ...................................................................................................................................239
10
Text-Editing Objects and Events .................................................................................................241
Editor Windows ..................................................................................................................................241
The Window Object.............................................................................................................241
The TextWindow and HTMLWindow Objects .............................................................242
The TextPane Object ...........................................................................................................244
Documents ...........................................................................................................................................246
The Document Object ........................................................................................................246
The TextDocument Object ................................................................................................250
Point Objects .......................................................................................................................................250
The TextPoint Object ...........................................................................................................250
The VirtualPoint Object .....................................................................................................251
The EditPoint Object ...........................................................................................................253
The TextSelection Object .................................................................................................................253
A Comparison of the TextSelection and EditPoint Objects ....................................254
Undo Contexts ....................................................................................................................................256
Automatic Undo Contexts ................................................................................................256
Creating Undo Contexts ....................................................................................................257
Stack Linkage .........................................................................................................................258
Text Editor Events...............................................................................................................................259
The BeforeKeyPress and AfterKeyPress Events ..........................................................259
The LineChanged Event .....................................................................................................261
Looking Ahead ...................................................................................................................................262
Table of Contents
xiii
Index ...................................................................................................................................................263
Acknowledgments
*UNJHIUOPUUBLFBWJMMBHFUPXSJUFBCPPLCVUJUUBLFTBHPPETJ[FEUFBNUPQV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xix
Questions and Comments
*GZPVIBWFRVFTUJPOTDPNNFOUTPSJEFBTSFHBSEJOHUIFCPPLPSUIFDPNQBOJPODPOUFOUPS
RVFTUJPOTUIBUBSFOPUBOTXFSFECZWJTJUJOHUIFTJUFTBCPWFQMFBTFTFOEUIFNUP.JDSPTPGU
1SFTTWJBFNBJMUP
OURKPRWV"OKETQUQHVEQO
0SWJBQPTUBMNBJMUP
.JDSPTPGU1SFTT
"UUOWorking with Microsoft Visual Studio 2005&EJUPS
0OF.JDSPTPGU8BZ
3FENPOE8"
1MFBTFOPUFUIBU.JDSPTPGUTPGUXBSFQSPEVDUTVQQPSUJTOPUPGGFSFEUISPVHIUIFBCPWF
BEESFTTFT
Chapter 1
Introducing Visual Studio 2005
In this chapter:
What Is Visual Studio 2005?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Visual Studio 2005 Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Visual Studio 2005 Extensibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Looking Ahead. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
*OUIJTDIBQUFSXFµMMQSPWJEFBCSJFGPWFSWJFXPG.JDSPTPGUˆ7JTVBM4UVEJPˆ8FµMM
TIPXZPVTPNFPGUIFGFBUVSFTPGUIFJOUFHSBUFEEFWFMPQNFOUFOWJSPONFOU*%&
UPQSPWJEF
TPNFDPOUFYUGPSUIFFYUFOTJCJMJUZBOEDVTUPNJ[BUJPOEJTDVTTJPOUISPVHIPVUUIFSFTUPGUIF
CPPL"OE¾OBMMZXFµMMEJTDVTTUIFFYUFOTJCJMJUZGFBUVSFTUIBUNBLF7JTVBM4UVEJPBO
FYUSFNFMZBUUSBDUJWFUPPMGPSQSPHSBNNFSTXIPBSFMPPLJOHUPDVTUPNJ[FBOEFYUFOEUIFJS
EFWFMPQN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FTXIBUB/&5DPNQMJBOUMBOHVBHFNVTU
QSPWJEFUPUIFTZTUFN5IFDPNNPOUZQFTZTUFN$54
FOTVSFTUIBUBOZUZQFTDSFBUFECZB
MBOHVBHFDPOGPSNJOHUPUIF$-4DBOCFDPOTVNFECZBOZPUIFS$-4DPNQMJBOUMBOHVBHF
-BOHVBHFTUIBUUBSHFUUIF$-3BSFDPNQJMFEUP.JDSPTPGUJOUFSNFEJBUFMBOHVBHF.4*-
5IFTFBQQMJDBUJPOTBSFDPNQJMFEBT1&QPSUBCMFFYFDVUBCMF
¾MFTBOE%--TTPUPVTFST
UIFZMPPLKVTUMJLFBOZ8JOEPXTCBTFEBQQMJDBUJPOT5IF.4*-DPEFJOUIFTF¾MFTJTUIFO
+*5DPNQJMFEUPNBDIJOFJOTUSVDUJPOTMPDBMMZBUSVOUJNF"MMPGUIJTNFBOTUIBUBOZ$-4
DPNQMJBOUMBOHVBHFUIBUUBSHFUTUIF$-3XJMMMPPLMJLFBOZPUIFSMBOHVBHFUPUIFSVOUJNF
BOEDBOBDUBOECFUSFBUFEBTB¾STUDMBTTDJUJ[FO'PSFYBNQMFB7JTVBM#BTJDQSPHSBN
XJMMIBWFUIFTBNFCBTFGVODUJPOBMJUZBTB7JTVBM$QSPHSBNPSFWFOBNBOBHFE
$QSPHSBN
Tip For a detailed look at the architecture of .NET, take a look at Applied Microsoft .NET
Framework Programming by Jeffrey Richter (Microsoft Press®, 2002).
5IFNBOBHFE$-3FOWJSPONFOUQSPWJEFTTPNFPUIFSTJHOJ¾DBOUBEWBOUBHFT*UµTEFTJHOFE
UPIFMQFMJNJOBUFWFSTJPOJOHDPO¿JDUT*UµTEFTJHOFEUPQSPWJEFBOFOWJSPONFOUUIBUFOTVSFT
UIBUDPEFJTFYFDVUFETBGFMZ"OE¾OBMMZJUµTEFTJHOFEXJUIBO"1*UIBUJTUBSHFUBCMFGSPN
CPUI8JOEPXTCBTFEBOE8FCCBTFEBQQMJDBUJPOT
:PVEPOµUOFFEUPUIJOLBCPVUUIF/&5'SBNFXPSLBTBNPOPMJUIJDWJSUVBMNBDIJOFUIBU
SFRVJSFTDPOTUBOUDBSFBOEGFFEJOH5IF/&5'SBNFXPSLQSPWJEFTBOFOWJSPONFOUUIBUDBO
CFIPTUFECZVONBOBHFEDPNQPOFOUT5IFVONBOBHFEDPNQPOFOUTTVDIBT.JDSPTPGU
*OUFSOFU&YQMPSFSBOEUIF.JDSPTPGU"41/&5SVOUJNF
MPBEUIF$-3BOEFYFDVUFUIF
NBOBHFEDPEF5IFNBOBHFE$-3QSPWJEFTHBSCBHFDPMMFDUJPOTFSWJDFTBOETFDVSJUZPOB
OVNCFSPGMFWFMT
Chapter 1:
Introducing Visual Studio 2005
3
'PSDPSQPSBUFEFWFMPQFSTUIJTSVOUJNFTPMWFTBIVHFQSPCMFN*ONBOZTIPQTUIF7JTVBM
#BTJDQSPHSBNNFSTUIF$$QSPHSBNNFSTBOEUIF$0#0-QSPHSBNNFSTBSFBMM
TFHSFHBUFE5IFZNFFUUP¾HVSFPVUIPXUPGVODUJPOBMMZJOUFSPQFSBUFCVUJOBOVNCFSPG
XBZTUIFZXPSLBTJOEJWJEVBMUFBNTJOTJEFUIFTBNFTQBDF*OBTIPQUIBUUBSHFUTUIF$-3
EFWFMPQNFOUCFDPNFTBMJUUMFNPSFNBOBHFBCMF5IFTBNF/&5'SBNFXPSLDMBTTMJCSBSZ
JTBWBJMBCMFBDSPTTMBOHVBHFT5IF$54JOUIFDMBTTMJCSBSZFOTVSFTUIBUDPNQPOFOUTDBO
CFFBTJMZTIBSFECFUXFFO/&5MBOHVBHFTBTTIPXOJO'JHVSF5IFTFDPNQPOFOUTDBO
FWFOCFFYQPTFEBT8FCTFSWJDFT
Visual C#
Web Forms application
MSIL
Native code
Visual Basic .NET
class library
MSIL
Native code
Managed Visual C++
class library
MSIL and native code
Native code
Figure 1-1 .NET allows different languages to target a managed environment and
to interoperate securely and efficiently.
5IF$-3QSPWJEFTBUBSHFUUIBU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VEJP
MJFTJOJUTBCJMJUZUPFNQPXFSVTFSTUPCVJMEUFTUBOEEFCVHQPXFSGVMBQQMJDBUJPOT
RVJDLMZBOEFBTJMZ
4
Working with Microsoft Visual Studio 2005
Visual Studio 2005 Features
*OUIJTTFDUJPOXFµMMQSFTFOUBOPWFSWJFXPGUIF7JTVBM4UVEJPGFBUVSFTFU8FµMMMPPLBU
TPNFPGUIFTFGFBUVSFTJONPSFEFUBJMJOUIFOFYUGFXDIBQUFSTPGUIFCPPL)FSFXFµMMEF¾OF
TPNFDPNNPOUFSNTUIBUXFDBOVTFUPEFTDSJCFUIFEJGGFSFOUQBSUTPGUIF*%&7JTVBM4UVEJP
JTBGBJSMZMBSHFBOEDPNQMFYQSPEVDU5IFUFSNTVTFEUPEFTDSJCFUIF*%&BSFIFMQGVM
GPSEFWFMPQFSTXIPBSFXPSLJOHUPVOEFSTUBOEUIFUPPMBOEQFSIBQTNPSFJNQPSUBOUGPS
EFWFMPQFSTXIPXJMMFWFOUVBMMZFYUFOEUIF*%&UISPVHINBDSPTBOEBEEJOT
Editors, Designers, and Tool Windows
5IFXJOEPXTJOUIF7JTVBM4UVEJP*%&GBMMJOUPUXPNBKPSHSPVQT&QEWOGPVYKPFQYU
BSFXJOEPXTUIBUVTVBMMZBQQFBSUBCCFEJOUIFDFOUFSPGUIF*%&BOEUIBUDPOUBJOFEJUPST
EFTJHOFST8FCQBHFTPS)FMQUPQJDT6QQNYKPFQYUBSFXJOEPXTJOUIF*%&UIBUQSFTFOU
VUJMJUZGVODUJPOTUPUIFQSPHSBNNFS5IFUPPMXJOEPXTJODMVEF4PMVUJPO&YQMPSFSUIF$MBTT
7JFXXJOEPXBOEUIF1SPQFSUJFTXJOEPX5PPMXJOEPXTBSFEJTUJODUGSPNFEJUPSTBOE
EFTJHOFSTJOUIFXBZUIFZEPDLBSPVOEUIFTJEFTPGUIF*%&
Note The extensibility API built into Visual Studio 2005 allows programmers to create tool
windows for use with language packages installed into the IDE. The editors and designers
in the IDE can be accessed through this API, but the extensibility model doesn’t allow the
creation of new document window types. For that you’ll need to look into the Visual Studio
software development kit (SDK), which we’ll describe at the end of this chapter.
'JHVSFTIPXTBUZQJDBMEFWFMPQFSTFUVQ
Figure 1-2
A typical solution in Visual Studio 2005
Chapter 1:
Introducing Visual Studio 2005
5
Note All of the managed languages that ship with Visual Studio 2005 feature designer
support in the IDE.
The Start Page
5IF¾STUUJNFZPVSVO7JTVBM4UVEJPZPVµSFQSFTFOUFEXJUIB4UBSU1BHFXIJDI
DPOUBJOTMJOLTUPQSFWJPVTMZPQFOFEQSPKFDUTBOEJOGPSNBUJPOSFMFWBOUUPUIFQSFGFSSFE
DPMMFDUJPOPGTFUUJOHTGPSUIFEFWFMPQFSUZQFDIPTFOUIF¾STUUJNFUIF*%&JTPQFOFE"
UGVVKPIUEQNNGEVKQPJTBXJOEPXLFZCPBSEBOE)FMQMBZPVUUIBUµTUBJMPSFEUPBTQFDJ¾DUZQFPG
QSPHSBNNFS:PVµSFQSFTFOUFEXJUIBDIPJDFPGTFUUJOHTXIFOZPV¾STUSVO7JTVBM4UVEJP
:PVDBODIBOHFUIFTFUUJOHTUIBUZPVXBOUUPSVOVOEFSCZPQFOJOHUIF*NQPSUBOE
&YQPSU4FUUJOHT8J[BSEBWBJMBCMFGSPNUIF5PPMTNFOV5IJTXJ[BSEBMMPXTZPVUPCBDLVQ
BOEUPUSBOTGFSZPVSTFUUJOHTBOEDVTUPNJ[BUJPOCFUXFFODPNQVUFST:PVDBOVTFUIJTXJ[BSE
UPSFTFUZPVSTFUUJOHTCZDIPPTJOH3FTFU"MM4FUUJOHTBTTIPXOJO'JHVSF
Figure 1-3
Resetting the IDE in the Import and Export Settings Wizard
'JHVSFTIPXTUIFJOJUJBMTFUUJOHTZPVDBODIPPTFGSPNJO7JTVBM4UVEJP
*O$IBQUFSXFµMMEJTDVTTDVTUPNJ[JOHUIF*%&BOETBWJOHZPVSTFUUJOHTBTBCBDLVQPSGPS
SFVTFPOPUIFSDPNQVUFST
The Editor
*OUBMLJOHXJUINFNCFSTPGUIFUFBNUIBUEFWFMPQFEUIFCBTFFEJUPSJO7JTVBM4UVEJP
JUµTDMFBSUIBUUIFZVOEFSTUBOEUIBUQSPHSBNNFSTMJWFJOUIFFEJUPS*UµTXIFSFUIFNPTU
JNQPSUBOUQSPHSBNNJOHXPSLJTEPOF5PUIJTFOEUIF7JTVBM4UVEJPUFBNIBTDPOUJOVFE
UPXPSLIBSEUPDSFBUFBDPEFFEJUPSUIBUµTPOFPGUIFCFTUBWBJMBCMFUPEBZ5PBHSFBUFYUFOU
6
Working with Microsoft Visual Studio 2005
UIFZIBWFTVDDFFEFEJOUIJTHPBMMBSHFMZCFDBVTFPGFOIBODFNFOUTUPUIFFYUFOTJCJMJUZ
GFBUVSFTPGUIF*%&5IFTFFOIBODFNFOUTJODMVEFBOFYUSFNFMZQPXFSGVMFYUFOTJCJMJUZ
QSPHSBNNJOHNPEFMBNBDSPSFDPSEJOHGBDJMJUZBOEBEFEJDBUFE.BDSPT*%&5IF7JTVBM
4UVEJPFYUFOTJCJMJUZNPEFMJTBNBKPSGPDVTPGUIFCPPLCFDBVTFJUµTXIBUXFVTF
UPDVTUPNJ[FBOEUPBEEGVODUJPOBMJUZUPUIF*%&
Figure 1-4
Choose from a number of different settings collections to begin to customize the IDE.
0UIFSGFBUVSFTJOUIFFEJUPSJODMVEFDPEFTOJQQFUTTNBSUUBHTDIBOHFUSBDLJOHPVUMJOJOH
MJOFOVNCFSJOHBOEBSFBMMZPVUTUBOEJOHTFBSDIBOESFQMBDFGBDJMJUZBMMPGXIJDIBSF
EJTDVTTFEJOEFUBJMJO$IBQUFS
"OFXGFBUVSFJO7JTVBM4UVEJPBMMPXTZPVUPUBCUISPVHIZPVSPQFOEPDVNFOUTBOE
UPPMXJOEPXTCZQSFTTJOH$USM5BC*O'JHVSFZPVDBOTFFUIFEJBMPHCPYUIBUBQQFBST
XIFOZPVQSFTTUIJTDPNCJOBUJPO:PVDBOVT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X
Figure 1-7
The Class View window gives you an alternative view of the objects in your solution.
Properties (F4) *OUIF1SPQFSUJFTXJOEPXZPVDBOHFUBOETFUQSPQFSUJFTGPSUIFVTFS
JOUFSGBDFJUFNTUIBUZPVBEEUP8JOEPXT'PSNTBOE8FC'PSNTBQQMJDBUJPOT:PVDBOBMTP
VTFUIJTXJOEPXUPTFUQSPQFSUJFTGPSTPMVUJPOTQSPKFDUTBOE¾MFTUIBUZPVIBWFTFMFDUFEJO
4PMVUJPO&YQMPSFS'JHVSFTIPXTUIF1SPQFSUJFTXJOEPXGPSBQSPKFDU
Figure 1-8 You can use the Properties window to set properties of components, projects, and solutions.
Chapter 1:
Introducing Visual Studio 2005
9
Server Explorer (Ctrl+Alt+S) :PVVTFUIF4FSWFS&YQMPSFSXJOEPXUPBDDFTTEBUBTPVSDFT
BOEJOGPSNBUJPOPOZPVSMPDBMNBDIJOFBOEPOSFNPUFTFSWFST5ISPVHIUIJTXJOEPXZPVDBO
NBLFEBUBDPOOFDUJPOTBDDFTTQFSGPSNBODFDPVOUFSTBOEFWFOUMPHTBOEFWFONBOBHFTZTUFN
TFSWJDFT&WFOXIFOVTFEMPDBMMZUIJTUPPMDBOTBWFZPVBHSFBUEFBMPGUJNFMFUUJOHZPVFBTJMZ
TUBSUBOETUPQTZTUFNTFSWJDFTUIBUZPVµSFUFTUJOHBOEBDDFTTTZTUFNMPHT*O'JHVSFZPVDBO
TFFUXPNBDIJOFTBWBJMBCMFJO4FSWFS&YQMPSFS5IF¾STUNBDIJOFJTBSFNPUFUFTUTFSWFS5IF
TFDPOENBDIJOFJTUIFMPDBMNBDIJOFPOXIJDI7JTVBM4UVEJPJTSVOOJOH
Note Keep in mind that you’ll need the proper level of access on a particular server to
access system information.
Figure 1-9 The Server Explorer window provides you with remote access to the machines you’re
working with.
Toolbox (Ctrl+Alt+X) 5IF7JTVBM4UVEJP5PPMCPYXJOEPXJTVTFEUPIPMEUIFDPOUSPMT
UIBUZPVBEEUPZPVS8JOEPX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$PNNBOENPEFUZQFJNNFE5IJTXJMMPQFO
BO*NNFEJBUF8JOEPXUBCBMMPXJOHZPVUPFYFDVUFEFCVHHJOHTUBUFNFOUT5PHFUCBDLUP
UIF$PNNBOENPEFUBCGSPN*NNFEJBUFNPEFUZQFDNE
Tip In Command mode, the > prompt will be visible on the line where you’re typing your
commands. Immediate mode shows no prompt.
'JHVSFTIPXTUIF$PNNBOE8JOEPX
Figure 1-11 The Command Window in Visual Studio 2005 provides easy access to named
commands in the IDE.
Chapter 1:
Introducing Visual Studio 2005
11
Macro Explorer (Alt+F8) 5IF.BDSP&YQMPSFSXJOEPXQSPWJEFTBWJFXPGUIFNBDSP
QSPKFDUTUIBUBSFDVSSFOUMZMPBEFEJOUIF*%&,FFQJONJOEUIBUBNBDSPQSPKFDUOFFETUP
CFMPBEFEJOPSEFSGPSUIFNBDSPTJOUIFQSPKFDUUPCFBWBJMBCMFGPSVTFPSGPSFEJUJOHJOUIF
.BDSPT*%&
8IFOZPVSFDPSEBUFNQPSBSZNBDSPCZQSFTTJOH$USM4IJGU3UIBUNBDSPCFDPNFTBWBJMBCMF
UISPVHIUIF.Z.BDSPT=3FDPSEJOH.PEVMF=5FNQPSBSZ.BDSPJUFNJO.BDSP&YQMPSFS:PV
DBOSFOBNFUIFUFNQPSBSZNBDSPUPTBWFJUPSZPVDBODPQZUIFDPEFGSPNUIFNBDSPJOUP
BOPUIFSNPEVMFJOUIF.BDSPT*%&8FµMMEJTDVTTVTJOHSFDPSEFENBDSPTJONPSFEFUBJMJO
$IBQUFS5IF.BDSP&YQMPSFSXJOEPXJTTIPXOJO'JHVSF
Figure 1-12
Macro Explorer gives you easy access to the macros available for use.
5IF*%&IBTBOVNCFSPGPUIFSJNQPSUBOUXJOEPXTXIJDIXFµMMUBMLBCPVUNPSFGVMMZJO
UIFOFYUDPVQMFPGDIBQUFST"NPOHUIFTFBSFUIFWBSJPVTEFCVHHJOHXJOEPXTUIF)FMQ
XJOEPXTBOEUIF0CKFDU#S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FTBSFJO7#*O7JTVBM4UVEJPXFVTFEUIFTFGPMEFSTUP
DSFBUFBOEBEEDVTUPNQSPKFDUTUPUIFWBSJPVTMBOHVBHFTJOUIF*%&*O7JTVBM4UVEJP
ZPVDBOTUPSFZPVSUFNQMBUFTJOUIF.Z%PDVNFOUT=7JTVBM4UVEJPGPMEFS5IJTNBLFT
JUNVDIFBTJFSGPSZPVUP¾OEBOEFYUFOEUIFTFQSPKFDUUZQFTJOBEEJPOUPNBLJOHJUFBTJFSUP
SVOBTBOPOBENJOJTUSBUPSPOZPVSXPSLTUBUJPO
Tip You’ll notice a file named Samples.vsmacros in the Common7\IDE folder. The sample
macros for Visual Studio 2005 that run in your IDE are actually stored in your My Documents\
Visual Studio\ Projects\VSMacros80 folder. The version in the IDE folder is a backup copy.
You can edit the Samples.vsmacros file in your My Documents\Visual Studio\Projects\
VSMacros80\Samples folder, but try to keep the version in your IDE folder clean. If you ever
run into a macro corruption problem, you can usually copy the Samples.vsmacros file from
your IDE folder to your VSMacros folder to get rid of the problem.
5IF*%&FYFDVUBCMFJUTFMGJT%FWFOWFYF5IJT¾MFJTBMTPBWBJMBCMFJOUIF$PNNPO=*%&
TVCGPMEFS5IF*%&GPMEFSDPOUBJOTBOVNCFSPGTVCGPMEFSTUIBUZPVµMMCFVTJOHUISPVHIPVUUIF
CPPL5IFTFGPMEFSTJODMVEFUIF1VCMJD"TTFNCMJFTBOE1SJWBUF"TTFNCMJFTGPMEFSTXIJDIZPVµMM
VTFUPBEEDVTUPNBTTFNCMJFTUIBUBSFBWBJMBCMFUPNBDSPTJOUIF*%&:PVµMMVTFUIF)5.-
GPMEFSUPDVTUPNJ[FUIF4UBSU1BHF5IFUFNQMBUFTGPSUIFNBDSPQSPKFDUTBSFTUPSFEJOUIF
.BDSP1SPKFDU*UFNTBOE.BDSP1SPKFDUTGPMEFST(FOFSJDJUFNUFNQMBUFTUIPTFOPUBTTPDJBUFE
XJUIBQBSUJDVMBSQSPKFDUUZQF
BSFTUP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F7JTVBM4UVEJP5PPMTGPMEFSXIJDIJTJOUIF.JDSPTPGU7JTVBM4UVEJPGPMEFS
Tip We suggest pinning the Visual Studio 2005 Command Prompt link to the Start menu
so that you’ll have easy access to it as your primary command prompt.
5IF7JTVBM4UVEJPFOWJSPONFOUBMWBSJBCMFTBSFBWBJMBCMFJOB¾MFOBNFEWTWBSTCBU
XIJDIJTJOUIF$PNNPO=5PPMTTVCGPMEFS*GZPVXBOUBDDFTTUPUIFTFWBSJBCMFTGSPNFWFSZ
JOTUBODFPG$NEFYFPOZPVSNBDIJOFZPVDBOBEE$=1SPHSBN'JMFT=.JDSPTPGU7JTVBM
4UVEJP=$PNNPO=5PPMTUPZPVSTZTUFNQBUI"MUFSOBUJWFMZZPVDBODPQZUIJT¾MFUPB
GPMEFSJOZPVSQBUI
5IFOZPVDBOKVTUUZQFWTWBSTGSPNBOZDPNNBOEQSPNQUBOEZPVµMM
IBWFB7JTVBM4UVEJPXPSLJOHFOWJSPONFOUGSPNZPVSDVSSFOUDPNNBOEQSPNQU
:PVDBOUBLFUIJTPOFTUFQGVSUIFSCZDSFBUJOHB$PNNBOEJUFNPOUIF5PPMTNFOV:PV
DSFBUFBOFXNFOVJUFNGSPNUIF&YUFSOBM5PPMTEJBMPHCPYCZDMJDLJOH"EENBLJOHUIF
5JUMFPGUIFOFXJUFN$PNNBOE1SPNQUBOENBLJOHUIF$PNNBOEJUFNDNEFYF*GZPV
XBOUZPVDBOTFUUIF*OJUJBM%JSFDUPSZCPYUP1SPKFDU%JS
4FUUJOHUIF*OJUJBM%JSFDUPSZUP
ZPVSQSPKFDUEJSFDUPSZXJMMPQFOUIFDPNNBOEQSPNQUUPUIBUEJSFDUPSZ5IJTDBONBLFJU
WFSZDPOWFOJFOUUPXPSLXJUIZPVSQSPKFDU¾MFTGSPNUIFDPNNBOEMJOF
'JOBMMZDPOTJEFSBEEJOHUIF$PNNPO=*%&QBUIUPZPVSTZTUFNWBSJBCMFT5IFGVMMQBUIJT
$=1SPHSBN'JMFT=.JDSPTPGU7JTVBM4UVEJP=$PNNPO=*%&5IJTXJMMNBLF%FWFOWFYF
BWBJMBCMFGSPNBOZDPNNBOEQSPNQUPOZPVSTZTUFN5IJTQBUIJTBEEFECZUIFWTWBSTCBU
DPNNBOECVUTPNFUJNFTZPVOFFEBDDFTTKVTUUP%FWFOWFYF
Tip Consider using Devenv over Notepad when you’re editing files for command-line
builds. Even though you might not get access to the build and project facility without a solution, you still have access to your custom tools and to your macros.
Visual Studio 2005 Extensibility
7JTVBM4UVEJPCVJMETPOBOFYUFOTJCJMJUZNPEFMUIBUXBT¾STUEFWFMPQFEGPS7JTVBM$
*O7JTVBM4UVEJPUIF&6'PCKFDU±&6'JOUIJTDPOUFYUTUBOETGPS%FWFMPQNFOU5PPMT
&YUFOTJCJMJUZ±TJUTBUUIFUPQMFWFMPGBOBVUPNBUJPONPEFMUIBUGFBUVSFTOFBSMZPCKFDUT
5IFGVODUJPOBMJUZQSPWJEFECZUIF%5&PCKFDUNPEFMDBOCFEFTDSJCFEBTVTFSEF¾OFE
DVTUPNJ[BUJPO5IF%5&"1*JTBWBJMBCMFUPEFWFMPQFSTXIPBSFQSPHSBNNJOHNBDSPT
XJ[BSETBOEBEEJOT&WFONPSFGVODUJPOBMJUZJTFYQPTFEUPDPNNFSDJBMMBOHVBHFEFWFMPQFST
XIPVTFUIF7JTVBM4UVEJP4%,UPBEEDVTUPNMBOHVBHFTBOEEFTJHOFSTVQQPSUUPUIF*%&
14
Working with Microsoft Visual Studio 2005
5IFGPMMPXJOHTFDUJPOTEFTDSJCFUIFBVUPNBUJPONFDIBOJTNTBWBJMBCMFUPEFWFMPQFSTXIPBSF
DVTUPNJ[JOH7JTVBM4UVEJP
Macros
5IFNBDSPTGBDJMJUZJO7JTVBM4UVEJPQSPWJEFTQSPHSBNNFSTXJUIFBTZBDDFTTUPUIF
GFBUVSFTBWBJMBCMFUISPVHIUIFBVUPNBUJPO"1*T5IFNBDSPTGBDJMJUZGFBUV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DSFBUFEVTJOHUIF+&69K\CTF
JOUFSGBDF8J[BSETBSFGBJSMZTJNQMFDPOTUSVDUJPOTUIBUBSFEFTJHOFEUPUBLFBVTFSTUFQCZ
TUFQUISPVHIBTQFDJ¾DUBTL
8J[BSETBSFVTFEJO7JTVBM4UVEJPGPSBWBSJFUZPGQVSQPTFT1SPKFDUXJ[BSETIFMQHFUZPV
TUBSUFEPOBQBSUJDVMBSUZQFPG7JTVBM4UVEJPQSPKFDUBTTIPXOJO'JHVSF0UIFSXJ[BSET
JOUIF*%&XBMLZPVUISPVHIBEEJOHDPEFUPBOFYJTUJOHQSPKFDU
Figure 1-14
The MFC ActiveX® Control Wizard provides a starting point for a project.
16
Working with Microsoft Visual Studio 2005
Starter Kits
4UBSUFSLJUTBSFBOFXUZQFPGQSPKFDUUFNQMBUFUIBUMFUTZPVUBLFBOFYJTUJOHQSPKFDUBOEUIFO
TVQQMZJUUPPUIFSTBTBOJOTUBMMBCMFQBDLBHF*O$IBQUFSXFµMMTIPXZPVIPXUPDSFBUFZPVS
PXOTUBSUFSLJUTTPUIBUZPVDBOTIBSFFYUFOEBCMFQSPKFDUTXJUIUIFEFWFMPQFSDPNNVOJUZ
The Visual Studio SDK
5IF7JTVBM4UVEJP4%,QSPWJEFTIPPLTJOUPUIF*%&UIBUBMMPXEFWFMPQFSTUPBEEUIFJSPXO
MBOHVBHFTBOEEFTJHOFSTUP7JTVBM4UVEJP%FWFMPQFSTDBOVTFUIFTFIPPLTUPJOUFHSBUFOFX
/&5MBOHVBHFTBOEIJHIFOEUPPMTJOUPUIF*%&#FDBVTFEFWFMPQJOHXJUIUIF4%,JTGBJSMZ
DPNQMJDBUFEXFEJEOµUUIJOLUIBUXFDPVMEEPJUKVTUJDFJOUIJTFEJUJPOPGUIFCPPL:PVDBO
¾OEPVUNPSFBCPVUJUUISPVHIUIF7JTVBM4UVEJP%FWFMPQFS$FOUFSBUJVVROUFPOKETQUQHV
EQOXUVWFKQ
Looking Ahead
*O$IBQUFSXFµMMDPOUJOVFPVSEJTDVTTJPOPG7JTVBM4UVEJPGPDVTJOHPOUIFQSPKFDU
NBOBHFNFOUGBDJMJUJFTPGUIF*%&5IFDIBQUFSXJMMQSPWJEFTPNFJOTJHIUJOUPPOFPGUIFHSFBU
TUSFOHUITPG7JTVBM4UVEJPUIFBCJMJUZUPIPTUQSPKFDUTCBTFEPOEJGGFSFOUQSPHSBNNJOH
MBOHVBHFTJOBTJOHMFTPMVUJPO
Chapter 2
Project Management in Visual
Studio 2005
In this chapter:
Overview of Solutions and Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building Projects and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
18
22
30
31
/FBSMZFWFSZUIJOHZPVEPJO.JDSPTPGUˆ7JTVBM4UVEJPˆSFWPMWFTBSPVOETPMVUJPOTBOE
QSPKFDUT*OUIJTDIBQUFSXFµMMUBMLBCPVUTPMVUJPOTBOEQSPKFDUTJOEFUBJMBOEHJWFZPVBHPPE
VOEFSTUBOEJOHPGXIBUUIPTFUFSNTSFBMMZNFBO8FµMMBMTPEFTDSJCFQSPKFDUNBOBHFNFOUJO
7JTVBM4UVEJPBOEFYQMBJOIPXZPVDBOPSHBOJ[FZPVSTPGUXBSFQSPKFDUTUPNBYJNJ[F
UIFGFBUVSFTPGUIFJOUFHSBUFEEFWFMPQNFOUFOWJSPONFOU*%&
Overview of Solutions and Projects
.BOBHJOHDPNQMFYTPGUXBSFQSPKFDUTDBOCFBEJG¾DVMUBOENFTTZBGGBJS7JTVBM4UVEJP
IFMQTCZPSHBOJ[JOHQSPHSBNNJOHQSPKFDUTBTTPMVUJPOTHSPVQTPGQSPKFDUT
BOEQSPKFDUT
BOECZIBOEMJOHSFGFSFODFTUPBTTFNCMJFTBOEUPDPNQPOFOUTPVUTJEFUIJTTUSVDUVSF5IJT
PSHBOJ[BUJPOBOESFGFSFODFGFBUVSFIFMQTQSPNPUFDPEFSFVTFCZBMMPXJOHZPVUPUBLF
BEWBOUBHFPGSFMBUFEQSPKFDUTFYJTUJOHBTTFNCMJFT$0.DPNQPOFOUTBOETPVSDFDPEF5IF
FBTJFTUXBZUPSFVTF.JDSPTPGU/&5DPEFJTUISPVHISFGFSFODFTUPBTTFNCMJFTJOZPVSQSPKFDUT
BOETPMVUJPOT
Important Visual Studio 2005 organizes software projects on two conceptual levels.
Solutions contain projects and solution items. Projects contain the source files that are
compiled into executables and assemblies.
5IFNPTUJNQPSUBOUUPPMGPSQSPKFDUNBOBHFNFOUJO7JTVBM4UVEJPJT4PMVUJPO&YQMPSFS
$USM"MU-
TIPXOJO'JHVSF4PMVUJPO&YQMPSFSVTFTBUSFFWJFXXJOEPXUPQSPWJEFBDDFTT
UPBMMUIFQSPKFDUTBOE¾MFTUIBUBSFQBSUPGUIFDVSSFOUMZPQFOTPMVUJPO7JTVBM4UVEJP
17
18
Working with Microsoft Visual Studio 2005
DBOIPTUPOFTPMVUJPOBUBUJNFCVUZPVDBOSVONVMUJQMFJOTUBODFTPG7JTVBM4UVEJPJGZPV
XBOUUPXPSLXJUINVMUJQMFTPMVUJPOTDPODVSSFOUMZ
Figure 2-1
Solutions act as containers for projects and solution items.
.PTUOFXQSPKFDUTJO7JTVBM4UVEJPBSFDSFBUFEVTJOHBUFNQMBUFEFWFMPQFECZB
MBOHVBHFJOUFHSBUPS'PSFYBNQMF7JTVBM4UVEJPTIJQTXJUITVQQPSUGPS.JDSPTPGU
7JTVBM#BTJDˆ7JTVBM$ˆ7JTVBM+ˆBOE7JTVBM$ˆ&BDIPGUIFTFMBOHVBHFTGFBUVSFTB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FMJOLFE¾MFJTBHHSFHBUFE
JOUPUIF.JTDFMMBOFPVT'JMFTGPMEFSJOBTPMVUJPO
,FFQJONJOEUIBUBOZ¾MFZPVPQFOGSPN7JTVBM4UVEJPHFUTBMJOLJOUIF.JTDFMMBOFPVT
'JMFTGPMEFS5IJTGPMEFSQFSTJTUTZPVSJUFNTCFUXFFOTFTTJPOTJGZPVTFU.JTDFMMBOFPVT
'JMFT1SPKFDU4BWFT-BTUUP¾WFJUFNTPSTP5IJTNFBOTUIBUZPVDBOPQFOTQFDJ¾DBUJPOT
TDIFEVMFTBOEOPUFTBOEIBWFUIPTF¾MFTBUZPVS¾OHFSUJQTFWFSZUJNFZPVPQFOZPVS
QSPKFDUBTTIPXOJO'JHVSF
20
Working with Microsoft Visual Studio 2005
Figure 2-3 You can use the Miscellaneous Files folder to store links to documents that relate to
your projects.
Solution Properties
5IF4PMVUJPO1SPQFSUZ1BHFTEJBMPHCPYHJWFTZPVFBTZBDDFTTUPUIFTFUUJOHTUIBUBQQMZ
UPBOFOUJSFTPMVUJPO"NPOHUIFPQUJPOTUIBUZPVDBODPOUSPMBSFUIFTUBSUVQQSPKFDUPS
QSPKFDUTGPSZPVSTPMVUJPOUIFMPDBUJPOTGPS¾MFTBOETZNCPMTVTFEGPSEFCVHHJOHBOEUIF
DPO¾HVSBUJPOTFUUJOHTUIBUBQQMZUPUIFEJGGFSFOUQSPKFDUTJOZPVSTPMVUJPO
5PHFUUPUIF4PMVUJPO1SPQFSUZ1BHFTEJBMPHCPYNBLFTVSFUIBUUIFTPMVUJPOOBNF
JTTFMFDUFEJO4PMVUJPO&YQMPSFSQSFTT$USM"MU"BOEUZQF1SPKFDU1SPQFSUJFTJOUIF
$PNNBOE8JOEPX"OPUIFSXBZJTUPSJHIUDMJDLUIFTPMVUJPOBOEDIPPTF1SPQFSUJFT.PTU
PGUIFNBKPSQSPHSBNNJOHQSPKFDUTJO7JTVBM4UVEJPQSFTFOUZPVXJUIUIF1SPQFSUZ
1BHF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PO¾HVSBUJPOT
UPTBWFBOENBOBHFEJGGFSFOUCVJMEPQUJPOTGPSZPVSBTTFNCMJFT
Solution and Solution User Options Files
5IFTPMVUJPOTPVSDFTMO¾MFJTBQMBJOUFYUEPDVNFOUUIBUEFTDSJCFTUIFTPMVUJPO5IF
TPMVUJPO¾MFDPOUBJOTMJOLTUPUIFQSPKFDUTDPOUBJOFEJOUIFTPMVUJPO*UBMTPDPOUBJOTWFSTJPO
JOGPSNBUJPOBCPVUUIFGPSNBUPGUIFTPMVUJPO¾MFJUTFMG
Important Once you convert a file to Visual Studio 2005, you can no longer open it in
earlier versions of Visual Studio.
5IFTMO¾MFBMTPDPOUBJOTJOGPSNBUJPOPOUIFWBSJPVTDPO¾HVSBUJPOTUIBUIBWFCFFOTFUVQ
JOUIFTPMVUJPO*OGPSNBUJPOBCPVUUIFEJGGFSFOUTPMVUJPODPO¾HVSBUJPOTJTTUPSFEJOUIJT¾MF
BMPOHXJUIJOGPSNBUJPOBCPVUIPXUIFEJGGFSFOUQSPKFDUDPO¾HVSBUJPOTBSFPSHBOJ[FEJOUIPTF
TPMVUJPODPO¾HVSBUJPOT
*GZPVUBLFBMPPLBUBOTMO¾MFJOXIJDITPMVUJPOJUFNTIBWFCFFOFOBCMFEBOEBEEFEZPVµMM
OPUJDFUIBUUIFSFµTOPJOGPSNBUJPOBCPVUUIFTF¾MFT4PMVUJPOJUFNTBSFDPOTJEFSFEVTFS
JUFNTTPMJOLTUPUIFTF¾MFTBSFTUPSFEJOUIFTPMVUJPOVTFSPQUJPOTTVP
¾MF*GZPVQBTTB
GPMEFSDPOUBJOJOHBOTMOBOEBOTVP¾MFUPBOPUIFSVTFSPOBOPUIFSNBDIJOFNVDIPGUIF
JOGPSNBUJPOJOUIFTVP¾MFXJMMCFDPNFVTFMFTTUPUIFTFDPOEVTFSBOEXJMMCFJHOPSFE
4PNFJNQPSUBOUJUFNTBSFTUPSFEJOUIFTVP¾MFUIBUZPVDBOTIBSFXJUIBOPUIFS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B¾MFJOB7JTVBM$QSPKFDUEPFTOµUOFDFTTBSJMZ
EFMFUFUIF¾MFUIBUµTPQFOFECZUIFMJOL*UµTBSBUIFS¾OFEJTUJODUJPOCVUJGZPVµWFFWFSNPWFE
B7JTVBM$QSPKFDUBOEGPVOEZPVSTFMGNJTTJOHBQSPKFDU¾MFJUNJHIUCFUIBUUIF¾MFFYJTUFE
PVUTJEFUIFQSPKFDUGPMEFS*O7JTVBM4UVEJP4IPX"MM'JMFTOPXXPSLTJO7JTVBM$UP
TIPXZPVBMMUIF¾MFTMPDBUFEJOUIFQIZTJDBMEJSFDUPSZ
/&58JOEPXTQSPKFDUTDBODPOTJTUPGBNJYPGMJOLTBOEBDUVBM¾MFJUFNT8FCTJUFTBSF
HFOFSBMMZDPOUBJOFEJOBTPMVUJPOCVUEPOµUIBWFBQSPKFDU¾MF5BCMFTIPXTUIFQPTTJCMF
SFMBUJPOTIJQTCFUXFFOQSPKFDUUZQFBOE¾MFJUFNTJO7JTVBM4UVEJP
Table 2-1
Project Items in Visual Studio 2005
Project Type
Associated Items
Visual C++
Links to items
Web site
Items in the Web folder
Visual Basic
Links and actual items
Visual C#
Links and actual items
Visual J#
Links and actual items
*GZPVUBLFBMPPLBU4PMVUJPO&YQMPSFSGPSB8JOEPXTBQQMJDBUJPOXSJUUFOJO7JTVBM$
PS7JTVBM#BTJDZPVDBOTFFUIFNJYPGQSPKFDUJUFNTBOE¾MFTUSVDUVSFJUFNTCZVTJOH
2TQLGEV5JQY#NN(KNGU'JMFTUIBUBSFQBSUPGUIFQSPKFDUXJMMBQQFBSOPSNBMMZ'JMFTUIBUBSF
OPUQBSUPGUIFQSPKFDUCVUBSFJOUIFQSPKFDUGPMEFSXJMMBQQFBSTMJHIUMZHSBZFE5IF
5JQY#NN(KNGUDPNNBOEJTBWBJMBCMFGSPNUIF1SPKFDUNFOVBOEUISPVHIUPPMCBSCVUUPOTJO
4PMVUJPO&YQMPSFS*OBEEJUJPOZPVµMMTFFBOVNCFS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UPNCVJMEUZQFZPVHFUPOFPGUIFTFUXPQBUITEFQFOEJOHPOXIJDIUZQF
PGCVJMEZPVHFUZPVSJOJUJBMTFUUJOHTGSPN*GZPVµSFDSFBUJOHBDVTUPNCVJMEJUNJHIUNBLF
26
Working with Microsoft Visual Studio 2005
TFOTFUPDPQZUIFPVUQVUPGUIBUCVJMEUPBOFXGPMEFSTPUIBUZPVDBODPNQBSFUIFPVUQVU
BTTFNCMJFT'PSDBTFTMJLFUIJTZPVDBODSFBUFBOFXCVJMEQBUIUPNBUDIZPVSCVJMEOBNF
'PSFYBNQMFJGZPVIBWFBCVJMEOBNFE%FCVH0WFS¿PXUPJOEJDBUFUIBUZPVµWFFOBCMFE
PWFS¿PXDIFDLTGPSUIJTCVJMEUZQF
ZPVDBODIBOHFUIFPVUQVUUP=CJO=%FCVH0WFS¿PX
Figure 2-8
The Build page for a Visual C# Windows Forms application
5IF%FCVHQBHFTIPXOJO'JHVSFDBOCFFTQFDJBMMZVTFGVMXIFOZPVµSFCVJMEJOHDMBTT
MJCSBSZ8JOEPXTBOE8FCTFSWJDFTQSPKFDUT:PVDBOFYQFSJNFOUXJUIBMPUPGTFUUJOHTPO
UIJTQBHFCVUPOFPGUIFNPTUVTFGVMUPPVSEJTDVTTJPOJTUIF4UBSU"DUJPOPQUJPO6TJOH
EJGGFSFOUCVJMEUZQFTZPVDBOTQFDJGZQBSUJDVMBS63-TUIBUZPVXBOUUPUFTUZPVS8FCTFSWJDF
BHBJOTU:PVDBOVTFUIF4UBSU"DUJPOPQUJPOJOUIFTBNFXBZUPUFTUZPVSMJCSBSJFT*UMFUTZPV
FBTJMZEFCVHZPVSTFSWJDFPSMJCSBSZBHBJOTUBOVNCFSPGUFTUBQQMJDBUJPOT
Figure 2-9
The Debugging page for a Web application
Visual C++ Projects
5IF1SPQFSUZ1BHFTEJBMPHCPYGPS7JTVBM$QSPKFDUTIBTBIVHFOVNCFSPGTFUUJOHTCFDBVTF
PGUIFMBSHFOVNCFSPGDPNQJMFBOEMJOLPQUJPOTBWBJMBCMF5IFDVTUPNCVJMEPQUJPOTUIBU
Chapter 2:
Project Management in Visual Studio 2005
27
XFµWFUBMLFEBCPVUJOUIJTDIBQUFSBQQMZUP7JTVBM$BTXFMM*OGBDUCFDBVTFPGUIFNBOZ
QSPQFSUJFTBWBJMBCMFZPVTIPVME¾OEDVTUPNTFUUJOHTGPSVONBOBHFEQSPKFDUTWFSZVTFGVM
FTQFDJBMMZJOUFTUJOHBOEUFBDIJOHTJUVBUJPOT
'JHVSFTIPXTUIF1SPQFSUZ1BHFTEJBMPHCPYGPSB7JTVBM$8JOˆQSPKFDU
Figure 2-10
A custom configuration in Visual C++
5IF1SPQFSUZ1BHFTEJBMPHCPYGPSB7JTVBM$QSPKFDUIBTBOVNCFSPGTVCGPMEFSTVOEFS
UIF$PO¾HVSBUJPO1SPQFSUJFTGPMEFS5BCMFDPOUBJOTBMJTUPGTPNFPGUIFTFGPMEFST
BOEUIFHFOFSBMQSPQFSUZUZQFTUIBUZPVDBOTFUGSPNFBDI*GZPVµSFBOFYQFSJFODFE7JTVBM
$QSPHSBNNFSZPVµMM¾OENPTUPGUIFTFTFUUJOHTGBJSMZTUSBJHIUGPSXBSE
Table 2-2 Selected Configuration Properties Subfolders in Visual C++
Subfolder
Properties
General
Specify output directories, log options, MFC/ATL options, CLR
support (/CLR).
Debugging
Specify which debugger you want to use from the IDE. These include
the local debugger, remote debugger, or other debugger.
C/C++
Compiler options, preprocessor definitions, paths to some output files,
and command-line compile options.
Linker
Link options, debug options, and command-line link options.
Resources
Resource file name and path, culture, and resource compiler
command line.
Browse Information
Options relating to BSCMAKE (browser files).
Build Events
Commands that you can run during the build process.
28
Working with Microsoft Visual Studio 2005
Table 2-2 Selected Configuration Properties Subfolders in Visual C++
Subfolder
Properties
Custom Build Step
Properties for configuring an additional task you specify when
building a file or a project. For example, you might pass an input file
to a tool that returns an output file.
Web Deployment
Specifies how a Web deployment tool will install your application.
5IF#VJME&WFOUTOPEFBMMPXTZPVUPEPBGFXJOUFSFTUJOHUIJOHTXJUIZPVSDVTUPNCVJMET
:PVDBOTFFUIF1PTU#VJME&WFOUQBHFJO'JHVSF*GZPVµSFXPSLJOHXJUINVMUJQMF
QSPKFDUTBOECVJMETZPVDBOVTFUIF#VJME&WFOUTGPMEFSUPSVOBQQMJDBUJPOTBOETDSJQUT
EVSJOHZPVSCVJMEQSPDFTT*OUIJTDBTFXFµWFBEEFEBDBMMUP3FHTWSFYFBTUIFDPNNBOE
MJOFGPSUIF1PTU#VJME&WFOUJOUIFQSPKFDU"GUFSUIJTQSPKFDUJTCVJMUVOEFSUIJTDPO¾HVSBUJPO
UIFUBSHFU¾MFJTSFHJTUFSFEXJUI8JOEPXT
Figure 2-11
Build events let you run applications during your build process
Project Source Files
1SPKFDUTPVSDF¾MFTIBWFEJGGFSFOUFYUFOTJPOTCBTFEPOUIFMBOHVBHFTQFDJ¾DUPUIFQSPKFDU
5BCMFMJTUTUIFQSPKFDUUZQFTBOEFYUFOTJPOTUIBUUIFZIPME
Table 2-3
Project Types and Extensions
Project Type
Extension
Visual Basic
.vbproj
Visual C#
.csproj
Visual C++
.vcproj
Chapter 2:
Table 2-3
Project Management in Visual Studio 2005
29
Project Types and Extensions
Project Type
Extension
Visual J#
.vjproj
Deployment
.vdproj
7JTVBM#BTJD7JTVBM$BOE7JTVBM+QSPKFDUTBMTPDPOUBJOVTFSPQUJPO¾MFT5IFTF¾MFT
UBLFUIFGPSN2TQLGEV0COG2TQLGEV'ZVVTFS"7JTVBM#BTJDVTFSPQUJPOT¾MFIBTUIFFYUFOTJPO
WCSPKVTFS5IFTFQSPKFDUVTFS¾MFTBSFJO9.-BOEDPOUBJOJOGPSNBUJPOTQFDJ¾DUPUIF
DVTUPNCVJMETUIBUZPVµWFDSFBUFE6OMJLFUIFCJOBSZTVP¾MFUIFVTFS¾MFTBSFJOUSJOTJDUP
DVTUPNCVJMEUZQFTBOETIPVMEVTVBMMZCFLFQUXJUIBQSPKFD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DDPSEJOHMZ
Figure 2-13
Configuring build dependencies for a project
Building Projects and Solutions
0ODFUIFQSPKFDUTDVTUPNCVJMEDPO¾HVSBUJPOTBOESFGFSFODFTBSFTFUJOBTPMVUJPOZPVDBO
CFHJOUPXPSLPVUUIFCVJMETDFOBSJPTUIBUZPVXBOUUPSVOXJUIUIFEJGGFSFOUDPO¾HVSBUJPOT
5PTQFDJGZXIJDIQSPKFDUTJOUIFTPMVUJPOTIPVMECFCVJMUZPVDBOVTFUIF$PO¾HVSBUJPO
.BOBHFSEJBMPHCPYTIPXOJO'JHVSF
:PVDBOFBTJMZFYDMVEFQSPKFDUTUIBUNJHIUHJWF
ZPVQSPCMFNTPSZPVDBOTJNQMZTBWFTPNFUJNFXIFOZPVXBOUUPDPODFOUSBUFPOBTQFDJ¾D
CVJMEJOBTPMVUJPO
Figure 2-14
Determining which project to build for a given build configuration
Chapter 2:
Project Management in Visual Studio 2005
31
5IFSFµTPOFNPSFQPXFSGVMCVJMEEJBMPHCPYZPVDBOVTFUPCVJMENVMUJQMFDPO¾HVSBUJPOTBU
POFUJNF5IF#BUDI#VJMEEJBMPHCPY2TQLGEV$CVEJ$WKNF
TIPXOJO'JHVSFJTIFMQGVM
GPSCVJMEJOHNVMUJQMFQSPKFDUTXJUIEJGGFSFOUDPO¾HVSBUJPOTCVUJUµTOPUBWBJMBCMFGPSFWFSZ
BQQMJDBUJPOUZQF
Figure 2-15
The Batch Build dialog box
5IF#BUDI#VJMEEJBMPHCPYMFUTZPVQFSGPSNBOVNCFSPGJNQPSUBOUBDUJPOTJODMVEJOH
CVJMEJOHSFCVJMEJOHBOEDMFBOJOHZPVSQSPKFDUT$MJDLJOHUIF#VJMECVUUPOJOJUJBUFTBO
JODSFNFOUBMCVJMEGPSQSPKFDUTUIBUBSFDPO¾HVSFEGPSTVDIBCVJME5IF3FCVJMECVUUPO
JOJUJBUFTB3FCVJME"MMGPSBMMUIFTFMFDUFEQSPKFDUT$MJDLJOHUIF$MFBOCVUUPOEFMFUFTUIF¾MFT
UIBUBSFPVUQVUCZBCVJMETPUIBUZPVDBOSFTUBSUUIFCVJMEDMFBOMZPSTIBSFZPVSQSPKFDUT
XJUIPVUVOOFDFTTBSZCVML
Looking Ahead
"MUIPVHIUIFQSPKFDUNBOBHFNFOUGBDJMJUJFTJO7JTVBM4UVEJPBSFGPSNJEBCMFUIFVTBCJMJUZ
PGUIF*%&GPSEFWFMPQFSTBMTPSFMJFTPOUIFFEJUPSGFBUVSFTJOUIF*%&*O$IBQUFSXFµMM
EJTDVTTUIFDPEFFEJUPSJOEFUBJMBOEEJTDVTTUFDIOJRVFTUIBUDBOIFMQZPVCFDPNFNPSF
QSPEVDUJWFBTZPVXSJUFDPEF
Chapter 3
The Visual Studio Editor
In this chapter:
Documents in the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Other Editing Features in Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Line Numbering and Outlining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Programming Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Command Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Search, Replace, and Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33
42
44
47
49
50
53
5IFFEJUPSJTUIFIFBSUPGBOZEFWFMPQNFOUFOWJSPONFOU*OUIJTDIBQUFSXFµMMUBLFBDMPTF
MPPLBUUIFFEJUJOHUPPMTCVJMUJOUP.JDSPTPGUˆ7JTVBM4UVEJPˆ8IFUIFSZPVµSFBOFXPS
FYQFSJFODFEQSPHSBNNFSUIFJOGPSNBUJPOJOUIJTDIBQUFSXJMMIFMQZPVCFDPNFFWFONPSF
QSPEVDUJ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VUTJTUPJNQSPWFZPVSTQFFEJOUIF*%&BOEUIFZUBLFTPNFUJNF
BOEQSBDUJDFUPMFBSO
Common Editing Shortcuts
"QQMJDBUJPOTXSJUUFOGPS.JDSPTPGU8JOEPXTˆVTFBOVNCFSPGTUBOEBSELFZCPBSETIPSUDVUT
UIBUZPVµSFQSPCBCMZGBNJMJBSXJUI5IFTFTIPSUDVUTBSFLOPXOBT$PNNPO6TFS"DDFTTJCJMJUZ
$6"
TIPSUDVUTBOEBSFCBTFEPOXPSLEPOFBU*#.UIBUIBTTUBOEBSEJ[FETIPSUDVUTBDSPTT
BOVNCFSPGQMBUGPSNT5IFCJHHFTUBEWBOUBHFPGVTJOHUIJTQBSUJDVMBSTFUPGTIPSUDVUTJTUIBU
PODFZPVMFBSOUIFNZPVDBOBQQMZUIFNJOBMNPTUBOZ8JOEPXTBQQMJDBUJPOJODMVEJOH
.JDSPTPGU0G¾DF5IFTFTIPSUDVUTIBWFBMTPCFFOMBCFMFEPOBOVNCFSPGQPQVMBSLFZCPBSET
JODMVEJOHNPTUPGUIF.JDSPTPGULFZCPBSET
Tip For more information about Windows keyboard shortcuts, see the book Microsoft
Windows User Experience (Microsoft Press®, 1999), which details how shortcuts such as these
should be used in Windows applications.
Chapter 3:
The Visual Studio Editor
37
5IFUBCMFTUIBUGPMMPXHSPVQUIFDPNNPOFEJUJOHTIPSUDVUTGPS7JTVBM4UVEJPCBTFEPO
GVODUJPOBOEXIFOZPVµSFMJLFMZUPVTFUIFNJOBOFEJUJOHTFTTJPO5BCMFMJTUTUIF¾MF
TIPSUDVUT:PVµMMVTFUIFTFUPPQFOOFX¾MFTPSFYJTUJOH¾MFTBOEUPTBWF¾MFTBTZPVXPSL
Table 3-1
Common File Shortcuts
Command
Keystroke
Named Command
New
Ctrl+N
File.NewFile
Open
Ctrl+O
File.OpenFile
Save
Ctrl+S
File.SaveSelectedItems
Save All
Ctrl+Shift+S
File.SaveAll
Print
Ctrl+P
File.Print
:PVµMMOPUJDFU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Navigation and Selection Shortcuts
Movement
Movement Keystroke(s)
Selection Keystroke
Character
Right Arrow
Shift+Right Arrow
Left Arrow
Shift+Left Arrow
Ctrl+Right Arrow
Ctrl+Shift+Right Arrow
Ctrl+Left Arrow
Ctrl+Shift+Left Arrow
End
Shift+End
Home
Shift+Home
Down Arrow
Shift+Down Arrow
Up Arrow
Shift+Up Arrow
Word
Line
38
Working with Microsoft Visual Studio 2005
Table 3-2
Common Navigation and Selection Shortcuts
Movement
Movement Keystroke(s)
Selection Keystroke
Code Pane
Page Down
Shift+Page Down
Page Up
Shift+Page Up
Ctrl+End
Ctrl+Shift+End
Ctrl+Home
Ctrl+Shift+Home
Document
0ODFZPVµWFTFMFDUFEUFYUZPVDBODPQZPSDVUJUUPUIF$MJQCPBSEBOEUIFOZPVDBOQBTUFJU
CBDLJOUPUIF$PEF&EJUPS5IFDPNNPOFEJUJOHTIPSUDVUTBSFMJTUFEJO5BCMF
Table 3-3
Common Editing Shortcuts
Command
Keystroke
Named Command
Cut
Ctrl+X
Edit.Cut
Copy
Ctrl+C
Edit.Copy
Paste
Ctrl+V
Edit.Paste
Undo
Ctrl+Z
Edit.Undo
Redo
Ctrl+Y
Edit.Redo
Select current word
Ctrl+W
Edit.SelectCurrentWord
Select all
Ctrl+A
Edit.SelectAll
'JOBMMZMFUµTUBLFBMPPLBUUIFTIPSUDVUTUIBUZPVDBOVTFUPUSBOTQPTFMFUUFSTXPSETBOE
MJOFT:PVDBOVTFUIFTIPSUDVUTTIPXOJO5BCMFUPTXBQUIFQPTJUJPOPGUXPJUFNTJO
UIF$PEF&EJUPS'PSFYBNQMFJGUIFDVSTPSJTQPTJUJPOFECFGPSFUIFMFUUFST#$QSFTTJOH
$USM5XJMMDBVTFUIFMFUUFSTUPTXJUDIUIFJSPSEFSUP$#5ZQJOH$USM4IJGU5XJUIUIFDVSTPS
BEKBDFOUUPPSJOUIFXPSEIQJOUIFTUSJOHIQDQNFN[XJMMSFTVMUJOBUSBOTQPTJUJPOUPDQNFN[
IQ5IFNPTUVTFGVMTIPSUDVUJOUIJTHSPVQSVOTUIFDPNNBOE'FKV.KPG6TCPURQUG6TJOHUIF
TIPSUDVU"MU4IJGU5TXBQTUIFMJOFXIFSFUIFDVSTPSJTMPDBUFEXJUIUIFOFYUMJOFNBLJOHJU
SFBMMZFBTZUPNPWFBMJOFPGDPEFEPXOUIFQBHF
Table 3-4
Transposition Shortcuts
Command
Keystroke
Named Command
Transpose character
Ctrl+T
Edit.CharTranspose
Transpose word
Ctrl+Shift+T
Edit.WordTranspose
Transpose line
Alt+Shift+T
Edit.LineTranspose
5IFTFTIPSUDVUTTIPVMEQSPWJEFZPVXJUIUIFGVODUJPOBMJUZZPVOFFEUPQFSGPSNBGBJS
OVNCFSPGFEJUJOHUBTLTXJUIPVUUIFNPVTFJGZPVDIPPTFUPXPSLUIBUXBZ5IFSFµTOPUIJOH
Chapter 3:
The Visual Studio Editor
39
XSPOHXJUIVTJOHUIFNPVTFGPSFEJUJOH*UµTOPUSFBMMZNVDITMPXFSUPVTFUIFNPVTFUIBOUP
VTFTIPSUDVUTCVUUIFFYUSBTFDPOEPSUXPUIBUJUUBLFTUPHPUPUIFNPVTFDBOUBLFZPVPVU
PGUIBUDSFBUJWFHSPPWFZPVDBOHFUJOUPXIFOZPVµSFFEJUJOH'PSNFUSBOTJUJPOJOHGSPNVTJOH
BNPVTFCBDLUPUIFLFZCPBSEUBLFTBMJUUMFNPSFUJNFUIBOVTJOHBTIPSUDVUTP*USZUPVTF
TIPSUDVUTXIFOFWFSQPTTJCMF
Custom Keyboard Shortcuts
&BSMJFSXFUBMLFEBCPVUUPHHMJOHBXJOEPXµT%PDLBCMFTUBUFUPBEEJUUPUIFDFOUFSPGUIF*%&
5IFSFµTOPTIPSUDVUBTTJHOFECZEFGBVMUUPUIF9KPFQY&QEMCDNGDPNNBOECVUZPVNJHIU
¾OEUIBUBEEJOHPOFXPVMECFIBOEZGPSNBLJOHBWFSZEBUBIFBWZXJOEPXFBTJF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LFZCPBSETIPSUDVUNBQQJOHTGPSZPVUPDIPPTFGSPN5PTFMFDUBOFX
TDIFNFHPUPUIF0QUJPOTEJBMPHCPY5PPMT0QUJPOT
BOEMPPLPOUIF,FZCPBSEQBHF
XIFSFXFFBSMJFSBTTJHOFETPNFLFZCPBSETIPSUDVUT"UUIFUPQPGUIFQBHFZPVµMMTFFBESPQ
EPXOMJTUUIBUBMMPXTZPVUPDIPPTFGSPNBOVNCFSPGEJGGFSFOULFZCPBSENBQQJOHT5IFTF
NBQQJOHTJODMVEF7JTVBM$7JTVBM4UVEJP&NBDTBOE#SJFG5PVTFPOFPGUIFTF
NBQQJOHTTFMFDUUIFPOFZPVXBOUGSPNUIFMJTUBOEDMJDL0,5IFTFNBQQJOHTDIFNFTBSF
EFTJHOFEUPNBLFJUFBTZGPSQSPHSBNNFSTUPNPWFGSPNUIFJSQSFGFSSFEFEJUPSUP7JTVBM
4UVEJP'PSNPSFJOGPSNBUJPOPOUIJTGFBUVSFTFFUIF4IPSUDVU,FZTUPQJDJO7JTVBM
4UVEJP)FMQ
Note Emacs and Brief support in Visual Studio 2005 are really more than just key bindings.
They are emulations that offer special text selection and indenting behavior in the IDE.
Understanding Tabs and Code Formatting
$PEFGPSNBUUJOHJTBOPUIFSPOFPGUIPTFJTTVFTUIBUEFWFMPQFSTUFOEUPGFFMTUSPOHMZBCPVU
8IFOJUDPNFTUPDPEFGPSNBUUJOHUIFCPUUPNMJOFGPSNPTUPSHBOJ[BUJPOTJTUIBUTPNFTPSU
PGTUBOEBSETIPVMEFYJTU5IFGPSNBUUJOHPQUJPOTGPSFBDIPGUIFMBOHVBHFTTVQQPSUFEJO
UIF7JTVBM4UVEJP*%&BSFTFUJOUIF5FYU&EJUPSOPEFPGUIF0QUJPOTEJBMPHCPY8IFOZPV
TFUPQUJPOTGPS"MM-BOHVBHFTBTTIPXOJO'JHVSFZPVPWFSSJEFUIFTFUUJOHTGPSFBDI
JOEJWJEVBMMBOHVBHFMJTUFEJOUIF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FBSMJFS
42
Working with Microsoft Visual Studio 2005
Figure 3-5 Displaying white space in the Code Editor
Figure 3-6 The C/C++ Formatting page
Other Editing Features in Visual Studio 2005
&EJUJOHJO7JTVBM4UVEJPJTNVDIFBTJFSUIBOLTUPBOVNCFSPGOFXBOEVQEBUFE
GFBUVSFTUIBUIBWFCFFOBEEFEUPUIF*%&TJODF7JTVBM4UVEJP"NPOHUIFTFBSF$PEF
4OJQQFUTSFGBDUPSJOHUIF$PEF%F¾OJUJPO8JOEPXBOEUIF$BMM#SPXTFS
Code Snippets
$PEF4OJQQFUTJTBGFBUVSFBEEFEGPS7JTVBM#BTJD7JTVBM$.JDSPTPGU7JTVBM+ˆBOE9.-
UIBUBMMPXTBQSPHSBNNFSUPJOTFSUDPNNPOMZVTFETOJQQFUTPGDPEFJOUPBQSPKFDU:PVDBO
JOTFSUBTOJQQFUCZSJHIUDMJDLJOHJOUIF$PEF&EJUPSJOBTVQQPSUFEQSPKFDUUZQFBOEDMJDLJOH
*OTFSU4OJQQFU'JHVSFTIPXTZPVIPXUIJTMPPLTJO7JTVBM#BTJD:PVDBOJOTFSUBTNBMM
Chapter 3:
The Visual Studio Editor
43
BNPVOUPGDPEFPSBTJHOJ¾DBOUCMPDLEFQFOEJOHPOUIFGVODUJPOBMJUZUIBUZPVµSFBEEJOH
5IJTDBOCFBSFBMUJNFTBWFSBTZPVXPSLPOZPVSQSPKFDUT
Figure 3-7 Inserting a code snippet into the Code Editor
4OJQQFUTBSFNBOBHFEUISPVHIUIF$PEF4OJQQFUT.BOBHFS:PVDBODSFBUFZPVSPXO
TOJQQFUTJO9.-5IF$PEF4OJQQFUT.BOBHFSBWBJMBCMFGSPNUIF5PPMTNFOVBMMPXTZPVUP
JNQPSU9.-¾MFTXJUIZPVSPXODPEFTOJQQFUT
Refactoring
3FGBDUPSJOHBMMPXTZPVUPJNQSPWFZPVSDPEFBGUFSZPVµWFXSJUUFOJUCZQSPWJEJOHUPPMT
GPSBVUPNBUJDBMMZVQE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PVDBOSFOBNFUIFOFXDPNNBOE5VSO0O-JOF
/VNCFSTUPNBLFJUBMJUUMFNPSFSFBEBCMF%PUIFTBNFXJUIUIF6WTP1HH.KPG0WODGTUNBDSP
BTTIPXOJO'JHVSF
Figure 3-9 Adding the TurnOffLineNumbers macro to a menu
46
Working with Microsoft Visual Studio 2005
:PVDBOBEEBNBDSPTVDIBTUIJTPOFUPBUPPMCBSJOKVTUUIFTBNFXBZ*OUIBUDBTFZPVµMM
QSPCBCMZXBOUUPTQFDJGZBCVUUPOJNBHFUPVTFXJUIUIFNBDSPUIBUZPVµSFBEEJOH
Outlining
5IF7JTVBM4UVEJPPVUMJOJOHGFBUVSFJTQSPCBCMZGBNJMJBSUPQSPHSBNNFSTXIPµWFVTFEPUIFS
BEWBODFEFEJUPST5IFJEFBJTUPHSPVQDPEFCZGVODUJPOBMJUZUPNBLFJUFBTJFSUPOBWJHBUF
UIFDPEFJOUIF$PEF&EJUPS'JHVSFTIPXTBDPEF¾MFJOXIJDIUIFPVUMJOFIBTCFFO
DPMMBQTFEUPUIFNFUIPETJOUIF¾MF"UUIFFOEPGFBDIDPMMBQTFEMJOFZPVµMMTFFBCPYXJUI
BOFMMJQTJTJOJU)PWFSZPVSNPVTFQPJOUFSPWFSUIBUCPYUPEJTQMBZB5PPM5JQUIBUTIPXT
TPNFPGXIBUµTJOUIFDPMMBQTFEOPEF
Figure 3-10 A ToolTip indicates the contents of a collapsed node when outlining is enabled.
:PVDBOUVSOPGGPVUMJOJOHCZQSFTTJOH$USM.$USM1'FKV5VQR1WVNKPKPI
5VSOJOHPGG
PVUMJOJOHNBLFTUIFPVUMJOJOHJOEJDBUPSTBMPOHUIFTJEFPGUIF$PEF&EJUPSEJTBQQFBS
DPNQMFUFMZ:PVDBOSFTUBSUPVUMJOJOHCZQSFTTJOH$USM.$USM0'FKV%QNNCRUGVQ&G¿PKVKQPU
5IF%QNNCRUGVQ&G¿PKVKQPUDPNNBOEXJMMSFTUBSUPVUMJOJOHJOUIF$PEF&EJUPSBOEXJMMDPMMBQTF
FBDIDPEFCMPDLJOUIF¾MF1SFTTJOH$USM.$USM-'FKV6QIING#NN1WVNKPKPI
XJMMPQFOBMMUIF
DPMMBQTFECMPDLTJOUIF$PEF&EJUPS:PVDBOBMTPUZQF&EJU4UBSU"VUPNBUJD0VUMJOJOHGSPN
UIF$PNNBOE8JOEPXUPSFTUBSUPVUMJOJOH
*OBEEJUJPOUPIJEJOHMPHJDBMDPEFCMPDLTJOUIF5FYU&EJUPSZPVDBODPMMBQTFBOBSCJUSBSZ
TFMFDUJPOJO7JTVBM$CZTFMFDUJOHTPNFUFYUBOEQSFTTJOH$USM.$USM)'FKV*KFG5GNGEVKQP
5IJTGVODUJPODBOCFWFSZIBOEZGPSDPMMBQTJOHTPNFDPEFCFUXFFOUXPEJTUBOUQPJOUTJOB
DPEF¾MFBOEJUFWFOXPSLTUPDPMMBQTFMJOFTJOBQMBJOUFYU¾MF*GZPVXBOUUPFYQBOEUIF
DPMMBQTFETFMFDUJPOQSFTT$USM.$USM6'FKV5VQR*KFKPI%WTTGPV
5BCMFMJTUTUIFTIPSUDVUTBTTPDJBUFEXJUIPVUMJOJOHJOUIF5FYU&EJUPS
Chapter 3:
Table 3-5
The Visual Studio Editor
47
Outlining Shortcuts
Command
Keystroke
Named Command
Stop outlining
Ctrl+M, Ctrl+P
Edit.StopOutlining
Toggle outlining
Ctrl+M, Ctrl+L
Edit.ToggleAllOutlining
Toggle expansion
Ctrl+M, Ctrl+M
Edit.ToggleOutliningExpansion
Hide selection
Ctrl+M, Ctrl+H
Edit.HideSelection
Stop hiding selection
Ctrl+M, Ctrl+U
Edit.StopHidingCurrent
Programming Help
"OVNCFSPGGFBUVSFTJOUIF*%&NBLFJUFBTJFSGPSQSPHSBNNFSTUPXSJUFDPEF:PVTIPVME
CFGBNJMJBSXJUI)FMQJOUIF*%&TPXFXPOµUUBMLUPPNVDIBCPVUJU)FMQJO7JTVBM4UVEJP
JTWFSZTJNQMF±Z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FOU$PNQMFUJPOTFDUJPODMFBSUIF"VUP-JTU.FNCFSTDIFDLCPY:PVDBOBMTPUPHHMF
UIF1BSBNFUFS*OGPSNBUJPOTFUUJOHGSPNUIJTQBHF
Note You can increase the number of options returned in a member list by clearing Hide
Advanced Members. Hide Advanced Members is the default setting for Visual Basic, so you
might be missing a number of possible completions if you leave that setting cleared.
8JUITUBUFNFOUDPNQMFUJPOUVSOFEPO*OUFMMJ4FOTFQSFTFOUTZPVXJUIJOGPSNBUJPOBTTPPO
BTZPVUZQFBOPQFSBUPSBTQBSUPGBTUBUFNFOU*GZPVIBWFTUBUFNFOUDPNQMFUJPOUVSOFEPGG
PSJGZPVXBOUUPEJTQMBZUIJTJOGPSNBUJPOJNNFEJBUFMZQSFTT$USM+5IFSFTVMUJTTIPXOJO
'JHVSF
Figure 3-11 Forcing statement completion by pressing Ctrl+J
5PTFMFDUBOFOUSZUPDPNQMFUFBTUBUFNFOUVTFUIFVQBOEEPXOBSSPXLFZTUPOBWJHBUFUP
UIFEFTJSFEDPNQMFUJPOBOEUIFOQSFTT5BC
5PGPSDFQBSBNFUFSJOGPSNBUJPOMJLFUIBUTIPXOJO'JHVSFQSFTT$USM4IJGU4QBDFCBS
5IJTHJWFTZPVBMJTUPGUIFQBSBNFUFSPWFSMPBETZPVDBODIPPTFGSPNGPSBQBSUJDVMBSNFUIPE
8JUIUIFQBSBNFUFSJOGPSNBUJPOTIPXJOHVTFZPVSVQBOEEPXOBSSPXLFZTUPWJFXUIF
BWBJMBCMFQBSBNFUFST
Figure 3-12 Viewing the parameter information for a method by pressing Ctrl+Shift+Spacebar
6TFUIFQBSBNFUFSJOGPSNBUJPOQSPWJEFECZTFMFDUJOHUIFJUFNUIBUCFTUTVJUTZPVSOFFETBOE
UIFOUZQFUIFQBSBNFUFSTJOUPZPVSNFUIPE:PVµMMOPUJDFUIBUBGUFSZPVUZQFFBDIQBSBNFUFS
UIFOFYUQBSBNFUFSJOUIFMJTUBQQFBSTJOCPME8BUD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MPHCPYJO2VJDL3FQMBDFNPEFCZQSFTTJOH
$USM)5IJTEJBMPHCPYDPOUBJOTBDPNCPCPYGPSUIF3FQMBDF8JUIUFYU6TJOHUIF'JOEBOE
3FQMBDFEJBMPHCPYJTTUSBJHIUGPSXBSE&TQFDJBMMZIBOEZBSFUIF4FBSDIPQUJPOTXIJDIMFUZPV
DIPPTFCFUXFFOUIFDVSSFOUEPDVNFOUBMMPQFOEPDVNFOUTUIFDVSSFOUQSPKFDUBOETQFDJ¾D
TFMFDUJPOTPSCMPDLTJOUIFFEJUPS
6TJOHUIF'JOE*O'JMFTPQUJPOJOUIF'JOEBOE3FQMBDFEJBMPHCPY$USM4IJGU'GPS'JOE
PS$USM4IJGU)GPS3FQMBDF
TIPXOJO'JHVSFNBLFJUGBJSMZFBTZUP¾OEUFYUXJUIJOB
QSPKFDUPSBEJSFDUPSZTUSVDUVSF4FUUJOHVQUIFTF¾MFTFBSDIFTUBLFTBMJUUMFNPSFXPSLUIBO
QFSGPSNJOHBTUBOEBSE'JOEDPNNBOE:PVDBOUZQFUIFQBUIZPVXBOUUPTFBSDIJOUIF
-PPL*O¾FMEPSZPVDBODMJDLUIF©CVUUPOXIJDIPQFOTUIF-PPL*OEJBMPHCPY:PVDBO
VTFUIJTEJBMPHCPYUPOBSSPXZPVSTFBSDIUPZPVSQSPKFDUPSUPBEJSFDUPSZTUSVDUVSFPOZPVS
NBDIJOF5IFPVUQVUGSPNUIF'JOE*O'JMFTTFBSDIJTTFOUUPUIF'JOE3FTVMUTXJOEPXCZ
EFGBVMU
52
Working with Microsoft Visual Studio 2005
Figure 3-15 Find In Files from the Find and Replace dialog box
5IF7JTVBM4UVEJP'JOEBOE3FQMBDFTIPSUDVUTBSFMJTUFEJO5BCMF
Table 3-6 Common Search Shortcuts
Command
Keystroke
Named Command
Find
Ctrl+F
Edit.QuickFind
Replace
Ctrl+H
Edit.QuickReplace
Find In Files
Ctrl+Shift+F
Edit.FindinFiles
Replace In Files
Ctrl+Shift+H
File.ReplaceinFiles
Incremental Searching
*ODSFNFOUBMTFBSDIJOHJTBGFBUVSFPG7JTVBM4UVEJPUIBUµTBSFBMUJNFTBWFS"OJODSFNFOUBM
TFBSDIJTQFSGPSNFEPOFDIBSBDUFSBUBUJNFNBUDIJOHFBDIXPSEJOUIFTFBSDITUSJOHGSPN
UIFQPTJUJPOPGUIFDVSTPS:PVDBOTUBSUBOJODSFNFOUBMTFBSDICZQSFTTJOH$USM*:PVµMMTFF
UIFNPVTFQPJOUFSUSBOTGPSNJOUPBEPXOBSSPXMJLFUIFPOFTIPXOJO'JHVSF
Figure 3-16 Starting an incremental search by pressing Ctrl+I
Chapter 3:
The Visual Studio Editor
53
8JUIUIFEPXOBSSPXTIPXJOHTUBSUUZQJOHUIFUFSNZPVXBOUUPTFBSDIGPS"TZPVUZQF
XPSETUIBUNBUDIUIFMFUUFSTBSFNBUDIFETUBSUJOHGSPNUIFUPQ8IFOZPVµWFDPNQMFUFE
UIFQBUUFSOZPVXBOUUPTFBSDIGPSQSFTT$USM*BHBJOUPNPWFUPUIFOFYUNBUDI:PVDBO
DPOUJOVFUPQSFTT$USM*VOUJMZPVSFBDIUIFFOEPGUIFEPDVNFOUUPNBUDIFWFSZJOTUBODFPG
UIFUFSNZPVµSFTFBSDIJOHGPS*GZPVXBOUUPTFBSDIVQXBSEKVTUQSFTT$USM4IJGU**UXPSLT
JOUIFTBNFXBZ:PVDBOFYJUUIFJODSFNFOUBMTFBSDICZQSFTTJOH&OUFSPS&TD
Looking Ahead
5IJTDIBQUFSIBTHJWFOZPVBQSFUUZHPPEJEFBPGIPXOBNFEDPNNBOETBQQMZUPUIF$PEF
&EJUPSJOUIF*%&BOEIPXZPVDBODVTUPNJ[FUIF*%&CZVTJOHBMJBTDPNNBOETJOUIF
$PNNBOE8JOEPXUISPVHILFZCPBSETIPSUDVUTBOECZBEEJOHNFOVJUFNTBTTPDJBUFE
XJUIOBNFEDPNNBOETBOENBDSPT*O$IBQUFSXFµMMUFMMZPVBCPVUTPNFPGUIF
DPNNVOJUZGFBUVSFTJOUIF*%&
Chapter 4
Community Content and
VSTemplates
In this chapter:
Community Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Downloadable Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implementing Your Own Downloadable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating VSTemplates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
55
56
58
67
75
90
.JDSPTPGUˆ7JTVBM4UVEJPˆXBTEFTJHOFEUPCFBOVQHSBEFBCMFBOEFWPMWJOHQSPHSBN*OUIJT
DIBQUFSXFµMMTIPXZPVIPXUPVTFTPNFPGUIFOFXGFBUVSFTPG7JTVBM4UVEJPUPNBLFZPVS
QSPHSBNNJOHNPSFQPXFSGVMCZTIBSJOHDPEFXJUIPUIFSVTFST
Community Content
*OUIFFBSMZIPCCZJTUEBZTPGQSPHSBNNJOHQFPQMFXPVMEPGUFOHBUIFSJOTNBMMHSPVQTUPTIBSF
JEFBTBCPVUIPXUPQSPHSBNPSUPTIBSFDPEF5IFTFVTFSHSPVQTXPVMEPGUFOIBWFBTIBSFE
HPBMJOBQSPHSBNUIBUUIFZOFFEFEUPXSJUF.BZCFUIFZXBOUFEUPXSJUFBQSPHSBNUPCBMBODF
UIFJSDIFDLCPPLTLFFQUSBDLPGSFDJQFTPSNBZCFTIBSFTPGUXBSFBMHPSJUINTUIBUXPVMENBLF
TPGUXBSFEFWFMPQNFOUFBTJFSBOEGBTUFS5IJTDPNNVOJUZPGTPGUXBSFEFWFMPQFSTTUJMMFYJTUTUPEBZ
CVUCFDBVTFPGUPPMTTVDIBTUIF*OUFSOFUUIFDPNNVOJUZIBTHSPXOUPBHMPCBMTDBMF5PEBZ
TJUFTTVDIBT.JDSPTPGUµTPXO(PU%PU/FUDPN8JOEPXT'PSNTDPNBOE"TQOFUIBWFTQSVOH
VQMFUUJOHBVTFSJO1JUUTCVSHIDPMMBCPSBUFPOBTPGUXBSFQSPKFDUXJUIBEFWFMPQFSJO5PLZP:PV
DBOBMTPVTFUIFUPPMTCVJMUJOUP7JTVBM4UVEJPPOUIF$PNNVOJUZ]4FBSDINFOVUPPQFOUIF
.JDSPTPGU%FWFMPQFS/FUXPSL.4%/ˆ
IFMQCSPXTFSXIFSFZPVDBOTFBSDIGPSDPOUFOU
7JTVBM4UVEJPNBLFTDPMMBCPSBUJPOCFUXFFOEFWFMPQFSTFBTJFSXJUIUPPMTEFTJHOFETQFDJ¾DBMMZ
UPBMMPXZPVUPTIBSFDPEFBOEJEFBT*GZPVOFFEIFMQTJNQMZHPUPPOFPGZPVSGBWPSJUF
QSPHSBNNJOHDPNNVOJUZ8FCTJUFTBOENBLFBSFRVFTU"OPUIFSVTFSNJHIUIBWFFYQFSJFODF
XJUIUIFQSPCMFNZPVBSFUSZJOHUPTPMWFBOEIBWFTPNFDPEFUIBUJTTJNJMBSUPXIBUZPVOFFE
)FPSTIFDBOQBDLBHFUIFDPEFBOEEJTUSJCVUFJUFJUIFSCZTFOEJOHUIFDPOUFOUUPZPVEJSFDUMZ
UISPVHIFNBJMPSCZQPTUJOHJUUPB8FCTJUF4IBSJOHDPEFJTUIFGPDVTPGUIF$PNNVOJUZ
$POUFOU*OTUBMMFS±BUPPMUIBUUBLFTDPEFDSFBUFECZBOPUIFSVTFSBOEQMBDFTJUPOZPVS
DPNQVUFSTPUIBUZPVDBOTUBSUVTJOHJUSJHIUBXBZ
55
56
Working with Microsoft Visual Studio 2005
Note The Content Installer also gives you an opportunity if you are in the business of selling components for Visual Studio. You could package controls for the Toolbox, code snippets,
and other items, and then sell those components from your Web site. Customers can then
use the Content Installer to install the components they purchased.
Installing Content
$POUFOUUIBUZPVJOTUBMMPOZPVSDPNQVUFSVTJOHUIF$POUFOU*OTUBMMFSJTDPOUBJOFEJOB¾MF
XJUIUIFFYUFOTJPOWTJ5IJT¾MFJTBDPNQSFTTFE¾MFDPOUBJOJOHBMMUIF¾MFTOFDFTTBSZUP
DPSSFDUMZJOTUBMMEBUBPOUPZPVSDPNQVUFS5IFEBUBUIBUDBOCFJOTUBMMFEJODMVEFTDPOUSPMT
UIBUZPVDBOESBHBOEESPQGSPNUIF5PPMCPYPOUPB8JO'PSNPSB8FCGPSNDPEF
TOJQQFUTUIBUNBLFXSJUJOHCMPDLTPGDPEFBTTJNQMFBTQSFTTJOHBGFXLFZTPOUIFLFZCPBSE
UFNQMBUFTGPSDSFBUJOHOFXQSPKFDUTBOEQSPKFDUJUFNTPSBEEJOTBOENBDSPTXIJDIBSF
TNBMMQSPHSBNTUIBUDVTUPNJ[FUIF7JTVBM4UVEJPJOUFHSBUFEEFWFMPQNFOUFOWJSPONFOU
*%&
8IFOZPVSFDFJWFDPOUFOUGSPNBOPUIFSVTFSJOTUBMMJOHUIBUDPOUFOUGPSVTFJTBTFBTZ
BTXPSLJOHUISPVHIBXJ[BSEBOETFMFDUJOHXIJDIJUFNTUPJOTUBMM"GUFSZPVSFDFJWFBQBDLBHF
PGDPOUFOUFODMPTFEJOBWTJ¾MFZPVDBOPQFOUIF¾MFCZFJUIFSEPVCMFDMJDLJOHUIF¾MFJGJU
JTTUPSFEPOEJTLPSJGZPVBSFEPXOMPBEJOHUIF¾MFEJSFDUMZGSPNUIF*OUFSOFUZPVDBODMJDL
UIF0QFOCVUUPOPOUIFEPXOMPBEEJBMPHCPYGSPNZPVS8FCCSPXTFS'JHVSFTIPXTUIF
¾STUQBHFPGUIF$POUFOU*OTUBMMFSTIPXJOHBWTJ¾MFUIBUDPOUBJOTBDPOUSPMGPSUIF5PPMCPY
Figure 4-1
The Content Installer with a content file open and ready to install
$POUSPMTEPOPUIB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*GJOTUBMMBUJPOPGBOZJUFNGBJMFEZPVDBOVTFUIFTUBUVTJOGPSNBUJPOUPDPSSFDUUIF
QSPCMFNTBOEUIFOSFUVSOUPUIFXJ[BSEUPDMJDLUIF'JOJTICVUUPOBHBJOUPUSZUPSFJOTUBMM
UIFVOTVDDFTTGVMMZJOTUBMMFEJUFNT
58
Working with Microsoft Visual Studio 2005
Security
"TXJUIBOZ¾MFUIBUZPVQMBDFPOZPVSDPNQVUFSZPVTIPVME¾STUNBLFTVSFUIBUZPVUSVTU
UIF¾MFTBOELOPXXIBUZPVBSFJOTUBMMJOHJUFNTJOTUBMMFECZNFBOTPGUIF$POUFOU*OTUBMMFS
BSFOPEJGGFSFOU5IF¾STUQBHFPGUIFXJ[BSEIBTBIZQFSMJOLMBCFMFE²7JFX¾MFTJO8JOEPXT
&YQMPSFS©³$MJDLJOHUIJTIZQFSMJOLXJMMPQFOB.JDSPTPGU8JOEPXTˆ&YQMPSFSXJOEPX
EJTQMBZJOHUIF¾MFTUIBUBSFXJUIJOUIFWTJ¾MFBMMPXJOHZPVUPWJFXUIF¾MFTBOEJOTQFDU
UIFDPOUFOUTPGUIPTF¾MFTUPWFSJGZUIBUUIFZXJMMEPOPIBSN"MTPPOUIF¾STUQBHFPGUIF
$POUFOU*OTUBMMFSJTBOBSFBUIBUEJTQMBZTJOGPSNBUJPOBCPVUUIFWTJ¾MF*GUIFWTJ¾MFUP
JOTUBMMXBTTJHOFEXJUIBO"VUIFOUJDPEFˆTJHOBUVSFUIFOBNFPGUIFDPNQBOZUIBUDSFBUFE
UIFDPOUFOUBTXFMMBTBMJOLUPNPSFJOGPSNBUJPOBCPVUUIBUDPOUFOUXJMMBQQFBS*GUIF¾MF
IBTOPUCFFOTJHOFEUIFOUIJTJOGPSNBUJPOXJMMOPUCFBWBJMBCMF
Note If you click the Review button to view the files to be installed, you should be careful
not to delete any of the files from within the Windows Explorer window. If you do delete any
files, installing the content might fail.
Creating Downloadable Content
:PVDBOBMTPFBTJ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GEVQT[0COG='JMFYZ[
8IFOBMMPGUIFTFUBHTBSFQVUUPHFUIFSXJUIBMJTUPGUIF¾MFTUIBUBSFUPCFJOTUBMMFEZPV
IBWFBWTDPOUFOU¾MFUIBUDBOCFMPBEFECZUIF$POUFOU*OTUBMMFSUPJOTUBMMPOZPVSDPNQVUFS
PSTPNFCPEZFMTFµT"OFYBNQMFPGBWTDPOUFOU¾MFXIJDIDBOCFVTFEUPJOTUBMMDPOUSPMT
POUPUIF5PPMCPYJTHJWFOIFSF
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
<Content>
<FileName>CompanyName\Control.dll</FileName>
<DisplayName>Test control</DisplayName>
<Description>A control to test with</Description>
<FileContentType>Toolbox Control</FileContentType>
<ContentVersion>1.0</ContentVersion>
</Content>
</VSContent>
'JHVSFTIPXFEBTDSFFOTIPUPGUIF$POUFOU*OTUBMMFSXJUIUIJTWTDPOUFOU¾MFPQFO)FSF
UIF$POUSPMEMM¾MFJTTUPSFEJOBGPMEFSOBNFE$PNQBOZ/BNFXIFSFBTUIFWTDPOUFOU¾MF
JTBTTVNFEUPCFJOUIFTBNFGPMEFSDPOUBJOJOHUIFGPMEFS$PNQBOZ/BNF:PVDBODPNCJOF
NVMUJQMF$POUFOUJUFNTXJUIJOBWTDPOUFOU¾MFBMMPXJOHZPVUPEJTUSJCVUFNVMUJQMFJUFNTBU
POFUJNFPSUPHSPVQJUFNTUIBUBSFSFMBUFEBOEEFQFOEFOUPOPOFBOPUIFS
Installing Templates and Starter Kits
$SFBUJOHOFXQSPKFDUTPSOFX¾MFTUIBUHPXJUIJOQSPKFDUTJTPOFPGUIFNPTUDPNNPOBOE
ZFUNPSFDPNQMJDBUFEUBTLTUIBUBEFWFMPQFSNBZQFSGPSN*GZPVOFFEFEUPDSFBUFB¾MFXJUI
BTJNQMFDMBTTXJUIJOUIBU¾MFUIFUBTLJTOµUPWFSMZDPNQMFY±ZPVTJNQMZBEEBOFXCMBOL¾MF
UPUIFQSPKF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POTPMF"QQMJDBUJPO[JQGPS8JOEPXT#BTFEPOUIFWBMVFTHJWFOJOUIF9.-UIF[JQ
¾MFGPSUIFUFNQMBUFJTJOTUBMMFEJOUPUIFMPDBUJPO$=%PDVNFOUTBOE4FUUJOHT=WUGTPCOG=.Z
%PDVNFOUT=7JTVBM4UVEJP=5FNQMBUFT=1SPKFDU5FNQMBUFT=7JTVBM$=8JOEPXT
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
<Content>
<FileName>MyConsoleApplication.zip</FileName>
<DisplayName>My Console Application</DisplayName>
<FileContentType>VSTemplate</FileContentType>
Chapter 4:
Community Content and VSTemplates
61
<ContentVersion>1.0</ContentVersion>
<Attributes>
<Attribute name="ProjectType" value="Visual C#"/>
<Attribute name="ProjectSubType" value="Windows"/>
<Attribute name="TemplateType" value="Project"/>
</Attributes>
</Content>
</VSContent>
8IFOUIF$POUFOU*OTUBMMFSIBTJOTUBMMFEUIJTWTDPOUFOU¾MFZPVXJMMCFBCMFUPDSFBUFB
DPOTPMFBQQMJDBUJPOXJUIJOUIFOFXQSPKFDUEJBMPHCPY5IFUFNQMBUFXJMMBQQFBSVOEFS
UIFOPEF7JTVBM$=8JOEPXTJOUIFUSFFPOUIFMFGUTJEFPGUIFEJBMPHCPYBOEJOUIF.Z
5FNQMBUFTTFDUJPOPGUIFMJTUPOUIFSJHIUTJEFPGUIFEJBMPHCPY
Wizards, Templates, and Starter Kits
8IFO ZPV DSFBUF OFX QSPKFDUT PS JUFNT GPS B QSPKFDU ZPV BSF MJLFMZ UP TFF UIF UFSNT
XJ[BSETUFNQMBUFTBOETUBSUFSLJUT&BDIJTBEJGGFSFOUNFUIPEGPSDSFBUJOHOFXDPEF
5IF NPTU CBTJD UFDIOJRVF GPS DSFBUJOH DPEF JT UISPVHI B XJ[BSE " XJ[BSE JT B $0.
PCKFDUUIBUZPVXSJUFGSPNTDSBUDIUPQFSGPSNBMMUIFXPSLOFDFTTBSZUPHFOFSBUFBQSPKFDU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BMMBDPOUSPMB%--DPOUBJOJOHUIFDPOUSPMNVTUCF
DPQJFEJOUPBQMBDFXIFSFJUDBOCFGPVOEBOEUIFO7JTVBM4UVEJPNVTUCFTUBSUFETPUIBUJU
DBOMPBEBOEUIFOBEEUIFDPOUSPMUPUIF5PPMCPY
1MBDJOHUIFDPOUSPMJOUPBQMBDFXIFSF7JTVBM4UVEJPDBO¾OEJUJTFBTZ±BUMFBTUXJUIUIF
$POUFOU*OTUBMMFS8IFOUIF$POUFOU*OTUBMMFSJOTUBMMTBDPOUSPMGPSUIF5PPMCPYUIFDPOUSPM
62
Working with Microsoft Visual Studio 2005
JTQMBDFEJOUPUIFEJSFDUPSZ.Z%PDVNFOUT=7JTVBM4UVEJP=$POUSPMT=%QORCP[0COG
XIFSF%QORCP[0COGJTUIFOBNFPGZPVSDPNQBOZHSPVQPSBOZPUIFSVOJRVFOBNFUIBU
ZPVXBOUUPVTF8IFOUIF$POUFOU*OTUBMMFSJOTUBMMTDPOUSPMTJUXJMMVTFUIJT%QORCP[0COG
UPDSFBUFBOFXUBCJOUIF5PPMCPYJGPOFXJUIUIBUOBNFEPFTOPUBMSFBEZFYJTU
BOEUIFO
UIFDPOUSPMTDPOUBJOFEXJUIJOUIF%--TJOUIJTEJSFDUPSZXJMMCFBEEFEUPUIBU5PPMCPYUBC
5IJTQSPDFEVSFBMMPXTZPVUPDSFBUFBUBCJOUIF5PPMCPYUIBUEJTUJOHVJTIFTZPVSDPOUSPMT
GSPNUIFEFGBVMUPOFTJOTUBMMFECZ7JTVBM4UVEJPPSPUIFSDPNQBOJFT
Note You can also place controls into the directory My Documents\Visual Studio 2005\
Controls. If you place a DLL here, the controls the DLL contain will be placed on a tab named
My Controls.
5IFTFDPOETUFQUPJOTUBMMUIFDPOUSPMJTUPJOWPLF7JTVBM4UVEJP7JTVBM#BTJD&YQSFTT
7JTVBM$&YQSFTTBOETPPOUPBMMPXUIPTFQSPHSBNTUPJOTUBMMUIFDPOUSPM*OTUBMMJOH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OOFDFTTBSZUPJOTUBMMBDPOUSPM5IFOBNFPGUIFDPOUFOUUZQFGPS
BDPOUSPMJT5PPMCPY$POUSPMXIJDITIPVMECFQMBDFEXJUIJOUIF'JMF$POUFOU5ZQFUBH
*OUIJTFYBNQMFBUBCOBNFE$PNQBOZ/BNFXJMMCFDSFBUFEXJUIJOUIF5PPMCPYBOEBOZ
DPOUSPMTJOUIF¾MF$POUSPMEMMXJMMCFBEEFEUPUIBUUBC
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
<Content>
<FileName>CompanyName\Control.dll</FileName>
<DisplayName>Test control</DisplayName>
<Description>A control to test with</Description>
<FileContentType>Toolbox Control</FileContentType>
<ContentVersion>1.0</ContentVersion>
</Content>
</VSContent>
Chapter 4:
Community Content and VSTemplates
63
5IJTBMTPNFBOTUIBUZPVOFFEUPDSFBUFBGPMEFSOBNFE$PNQBOZ/BNFJOUIFGPMEFS
DPOUBJOJOHUIFWTDPOUFOU¾MFBOEZPVXPVMEQMBDFUIF$POUSPMEMM¾MFXJUIJOUIF
$PNQBOZ/BNFGPMEFS
Installing Code Snippets
$PEF4OJQQFUTBSFBTPVSDFDPEFFEJUPSQSPEVDUJWJUZFOIBODFS*GZPVBSFFEJUJOHB$¾MF
BOEZPVOFFEUPDSFBUFGPSFYBNQMFBHQTMPPQZPVDPVMENBOVBMMZFOUFSUIFUFYUGPSUIBU
HQTMPPQPSZPVDPVMEVTFBTOJQQFUUPDSFBUFUIFMPPQGPSZPV5PVTFBTOJQQFU¾STUUZQF
UIFXPSEGPSUIJTXJMMDBVTFUIF*OUFMMJ4FOTFˆXJOEPXUPBQQFBS*GZPVXFSFUPQSFTTUIF
5BCLFZUXJDFXJUIUIFHQTLFZXPSETFMFDUFEJOUIF*OUFMMJ4FOTFXJOEPX7JTVBM4UVEJPXJMM
BVUPNBUJDBMMZHFOFSBUFUIFCBTJDTUSVDUVSFPGBHQTMPPQBTTIPXOJO'JHVSF
Figure 4-4
The basic structure of a for loop, as generated from a snippet
5IFDPOUSPMWBSJBCMFJTOBNFEKCVUZPVNJHIUOPUBMXBZTXBOUUIJTWBSJBCMFUPIBWFUIBUOBNF
5PDIBOHFUIJTWBSJBCMFOBNFKVTUUZQFJOUIFOFXOBNF8IFOZPVBSF¾OJTIFEUZQJOHQSFTT
UIF5BCLFZBOEUIFWBSJBCMFOBNFKBTXFMMBTBMMVTFTPGUIFWBSJBCMFKXJUIJOUIFTOJQQFU
DPEFXJMMCFSFOBNFEBTTIPXOJO'JHVSFXIFSFUIFWBSJBCMFOBNFJTDIBOHFEUPL
Figure 4-5
The basic for loop, with the control variable renamed to j
5IFWBSJBCMFOBNFMFOHUIJTIJHIMJHIUFEOFYUBOEJUUPPDBOCFFEJUFE"GUFSUZQJOHUIF
OVNCFSDSFBUJOHBMPPQPWFSUIFOVNCFSTUPJODMVTJWF
UIFHQTMPPQBQQFBSTMJLFUIF
POFJO'JHVSF
Figure 4-6
The basic for loop, with the upper limit set to 10
"MUIPVHIUIJTJTOPUBMPUPGUFYUUPUZQFJNBHJOFUZQJOHUIJTCJUPGDPEFPWFSBOEPWFSKVTU
BTZPVEPEVSJOHZPVSEFWFMPQNFOUXPSLBOEZPVDBOTFFUIFBNPVOUPGLFZTUSPLFTZPV
XJMMTBWF4OJQQFUTBSFTUPSFEJOBO9.-GPSNBUBOEDBOCFJOTUBMMFEJOUIFTBNFNBOOFSBT
PUIFSDPOUFOUJUFNT
"TOJQQFU¾MFDBODPOUBJONBOZEJGGFSFOUTOJQQFUTCVUFBDI¾MFDBODPOUBJOTOJQQFUTGPS
POMZPOFQBSUJDVMBSQSPHSBNNJOHMBOHVBHF5IF7JTVBM#BTJD7JTVBM$7JTVBM+BOE
64
Working with Microsoft Visual Studio 2005
9.-FEJUPSTDVSSFOUMZTVQQPSUTOJQQFUT8IFOJOTUBMMJOHBTOJQQFUUIF$POUFOUUBHNVTU
DPOUBJOBO"UUSJCVUFUBHJOEJDBUJOHXIJDIQSPHSBNNJOHMBOHVBHFUIFTOJQQFUTVQQPSUT
5IFOBNFPGUIJTBUUSJCVUFJTNCPIBOEUIFQPTTJCMFWBMVFTBSFXDEUJCTRLUJCTRBOEZON
5IFGPMMPXJOHWTDPOUFOU¾MFDPOUBJOTUXPJUFNTUPJOTUBMMB7JTVBM#BTJDBOEB7JTVBM$
TOJQQFU
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
<Content>
<FileName>VBSnippets.Snippet</FileName>
<DisplayName>Visual Basic snippets</DisplayName>
<Description>Visual Basic snippets to install.</Description>
<FileContentType>Snippet</FileContentType>
<ContentVersion>1.0</ContentVersion>
<Attributes>
<Attribute name="lang" value="vb"></Attribute>
</Attributes>
</Content>
<Content>
<FileName>CSSnippet.Snippet</FileName>
<DisplayName>C# snippets</DisplayName>
<Description>C# snippets to install. </Description>
<FileContentType>Snippet</FileContentType>
<ContentVersion>1.0</ContentVersion>
<Attributes>
<Attribute name="lang" value="csharp"></Attribute>
</Attributes>
</Content>
</VSContent>
Installing Add-ins and Macros
*O$IBQUFSTBOEXFµMMTFFIPXUPDSFBUFNBDSPTBOEBEEJOTCVUXIJMFXFBSFEJTDVTTJOH
JOTUBMMJOH¾MFTXFDBOTIPXZPVIPXUPJOTUBMMUIFTFUZQFTPG¾MFT"OBEEJOBUMFBTUBOBEE
JOXSJUUFOVTJOHB/&5'SBNFXPSLQSPHSBNNJOHMBOHVBHF
DPOTJTUTPGBUMFBTUPOF¾MFBOE
QPTTJCMZNPSF5IFSFRVJSFE¾MFJTB¾MFXJUIUIFBEEJOFYUFOTJPO5IJT¾MFEFTDSJCFTUIF
BEEJOUP7JTVBM4UVEJPBOEQSPWJEFTJOGPSNBUJPOTVDIBTUIFOBNFBOEEFTDSJQUJPOPGUIF
BEEJO5IFTFDPOEQPTTJCMF¾MFJTBEMMUIBUDPOUBJOTUIFDPEFGPSUIFBEEJO5IFDPOUFOU
UZQFGPSBOBEEJOJT"EEJOBOEUIJTJTPOFPGUIFGFXDPOUFOUUZQFTUIBUXJMMBDDFQUNPSF
UIBOPOF'JMF/BNFUBHUPTQFDJGZNPSFUIBOPOF¾MFUPJOTUBMM5IJTDPOUFOUUZQFEPFTOPU
OFFEBOZ"UUSJCVUFUBHT)FSFJTBTJNQMFWTDPOUFOU¾MFVTFEUPJOTUBMMBOBEEJO
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
<Content>
<FileName>MyAddin.Addin</FileName>
<FileName>MyAddin.dll</FileName>
<DisplayName>An add-in to install.</DisplayName>
<Description>This is a test add-in.</Description>
<FileContentType>Addin</FileContentType>
<ContentVersion>1.0</ContentVersion>
</Content>
</VSContent>
Chapter 4:
Community Content and VSTemplates
65
"NBDSPNVDIMJLFBOBEEJOJTB¾MFUIBUDPOUBJOTQSPHSBNDPEFUIBUUIFVTFSDBOSVO
UPDVTUPNJ[FBOENPEJGZEBUBXJUIJO7JTVBM4UVEJP5IFEJGGFSFODFCFUXFFOBNBDSPBOE
BOBEEJOJTUIBUUIFVTFSDBOFEJUBOENPEJGZUIFNBDSPDPEFBOEUIFSFJTBOFEJUJOHBOE
EFCVHHJOHFOWJSPONFOUEFWPUFEUPKVTUUIJTUZQFPGDPEF5IF9.-VTFEUPJOTUBMMBNBDSP
IBTUIF'JMF$POUFOU5ZQFPG.BDSP1SPKFDUBOEUIFPOMZ¾MFUZQFTUIBUDBOCFJOTUBMMFECZ
UIFNBDSPJOTUBMMFSBSF¾MFTUIBUFOEXJUIBWTNBDSPTFYUFOTJPO
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
<Content>
<FileName>Samples.vsmacros</FileName>
<DisplayName>A macro to install.</DisplayName>
<Description>A macro project to install.</Description>
<FileContentType>Macro Project</FileContentType>
<ContentVersion>1.0</ContentVersion>
</Content>
</VSContent>
"EEJOBOENBDSPDPOUFOUJOTUBMMFSTBSFBCJUEJGGFSFOUUIBOUIFPUIFSJOTUBMMFSUZQFTJOUIBU
UIFTFDPOUFOUUZQFTXJMMJOTUBMMPOMZG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F[JQVUJMJUZUIBUUIF$POUFOU*OTUBMMFSVTFTTVQQPSUTPOMZCBTJD
[JQQJOHGVODUJPOBMJUZ
Note The extension .vsi has its roots in the file extension .msi, which stands for Microsoft
installer. VSI is short for Visual Studio installer. The two technologies, however, should not be
confused with one another. The Content Installer cannot be used to install .msi files, and the
Microsoft installer cannot be used to install .vsi files.
"GUFSZPVIBWFSFOBNFEUIF[JQ¾MFUPIBWFUIFFYUFOTJPOWTJZPVDBOFJUIFSFNBJMPSQPTU
UIF¾MFUPB8FCTJUFTPUIBUGSJFOETPSDPMMFBHVFTDBOJOTUBMMUIFDPOUFOUUIBUZPVIBWF
66
Working with Microsoft Visual Studio 2005
DSFBUFE#VUCFGPSFZPVNBLFUIFWTJ¾MFBWBJMBCMFUPPUIFSTGPSVTFZPVXJMMXBOUUPUFTU
UIF¾MFUPNBLFTVSFJUXPSLT:PVDPVMEKVTUEPVCMFDMJDLUIFWTJ¾MFGSPNXJUIJO8JOEPXT
&YQMPSFSCVUJGZPV¾OEBCVHJOZPVSDPOUFOUZPVXPVMEOFFEUPDPSSFDUUIFQSPCMFNUIFO
SF[JQBOESFOBNFUIFFYUFOTJPOBOESFQFBU3BUIFSUIBOUBLJOHUIFUJNFUPHPUISPVHIUIF
[JQSFOBNFQSPDFTTZPVDBOKVTUEPVCMFDMJDLUIFWTDPOUFOU¾MF#PUIUIFWTJBOEWTDPOUFOU
FYUFOTJPOTBSFBTTPDJBUFEXJUIUIF$POUFOU*OTUBMMFS:PVDBOBMTPVTFUIF$POUFOU*OTUBMMFS
UPCSPXTFUPBWTJ[JQPSWTDPOUFOU¾MF'SPNUIF3VOEJBMPHCPYJOUIF8JOEPXT4UBSU
NFOVOBWJHBUFUP$=1SPHSBN'JMFT=$PNNPO'JMFT=.JDSPTPGU4IBSFE=.4&OWBOESVOUIF
DPNNBOEWTDPOUFOUJOTUBMMFSFYFCSPXTF5IJTXJMMEJTQMBZUIFTUBOEBSE8JOEPXT0QFO'JMF
EJBMPH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UBMMFS
Chapter 4:
Community Content and VSTemplates
67
The ContentBuilder Utility
$SFBUJOH WTJ ¾MFT JT OPU PWFSMZ DPNQMJDBUFE CVU JU DBO CF NBEF FBTJFS 5IF TBNQMFT
GPSUIJTCPPLJODMVEFUIFTPVSDFTUPBUPPMOBNFE$POUFOU#VJMEFS5IJTUPPMQSPWJEFT
B HSBQIJDBM VTFS JOUFSGBDF (6*
GPS CVJMEJOH B WTJ ¾MF KVTU CZ TFMFDUJOH ¾MFT PO ZPVS
DPNQVUFSµTEJTLESJWF
Implementing Your Own Downloadable Types
"TXFIBWFTFFOUIF$POUFOU*OTUBMMFSPGGFSTEJGGFSFOUDPOUFOUUZQFTUIBUZPVDBOJOTUBMM
UFNQMBUFTBEEJOTNBDSPTDPEFTOJQQFUTBOEDPOUSPMT)PXFWFSZPVNJHIUIBWFBOJEFBGPS
BOPUIFSLJOEPGDPOUFOUUIBUDPVMEIFMQVTFSTCFUUFSDPMMBCPSBUFXJUIPOFBOPUIFS5IF$POUFOU
*OTUBMMFSJTFYUFOTJCMFBMMPXJOHZPVUPDSFBUFZPVSPXODPOUFOUUZQFJOTUBMMFST*OUIJTTFDUJPO
XFµMMTIPXZPVIPXZPVDBOFYUFOEUIF$POUFOU*OTUBMMFSXJUIZPVSPXOJOTUBMMFSUZQFT
Creating the Project
"DVTUPNJOTUBMMFSJTTJNQMZB/&5DMBTTMJCSBSZ%--UIBUUIF$POUFOU*OTUBMMFSXJMMMPBE
BOESVOXIFOBWTDPOUFOU¾MFIBTB'JMF$POUFOU5ZQFUBHWBMVFUIBUDPSSFTQPOETUPUIF
OBNFZPVHJWFUPZPVSPXODVTUPNJOTUBMMFS5IFFBTJFTUXBZUPHFUTUBSUFEDSFBUJOHZPVS
DPOUFOUJOTUBMMFSJTUPVTFBTUBSUFSLJU"DDPNQBOZJOHUIFTBNQMFTGPSUIJTCPPLBSFUXP
TUBSUFSLJUTOBNFE%QPVGPV+PUVCNNGT7+.GUU2CIGBOE%QPVGPV+PUVCNNGT7+2CIG#PUIBQQFBS
JOUIF7JTVBM$TFDUJPOPGUIF/FX1SPKFDUEJBMPHCPY5IF¾STUPGUIFTFTUBSUFSLJUTJTVTFE
UPDSFBUFBQSPKFDUUIBUEPFTOPUIBWFBOZDPO¾HVSBUJPOPQUJPOTJOUIF$POUFOU*OTUBMMFS
XJ[BSE5IFTFDPOETUBSUFSLJUJTVTFEUPDSFBUFBQSPKFDUUIBUEJTQMBZTVTFSJOUFSGBDF6*
GPSDPO¾HVSBUJPO8IFOZPVDSFBUFBQSPKFDUVTJOHUIFTUBSUFSLJUUIFOBNFPGUIFQSPKFDU
BTFOUFSFEJOUIF/FX1SPKFDUEJBMPHCPYXJMMCFDPNFCPUIUIFOBNFPGUIFJOTUBMMFSBOE
UIFUFYUUIBUZPVVTFXJUIJOUIF'JMF$POUFOU5ZQFUBHJOBWTDPOUFOU¾MF5IFGPMMPXJOH
TFDUJPOTEFTDSJCFIPXUIFDPEFHFOFSBUFECZUIFTFTUBSUFSLJUTXPSLBOEIPXZPVDBOXSJUF
DPEFUPJOUFSBDUXJUIUIF$POUFOU*OTUBMMFS
Interface Implementation
"OJOTUBMMFSJTTJNQMZBDMBTTMJCSBSZUIBUJNQMFNFOUTUIFJOUFSGBDF++ORQTV%QOOWPKV[%QPVGPV
5IJTBOEPUIFSJOUFSGBDFTUIBUZPVXJMMVTFUPQSPHSBNUIF$POUFOU*OTUBMMFSBSFEF¾OFEJO
UIFBTTFNCMZ.JDSPTPGU7JTVBM4UVEJP74$POUFOU*OTUBMMFSEMM5IF++ORQTV%QOOWPKV[%QPVGPV
JOUFSGBDFIBTUIFGPMMPXJOHTJHOBUVSF
public interface IImportCommunityContent
{
bool AddContentItem(IContentItem[] contentItems, IContentInstallerSite site);
string Import(IContentItem contentItem);
bool SupportsImportUI { get; }
IImportPageData[] GetImportPages();
void UpdateContentItemInstallStatus(IContentItem[] contentItems);
}
68
Working with Microsoft Visual Studio 2005
8IFOUIF$POUFOU*OTUBMMF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FTBT
XFMMBTUIFOBNFTBOEWBMVFTPGUIPTFBUUSJCVUFT+%QPVGPV+VGOBMTPFYQPTFTUXPNFUIPET
)GV(KNG0COGUBOE)GV4QQV(KNG0COGU8IFOUIF$POUFOU*OTUBMMFSSFBETUIF$POUFOU
TFDUJPOPGBWTDPOUFOU¾MFJUHBUIFSTBMMUIF'JMF/BNFUBHTUPHFUIFSBOETUPSFTUIFNGPS
MBUFSVTF8IFOZPVDBMMUIF)GV4QQV(KNG0COGUNFUIPEUIFMJTUPG¾MFOBNFTBTHJWFOJOUIF
WTDPOUFOU¾MFJTSFUVSOFE)GV(KNG0COGUBMTPSFUVSOTUIFMJTUPG¾MFOBNFTFYDFQUUIBUUIFTF
¾MFOBNFTBSFQSFQFOEFEXJUIUIFEJSFDUPSZJOXIJDIUIF¾MFTBSFQMBDFEBOEDBOCFVTFE
BTUIFTPVSDFPGBDPQZPQFSBUJPO"TTVNJOHUIBUZPVIBWFBWBSJBCMFOBNFEFGUVKPCVKQP2CVJ
DPOUBJOJOHUIFQBUIJOXIJDIZPVBSFDPQZJOHJUFNTZPVDBOVTFDPEFTVDIBTUIJTUPDPQZ
UIF¾MFTJOUPUIFDPSSFDUMPDBUJPO
foreach(VSContentInstaller.ContentItem
contentItem in contentItems)
{
string []sourceFileNames =
contentItem.GetFileNames();
string []rootFileNames =
contentItem.GetRootFileNames();
for (int i = 0; i < sourceFileNames.Length ; i++)
{
string combinedDirectory = Path.Combine(
destinationPath, rootFileNames);
Directory.CreateDirectory(
Path.GetDirectoryName(combinedDirectory));
File.Copy(sourceFileNames[i], combinedDirectory);
}
}
5IFSFBTPOGPSUIFTFUXPNFUIPETJTRVJUFTJNQMF4VQQPTFUIFQBUIHJWFOJOB
'JMF/BNFUBHDPOUBJOTBEJSFDUPSZ.Z4VC%JSFDUPSZ='JMFFYUGPSFYBNQMF*GZPV
XFSFHJWFOPOMZUIFTPVSDFEJSFDUPSZUPDPQZUIF¾MFGSPNZPVXPVMEOPULOPXIPX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UVT[4QQV#RRNKECVKQP0COG7UGT&CVC(QNFGT#RRNKECVKQP2CVJ
2TQI+FBOE#RRNKECVKQP+OCIG&BDIPGUIFTFQSPQFSUJFTJTWBMJEGPSFWFSZFEJUJPOPG7JTVBM
4UVEJPFYDFQUUIF2TQI+FQSPQFSUZ7JTVBM4UVEJPJTUIFPOMZWFSTJPOUIBUTVQQPSUTBO
BVUPNBUJPOPCKFDUNPEFMTPGPSBOZPGUIF&YQSFTTWFSTJPOTPG7JTVBM4UVEJPUIJTQSPQFSUZ
XJMMSFUVSOBOFNQUZTUSJOH5BCMFMJTUTFBDIPGUIFTFQSPQFSUJFTBOEFYBNQMFWBMVFT
SFUVSOFEGSPNUIFTFQSPQFSUJFT
Table 4-1
IApplicationData properties
IApplicationData
property
Example Value
Property Use
RegistryRoot
Software\Microsoft\
VisualStudio\8.0
This is the location where data for the
application is stored in the registry.
Prepend the registry hive key, either HKEY_
LOCAL_MACHINE or HKEY_CURRENT_
USER, as appropriate.
ApplicationName
Microsoft Visual Studio 2005
Display text for the name of the
application.
UserDataFolder
%USERPROFILE%\My
Documents\Visual Studio
2005
Location where user files are stored for the
application.
ApplicationPath
“c:\Program Files\Microsoft
Visual Studio 8\Common7\
IDE\devenv.exe”
The path to the application.
ProgId
VisualStudio.DTE.8.0
COM ProgID for the application. This is
not valid for the Express versions of Visual
Studio.
ExpressVersion
False
Returns true if the program is an express
version (such as Visual Basic Express), or
false if the program is Visual Studio.
ApplicationImage
An image for the application. If the content
item does not supply an image, the default
Visual Studio logo (the “Infinity” icon) is used.
5IFMBTUJNQPSUBOUNFUIPEPOUIF+%QPVGPV+PUVCNNGT5KVGJTUIF5JQWNF%QPVKPWGNFUIPE5IF
$POUFOU*OTUBMMFSJTNVMUJUISFBEFEBMMPXJOHUIFVTFSUPDBODFMJOTUBMMBUJPOFWFOXIJMFBO
JOTUBMMFSJTJOTUBMMJOHEBUBUPUIFDPNQVUFS1FSJPEJDBMMZXIJMFJOTUBMMJOHTVDIB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DPOUBJOUFYUTVDIBT64&3130'*-&8IFOUIF
$POUFOU*OTUBMMFSSFBETUIFTFWBMVFTGSPNUIFTZTUFNSFHJTUSZJUXJMMFYQBOEBMMFOWJSPONFOU
WBSJBCMFTJOUPUIFJSTFUWBMVFT
Listing 4-1
An example registry script to register a content installer
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSEnvCommunityContent\
ContentTypes\MyContentType]
"ClassName"="MyContentType.MyContentType"
"Assembly"="C:\Documents and Settings\CRAIGS\My Documents\
Visual Studio\Projects\MyContentType\MyContentType\
bin\debug\MyContentType.dll"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSEnvCommunityContent\
ContentTypes\MyContentType\ContentHosts]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSEnvCommunityContent\
Chapter 4:
Community Content and VSTemplates
ContentTypes\MyContentType\ContentHosts\1.0]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSEnvCommunityContent\
ContentTypes\MyContentType\ContentHosts\1.0\Visual Studio 2005]
"ApplicationName"="Microsoft Visual Studio 2005"
"ApplicationPath"="C:\\Program Files\\Microsoft Visual Studio 8\\
Common7\\IDE\\devenv.exe"
"RegistryRoot"="Software\\Microsoft\\VisualStudio\\8.0"
"UserDataFolder"="%USERPROFILE%\\My Documents\\Visual Studio 2005"
"ProgId"="VisualStudio.DTE.8.0"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSEnvCommunityContent\
ContentTypes\MyContentType\ContentHosts\1.0\Visual Basic Express 2005]
"ApplicationName"="Microsoft Visual Basic Express 2005"
"ApplicationPath"="C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE\\
vbexpress.exe"
"RegistryRoot"="Software\\Microsoft\\VBExpress\\8.0"
"UserDataFolder"="%USERPROFILE%\\My Documents\\Visual Studio 2005"
"ExpressVersion"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSEnvCommunityContent\
ContentTypes\MyContentType\ContentHosts\1.0\
Visual C# Express 2005]
"ApplicationName"="Microsoft Visual C# Express 2005"
"ApplicationPath"="C:\\Program Files\\Microsoft Visual Studio 8\\
Common7\\IDE\\csexpress.exe"
"RegistryRoot"="Software\\Microsoft\\VCSExpress\\8.0"
"UserDataFolder"="%USERPROFILE%\\My Documents\\Visual Studio 2005"
"ExpressVersion"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSEnvCommunityContent\ContentTypes\
MyContentType\ContentHosts\1.0\Visual C++ Express 2005]
"ApplicationName"="Microsoft Visual C++ Express 2005"
"ApplicationPath"="C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE\\
vcexpress.exe"
"RegistryRoot"="Software\\Microsoft\\VCExpress\\8.0"
"UserDataFolder"="%USERPROFILE%\\My Documents\\Visual Studio 2005"
"ExpressVersion"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSEnvCommunityContent\
ContentTypes\MyContentType\ContentHosts\1.0\
Visual J# Express 2005]
"ApplicationName"="Microsoft Visual J# Express 2005"
"ApplicationPath"="C:\\Program Files\\Microsoft Visual Studio 8\\
Common7\\IDE\\vjsexpress.exe"
"RegistryRoot"="Software\\Microsoft\\VJSExpress\\8.0"
"UserDataFolder"="%USERPROFILE%\\My Documents\\Visual Studio 2005"
"ExpressVersion"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSEnvCommunityContent\
ContentTypes\MyContentType\ContentHosts\1.0\
Visual Web Developer Express 2005]
73
74
Working with Microsoft Visual Studio 2005
"ApplicationName"="Microsoft Visual Web Developer Express 2005"
"ApplicationPath"="C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE\\
vwdexpress.exe"
"RegistryRoot"="Software\\Microsoft\\VWDExpress\\8.0"
"UserDataFolder"="%USERPROFILE%\\My Documents\\Visual Studio 2005"
"ExpressVersion"=dword:00000001
An Example—Samples Installer
/PXUIBUZP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TFDVSJUZNJOEFEXIFODSFBUJOHZPVSJOTUBMMFS"MMPXJOTUBMMBUJPOPGPOMZUIFNJOJNVNMJTU
PG¾MFUZQFTOPUFWFSZUIJOHUIBUDPVMEQPTTJCMZCFJOTUBMMFE*GZPVSJOTUBMMFSOFFETUPJOTUBMM
NVMUJQMF¾MFUZQFTZPVDBOTQFDJGZUIF%QPVGPV+PUVCNNGT5WRRQTVGF(KNG5GEWTKV[#VVTKDWVGBUUSJCVUF
Chapter 4:
Community Content and VSTemplates
75
NVMUJQMFUJNFT5IJTJTBQPSUJPOPGUIFDMBTTVTFEUPJOTUBMMBEEJO¾MFUZQFTBOEJUTQFDJ¾FT
UIBUJUDBOJOTUBMMBEEJOBOEEMM¾MFT
[ContentInstallerContentTypeRestriction("Addin")]
[ContentInstallerSupportedFileSecurity(".addin")]
[ContentInstallerSupportedFileSecurity(".dll")]
class AddinInstallerPage : IImportCommunityContent
{
...
}
:PVSJOTUBMMFSBMTPOFFETUPNBLFTVSFUIBUUIF¾MFTUIBUBSFCFJOHJOTUBMMFEBSFJOTUBMMJOHUP
UIFMPDBUJPOZPVJOUFOEFE4VQQPTFZPVXBOUFEUPJOTUBMMZPVSDPOUFOUJOUPUIFGPMEFS.Z
%PDVNFOUT=7JTVBM4UVEJP=.Z$POUFOU5ZQFUIFJOTUBMMFSQMBDFTFYF¾MFTPOEJTLBOE
UIFWTDPOUFOU¾MFHJWFTBEFTUJOBUJPOQBUIPG=8JOEPXT=OPUFQBEFYF8IFOZPVSJOTUBMMFS
SVOTUIJTWTDPOUFOU¾MFJUXJMMUSZUPPWFSXSJUFUIF/PUFQBEQSPHSBNXJUIDPEFUIBUDPVME
CFIBSNGVMUPUIFDPNQVUFS5IFOFYUUJNFUIFVTFSUSJFTUPSVOUIF/PUFQBEQSPHSBNUIBU
NBMJDJPVTDPEFXJMMSVO5IFSFGPSFZPVTIPVMEDIFDLXIFSFZPVBSFJOTUBMMJOHDPOUFOUCFGPSF
ZPVDBMM+%QPV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JOUIFUSFFUIFZXJMM
Chapter 4:
Community Content and VSTemplates
77
BVUPNBUJDBMMZCFFYQPSUFE8IFOZPVDMJDLUIF/FYUCVUUPOBMJTUPGBTTFNCMJFTSFGFSFODFE
CZUIFQSPKFDUJOXIJDIUIFJUFNJTMPDBUFEJTHJWFO4FF'JHVSF
8IFOUIFUFNQMBUFJT
BEEFEUPBQSPKFDUBOZSFGFSFODFTUIBUZPVTFMFDUJOUIJTTUFQPGUIFXJ[BSEXJMMBMTPCFBEEFE
UPUIFQSPKFDU4VQQPTFZPVBSFBEEJOHB8JOEPXT'PSNUPBDPOTPMFBQQMJDBUJPO*GZPV
XFSFUPTFMFDUUIFDIFDLCPYOFYUUPUIF5[UVGO9KPFQYU(QTOUBTTFNCMZUIFVTFSJNQPSUJOH
ZPVSGPSNXJMMOPUOFFEUPNBOVBMMZBEEUIFSFGFSFODFUP5[UVGO9KPFQYU(QTOUCFDBVTFUIF
UFNQMBUFXJ[BSEXJMMUBLFDBSFPGBEEJOHUIBUSFGFSFODFGPSZPV
Figure 4-8
Selecting a project item to export
Figure 4-9
Selecting the references to import when the project item is added to a project
78
Working with Microsoft Visual Studio 2005
5IF¾OBMQBHFPGFYQPSUJOHBQSPKFDUJUFNJTUIFTBNFBTUIF¾OBMTUFQPGFYQPSUJOHBQSPKFDU
4FF'JHVSF
5IJTTUFQJOUIFXJ[BSEBMMPXTZPVUPTFMFDUBOJDPOUPVTFXJUIJOUIF/FX
1SPKFDUPS"EE/FX*UFNEJBMPHCPYFT*UBMTPBMMPXTZPVUPTQFDJGZUIFUFYUUPTIPXCFOFBUI
UIFJDPOCZDIBOHJOHUIF6GORNCVGPCOG¾FME5IFEFTDSJQUJPOPGUIFUFNQMBUFTIPXOXJUIJO
UIF/FX1SPKFDUBOE"EE/FX*UFNEJBMPHCPYFTDBOCFDIBOHFECZNPEJGZJOHUIF6GORNCVG
FGUETKRVKQP¾FME
Figure 4-10
The final page of the Export Template Wizard
"GUFSZPVDMJDL'JOJT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UNVTUCFPGPOFPGUIFTFUZQFT"GUFSZPVIBWFDSFBUFEZPVSCBTF
QSPKFDUPSQSPKFDUJUFNZPVUIFOOFFEUPHPUISPVHIUIPTF¾MFTBOEJOTFSUTQFDJBMTUSJOHT
Chapter 4:
Community Content and VSTemplates
79
DBMMFETGRNCEGOGPVVQMGPUJOUPUIFTPVSDFBOEQSPKFDU¾MFT5IF&YQPSU5FNQMBUF8J[BSE
BVUPNBUJDBMMZTDBOTBMMUIF¾MFTJOUIFQSPKFDUGPSTQFDJ¾DTUSJOHTTVDIBTUIFOBNFPGUIF
QSPKFDUBOESFQMBDFTUIPTFTUSJOHTXJUIBSFQMBDFNFOUUPLFO8IFOUIFUFNQMBUFXJ[BSE
QSPDFTTFTZPVSTPVSDF¾MFTUIF¾MFTBSFTFBSDIFEGPSUIFSFQMBDFNFOUUPLFOT8IFOPOFJT
GPVOEUIFUPLFOJTSFQMBDFEXJUIBWBMVFUIBUJTEFQFOEFOUPOUIFTUBUFPGZPVSDPNQVUFS
BOEUIF¾MFTUIBUBSFDVSSFOUMZPQFOJOTJEFPG7JTVBM4UVEJP'PSFYBNQMFTVQQPTFZPVS
DPNQBOZVTFEBDPNNPOIFBEFSBUUIFUPQPGFBDITPVSDF¾MFUIBUMPPLFEMJLFUIFGPMMPXJOH
//-----------------------------------------------------------------// Class1.cs
//
// (C) Copyright 1999 My Company.
//
// Contents:
My source code file
//
// Owner:
UserName
//
// Revisions: 02/07/2005 14:24:35
Created by UserName
//
//------------------------------------------------------------------
:PVDPVMENPEJGZZPVSUFNQMBUF¾MFTPUIBUXIFOQSPDFTTFECZUIFXJ[BSEJUJTBVUPNBUJDBMMZ
NPEJ¾FEUPDPOUBJOUIFSFMFWBOUJOGPSNBUJPO5IFNPEJ¾FEUFNQMBUFXPVMEMPPLMJLFUIJT
//-----------------------------------------------------------------// $itemname$
//
// (C) Copyright $year$ $registeredorganization$.
//
// Contents:
My source code file
//
// Owner:
$username$
//
// Revisions: $time$
Created by $username$
//
//------------------------------------------------------------------
5IFSFBSFNBOZEJGGFSFOUSFQMBDFNFOUWBMVFTBWBJMBCMFGPSVTFXJUIJOZPVSQSPKFDU¾MFT5BCMF
MJTUTUIFTFWBMVFTBTXFMMBTQPTTJCMFWBMVFTBOEBEFTDSJQUJPO
Table 4-2
Replacement Values
Replacement Variable
Example Value
Description
$guid1$, $guid2$, $guid3$,
$guid4$, $guid5$, $guid6$,
$guid7$, $guid8$, $guid9$,
$guid10$
e3593046-f53f-48f99c6e-c1761196384f
These variables are used for generating globally
unique identifiers (GUIDs) within source code. They
can be used when a unique value is necessary,
including for generating COM object code.
$time$
02/07/2005 14:24:35
The time that the source code file was generated.
$year$
2005
The year that the source code was generated.
80
Working with Microsoft Visual Studio 2005
Table 4-2
Replacement Values
Replacement Variable
Example Value
Description
$username$
Craigs
The name of the user who is currently logged on to
the computer.
$userdomain$
Redmond
The name of the domain, if available, that the
computer is a member of. At Microsoft, the
name of the domain that most users belong to is
Redmond.
$machinename$
CraigsLaptop
The name of the computer on the network.
$clrversion$
v2. 0.50215
The version of the .NET Framework that is being
used by Visual Studio at the time the file is
processed to build the program.
$registeredorganization$
Microsoft
The name of the organization that owns the license
for the operating system. This value is entered
when installing the operating system when the user
is prompted for the user name and organization.
$itemname$
My ClassFile
The file name as entered by the user, but with the
extension removed.
$safeitemname$
My_ClassFile
The file name as entered by the user, but modified
so that the n ame can be used as an identifier. Any
character that would not be recognized as a valid
identifier character is replaced with an underscore.
This name does not include the extension.
$itemrootname$
My Form1.vb
This is the name of an item being added to either a
new or existing project.
$safeitemrootname$
My_Form1.vb
The same as $itemrootname$, but in a form that
can be used as a programmatic identifier.
$fileinputname$
Form1
Available only to Add New item templates, this
is the file name entered into the Add New Item
dialog box. If the name entered into the dialog
box is Form1.MyForm.vb, this value will be Form1.
MyForm.
$fileinputextension$
.vb
Available only to Add New Item templates, this is
the extension of the file name entered into the Add
New Item dialog box.
$rootnamespace$
WindowsApplication1
Available only to Add New item templates, this is
the default namespace, as specified in the project
properties window, for the project. If an item is
added to a folder of a project, this value will also
include the folder name. For example, adding
a file to a folder named NewFolder will cause
$rootnamespace$ to be WindowsApplication.
NewFolder.
Chapter 4:
Table 4-2
Community Content and VSTemplates
81
Replacement Values
Replacement Variable
Example Value
Description
$runsilent$
true/false
This value is set to true when any user interface
that the wizard may display should be hidden, and
it’s set to false if UI can be shown
$wizarddata$
Within the VSContent XML file, you can create a
tag named <WizardData> under the document
node. Any data within this tag is passed along
through this value.
$rootname$
Form1.vb
This is the complete file name entered in the Add
New Item dialog box.
$projectname$
My Project1
The name of the project.
$safeprojectname$
My_Project1
The name of the project, but modified in a way
that allows you to use the name as an identifier in
your source code.
$installpath$
C:\Program Files\
Microsoft Visual Studio
8\Common7\IDE
The directory in which Visual Studio is installed.
$exclusiveproject$
true/false
If this value is false, the project is being added to
an existing project; otherwise, the project is being
added to a new solution.
$destinationdirectory$
C:\Documents and
Settings\craigs\My
Documents\Visual
Studio 2005\Projects\
MyProjectName
The directory into which the new project is being
created.
Note Not only can you create your own templates by hand, but you can also modify templates generated with the Export Template wizard. Simply export a template, open the .zip
file containing the template, and then modify the files to your liking. When you are done,
add the modified files to the .zip file.
The VSTemplate Schema
"GUFSZPVIBWFNPEJ¾FEZPVSQSPKFDUPSQSPKFDUJUFNUPDPOUBJOUIFSFQMBDFNFOUUPLFOTZPVOPX
OFFEUPDSFBUFBO9.-¾MFUIBUEFTDSJCFTUPUIFUFNQMBUFXJ[BSEIPXUIFQSPKFDUPSQSPKFDUJUFN
TIPVMECFSFDSFBUFE5IJT¾MFXIJDIIBTUIFFYUFOTJPOWTUFNQMBUFCFHJOTXJUIB745FNQMBUF
UBHBOEIBTUISFF9.-BUUSJCVUFT5IF8GTUKQPBUUSJCVUFTQFDJ¾FTUIFWFSTJPOPGUIFXJ[BSEUIBU
UIF9.-¾MFJTEFTJHOFEUPXPSLXJUI'PS7JTVBM4UVEJPUIFWFSTJPOOVNCFSJT5IF
TFDPOEBUUSJCVUF6[RGTQFDJ¾FTUIFUZQFPGJUFNTUIBUDBOCFHFOFSBUFEXJUIUIFWTDPOUFOU¾MF
$VSSFOUMZUISFFUZQFTPGJUFNTBSFTVQQPSUFE2TQLGEV+VGOBOE2TQLGEV)TQWR*GZPVBSFDSFBUJOH
82
Working with Microsoft Visual Studio 2005
BOFXQSPKFDUUFNQMBUFUIFWBMVFJT2TQLGEVJGZPVBSFDSFBUJOHBOFXQSPKFDUJUFNUIFWBMVFJT
+VGO5IFUFNQMBUFXJ[BSEBMTPTVQQPSUTDSFBUJOHNVMUJQMFQSPKFDUTBUBUJNFXJUIJOUIFTPMVUJPO
4VQQPTFZPVOFFEUPDSFBUFB8FC4FSWJDFBQQMJDBUJPOBOEBDPOTPMFBQQMJDBUJPOUIBUDPOTVNFT
UIBU8FC4FSWJDF8JUIBUFNQMBUFUZQFPG2TQLGEV)TQWRZPVDPVMEDSFBUFCPUIPGUIFTFQSPKFDUTBU
PODFSBUIFSUIBODSFBUFUIFNTFQBSBUFMZ'JOBMMZUIFZONPUBUUSJCVUFMJTUTUIF9.-4DIFNBPGUIF
¾MF5IFGPMMPXJOH9.-JTUIFNPTUCBTJDPGWTUFNQMBUF¾MFT
<VSTemplate Version="2.0.0" Type="Project" xmlns=
"http://schemas.microsoft.com/developer/vstemplate/2005">
</VSTemplate>
"WTUFNQMBUF¾MFIBTUXPNBJOTFDUJPOT5IF6GORNCVG&CVCTFDUJPOEFTDSJCFTUIFWJTVBMSFQSF
TFOUBUJPOPGUIFUFNQMBUFJOUIF6*5IFTFDPOETFDUJPO6GORNCVG%QPVGPVEFUBJMTFBDI¾MFUIBUJT
QBSUPGZPVSUFNQMBUFBOEIPXUIPTF¾MFTTIPVMECFSFDSFBUFEJOUIFUBSHFUTPMVUJPOPSQSPKFDU
The TemplateData Section
5IF6GORNCVG&CVCTFDUJPOJTXJUIJOUIF745FNQMBUFTFDUJPOPGUIFWTDPOUFOU¾MF5IJT
TFDUJPOEFUFSNJOFTUIFBQQFBSBODFPGUFNQMBUFTXJUIJOUIF/FX1SPKFDUBOE"EE/FX*UFN
EJBMPHCPYFT"OFYBNQMFPGUIJTTFDUJPOMPPLTMJLFUIJT9.-GSBHNFOUXIJDIEF¾OFTB
DVTUPN$DMBTTMJCSBSZUFNQMBUF
<TemplateData>
<Name>My Class Library</Name>
<Description>A project for creating a C# class library (.dll)</Description>
<Icon>AnIcon.ico</Icon>
<ProjectType>CSharp</ProjectType>
<SortOrder>20</SortOrder>
<DefaultName>ClassLibrary</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
</TemplateData>
5IFUBHTPGUIJT9.-IBWFUIFGPMMPXJOHNFBOJOHT
N
/BNF 5IJTJTUIFOBNFPGUIFJUFNTIPXOVOEFSOFBUIUIFJDPOXJUIJOUIF/FX
1SPKFDUPS"EE/FX*UFNEJBMPHCPYFT
N
%FTDSJQUJPO 5IJTJTUIFUFYUTIPXOXIFOUIFVTFSTFMFDUTUIFJDPOJOUIF/FX1SPKFDU
PS"EE/FX*UFNEJBMPHCPYFTHJWJOHUIFVTFSNPSFJOGPSNBUJPOBCPVUXIBUUZQFPG
QSPKFDUXJMMCFDSFBUFEXIFOUIFUFNQMBUFJTQSPDFTTFE
N
*DPO "QBUISFMBUJWFUPXIFSFUIFWTUFNQMBUF¾MFJTMPDBUFEUPUIFJDPOUPEJTQMBZ
XJUIJOUIF/FX1SPKFDUPS"EE/FX*UFNEJBMPHCPYFT
N
1SPKFDU5ZQF $BOFJUIFSCF$4IBSQ+4IBSQ7JTVBM#BTJDPS8FC5IJTDPOUSPMTXIJDI
OPEFPGUIFUSFFPOUIFMFGUTJEFPGUIF/FX1SPKFDUEJBMPHCPYUIFJUFNXJMMBQQFBSVOEFS
PSJGUIFUFNQMBUFJTBOJUFNUFNQMBUFJUTQFDJ¾FTUIFQSPKFDUUIBUUIFJUFNDBOCFBEEFE
UP
Chapter 4:
Community Content and VSTemplates
83
N
4PSU0SEFS 5IJTJTUIFQSJPSJUZPGUIFJUFNXJUIJOU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PVBSFDSFBUJOHBQSPKFDUBQSPKFDUJUFNPSBHSPVQQSPKFDU"O
FYBNQMF6GORNCVG%QPVGPVTFDUJPOGPSDSFBUJOHOFXQSPKFDUTUBLFOGSPNUIF$DMBTTMJCSBSZ
UFNQMBUFMPPLTMJLFUIJT
<TemplateContent>
<Project File="ClassLibrary.csproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="true"
TargetFileName=
"Properties\AssemblyInfo.cs">
AssemblyInfo.cs</ProjectItem>
<ProjectItem ReplaceParameters="true"
OpenInEditor="true">
Class1.cs</ProjectItem>
</Project>
</TemplateContent>
)FSFBQSPKFDU¾MFXJUIUIFOBNF$MBTT-JCSBSZDTQSPKJTFYQFDUFEUPCFJOUIFTBNF
GPMEFSBTUIFWTUFNQMBUF¾MF5IJTQSPKFDU¾MFBMPOHXJUIUXP¾MFT"TTFNCMZ*OGPDT
BOE$MBTTDTBSFDPQJFEJOUPUIFUFNQPSBSZGPMEFS$MBTTDTXJMMCFDPQJFEJOUPUIF
TBNFGPMEFSBT$MBTT-JCSBSZDTQSPKCVUGPS"TTFNCMZ*OGPDTUXP¾MFOBNFTBSFHJWFO
"TTFNCMZ*OGPDTBOE1SPQFSUJFT="TTFNCMZ*OGPDT*GPOMZPOF¾MFOBNFJTHJWFOCFDBVTF
UIF6CTIGV(KNG0COGBUUSJCVUFJTOPUTVQQMJFE
UIFTPVSDF¾MFJTSFBEGSPNUIFTBNF
GPMEFSDPOUBJOJOHUIFWTUFNQMBUF¾MFBOEUIF¾MFJTDPQJFEJOUPUIFQSPKFDUEFTUJOBUJPO
GPMEFS#FDBVTFJOUIJTFYBNQMFUXP¾MFOBNFTBSFHJWFOJOUIF1SPKFDU*UFNUBHB
GPMEFSOBNFE1SPQFSUJFTXJMMCFDSFBUFEBOEUIF"TTFNCMZ*OGPDT¾MFXJMMCFDPQJFE
JOUPUIBUGPMEFS5IJTBMMPXTZPVUP¾OFUVOFFYBDUMZIPXUIFEJSFDUPSZTUSVDUVSFGPSB
QSPKFDUPSQSPKFDUJUFNTBSFSFDSFBUFEPOEJTL:PVDPVMEBMTPHJWFBSFMBUJWFQBUIGPS
84
Working with Microsoft Visual Studio 2005
UIFUFYUPGUIF1SPKFDU*UFNUBHTVDIBT1SPQFSUJFT="TTFNCMZ*OGPDT8IFOUIFUFNQMBUF
XJ[BSEQSPDFTTFTUIBUJUFNUIF"TTFNCMZ*OGPDT¾MFJTDPQJFEGSPNBTVCGPMEFSOBNFE
1SPQFSUJFTJOUPUIFGPMEFSOBNFE1SPQFSUJFTXJUIJOUIFUFNQPSBSZGPMEFS5IVTZPVDBO
XSJUFUIFMJOFPG9.-UPDPQZUIF"TTFNCMZ*OGPDTJOUPB1SPQFSUJFTGPMEFSVTJOHUIFTF
UISFFTUZMFT
<ProjectItem ReplaceParameters="true"
TargetFileName="Properties\AssemblyInfo.cs">
AssemblyInfo.cs
</ProjectItem>
<ProjectItem ReplaceParameters="true">
Properties\AssemblyInfo.cs
</ProjectItem>
<ProjectItem ReplaceParameters="true"
TargetFileName="Properties\AssemblyInfo.cs">
Properties\AssemblyInfo.cs
</ProjectItem>
5IF¾STUMJOFSFRVJSFTUIF"TTFNCMZJOGPDT¾MFUPCFJOUIFTBNFGPMEFSBTUIFWTUFNQMBUF
¾MFXIFSFBTGPSUIFTFDPOEBOEUIJSEQPTTJCJMJUJFTUIFUFNQMBUFXJ[BSEFYQFDUTUIF¾MF
UPCFJOBGPMEFSOBNFE1SPQFSUJFT"MMUISFFTUZMFTXJMMDPQZUIF¾MFJOUPBGPMEFSOBNFE
1SPQFSUJFT
Note It is important to distinguish a project file from the TemplateContent section of a
.vscontent file for creating projects. A project file, such as a file ending in the extensions
.csproj, .vbproj, or vjsproj, defines the programming language–specific layout of a project on
disk and how that project is loaded into Visual Studio. A .vstemplate file is a programming
language–agnostic way of defining a project or project item. It might seem unnecessary to
have both a project and a template file because they both contain similar data, but a separate .vstemplate file has its purpose. A group project template and project items do not have
an associated project file, so the .vstemplate file is needed in that case. In addition, because
the .vstemplate file is independent of language, you can use one file format to create projects for multiple languages, even languages not created by Microsoft.
*OBMMUISFF¾MFTGSPNUIFFYBNQMFWTUFNQMBUF¾MFUIF4GRNCEG2CTCOGVGTUBUUSJCVUFJT
TFUUPUSVFNFBOJOHUIBU¾MFTXJMMCFPQFOFEJOUIFUFNQPSBSZGPMEFSBOEFYBNJOFEGPS
SFQMBDFNFOUQBSBNFUFSTBOEJGBOZBSFGPVOEUIFZXJMMCFSFQMBDFE*GZPVEPOPUIBWF
BOZSFQMBDFNFOUTUPNBLFJOB¾MFZPVDBOTFUUIJTWBMVFUPGBMTFPSOPUFWFOHJWFUIF
BUUSJCVUF±JUEFGBVMUTUPGBMTF
BOEJODSFBTFQFSGPSNBODFCZBMJUUMFCJUCFDBVTFJGUIJTWBMVF
JTGBMTFUIFTUFQTUPPQFOUIF¾MFBOETDBOGPSSFQMBDFNFOUUPLFOTUIBUXJMMOPUFYJTUDBO
CFCZQBTTFE
5IJTTBNQMFBMTPNBLFTVTFPGUIF1RGP+P'FKVQTBUUSJCVUF*GUIJTBUUSJCVUFJTTFUUPUSVF
XIFOUIFXJ[BSE¾OJTIFTCVJMEJOHUIFQSPKFDUPSQSPKFDUJUFNJUXJMMPQFOBMMUIF¾MFTXJUI
UIJTBUUSJCVUF8JUIUIF1RGP+P'FKVQTBUUSJCVUFZPVDBOBMTPPQUJPOBMMZVTFUIF1RGP1TFGT
BUUSJCVUF5IJTBUUSJCVUFUBLFTBOJOUFHFSWBMVFBOEMFUTZPVDPOUSPMUIFPSEFSJOXIJDI
EPDVNFOUTBSFPQFOFE*GZPVXFSFUPVTFUIF1RGP+P'FKVQT²VTWG³BUUSJCVUFPONVMUJQMF¾MFT
Chapter 4:
Community Content and VSTemplates
85
UIFOBMMUIF¾MFTXJMMCFPQFOFECVUPOMZPOFXJMMCFUIFBDUJWFXJOEPXJOUIF7JTVBM4UVEJP
6*XIFOUIFXJ[BSEJTDPNQMFUF*GZPVVTFUIF1RGP1TFGTBUUSJCVUFUIF¾MFXJUIUIFIJHIFTU
WBMVFJTPQFOFEMBTUBOEXJMMUIFOCFBDUJWFJOUIF6*
"OFYBNQMF5FNQMBUF$POUFOUTFDUJPOGPSDSFBUJOHOFXQSPKFDUJUFNTXIJDIXBTUBLFO
GSPNUIF$DMBTTUFNQMBUFMPPLTMJLFUIJT
<TemplateContent>
<References>
<Reference>
<Assembly>
System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
F</Assembly>
</Reference>
</References>
<ProjectItem ReplaceParameters="true">Class.cs</ProjectItem>
</TemplateContent>
"OFXQSPKFDUJUFNUFNQMBUFJTTJNJMBSUPOFXQSPKFDUUFNQMBUFTFYDFQUUIBUBQSPKFDUJUFN
UFNQMBUFEPFTOPUIBWFBOBTTPDJBUFEQSPKFDU¾MFTPUIF1SPKFDUUBHJTSFNPWFEBOEUIF
1SPKFDU*UFNUBHCFDPNFTBDIJMEPGUIF5FNQMBUF$POUFOUUBH"MTPB3FGFSFODFT
3FGFSFODFBOE"TTFNCMZUBHIBTCFFOBEEFEUPUIF9.-"GUFSUIF$MBTTDT¾MF
IBTCFFOBEEFEUPUIFQSPKFDUUIFUFNQMBUFXJ[BSEXJMMVTFUIF9.-91BUIRVFSZGPS
3FGFSFODFT3FGFSFODF"TTFNCMZUBHTBOEJGGPVOEUIFXJ[BSEXJMMBEEUIFBTTFNCMZOBNFE
UPUIFMJTUPGSFGFSFODFTGPSUIFQSPKFDUUIFJUFNJTCFJOHBEEFEUP)FSFUIF4ZTUFNBTTFNCMZ
JTSFGFSFODFECZJUTGVMMTUSPOHOBNF5IF3FGFSFODFTUBHJTBWBJMBCMFPOMZGPSOFXJUFN
UFNQMBUFTBOEOPUJOOFXQSPKFDUUFNQMBUFT"OZSFGFSFODFTGPSQSPKFDUUFNQMBUFTBSFBMSFBEZ
TQFDJ¾FEXJUIJOUIFQSPKFDU¾MFTPSFGFSFODFTGPSOFXQSPKFDUTBSFOPUOFDFTTBSZ:PVDBO
VTFUIF3FGFSFODFTUBHUPQSFQBSFBQSPKFDUTPUIBUJUXJMMCVJMEDPSSFDUMZXJUIPVUUIFVTFS
OFFEJOHUPNBOVBMMZBEESFGFSFODFT
5IFGJOBMUZQFPGUFNQMBUFJTUIFQSPKFDUHSPVQ*GZPVBSFUSZJOHUPQBDLBHFBHSPVQ
QSPKFDUGPSJOTUBMMBUJPOJOB74*GJMFUIFOUIF5FNQMBUF5ZQFBUUSJCVUFTIPVMECFTFU
UP1SPKFDU(SPVQ3BUIFSUIBOVTJOHUIF1SPKFDUUBHBTZPVEPGPSOFXQSPKFDUTUIF
1SPKFDU$PMMFDUJPOUBHJTVTFEXJUIJOUIF5FNQMBUF$POUFOUUBH8JUIJOUIJTUBH
ZPVDBODPNCJOFUIF4PMVUJPO'PMEFSUBHUPDSFBUFBOFXTPMVUJPOGPMEFSXJUIJO
UIFTPMVUJPOBOEB1SPKFDU5FNQMBUF-JOLUBHUPSFGFSFODFBOPUIFSWTUFNQMBUFGJMF
*GUIF1SPKFDU5FNQMBUF-JOLUBHJTHJWFOXJUIJOB4PMVUJPO'PMEFSUBHUIFOFX
QSPKFDUXJMMCFDSFBUFEXJUIJOUIFOFXTPMVUJPOGPMEFS"OZ1SPKFDU5FNQMBUF-JOLPS
4PMVUJPO'PMEFSUBHTUIBUBQQFBSEJSFDUMZVOEFSUIF1SPKFDU$PMMFDUJPOUBHXJMMDSFBUF
UIFQSPKFDUPSTPMVUJPOGPMEFSJOUIFMPDBUJPOXIFSFUIFQSPKFDUHSPVQJTBEEFEUPUIF
TPMVUJPO
<TemplateContent>
<ProjectCollection>
<SolutionFolder Name="Folder1">
<ProjectTemplateLink ProjectName="ConsoleApp1">
86
Working with Microsoft Visual Studio 2005
ConsoleApplication\csconsoleapplication.vstemplate
</ProjectTemplateLink>
</SolutionFolder>
<ProjectTemplateLink ProjectName="ConsoleApp2">
ConsoleApplication\csconsoleapplication.vstemplate
</ProjectTemplateLink>
</ProjectCollection>
</TemplateContent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
Looking Ahead
/PXUIBUXFIBWFTIPXOZPVIPXUPVTFTPNFPGUIFGFBUVSFTPG7JTVBM4UVEJPXFDBOCFHJO
FYQMPSJOHIPXZPVDBODVTUPNJ[F7JTVBM4UVEJPQSPHSBNNBUJDBMMZXJUINBDSPT
Chapter 5
Using Visual Studio Macros
In this chapter:
Macros: The Duct Tape of Visual Studio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Working with Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Sharing Macros with Others . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Looking Ahead. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
.BDSPTBSFBVUPNBUJPOTDSJQUTUIBUZPVDBOXSJUFUPUBLFBEWBOUBHFPGGVODUJPOBMJUZCVJMUJOUP
UIFJOUFHSBUFEEFWFMPQNFOUFOWJSPONFOU*%&
*OUIJTDIBQUFSXFµMMJOUSPEVDFZPVUPBGFX
PGUIFEJGGFSFOUNBDSPUPPMTJO.JDSPTPGUˆ7JTVBM4UVEJPˆ5IFNBDSPTFOHJOFJO7JTVBM4UVEJP
MFUTZPVSFDPSENBDSPTUPQMBZCBDLMBUFS:PVDBOBDDFTTNBDSPTGPSQMBZCBDLUISPVHIB
XJOEPXJOUIF*%&DBMMFEUIF.BDSP&YQMPSFS:PVDBOFEJUBOEEFCVHZPVSNBDSPTJOB
TQFDJBM*%&DBMMFEUIF.BDSPT*%&8FµMMTIPXZPVIPXUPSFDPSEBOEQMBZNBDSPTJO7JTVBM
4UVEJPBOEIPXUPXSJUFBOEFEJUNBDSPQSPKFDUTJOUIF.BDSPT*%&'JOBMMZXFµMMBMTPTIPX
ZPVIPXZPVDBOTIBSFZPVSNBDSPTXJUIPUIFST
Macros: The Duct Tape of Visual Studio
5IFNBDSPTGBDJMJUZPG7JTVBM4UVEJPVTFT.J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PXOBVUPNBUJPOQSPKFDU
91
92
Working with Microsoft Visual Studio 2005
7JTVBM4UVEJPNBDSPTBSFBDDFTTFEJOUIF*%&KVTUMJLFBOZPUIFSOBNFEDPNNBOE:PVDBO
FOUFSUIFOBNFPGUIFNBDSPJOUIF$PNNBOE8JOEPX$USM"MU"
ZPVDBOBEEUIFNBDSP
UPBUPPMCBSPSBNFOVZPVDBOBTTJHOUIFNBDSPBLFZTUSPLFTIPSUDVUZPVDBOSVOUIF
NBDSPCZEPVCMFDMJDLJOHJUJO.BDSP&YQMPSFSBOEZPVDBOSVOUIFNBDSPEJSFDUMZGSPNUIF
.BDSPT*%&
Note When you run a macro by double-clicking it in the Macro Explorer window, the focus
returns to the last active window. As a result, you can set the active document, open Macro
Explorer, and then double-click the macro to have it affect the last active document.
8FDPOTJEFSNBDSPTUIF²EVDUUBQF³PG7JTVBM4UVEJP±JOUIFCFTUTFOTFPGUIFUFSN%VDU
UBQFJTNBEFPGBOFYUSFNFMZTUSPOHNBUFSJBMBOEDBOIFMQZPVBDDPNQMJTIUBTLTRVJDLMZBOE
FBTJMZ8FXPVMEEFTDSJCFNBDSPTJOUIFTBNFXBZ±UIFZµSFFYUSFNFMZQPXFSGVMUPPMTJOUIF
*%&UIBUZPVEPOµUIBWFUPTQFOEBUPOPGUJNFUIJOLJOHBCPVU:PVDBODSFBUFZPVSNBDSPUP
QFSGPSNZPVSUBTLBOEUIFOUVDLJUBXBZ*GUIFNBDSPJTTVG¾DJFOUMZJNQPSUBOUBOEQPXFSGVM
ZPVDBOMBUFSUVSOJUJOUPBGVMMCMPXOBEEJOBOEUIFOQPMJTIUIBUDPEFBTNVDIBTZPVXBOU
Recording Visual Studio Macros
5PSFDPSEB7JTVBM4UVEJPNBDSP¾STUQSFTTUIF$USM4IJGU3LFZCPBSETIPSUDVU5IJT
DPNCJOBUJPOCSJOHTVQUIF3FDPSEFSUPPMCBSBOEDSFBUFTBNBDSPTNPEVMFOBNFE
4GEQTFKPI/QFWNGJG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OHTFTTJPO
Option Strict Off
Option Explicit Off
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Public Module RecordingModule
Sub TemporaryMacro()
DTE.ExecuteCommand("Edit.Find")
DTE.Windows.Item("ScreenSaverForm.vb").Activate()
DTE.Find.FindWhat = "ItemListView"
DTE.Find.Target = _
vsFindTarget.vsFindTargetCurrentDocument
DTE.Find.MatchCase = False
DTE.Find.MatchWholeWord = False
DTE.Find.Backwards = False
DTE.Find.MatchInHiddenText = True
DTE.Find.PatternSyntax = _
vsFindPatternSyntax.vsFindPatternSyntaxLiteral
DTE.Find.Action = vsFindAction.vsFindActionFind
If (DTE.Find.Execute() = _
vsFindResult.vsFindResultNotFound) Then
Throw New System.Exception("vsFindResultNotFound")
End If
End Sub
End Module
5PQMBZCBDLUIJTNBDSPQSFTT$USM4IJGU1XIJDIJTTJNQMZBTIPSUDVUUPUIF.BDSPT
.BDSPT3FDPSEJOH.PEVMF5FNQPSBSZ.BDSPDPNNBOE:PVTIPVMETFFUIF'JOEEJBMPHCPY
94
Working with Microsoft Visual Studio 2005
PQFOXJUIUIF¾STUJOTUBODFPGUIFXPSEZPVµSFTFBSDIJOHGPSTFMFDUFE*OPVSDBTFUIJTJTUIF
¾STUJOTUBODFPG+VGO.KUV8KGYJOB¾MFOBNFE$POOFDUDQQ
5BLFBMPPLBUUIFMJOF&6'9KPFQYU+VGO
Þ5ETGGP5CXGT(QTOXDÞ#EVKXCVG
*G
4DSFFO4BWFS'PSNWCJTOµUBMSFBEZPQFOUIJTMJOFXJMMCSJOHJUJOUPGPDVTJOUIF*%&TPUIJT
NBDSPXPOµUCFWFSZVTFGVMJGZPVXBOUUPTBWFJUGPSVTFXJUIBOVNCFSPGEJGGFSFOU¾MFTPS
QSPKFDUT$PNNFOUJOHPVUPSSFNPWJOHUIJTMJOFGSPNUIFDPEFXJMMDBVTFUIFNBDSPUPXPSL
XJUIUIFDVSSFOUMZBDUJWFEPDVNFOU
5PTBWFUIFSFDPSEFENBDSPZPVDBOFJUIFSSFOBNF6GORQTCT[/CETQUPTPNFUIJOHFMTFJO
.BDSP&YQMPSFSPSZPVDBODPQZBOEQBTUFUIFSFDPSEFEDPEFJOUPBOPUIFSNBDSPNPEVMFPS
NFUIPE
Macro Commands
.BDSP&YQMPSFSMFUTZPVNBOBHFZPVSNBDSPTGSPNJOTJEFUIF7JTVBM4UVEJP*%&:PVDBO
BDDFTTUIFDPNNBOETSFMBUFEUPNBDSPTJOUIF*%&GSPNUIF.BDSPTTVCNFOVPGUIF5PPMT
NFOVPSUISPVHIUIFTIPSUDVUNFOVTXJUIJO.BDSP&YQMPSFS
.BDSPTBSFEJWJEFEJOUPQSPKFDUTDPOUBJOJOHNPEVMFTXIJDIJOUVSODPOUBJONFUIPET
1SPKFDUTBSFSFQSFTFOUFEIJFSBSDIJDBMMZJO.BDSP&YQMPSFSCFMPXUIF.BDSPJDPO3JHIU
DMJDLJOHUIF.BDSPJDPOCSJOHTVQUIFTIPSUDVUNFOVDPOUBJOJOHDPNNBOETGPSDSFBUJOHBOE
MPBEJOHNBDSPQSPKFDUT:PVDBOBDDFTTUIFTBNFGVODUJPOBMJUZWJBOBNFEDPNNBOETJOUIF
$PNNBOE8JOEPX5BCMFMJTUTUIFNBDSPDPNNBOETSFMBUFEUPNBDSPQSPKFDUT
Table 5-1
Macro Project Commands
Command
Description
Tools.LoadMacroProject
Brings up the Add Macro Project dialog box, in which you
can select a macro project file.
Tools.NewMacroProject
Brings up the New Macro Project dialog box, in which you
can save your macros into specific projects.
Tools.MacrosIDE
Brings up the Macros IDE. This command is mapped to
Alt+F11.
:PVDBOPQFO.BDSP&YQMPSFSCZQSFTTJOH"MU'.PTUDPNNBOETBWBJMBCMFGSPNUIF
TIPSUDVUNFOVTJO.BDSP&YQMPSFSBSFBMTPBWBJMBCMFGSPNUIF$PNNBOE8JOEPXCFDBVTF
UIFJUFNTJO.BDSP&YQMPSFSMPTFGPDVTXIFOZPVDIBOHFUPUIF$PNNBOE8JOEPX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1VCMJD"TTFNCMJFTGPMEFS:PVDBOUIFOBEEZPVS
PXOSFGFSFODFUPUIFBTTFNCMZGSPNUIF"EE3FGFSFODFEJBMPHCPY6TJOHBTTFNCMJFTZPVDBO
XSJUFZPVSNBDSPGVODUJPOBMJUZJOBOZMBOHVBHFZPVXBOUBOEUIFOBDDFTTUIBUGVODUJPOBMJUZ
GSPNBGBJSMZTJNQMFNBDSP:PVDBOBMTPXSJUFBTTFNCMJFTUIBUDBMMUPVONBOBHFEDPEFBOE
BTTFNCMJFTUIBUBDUBT$0.XSBQQFSTUPBDDFTT$0.GVODUJPOBMJUZGSPNXJUIJOZPVSNBDSPT
-FUµTHPPWFSBGFXFYBNQMFTCVJMUGSPNBOFXQSPKFDU
Chapter 5:
Using Visual Studio Macros
97
Figure 5-5
Add Reference dialog box
A Simple Macro
5IF'JMF/FX'JMFDPNNBOEJO7JTVBM4UVEJPPQFOTBEJBMPHCPYUIBUBMMPXTZPVUPDIPPTFUIF
UZQFPG¾MFUIBUZPVXBOUUPDSFBUF4PNFQSPHSBNNFSTXPVMESBUIFSIBWFBDPNNBOEMJLF
UIJTHFOFSBUFBOFX¾MFUIBOQSFTFOUBEJBMPHCPY5IFTPMVUJPOJTTJNQMFZPVKVTUDSFBUFB
NBDSPUIBUEPFTFYBDUMZXIBUZPVXBOUBOEUIFOBTTJHOUIBUNBDSPBOBMJBTJOUIF$PNNBOE
8JOEPX5IFGPMMPXJOHDPEFJTBMMZPVSFBMMZOFFEUPDSFBUFBOFXUFYU¾MFJOUIF*%&
Imports EnvDTE
Imports System.Diagnostics
Public Module NewFile
Sub NewTextFile()
DTE.ItemOperations.NewFile("General\Text File")
End Sub
End Module
"TZPVDBOTFFUIJTNBDSPIBTCFFODSFBUFEJOBNPEVMFOBNFE0GY(KNG*UDPOTJTUTPGB
TJOHMFNFUIPE0GY6GZV(KNG5IFTJOHMFMJOFPGDPEFJOUIJTNBDSPTJNQMZDSFBUFTBOFX¾MF
PGUIFUZQF5FYU'J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BUQPJOUT
UPUIFFOEPGUIFEPDVNFOU"OE¾OBMMZUIF5GNGEVKQPQSPQFSUZSFUVSOTB6GZV5GNGEVKQPPCKFDU
UIBUPGGFSTBOVNCFSPGQSPQFSUJFTBOENFUIPETZPVDBOVTFPOTFMFDUFEUFYU
5IF6GZV2QKPVPCKFDUQSPWJEFTMPDBUJPOJOGPSNBUJPOGPSUIFFEJUJOHGVODUJPOBMJUZJOTJEFB
EPDVNFOU:PVDSFBUFB6GZV2QKPVJOBEPDVNFOUXIFOFWFSZPVXBOUUPJOTFSUPSNBOJQVMBUF
UFYUJOUIFEPDVNFOUPSXIFOZPVXBOUUPHFUTPNFJOGPSNBUJPOBCPVUBQBSUJDVMBS
EPDVNFOU6GZV2QKPVPCKFDUTBSFOµUEFQFOEFOUPOUFYUTFMFDUJPOBOEZPVDBOVTFNVMUJQMF
6GZV2QKPVPCKFDUTJOBTJOHMFEPDVNFOU
-FUµTMPPLBUBDPVQMFPGFYBNQMFTUIBUVTFUIFPCKFDUTXFµWFNFOUJPOFE:PVTIPVME
CFDPNFGBNJMJBSXJUIUIJTDPEFCFDBVTFNVDIPGUIFNBDSPBVUPNBUJPODPEFZPVµMMXSJUF
XJMMEFQFOEPOJU
'JSTUMFUµTHFUUIF#EVKXG&QEWOGPVDSFBUFBDPVQMFPG'FKV2QKPVPCKFDUTBOEUIFOBEETPNF
UFYUUPUIF#EVKXG&QEWOGPVCZVTJOHUIBUJOGPSNBUJPO
Sub CommentWholeDoc()
Dim td As TextDocument = ActiveDocument.Object
Dim sp As TextPoint
Dim ep As TextPoint
sp = td.StartPoint.CreateEditPoint()
ep = td.EndPoint.CreateEditPoint()
sp.Insert("/* ")
ep.Insert(" */")
End Sub
3VOOJOHUIJTTBNQMFPOB.JDSPTPGU7JTVBM$ˆPSB.JDSPTPGU7JTVBM$ˆDPEFEPDVNFOU
XJMMDPNNFOUPVUUIFFOUJSFEPDVNFOU5IFNBDSPJTOµUWFSZQSBDUJDBMCVUJUEPFTTIPXZPV
IPXUPQVUUIPTFQBSUTUPHFUIFS:PVDBOVTF*OUFMMJ4FOTFUPNBLFZPVSXBZUISPVHIUIF
PCKFDUTDSFBUFETPUIBUZPVDBOFYQFSJNFOUXJUITPNFPGUIFPUIFSGVODUJPOBMJUZ
-FUµTUBLFBMPPLBUBTFDPOENPSFVTFGVMFYBNQMFUIBUJOTFSUTUFYUJOUPBEPDVNFOUCBTFE
POBTFMFDUJPO5IFGPMMPXJOHFYBNQMFXSBQTTFMFDUFEUFYUXJUIXIBUFWFSUFYUXFXBOUJOB
EPDVNFOU)FSFXFµMMEFDMBSFVUBTB6GZV5GNGEVKQPPCKFDUBOEBTTJHOJUUIFDVSSFOUTFMFDUJPO
VTJOH&6'#EVKXG&QEWOGPV5GNGEVKQP
Sub HTMLComment()
Dim ts As TextSelection = DTE.ActiveDocument.Selection
Dim ep As TextPoint
ep = ts.BottomPoint.CreateEditPoint()
ts.Insert("<!-- ", vsInsertFlags.vsInsertFlagsInsertAtStart)
ep.Insert(" -->")
ts.Collapse()
End Sub
100
Working with Microsoft Visual Studio 2005
5IJTNBDSPVTFTUIF6GZV5GNGEVKQP+PUGTVNFUIPEUPJOTFSUUFYUBUUIFCFHJOOJOHPGUIF5GNGEVKQP
PCKFDU5IF+PUGTVNFUIPEUBLFTUXPBSHVNFOUT5IF¾STUBSHVNFOUJTUIFTUSJOHUIBUZPV
XBOUUPJOTFSUJOUPUIFTFMFDUJPO5IFTFDPOEBSHVNFOUJTBXU+PUGTV(NCIUDPOTUBOUUIBUEF¾OFT
XIFSFUIFJOTFSUJPOJTUPUBLFQMBDF5IF+PUGTVDBMMJOUIFFYBNQMFVTFTXU+PUGTV(NCIU#V5VCTV
5PDMPTFUIFUBHXFVTFBO+PUGTVXJUIB6GZV2QKPVCFDBVTFUIFJOTFSUJPOQPJOUDIBOHFTBGUFS
UIF¾STUJOTFSU*GXFKVTUXBOUFEUPBEEUFYUUPUIFFOEPGBTFMFDUJPOXFDPVMEKVTUVTF
XU+PUGTV(NCIU+PUGTV#V'PF5BCMFMJTUTUIFTFDPOTUBOUT
Table 5-2
vsInsertFlags Constants
Constant
Description
vsInsertFlagsCollapseToStart
Collapses the insertion point from the end of the
selection to the current TextPoint
vsInsertFlagsCollapseToEnd
Collapses the insertion point from the beginning of the
selection to the current TextPoint
vsInsertFlagsContainNewText
Replaces the current selection
vsInsertFlagsInsertAtStart
Inserts the text before the start point of the selection
vsInsertFlagsInsertAtEnd
Inserts text just after the end point of the selection
8JUIB5GNGEVKQPB6GZV2QKPVBOEUIFNFUIPETBWBJMBCMFUISPVHIUIF&6'ZPVTIPVMEIBWFB
HPPECBTJTGPSUIFUZQFTPGPQFSBUJPOTZPVDBOQFSGPSNPOTPVSDFDPEFCZVTJOHNBDSPT
Moving Windows
8JOEPXTJO7JTVBM4UVEJPBSFDPOUSPMMFEUISPVHIUIF9KPFQYPCKFDUXIJDIJTQBSUPGUIF
&6'9KPFQYUDPMMFDUJPO5IF9KPFQYPCKFDUQSPWJEFTGVODUJPOBMJUZCBTFEPOUIFXJOEPX
UZQF4QFDJ¾DBMMZUIF%QOOCPF9KPFQY1WVRWV9KPFQY6CUM.KUV6GZV9KPFQYBOE6QQN$QZ
EFSJWFGSPNUIF9KPFQYPCKFDU
0GUIFXJOEPXPCKFDUT1WVRWV9KPFQYJTBNPOHUIFNPTUQSBDUJDBMGPSNBDSPXSJUJOH
:PVDBOVTFJUUPEJTQMBZBOEIPMENFTTBHFTJONVDIUIFTBNFXBZZPVXPVMEVTFRTKPVH
PS%QPUQNG9TKVGJOBDPOTPMFBQQMJDBUJPOPSJOUIFTBNFXBZUIBUZPVVTF/UI$QZPS
/GUUCIG$QZ5JQYJOB.JDSPTPGU8JOEPXTˆ°CBTFEBQQMJDBUJPO
5PVTFUIF1WVRWV9KPFQYPCKFDUUPEJTQMBZNFTTBHFTZPVNVTUDSFBUFBOFXNFUIPEUIBU
UBLFTBTUSJOHBSHVNFOU:PVDBOUIFODBMMUIFNFUIPEXJUIUIFBSHVNFOUJOUIFTBNF
XBZZPVVTFUIF/UI$QZNFUIPEUPEJTQMBZBNFTTBHF5IFGPMMPXJOHFYBNQMFJTBNFUIPE
OBNFE/UI9KP*UUBLFTPOMZBTUSJOHBTBOBSHVNFOU:PVDBOVTFUIJTNFUIPEJOQMBDFPG
/UI$QZXIFOZPVXBOUUPTFFBCJUPGUFYUJOGPSNBUJPORVJDLMZ
Chapter 5:
Using Visual Studio Macros
101
Sub MsgWin(ByVal msg As String)
Dim win As Window = DTE.Windows.Item(Constants.vsWindowKindOutput)
Dim cwin As Window =
DTE.Windows.Item(Constants.vsWindowKindCommandWindow)
Dim ow As OutputWindow = win.Object
Dim owp As OutputWindowPane
Dim cwp As CommandWindow = cwin.Object
Dim i As Integer
Dim exists As Boolean = False
' Check to see if we're running in the Command Window. If so,
' we'll send our output there. If not, we'll send it to a Command
' window.
If (DTE.ActiveWindow Is cwin) Then
cwp.OutputString(msg + vbCrLf)
Else
' Determine if the output pane name exists. If it does, we need
' to send our message there, or we end up with multiple windows of
' the same name.
For i = 1 To ow.OutputWindowPanes.Count
If ow.OutputWindowPanes().Item(i).Name() = "MsgWin Output" Then
exists = True
Exit For
End If
Next
' If our output pane exists, we'll use that to output the string,
' otherwise, we'll add it to the list.
If exists Then
owp = ow.OutputWindowPanes().Item(i)
Else
owp = ow.OutputWindowPanes.Add("MsgWin Output")
End If
' Here we set the Output window to visible, activate the pane,
' and send the string to the pane.
win.Visible = True
owp.Activate()
owp.OutputString(msg + vbCrLf)
End If
End Sub
5PVTFUIF/UI9KPNBDSPZPVNVTUDBMMJUGSPNBOPUIFSNFUIPE'PSUIJTFYBNQMFXFµWF
DSFBUFEBNFUIPEUIBUMJTUTBMMUIFDVSSFOUMZPQFOXJOEPXTJOUIF*%&
Sub MsgWinTest()
Dim wins As Windows = DTE.Windows()
Dim i As Integer
For i = 1 To wins.Count
MsgWin(wins.Item(i).Caption.ToString())
Next
End Sub
102
Working with Microsoft Visual Studio 2005
'JHVSFTIPXTXIBUUIF7JTVBM4UVEJP*%&MPPLTMJLFBGUFSJUIBTCFFOJOWPLFEGSPNUIF
/UI9KP6GZVNBDSPJOUIF*%&
Figure 5-6
The MsgBox Output window in the IDE
:PVDBOEPBMPUPGUIJOHTXJUIUIJTCBTJD/UI9KPNBDSPUPJNQSPWFJU*UXPVMECFQSFUUZ
USJWJBMUPPWFSMPBEUIF/UI9KPNFUIPEUPBMMPXGPSTVDIBDUJPOTBTDMFBSJOHUIFPVUQVUQBOF
PSBEEJOHBIFBEJOHUPUIFMJTU'PSFYBNQMFUPDSFBUFBOPWFSMPBEGPSUIF/UI9KPGVODUJPO
UIBUDMFBSTUIFPVUQVUQBOFZPVDBONBLFUIFNFUIPEMPPLTPNFUIJOHMJLFUIJT
Sub MsgWin(ByVal msg As String, ByVal clr As Boolean)
§
' If clr is True then we'll clear the output pane.
If clr = True Then
owp.Clear()
End If
' Here we set the Output window to visible, activate the pane,
' and then send the string to the pane.
win.Visible = True
owp.Activate()
owp.OutputString(msg + vbCrLf)
End If
End Sub
Macro Events
0OFPGUIFNPTUQPXFSGVMGFBUVSFTPGNBDSPTJOUIF*%&JTBOFWFOUNPEFMUIBUMFUTZPV¾SF
NBDSPTCBTFEPOFWFOUTUIBUUBLFQMBDFJOUIF*%&:PVDBOVTFFWFOUTUP¾SFNBDSPTUIBU
DSFBUFMPHTSFTFUUFTUTPSNBOJQVMBUFEJGGFSFOUQBSUTPGUIF*%&JOUIFXBZTXFµWFBMSFBEZ
UBMLFEBCPVUJOUIJTDIBQUFS*OUIJTTIPSUTFDUJPOXFµMMTIPXZPVIPXUPDSFBUFFWFOU
IBOEMFSTGPSEJGGFSFOUFWFOUTJOUIF*%&6TJOHUIJTJOGPSNBUJPOBOEUIFEFUBJMFEJOGPSNBUJPO
BCPVUUIFEJGGFSFOUQBSUTPGUIFBVUPNBUJPO"1*EJTDVTTFEUISPVHIPVUUIFSFTUPGUIFCPPL
ZPVTIPVMEIBWFBHPPEJEFBPGIPXUPUBLFBEWBOUBHFPGFWFOUTJOZPVSPXOQSPKFDUT
5IFFBTJFTUXBZUPHFUUPUIFFWFOUIBOEMFSTGPSBNBDSPTQSPKFDUJTUISPVHIUIF1SPKFDU
&YQMPSFSXJOEPXJOUIF.BDSPT*%&&YQBOEBQSPKFDUBOEZPVµMMTFFBO'PXKTQPOGPV'XGPVU
NPEVMFMJTUFE0QFOUIBU¾MFBOEZPVµMMTFFBCMPDLPGDPEFUIBUµTCFFOHFOFSBUFE
Chapter 5:
Using Visual Studio Macros
103
BVUPNBUJDBMMZCZUIF*%&)FSFµTUIFJNQPSUBOUQBSUPGUIFCMPDLUIFBUUSJCVUFTIBWFCFFO
SFNPWFEUPNBLFUIJT¾UUIFQBHF
Public
Public
Public
Public
Public
Public
Public
Public
Public
Public
Public
Public
WithEvents
WithEvents
WithEvents
WithEvents
WithEvents
WithEvents
WithEvents
WithEvents
WithEvents
WithEvents
WithEvents
WithEvents
DTEEvents As EnvDTE.DTEEvents
DocumentEvents As EnvDTE.DocumentEvents
WindowEvents As EnvDTE.WindowEvents
TaskListEvents As EnvDTE.TaskListEvents
FindEvents As EnvDTE.FindEvents
OutputWindowEvents As EnvDTE.OutputWindowEvents
SelectionEvents As EnvDTE.SelectionEvents
BuildEvents As EnvDTE.BuildEvents
SolutionEvents As EnvDTE.SolutionEvents
SolutionItemsEvents As EnvDTE.ProjectItemsEvents
MiscFilesEvents As EnvDTE.ProjectItemsEvents
DebuggerEvents As EnvDTE.DebuggerEvents
"TZPVDBOTFFGSPNUIJTDPEFUIFSFBSFBMPUPGFWFOUUZQFTZPVDBOUBLFBEWBOUBHFPGJOUIF
*%&*OGBDUZPVDBOVTFBMMUIF%5&FWFOUTFWFOUIPVHIUIFZµSFOPUJODMVEFECZEFGBVMU
:PVDBOBEEUIFTFPUIFSFWFOUTUPUIJTMJTUUPHFUUPUIFFWFOUTUIBUZPVµSFJOUFSFTUFEJO5P
DSFBUFBOFXFWFOUIBOEMFSZPVTFMFDUUIFFWFOUUZQFZPVXBOUUPIBOEMFGSPNUIFDMBTT
OBNFMJTUBUUIFUPQPGUIFDPEFXJOEPX:PVDBOTFFIPXUIJTMPPLTJO'JHVSF
Figure 5-7
Selecting the event type you want to handle from the class name list
104
Working with Microsoft Visual Studio 2005
"GUFSZPVTFMFDUBOFWFOUUZQFUIFNFUIPEOBNFMJTUJOUIFVQQFSSJHIUQPSUJPOPGUIFDPEF
QBOFXJMMMJTUUIFFWFOUTZPVDBOIBOEMFBTTIPXOJO'JHVSF
Figure 5-8
Selecting the event you want to handle from the method name list
4FMFDUUIFFWFOUZPVXBOUGSPNUIFMJTUBOEZPVSFWFOUIBOEMFSXJMMCFHFOFSBUFE
BVUPNBUJDBMMZ'SPNUIJTHFOFSBUFEFWFOUIBOEMFSZPVDBODBMMBNFUIPEUIBUZPVµWFDSFBUFE
JOUIFQSPKFDUPSZPVDBOBEEZPVSFWFOUIBOEMJOHGVODUJPOBMJUZEJSFDUMZUPUIFFWFOUIBOEMFS
DPEF*OUIJTFYBNQMFXFµMMDBMMUIF/UI9KPGVODUJPOUIBUXFXPSLFEUISPVHIFBSMJFSUP
EJTQMBZBNFTTBHFUIBUJOEJDBUFTUIBUUIFCVJMEIBTDPNQMFUFE
Private _
Sub BuildEvents_OnBuildDone(ByVal Scope _
As EnvDTE.vsBuildScope, _
ByVal Action As EnvDTE.vsBuildAction) _
Handles BuildEvents.OnBuildDone
MsgWin("Build is done!")
End Sub
"TZPVDBOJNBHJOFUIFTFFWFOUTPQFOVQBMMTPSUTPGQPTTJCJMJUJFTGPSBVUPNBUJPOBOE
DVTUPNJ[BUJPOJOUIF*%&0OFUIJOHZPVTIPVMELFFQJONJOEXIFOXPSLJOHXJUIFWFOUTJT
UIBUBMMUIFDPEFJOBTJOHMFNBDSPQSPKFDUTIBSFTUIFTBNFFWFOUNPEVMF5IJTNFBOTUIBUJG
ZPVXBOUUPDSFBUFEJGGFSFOUFWFOUIBOEMFSTGPSUIFTBNFFWFOUZPVµMMOF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OCVQSPQFSUZJOUIF1SPQFSUJFTXJOEPXGSPN#JOBSZWTNBDSPT
UP
5FYU6OJDPEF
5IJTDIBOHFXJMMDSFBUFBOVNCFSPG¾MFTJOUIFNBDSPQSPKFDUµTGPMEFSXIJDI
MPPLTNVDIMJLFBSFHVMBS7JTVBM4UVEJPQSPKFDUGPMEFS*O'JHVSFZPVDBOTFFUIFGPMEFS
GPSUIF4BNQMFTQSPKFDUBGUFSJUIBTCFFODPOWFSUFEUP5FYUGPSNBU
Figure 5-9
A macro project that has been stored in Text format
5IFBEWBOUBHFPGQBTTJOHBMPOHBUFYUCBTFEQSPKFDUJTUIBUJUBMMPXTPUIFSQSPHSBNNFSTUP
MPPLBUUIFTPVSDF¾MFTJOZPVSQSPKFDUCFGPSFMPBEJOHUIFNJOUPUIFJS*%&T
5IFSFµTBMXBZTBTFDVSJUZSJTLJOPQFOJOHVOLOPXONBDSPQSPKFDUTJOBOZBQQMJDBUJPO
#FTVSFZPVLOPXXIFSFBOZCJOBSJFTZPVPQFODPNFGSPN"UUIFWFSZMFBTUDIFDLUIF
&OWJSPONFOU&WFOUTWCNPEVMFUPNBLFTVSFJUEPFTOµUJODMVEFBOZVOFYQFDUFEDPEF
Looking Ahead
5IJTDIBQUFSHBWFZPVTPNFJOGPSNBUJPOBCPVUVTJOHUIF7JTVBM4UVEJPNBDSPGBDJMJUZUP
QFSGPSNTPNFTJNQMFBVUPNBUJPOUBTLT5IFSFNBJOEFSPGUIFCPPLTIPVMEQSPWJEFZPVXJUI
FOPVHIJOGPSNBUJPOUPGVMMZVUJMJ[FBVUPNBUJPOJO7JTVBM4UVEJPGSPNNBDSPTPSBEEJOT
Chapter 6
Extending the IDE with Add-Ins
In this chapter:
Running the Add-In Wizard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Add-In Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Loading the Add-In. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Debugging the Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Add-In Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
107
109
111
113
114
130
:PVMFBSOFEJO$IBQUFSUIBUNBDSPTQSPWJEFBDPOWFOJFOUXBZUPBVUPNBUFUBTLTXJUIJO
.JDSPTPGUˆ7JTVBM4UVEJPˆBOEXFFODPVSBHFZPVUPXSJUFNBDSPT¾STUXIFODVTUPNJ[JOH
UIFJOUFHSBUFEEFWFMPQNFOUFOWJSPONFOU*%&
#VUGPSTPNFQVSQPTFTTVDIBTXSJUJOH
DPNNFSDJBMTPGUXBSFZPVNJHIU¾OEUIBUNBDSPTBSFBQPPSDIPJDFJOUFSNTPGQFSGPSNBODF
BOEQSPUFDUJPOPGJOUFMMFDUVBMQSPQFSUZ*OTVDIDBTFTUIFBQQSPQSJBUFWFIJDMFJTBOCFFKP
XIJDIJTBDPNQJMFEEZOBNJDMJOLMJCSBSZ%--
QSPWJEJOHJODSFBTFEQSPUFDUJPO
UIBUSVOT
XJUIJOUIF*%&QSPWJEJOHJODSFBTFEQFSGPSNBODF
5IFRVJDLFTUXBZUPHFUTUBSUFEBTBOBEEJOQSPHSBNNFSJTUISPVHIUIF"EEJO8J[BSE
XIJDINBLFTDSFBUJOHBEEJOTFBTZ±KVTUDIPPTFBGFXPQUJPOTBOEUIFXJ[BSEHFOFSBUFTBO
BEEJOUIBUJTSFBEZGPSUIF*%&0GDPVSTFZPVXPOµUBDDPNQMJTINVDIJGZPVEPOµULOPX
IPXUPDPOUSPMBOEDVTUPNJ[FUIFBEEJOTPJOUIF¾STUIBMGPGUIJTDIBQUFSXFµMMTIPXZPV
IPXUIFEJGGFSFOUQBSUTPGBOBEE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Add-Ins
109
The Add-In Project
"EEJOTBSF%--TTPUIF"EEJO8J[BSEDSFBUFTB$MBTT-JCSBSZQSPKFDUGPSZPVSBEEJO5IJT
QSPKFDUDPOUBJOTBTPVSDF¾MFOBNFE$POOFDUXIJDIEF¾OFTUIFBEEJODMBTTBMTPOBNFE
%QPPGEV5IF%QPPGEVDMBTTJNQMFNFOUTUIF+&6'ZVGPUKDKNKV[JOUFSGBDFXIJDITFSWFTBTUIF
NBJODPOEVJUGPSBEEJO*%&DPNNVOJDBUJPO%QPPGEVBMTPJNQMFNFOUT+&6%QOOCPF6CTIGV
JGZPVTFMFDUUIFVTFSJOUFSGBDFPQUJPOJOUIF"EEJO8J[BSE
5BCMFMJTUTUIF¾WFNFUIPET
PGUIF+&6'ZVGPUKDKNKV[JOUFSGBDF
Table 6-1 IDTExtensibility2 Interface Methods
Method
Description
OnConnection
Called when the add-in is loaded
OnStartupComplete
Called when Visual Studio finishes loading
OnAddInsUpdate
Called whenever an add-in is loaded or unloaded from Visual Studio
OnBeginShutdown
Called when Visual Studio is closed
OnDisconnection
Called when the add-in is unloaded
5IF$POOFDUDT¾MFJO-JTUJOHTIPXTUIFDPEFNJOVTTPNFDPNNFOUT
UIBUUIF"EEJO
8J[BSEHFOFSBUFTGPSBUZQJDBM$BEEJOXJUIBNFOVDPNNBOE8FµMMXBMLUISPVHIUIF
TPVSDFDPEFQPJOUJOHPVUBOZJOUFSFTUJOHGFBUVSFTBMPOHUIFXBZ
Listing 6-1
using
using
using
using
using
using
using
using
Connect.cs, the add-in source code generated by the Add-in Wizard
System;
Extensibility;
EnvDTE;
EnvDTE80;
Microsoft.VisualStudio.CommandBars;
System.Resources;
System.Reflection;
System.Globalization;
public class Connect : Object, IDTExtensibility2, IDTCommandTarget
{
public Connect()
{
}
public void OnConnection(object application,
ext_ConnectMode connectMode, object addInInst,
ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
// Generate the add-in’s menu item...
}
110
Working with Microsoft Visual Studio 2005
}
public void OnDisconnection(ext_DisconnectMode disconnectMode,
ref Array custom)
{
}
public void OnAddInsUpdate(ref Array custom)
{
}
public void OnStartupComplete(ref Array custom)
{
}
public void OnBeginShutdown(ref Array custom)
{
}
public void QueryStatus(string commandName,
vsCommandStatusTextWanted neededText, ref vsCommandStatus status,
ref object commandText)
{
if(neededText ==
vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if(commandName == "MyAddin1.Connect.MyAddin1")
{
status = (vsCommandStatus)
vsCommandStatus.vsCommandStatusSupported |
vsCommandStatus.vsCommandStatusEnabled;
}
}
}
public void Exec(string commandName, vsCommandExecOption executeOption,
ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption ==
vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "MyAddin1.Connect.MyAddin1")
{
handled = true;
return;
}
}
}
private DTE2 _applicationObject;
private AddIn _addInInstance;
}
"UUIFUPQPGUIFMJTUJOHZPVµMMTFFUIBUUIF"EEJO8J[BSEHFOFSBUFTBTFUPGWUKPITUBUFNFOUT
GPSUIFQSPHSBNNFSµTDPOWFOJFODF5IFUISFFNPTUJNQPSUBOUOBNFTQBDFTJOUIFWUKPI
Chapter 6:
Extending the IDE with Add-Ins
111
TUBUFNFOUTBSF'ZVGPUKDKNKV['PX&6'BOE'PX&6'UIF¾STUOBNFTQBDFEF¾OFTUIFUZQFT
VTFECZ+&6'ZVGPUKDKNKV[BOEUIFMBUUFSUXPEF¾OFUIFUZQFTJOUIFBVUPNBUJPOPCKFDU
NPEFM
5IF¾STUNFUIPEJOUIFMJTUJOH1P%QPPGEVKQPXJOTUIFQSJ[FGPS²NPTUJNQPSUBOUBEEJO
NFUIPE³7JTVBM4UVEJPDBMMTUIJTNFUIPEXIFOJUMPBETUIFBEEJOBOEJUQBTTFTUIFBEE
JOBSFGFSFODFUPUIFSPPUPCKFDUPGUIFBVUPNBUJPOPCKFDUN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BOE1P&KUEQPPGEVKQP5IFSFµTOP
/CKPNFUIPECFDBVTF#BTJDBTXJUIBMMBEEJOTJTEFTUJOFEUPCFDPNFB%--*OTUFBEUIF
1P%QPPGEVKQPNFUIPETFSWFTBTUIFBEEJOµTFOUSZQPJOUBOEUIF#BTJDBEEJOJNQMFNFOUT
UIBUNFUIPECZEJTQMBZJOHJUTPXOOBNFJOBNFTTBHFCPY
Listing 6-2
Basic.cs, the Basic add-in source code
using System;
using System.Windows.Forms;
using Extensibility;
public class Basic : IDTExtensibility2
{
public void OnConnection(object application,
ext_ConnectMode connectMode,
Chapter 6:
Extending the IDE with Add-Ins
115
object addInInst,
ref Array custom)
{
MessageBox.Show("Basic Add-in");
}
public void OnStartupComplete(ref Array custom)
{
}
public void OnAddInsUpdate(ref Array custom)
{
}
public void OnBeginShutdown(ref Array custom)
{
}
public void OnDisconnection(ext_DisconnectMode removeMode,
ref Array custom)
{
}
}
Compiling the Basic Add-In
*GZPVBEEUIFTPVSDFDPEFJO-JTUJOHUPBUFYU¾MFOBNFE#BTJDDTZPVDBODPNQJMFUIF
#BTJDBEEJOGSPNUIFDPNNBOEMJOFCZVTJOHUIFGPMMPXJOHDPNNBOE
csc /t:library /r:"c:\program files\common files\microsoft
shared\msenv\publicassemblies\extensibility.dll" basic.cs
5IFVNKDTCT[¿BHEJSFDUTUIF$DPNQJMFSUPDSFBUFB%--#BTJDEMM
GSPNUIFTPVSDF
¾MFBOEUIFT´E>RTQITCO¿NGU>EQOOQP¿NGU>OKETQUQHVUJCTGF>OUGPX>RWDNKECUUGODNKGU>
GZVGPUKDKNKV[FNN´¿BHQPJOUTUIFDPNQJMFSUPUIFBTTFNCMZUIBUDPOUBJOTUIF'ZVGPUKDKNKV[
OBNFTQBDF&YUFOTJCJMJUZEMM
5IF'ZVGPUKDKNKV[OBNFTQBDFEF¾OFTUISFFUZQFTXIJDIBMM
BEEJOTVTFUIF+&6'ZVGPUKDKNKV[JOUFSGBDFBOEUIFGZVA%QPPGEV/QFGBOEGZVA&KUEQPPGEV/QFG
FOVNFSBUJPOTXIJDIEF¾OFWBMVFTQBTTFEUPUIF1P%QPPGEVKQPBOE1P&KUEQPPGEVKQP
NFUIPETSFTQFDUJWFMZ
Tip Typing long references at the command line invites both carpal tunnel syndrome and
boredom. As an alternative, you can add a reference to the list of default references in the
global CSC.rsp file, located at <WinDir>\Microsoft.NET\Framework\<Version>\CSC.rsp.
For example, if you add /r: “c:\program files\common files\microsoft shared\msenv\publicassemblies\extensibility.dll” to the global CSC.rsp file, you can compile the Basic add-in with
the following command:
csc /t:library basic.cs
116
Working with Microsoft Visual Studio 2005
Registering the Basic Add-In with Visual Studio
#BTJDEMMJTBGVMMZGVODUJPOBMBEEJOCVU7JTVBM4UVEJPXPOµUIBWFUIFJOGPSNBUJPOUIBU#BTJD
EMMJTJOFYJTUFODFCZUIJTQPJOU"EEJOTTJHOBMUIFJSBWBJMBCJMJUZUPUIF7JTVBM4UVEJP*%&
UISPVHIBO9.-¾MFXJUIBOFYUFOTJPOPGBEEJO-JTUJOHTIPXTUIFNJOJNBMBEEJO¾MF
GPSUIF#BTJDBEEJO
Listing 6-3
The Basic.addin XML file
<?xml version="1.0" ?>
<Extensibility
xmlns="http://schemas.microsoft.com/AutomationExtensibility">
<HostApplication>
<Name>Microsoft Visual Studio</Name>
<Version>8.0</Version>
</HostApplication>
<Addin>
<Assembly>basic.dll</Assembly>
<FullClassName>Basic</FullClassName>
</Addin>
</Extensibility>
"TZPVDBOTFFGSPN-JTUJOHUIF#BTJDBEEJO¾MFIBTBTJNQMFTUSVDUVSFUIBUEFTDSJCFTUIF
BEEJOBOEJUTIPTUFOWJSPONFOU5IF)PTU"QQMJDBUJPOOPEFJEFOUJ¾FTUIF7JTVBM4UVEJP
*%&BTUIFBEEJOIPTUUIF"EEJOOPEFHJWFTUIFQBUIUPUIFBEEJOBTTFNCMZSFMBUJWF
UPUIFBEEJO¾MF
BOEBMTPQSPWJEFTUIFGVMMZRVBMJ¾FEOB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OBEEJOJOUIF7JTVBM4UVEJPFOWJSPONFOUXIFOUIFBEEJOMPBETXIFO
UIFBEEJOVOMPBETBOEBMMUIFUJNFJOCFUXFFO
Chapter 6:
Extending the IDE with Add-Ins
117
Figure 6-5
The Add-in Manager dialog box showing the Basic add-in
Figure 6-6
The message box displayed by the Basic add-in
Add-In Events
"EEJOTBSFFWFOUESJWFO.PTUFWFSZUIJOHBOBEEJOEPFTJUEPFTJOSFTQPOTFUPTPNF
FYUFSOBMQSPEEJOHBOE7JTVBM4UVEJPQSPETBEEJOTXJUIUIF+&6'ZVGPUKDKNKV[JOUFSGBDF
8FµMMCFHJOPVSFYQMPSBUJPOPGBEEJOFWFOUTCZFYBNJOJOHUIFTFRVFODFJOXIJDI7JTVBM
4UVEJPDBMMTUIF+&6'ZVGPUKDKNKV[NFUIPET
The Add-In Event Sequence
$BMMTUPUIF+&6'ZVGPUKDKNKV[NFUIPETXIJDIXFµMMBMTPSFGFSUPBTGXGPVUPDDVSBUQSFEJDUBCMF
QPJOUTJOUIFMJGFUJNFPGBOBEEJO'JHVSFTIPXTUIFTFRVFODFPGFWFOUTGSPNUIFUJNFBO
BEEJOJTMPBEFEUPUIFUJNFJUJTVOMPBEFE
:PVDBOHVFTTUIFBDUJPOTUIBUUSJHHFSUIFFWFOUTKVTUGSPNUIFFWFOUTµOBNFTBOEUIFFWFOUT
PDDVSQSFUUZNVDIJOUIFPSEFSZPVXPVMEFYQFDU1P%QPPGEVKQPXIFOBOBEEJOMPBET
1P&KUEQPPGEVKQPXIFOBOBEEJOVOMPBETBOETPPO
:PVDBOHFUBGFFMGPSUIFBEEJOFWFOUTFRVFODFCZSVOOJOHUIF-JGF$ZDMFTBNQMFBEEJO
-JGF$ZDMFTIPXOJO-JTUJOHIBOEMFTFBDI+&6'ZVGPUKDKNKV[FWFOUCZEJTQMBZJOHUIFOBNF
PGUIFFWFOUJOUIF0VUQVUXJOEPX"GUFSZPVCVJMEUIF-JGF$ZDMFBEEJOMPBEJUJOUP7JTVBM
4UVEJPCZVTJOHUIF"EEJO.BOBHFS5IFOUSZUPMPBEBOEVOMPBEPUIFSBEEJOTTVDIBT
#BTJDUPUSJHHFSUIFEJGGFSFOU+&6'ZVGPUKDKNKV[FWFOUT5P¾SFUIF1P5VCTVWR%QORNGVGFWFOU
118
Working with Microsoft Visual Studio 2005
ZPV¾STUOFFEUPTFMFDUUIF4UBSUVQDIFDLCPYGPS-JGF$ZDMFJOUIF"EEJO.BOBHFSBOEUIFO
ZPVNVTUSFTUBSU7JTVBM4UVEJP5P¾SFUIF1P$GIKP5JWVFQYPFWFOUDMPTF7JTVBM4UVEJPXIJMF
-JGF$ZDMFJTMPBEFE
Add-in begins loading into Visual Studio
OnConnection
Add-in finishes loading1
OnAddInsUpdate
If loaded at startup
OnStartupComplete
Other add-ins are
loaded or unloaded2
OnAddInsUpdate
Load
Unload
If Visual Studio
shuts down while
add-in is loaded
If add-in is unloaded
while Visual Studio
is running
OnBeginShutdown
OnDisconnection
1
Add-ins loaded by named commands 2 Clicking OK in the Add-in Manager
and add-ins loaded or unloaded by
dialog box triggers OnAddInsUpdate events
their Connected properties don't
even if no add-ins were loaded or unloaded.
trigger OnAddInsUpdate events.
Figure 6-7
The add-in event sequence
Listing 6-4
LifeCycle.cs, the LifeCycle add-in source code
using
using
using
using
EnvDTE;
EnvDTE80;
Extensibility;
System;
public class LifeCycle : IDTExtensibility2
{
private OutputWindowPane output;
Chapter 6:
Extending the IDE with Add-Ins
119
public void OnConnection(object application,
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
DTE2 dte = (DTE2)application;
try
{
this.output =
dte.ToolWindows.OutputWindow.OutputWindowPanes.Item(
"LifeCycle");
}
catch
{
this.output =
dte.ToolWindows.OutputWindow.OutputWindowPanes.Add(
"LifeCycle");
}
this.output.OutputString("OnConnection event fired\n");
}
public void OnStartupComplete(ref Array custom)
{
this.output.OutputString("OnStartupComplete event fired\n");
}
public void OnAddInsUpdate(ref Array custom)
{
this.output.OutputString("OnAddInsUpdate event fired\n");
}
public void OnBeginShutdown(ref Array custom)
{
this.output.OutputString("OnBeginShutdown event fired\n");
}
public void OnDisconnection(ext_DisconnectMode removeMode,
ref Array custom)
{
this.output.OutputString("OnDisconnection event fired\n");
}
}
The IDTExtensibility2 Interface
"TZPVOPXLOPXBOJNQMFNFOUBUJPOPG+&6'ZVGPUKDKNKV[MJFTBUUIFDPSFPGFWFSZBEEJO
7JTVBM4UVEJPDBMMTUIFNFUIPETPOUIJTJOUFSGBDFXIFOFWFSJUOFFETUPBQQSJTFBOBEEJO
PGJNQPSUBOUFWFOUTTVDIBTXIFOBOPUIFSBEEJOJTMPBEFEPSVOMPBEFEPSXIFO7JTVBM
4UVEJPJTBCPVUUPTIVUEPXO5IFDPNNVOJDBUJPOJTOµUKVTUPOFXBZFJUIFSUISPVHIUIF
+&6'ZVGPUKDKNKV[JOUFSGBDFUIFBEEJOIBTBDDFTTUPBOEDPOUSPMPWFSUIFFOUJSF7JTVBM4UVEJP
BVUPNBUJPOPCKFDUNPEFM
120
Working with Microsoft Visual Studio 2005
The EnvDTE and EnvDTE80 Namespaces
#FGPSFFYBNJOJOHUIFJOEJWJEVBM+&6'ZVGPUKDKNKV[NFUIPETXFOFFEUPUBLFBRVJDLMPPL
BUUIFSFBMPCKFDUJWFPGBEEJOT±DPOUSPMMJOHUIFPCKFDUTJOUIF'PX&6'BOE'PX&6'
OBNFTQBDFT5IFOBNF'PX&6'TUBOETGPS'PXKTQPOGPV&GXGNQROGPV6QQNU'ZVGPUKDKNKV[
XIJDIQSFUUZNVDIEFTDSJCFTJUTQVSQPTFJUEF¾OFTUIF7JTVBM4UVEJPBVUPNBUJPOPCKFDU
NPEFM"TZPVNJHIUHVFTT'PX&6'EF¾OFTUIFWFSTJPOPCKFDUTJOUIFBVUPNBUJPO
PCKFDUNPEFM
5IF7JTVBM4UVEJPEPDVNFOUBUJPOJODMVEFTBDIBSUPGUIFBVUPNBUJPOPCKFDU
NPEFMUIBUEJTQMBZTBIJFSBSDIZPGNPSFUIBOPCKFDUTEF¾OFECZUIF'PX&6'BOE
'PX&6'OBNFTQBDFT5IFBEEJOTJOUIJTCPPLXJMMNBLFVTFPGNPTUPGUIPTFPCKFDUTCVU
BGFXPGUIFPCKFDUTBSFPGTQFDJBMJOUFSFTUUPBEEJOT
N
%5&%5& 5IFSPPUPCKFDUTPGUIFBVUPNBUJPOPCKFDUNPEFM
N
"EE*O "OPCKFDUUIBUSFQSFTFOUTBOBEEJO
N
%5&"EE*OT%5&"EE*OT "DPMMFDUJPOPG#FF+PPCKFDUTUIBUJODMVEFTBMMBEEJOT
SFHJTUFSFEXJUIUIF7JTVBM4UVEJP*%&
N
%5&4PMVUJPO"EE*OT%5&4PMVUJPO"EE*OT "DPMMFDUJPOPG#FF+PPCKFDUTBTTPDJBUFE
XJUIBTPMVUJPO
5IFOFYUTFWFSBMFYBNQMFTXJMMGPDVTPOUIF&6'&6'#FF+PBOE&6'#FF+PU&6'
#FF+PUPCKFDUTXIJDIDPMMFDUJWFMZHJWFZPVDPOUSPMPWFSZPVSPXOBEEJOBOEPUIFST8FµMM
DPWFSUIF&6'5QNWVKQP#FF+PUPCKFDUJO$IBQUFS
Note The main purpose of an add-in class is to provide an implementation of
IDTExtensibility2, but that doesn’t have to be its only purpose. An add-in class is a class, after all,
and it can define any number of non-IDTExtensibility2-related methods, properties, and events.
The automation object model provides access to your add-in class through the AddIn.Object
property, which returns the add-in’s IDispatch interface. The following macro code shows how
you would call a public method named DisplayMessage on the MyAddIn.Connect add-in class:
Dim dispObj As Object = DTE.AddIns.Item("MyAddIn.Connect").Object
dispObj.DisplayMessage("IDispatch a message to you.")
OnConnection
1P%QPPGEVKQPQSPWJEFTBOBEEJOXJUIUIFNBJOPCKFDUSFGFSFODFJUOFFETUPDPNNVOJDBUF
EJSFDUMZXJUIUIF*%&5IF1P%QPPGEVKQPNFUIPEIBTUIFGPMMPXJOHQSPUPUZQF
public void OnConnection(object application,
ext_ConnectMode connectMode,
object addInInst,
ref Array custom);
5IFCRRNKECVKQPQBSBNFUFSIPMETBSFGFSFODFUPUIFSPPUPCKFDUPGUIFBVUPNBUJPOPCKFDU
NPEFM5FDIOJDBMMZCRRNKECVKQPIPMETBSFGFSFODFUPBOPCKFDUUIBUJNQMFNFOUTCPUIUIF
Chapter 6:
Extending the IDE with Add-Ins
121
'PX&6'&6'BOE'PX&6'&6'JOUFSGBDFTTPZPVDBODBTUCRRNKECVKQPUP'PX&6'&6'PS
'PX&6'&6'BDDPSEJOHUPZPVSOFFET"MNPTUFWFSZBEEJOUIBUEPFTTPNFUIJOHVTFGVM
IBTOFFEPGUIF&6'PS&6'PCKFDUTPUIF¾STUTUBUFNFOUTJO1P%QPPGEVKQPUZQJDBMMZDBDIF
UIFPCKFDUJOBHMPCBMWBSJBCMF
5IFEQPPGEV/QFGQBSBNFUFSUFMMTBOBEEJOUIFDJSDVNTUBODFVOEFSXIJDIJUXBTMPBEFE5IJT
QBSBNFUFSUBLFTPOPOFPGUIF'ZVGPUKDKNKV[GZVA%QPPGEV/QFGFOVNFSBUJPOWBMVFTTIPXOJO
5BCMF
Table 6-2 The Extensibility.ext_ConnectMode Enumeration
Constant
Value (Int32)
Description
ext_cm_AfterStartup
0x00000000
Loaded after Visual Studio started
ext_cm_Startup
0x00000001
Loaded when Visual Studio started
ext_cm_External
0x00000002
Loaded by an external client (no longer used by
Visual Studio)
ext_cm_CommandLine
0x00000003
Loaded from the command line
ext_cm_Solution
0x00000004
Loaded with a solution
ext_cm_UISetup
0x00000005
Loaded for user interface setup
"OBEEJODBODIFDLUIFEQPPGEV/QFGWBMVFBOEBMUFSJUTCFIBWJPSBDDPSEJOHMZ'PSFYBNQMF
XIFOBOBEEJOSFDFJWFTUIFGZVAEOA7+5GVWRWBMVFJUDBOBEEJUTDVTUPNDPNNBOETUPUIF
*%&NFOVTBOEUPPMCBST5IF"EEJO8J[BSEHFOFSBUFTDPEFUIBUIBOEMFTUIFGZVAEOA
7+5GVWRDBTFJOUIJTNBOOFS
5IFCFF+P+PUVQBSBNFUFSQBTTFTBOBEEJOBSFGFSFODFUPJUTPXO#FF+PJOTUBODFXIJDIJUDBO
TUPSFGPSMBUFSVTF5IF#FF+PJOTUBODFQSPWFTJOWBMVBCMFGPSEJTDPWFSJOHUIFBEEJOµTQBSFOU
DPMMFDUJPO
'JOBMMZFBDIPGUIF+&6'ZVGPUKDKNKV[NFUIPETJODMVEFTBEWUVQOQBSBNFUFS
XIJDIBMMPXTBEEJOIPTUTUPQBTTJOBOBSSBZPGIPTUTQFDJ¾DEBUB7JTVBM4UVEJPBMXBZT
QBTTFTBOFNQUZBSSBZJOEWUVQO
OnStartupComplete
5IF1P5VCTVWR%QORNGVGFWFOU¾SFTPOMZJOBEEJOTUIBUMPBEXIFO7JTVBM4UVEJPTUBSUT5IF
1P5VCTVWR%QORNGVGQSPUPUZQFMPPLTMJLFUIJT
public void OnStartupComplete(ref Array custom);
"OBEEJOUIBUMPBETBUTUBSUVQDBOµUBMXBZTSFMZPO1P%QPPGEVKQPGPSJUTJOJUJBMJ[BUJPO±JGUIF
BEEJOBSSJWFTUPPFBSMZJUXJMMGBJMXIFOJUUSJFTUPBDDFTTB7JTVBM4UVEJPDPNQPOFOUUIBU
IBTOµUZFUMPBEFE*OTVDIDBTFTUIFBEEJODBOVTF1P5VCTVWR%QORNGVGUPHVBSBOUFFUIBU
7JTVBM4UVEJPJTVQBOESVOOJOH¾STU
122
Working with Microsoft Visual Studio 2005
OnAddInsUpdate
5IF1P#FF+PU7RFCVGFWFOU¾SFTXIFOBOBEEJOKPJOTPSMFBWFTUIF7JTVBM4UVEJP
FOWJSPONFOU"OBEEJODBOVTFUIJTFWFOUUPFOGPSDFEFQFOEFODJFTPOPUIFSBEEJOT)FSFµT
UIF1P#FF+PU7RFCVGQSPUPUZQF
public void OnAddInsUpdate(ref Array custom);
5IF1P#FF+PU7RFCVGFWFOUEPFTOµUQSPWJEFZPVXJUIJOGPSNBUJPOBCPVUXIJDIBEEJO
USJHHFSFEUIFFWFOUPSXIZ*GZPVOFFEUPLOPXUIFBEEJOSFTQPOTJCMFGPSUIFFWFOUZPV
IBWFUPEJTDPWFSJUTJEFOUJUZPOZPVSPXO'PSUVOBUFMZZPVIBWFUIF&6'#FF+PU&6'
#FF+PUDPMMFDUJPOUPBJEZPVJOZPVSJOWFTUJHBUJPO5IJTDPMMFDUJPOIPMETBMJTUPG#FF+P
PCKFDUTPOFGPSFBDISFHJTUFSFEBEEJO
BOEFBDI#FF+PPCKFDUIBTB%QPPGEVGFQSPQFSUZ
UIBUFYQPTFTJUTDPOOFDUJPOTUBUVT:PVSFUSJFWFBTQFDJ¾DBEEJOGSPNUIF#FF+PUDPMMFDUJPO
CZQBTTJOHUIF#FF+PU+VGONFUIPEBGVMMZRVBMJ¾FEDMBTTOBNFPSBCBTFEJOEFYJGUIF
SFRVFTUFEJOEFYEPFTOµUFYJTUJOUIFDPMMFDUJPOUIF+VGONFUIPEUISPXTBOJOWBMJEJOEFY
%1/'ZEGRVKQPPUIFSXJTFJUSFUVSOTBO#FF+PSFGFSFODF)FSFµTPOFXBZUPDIFDL-JGF$ZDMFµT
DPOOFDUJPOTUBUVT
public void OnAddInsUpdate(ref Array custom)
{
try
{
AddIn addIn = this.dte.AddIns.Item("LifeCycle");
if (addIn.Connected)
{
// LifeCycle is connected
}
else
{
// LifeCycle isn't connected
}
}
catch (COMException)
{
// LifeCycle isn't a registered add-in
}
}
0GDPVSTFXIFUIFS-JGF$ZDMFDBVTFEUIFFWFOUSFNBJOTBNZTUFSZ5IF-PBE6OMPBEBEE
JOTIPXOJO-JTUJOHEPFTXIBUUIFQSFWJPVTTBNQMFDBOOPUJUEFEVDFTXIJDIBEEJO
USJHHFSTUIF1P#FF+PU7RFCVGFWFOU
Listing 6-5
namespace
{
using
using
using
LoadUnload.cs, the LoadUnload source code
LoadUnload
EnvDTE;
EnvDTE80;
Extensibility;
Chapter 6:
Extending the IDE with Add-Ins
using Microsoft.VisualStudio.CommandBars;
using System;
using System.Collections.Generic;
public class Connect : Object, IDTExtensibility2, IDTCommandTarget
{
public Connect()
{
}
public void OnConnection(object application,
ext_ConnectMode connectMode, object addInInst,
ref Array custom)
{
this.applicationObject = (DTE2)application;
this.addInInstance = (AddIn)addInInst;
this.addInsCollection = this.applicationObject.AddIns;
this.addInsList = new SortedList<string, bool>();
foreach (AddIn addIn in this.addInsCollection)
this.addInsList[addIn.ProgID] = addIn.Connected;
OutputWindow win =
this.applicationObject.ToolWindows.OutputWindow;
try
{
this.output = win.OutputWindowPanes.Item("LoadUnload");
}
catch
{
this.output = win.OutputWindowPanes.Add("LoadUnload");
}
.
.
.
}
public void OnAddInsUpdate(ref Array custom)
{
this.addInsCollection.Update();
foreach (AddIn addIn in this.addInsCollection)
{
string action = addIn.ProgID + " was ";
if (this.addInsList.ContainsKey(addIn.ProgID))
{
if (addIn.Connected != this.addInsList[addIn.ProgID])
{
action += addIn.Connected ? "loaded" : "unloaded";
this.output.OutputString(action + "\n");
}
}
123
124
Working with Microsoft Visual Studio 2005
else
{
action += "added" +
(addIn.Connected ? " and loaded" : "");
this.output.OutputString(action + "\n");
}
this.addInsList[addIn.ProgID] = addIn.Connected;
}
}
.
.
.
private
private
private
private
private
DTE2 applicationObject;
AddIn addInInstance;
AddIns addInsCollection;
SortedList<string, bool> addInsList;
OutputWindowPane output;
}
}
-PBE6OMPBENBJOUBJOTBSVOOJOHMJTUPGBEEJOTBOEUIFJSDPOOFDUJPOTUBUVTFTJOJUT
CFF+PU.KUVWBSJBCMFXIJDIJTEFDMBSFEBTUZQF5QTVGF.KUVUVTKPIDQQN 8IFO1P#FF+PU7RFCVG
¾SFT-PBE6OMPBEDPNQBSFTUIFDPOOFDUJPOTUBUVTFTPGUIFBEEJOTJOJUTJOUFSOBMMJTU
XJUIUIFDPOOFDUJPOTUBUVTFTPGUIFBEEJOTJOUIF&6'#FF+PUDPMMFDUJPO±JGJU¾OETB
EJTDSFQBODZJULOPXTXIJDIBEEJOUPCMBNFGPSUIFFWFOU)FSFµTUIF¾STUQBSUPGUIFNBJO
MPPQGSPN-JTUJOH
this.addInsCollection.Update();
foreach (AddIn addIn in this.addInsCollection)
{
string action = addIn.ProgID + " was ";
if (this.addInsList.ContainsKey(addIn.ProgID))
{
if (addIn.Connected != this.addInsList[addIn.ProgID])
{
action += addIn.Connected ? "loaded" : "unloaded";
this.output.OutputString(action + "\n");
}
}
§
5IFCFF+PU%QNNGEVKQPWBSJBCMFIPMETBSFGFSFODFUPUIF&6'#FF+PUDPMMFDUJPOBOEUIFDBMMUP
7RFCVGTZODISPOJ[FTUIFDPMMFDUJPOXJUIUIFSFHJTUSZTPUIBUBOZOFXMZDSFBUFEBEEJOTBSF
JODMVEFE5IF"EEJO.BOBHFSQFSGPSNTUIFFRVJWBMFOUPG7RFCVGFBDIUJNFJUSVOT
"GUFS
UIFDBMMUP7RFCVGUIFNBJOMPPQJUFSBUFTUISPVHIUIFDVSSFOUBEEJOTJOCFF+PU%QNNGEVKQPBOE
DIFDLTXIFUIFSFBDIBEEJOBMSFBEZFYJTUTJOJUTJOUFSOBMMJTU*GTPUIF%QPPGEVGFQSPQFSUZ
Chapter 6:
Extending the IDE with Add-Ins
125
PGUIFBEEJOJTDPNQBSFEXJUIUIFDPSSFTQPOEJOHWBMVFTUPSFEJOUIFJOUFSOBMMJTUJGUIFZ
EJGGFSUIF%QPPGEVGFQSPQFSUZEFUFSNJOFTXIFUIFSUIFBEEJOXBTMPBEFEUSVF
PSVOMPBEFE
GBMTF
*GUIFDVSSFOUBEEJOEPFTOµUFYJTUJOCFF+PU.KUVUIFBEEJOXBTSFHJTUFSFETPNFUJNFCFUXFFO
UIFQSFWJPVT1P#FF+PU7RFCVGFWFOUBOEUIJT1P#FF+PU7RFCVGFWFOU)FSFµTUIFTFDPOEQBSU
PGUIFNBJOMPPQXIJDIIBOEMFTOFXBEEJOT
§
else
{
action += "added" +
(addIn.Connected ? " and loaded" : "");
this.output.OutputString(action + "\n");
}
this.addInsList[addIn.ProgID] = addIn.Connected;
}
5IFMBTUTUBUFNFOUFJUIFSXSJUFTUIFDVSSFOU%QPPGEVGFWBMVFUPBOFYJTUJOHFOUSZPSDSFBUFTB
GSFTIFOUSZGPSBOFXMZSFHJTUFSFEBEEJO
-PBE6OMPBEJTOµUGPPMQSPPG±GPSFYBNQMFBEEJOTMPBEFECZDPNNBOETBSSJWFBOEMFBWF
VOBOOPVODFE±CVUJUXPSLTXFMMFOPVHIGPSEFNPOTUSBUJPOQVSQPTFT
OnBeginShutdown
)FSFµTUIFQSPUPUZQFGPS1P$GIKP5JWVFQYP
public void OnBeginShutdown(ref Array custom);
5IJTFWFOU¾SFTPOMZXIFOUIF*%&TIVUTEPXOXIJMFBOBEEJOJTSVOOJOH"MUIPVHI
BO*%&TIVUEPXONJHIUHFUDBODFMFEBMPOHUIFXBZ1P$GIKP5JWVFQYPEPFTOµUQSPWJEF
BDBODFMMBUJPONFDIBOJTNTPBOBEEJOTIPVMEBTTVNFUIBUTIVUEPXOJTJOFWJUBCMFBOE
QFSGPSNJUTDMFBOVQSPVUJOFTBDDPSEJOHMZ"OBEEJOUIBUNBOJQVMBUFT*%&TUBUFNJHIUVTF
UIJTFWFOUUPSFTUPSFUIFPSJHJOBM*%&TFUUJOHT
OnDisconnection
5IJTFWFOUJTTJNJMBSUP1P$GIKP5JWVFQYPJOUIBUJUTJHOBMTUIFFOEPGBOBEEJOµTMJGFJUEJGGFST
GSPN1P$GIKP5JWVFQYPJOUIBUUIF*%&JTOµUOFDFTTBSJMZBCPVUUPTIVUEPXO1P&KUEQPPGEVKQP
BMTPQSPWJEFTNPSFJOGPSNBUJPOUPBOBEEJOUIBO1P$GIKP5JWVFQYPEPFT1P&KUEQPPGEVKQPµT
QSPUPUZQFMPPLTMJLFUIJT
public void OnDisconnection(ext_DisconnectMode removeMode,
ref Array custom);
5IFTGOQXG/QFGQBSBNFUFSQBTTFTJOBO+&6'ZVGPUKDKNKV[GZVA&KUEQPPGEV/QFGFOVNFSBUJPO
WBMVFUIBUUFMMTBOBEEJOXIZJUXBTVOMPBEFE5BCMFMJTUTUIFGZVA&KUEQPPGEV/QFGWBMVFT
126
Working with Microsoft Visual Studio 2005
Table 6-3 The Extensibility.ext_DisconnectMode Enumeration
Constant
Value (Int32)
Description
ext_dm_HostShutdown
0x00000000
Unloaded when Visual Studio shuts down
ext_dm_UserClosed
0x00000001
Unloaded while Visual Studio is running
ext_dm_UISetupComplete
0x00000002
Unloaded after user interface setup
ext_dm_SolutionClosed
0x00000003
Unloaded when solution closed
5IFGZVA&KUEQPPGEV/QFGFOVNFSBUJPOTFSWFTBQVSQPTFTJNJMBSUPGZVA%QPPGEV/QFGJUBMMPXT
BOBEEJOUPBMUFSJUTCFIBWJPSUPTVJUJUTDVSSFOUDJSDVNTUBODFT'PSFYBNQMFBOBEEJOUIBU
SFDFJWFTGZVAFOA7+5GVWR%QORNGVGQSPCBCMZXPVMECZQBTTJUTDMFBOVQSPVUJOFTCFDBVTFJUXBT
MPBEFEGPSJOJUJBMJ[BUJPOQVSQPTFTPOMZ
The .Addin File
"TZPVMFBSOFEFBSMJFSJOUIJTDIBQUFSBOBEEJONBLFTJUTFMGLOPXOUP7JTVBM4UVEJPCZ
SFHJTUFSJOHBO9.-¾MFXJUIBOBEEJOFYUFOTJPO5IFGPMMPXJOHTFDUJPOTFYQMPSFUIF
TUSVDUVSFPGUIJT¾MFJOHSFBUFSEFUBJM
Host Application Information
5IFBEEJO¾MFDBOTQFDJGZBOZOVNCFSPGIPTUBQQMJDBUJPOTCZJODMVEJOHB*QUV#RRNKECVKQP FMFNFOUGPSFBDITVQQPSUFEIPTU5IF*QUV#RRNKECVKQP FMFNFOUDPOUBJOTUXPDIJME
FMFNFOUT0COG BOE8GTUKQP JOUIBUPSEFS'PS7JTVBM4UVEJPBEEJOTUIF0COG WBMVF
JTFJUIFS/KETQUQHV8KUWCN5VWFKQUPTQFDJGZUIFNBJO*%&BTUIFIPTUPS/KETQUQHV8KUWCN5VWFKQ
/CETQUUPTQFDJGZUIF.BDSPT*%&BTUIFIPTU5IF8GTUKQP WBMVFJEFOUJ¾FTUIFWFSTJPOPG
7JTVBM4UVEJPUIBUTVQQPSUTUIJTBEEJOTVDIBTPSPSZPVDBOVTFBXJMEDBSEWBMVF
PGUPJOEJDBUFUIBUBOZWFSTJPOPG7JTVBM4UVEJPXJMMXPSL
5IFGPMMPXJOHTMJHIUMZDPOUSJWFE
FYBNQMFTIPXTIPXUPTQFDJGZUIBUBQBSUJDVMBSBEEJO
XPSLTJOBMMWFSTJPOTPGUIF.BDSPT*%&CVUPOMZJOUIFUXPNPTUSFDFOUWFSTJPOTPGUIF
7JTVBM4UVEJP*%&
<HostApplication>
<Name>Microsoft Visual Studio Macros</Name>
<Version>*</Version>
</HostApplication>
<HostApplication>
<Name>Microsoft Visual Studio</Name>
<Version>7.1</Version>
</HostApplication>
<HostApplication>
<Name>Microsoft Visual Studio</Name>
<Version>8.0</Version>
</HostApplication>
Chapter 6:
Extending the IDE with Add-Ins
127
1SBDUJDBMMZTQFBLJOHTQFDJGZJOHBWFSTJPOFBSMJFSUIBONBLFTMJUUMFTFOTFCFDBVTFQSFWJPVT
WFSTJPOTPG7JTVBM4UVEJPEPOµUVTFBEEJO¾MFTGPSBEEJOSFHJTUSBUJPO
Add-In Information
5IFOFYUFMFNFOUJOUIFBEEJO¾MF±UIF#FFKP FMFNFOU±TQFDJ¾FTUIFBEEJOJUTFMGBOEUIF
DIJMESFOPGUIJTFMFNFOUBMMPXZPVUP¾OFUVOFUIFBEEJOµTCFIBWJPS*OPSEFSUIF#FFKP FMFNFOUIBTUIFGPMMPXJOHDIJMEFMFNFOUT
N
(TKGPFN[0COG PQUJPOBM
N
&GUETKRVKQP PQUJPOBM
N
#DQWV$QZ&GVCKNU PQUJPOBM
N
#DQWV+EQP&CVC PQUJPOBM
N
#UUGODN[ SFRVJSFE
N
(WNN%NCUU0COG SFRVJSFE
N
.QCF$GJCXKQT PQUJPOBM
N
%QOOCPF2TGNQCF PQUJP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OPSUIF*%PGBTUSJOH
SFTPVSDFJOUIFBEEJOµTTBUFMMJUF%--JOUIFGPSN"TGUQWTEG+& 5IF#DQWV+EQP&CVC WBMVFTUPSFTUIFCJOBSZEBUBPGBOJDPOBTBOBSSBZPGIFYBEFDJNBMDIBSBDUFST
128
Working with Microsoft Visual Studio 2005
Figure 6-8 Add-in information displayed in the Visual Studio About dialog box
<LoadBehavior> 5IF.QCF$GJCXKQT WBMVFDPOUSPMTIPXBOBEEJOJTMPBEFECZ7JTVBM
4UVEJP5BCMFMJTUTUIFQPTTJCMF.QCF$GJCXKQT WBMVFT5IFTFWBMVFTBSFCJU¿BHTTPZPV
DBODPNCJOFUIFNUPDSFBUFZPVSPXODVTUPNMPBETFUUJOHT
Table 6-4
<LoadBehavior> Values
Value
Description
0x0
Add-in currently is unloaded.
0x1
Add-in loads at startup.
0x2
Add-in currently is loaded.
0x4
Add-in loads during command-line builds.
5IFZBOEZWBMVFTOPMPOHFSTFSWFBVTFGVMQVSQPTF6TFUIF#FF+P%QPPGEVGFQSPQFSUZ
JOTUFBEUPEJTDPWFSUIFMPBETUBUFPGBOBEEJO
"WBMVFPGZUFMMT7JTVBM4UVEJPUPMPBEUIFBEEJOXIFOUIF*%&TUBSUTVQ"EEJOTUIBU
NPOJUPS*%&FWFOUTJOQBSUJDVMBSOFFEUPCFVQBOESVOOJOHGSPNUIFCFHJOOJOH±PUIFSXJTF
UIFZNJHIUNJTTTPNFPGUIFBDUJPO"EEJOTUIBUEPOµUNPOJUPS*%&FWFOUTDBOPNJUUIJT¿BH
BOEXBJUUPCFMPBEFEPOEFNBOE
"WBMVFPGZTJHOBMTUIBUBOBEEJOTIPVMECFMPBEFEEVSJOHDPNNBOEMJOFCVJMET
<CommandPreload> .BOZBEEJOTFYQPTFUIFJSGVODUJPOBMJUZUISPVHINFOVJUFNTBOE
UPPMCBSCVUUPOTJOUIF*%&±XIFOTFMFDUFEPSDMJDLFEUIFTFVTFSJOUFSGBDFJUFNTMPBEUIFBEE
JOBOEQBTTBMPOHUIFBQQSPQSJBUFDPNNBOEGPSQSPDFTTJOH0GDPVSTFUIFVTFSJOUFSGBDF
JUFNTEPOµUBQQFBSNBHJDBMMZBOBEEJODSFBUFTUIFNBOEBEETUIFNUPUIF*%&UIF¾STUUJNF
Chapter 6:
Extending the IDE with Add-Ins
129
UIBUUIFBEEJOMPBET#VUXJUIPVUVTFSJOUFSWFOUJPOIPXEPFTBOBEEJO¾STUHFUMPBEFEJO
PSEFSUPDSFBUFUIFVTFSJOUFSGBDFJUFNUPMPBEJU 5IFTPMVUJPOUPUIJTQSPCMFNCFHJOTXJUI
UIF%QOOCPF2TGNQCF WBMVFBOEFOETXJUIUIF1SFMPBE"EEJO4UBUF.BOBHFESFHJTUSZLFZ"O
BEEJOTFUTJUT%QOOCPF2TGNQCF WBMVFUPZUPUFMM7JTVBM4UVEJPUIBUJUXBOUTUPCFMPBEFE
PODFUIFOFYUUJNFUIF*%&TUBSUTVQGPSUIFQVSQPTFPGBEEJOHJUTVTFSJOUFSGBDFJUFNTUP
UIF*%&BQSPDFTTLOPXOBTQSFMPBEJOH
Note Actually, there is no requirement for preloaded add-ins to create user interface items;
they’re free to perform any kind of initialization they need, such as creating data files and
adding custom registry entries.
"UTUBSUVQ7JTVBM4UVEJPFYBNJOFTFWFSZBEEJO¾MFBOEQSFMPBETFBDIBEEJOUIBUIBTB
%QOOCPF2TGNQCF WBMVFPGZCVUPOMZJGUIFBEEJOIBTOµUZFUCFFOQSFMPBEFE7JTVBM
4UVEJPEFUFSNJOFTUIBUBOBEEJOIBTOµUCFFOQSFMPBEFECZTFBSDIJOHUIF),&:@$633&/5@
64&3=4PGUXBSF=.JDSPTPGU=7JTVBM4UVEJP==1SFMPBE"EEJO4UBUF.BOBHFESFHJTUSZLFZ
XIJDIIPMETBMJTUPGBMSFBEZQSFMPBEFEBEEJOT*GUIF1SFMPBE"EEJO4UBUF.BOBHFELFZJT
NJTTJOHPSUIFBEEJOJTOµUPOUIFMJTU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ahead
5IFOFYUDIBQUFSJTGPSUIBUMJUUMFCJUPGESJMMJOTUSVDUPSJOTJEFBMMPGVT±UIFQBSUUIBUXBOUTUP
IFBSPUIFSTTIPVU²)08)*() ³XIFOXFTBZ²+6.1³"TZPVµMMTPPOMFBSONBDSPTBEE
JOTBOE7JTVBM4UVEJPNBLFUIFNPTUMPZBMBOEPCFEJFOUPGTPMEJFSTBMXBZTBUUIFSFBEZBOE
FBHFSUPEPZPVSCJEEJOHBMMZPVIBWFUPEPJTHJWFUIFSJHIUDPNNBOET*GZPVUVSOUIFQBHF
XFµMMTIPXZPVIPX©²/08³
Chapter 7
Exploring Commands
Programmatically
In this chapter:
What Is a Command? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating an Add-In Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Command User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Regenerating Commands and Their User Interface . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
131
135
144
150
151
$PNNBOETBSFUIFNPTUGVOEBNFOUBMNFDIBOJTNPGDPNNVOJDBUJPOCFUXFFOUIFVTFSBOE
UIF.JDSPTPGUˆ7JTVBM4UVEJPˆJOUFHSBUFEEFWFMPQNFOUFOWJSPONFOU*%&
*OUIJTDIBQUFS
XFµMMFYQMPSFIPXZPVDBOVTFFYJTUJOHDPNNBOETBTXFMMBTDSFBUFZPVSPXODPNNBOETCZ
VTJOHBEEJOTBOENBDSPT
What Is a Command?
*GZPVµWFXSJUUFOVTFSJOUFSGBDFTPGUXBSFGPSUIF.JDSPTPGU8JOEPXTˆPQFSBUJOHTZTUFN
ZPVµSFQSPCBCMZGBNJMJBSXJUIUIFFWFOUESJWFOQSPHSBNNJOHNPEFM8IFOUIFVTFSDMJDLT
BCVUUPOPOBG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PNNBOE
Note A command in Visual Studio exists independently of any user interface elements
(such as menu items) for that command. Commands can be created and destroyed, and a
user interface element might have never been created for that command. But the opposite
won’t happen—a user interface element can’t be created without having a corresponding
command.
Locating Commands
*O7JTVBM4UVEJPBMMUIFDPNNBOETUIBUBVTFSDBOJTTVFBSFSFQSFTFOUFEJOUIFPCKFDUNPEFM
CZB%QOOCPFPCKFDUBOEUIF%QOOCPFUDPMMFDUJPODPOUBJOTBMJTUPGUIFTFPCKFDUT"TEP
PUIFSDPMMFDUJPOPCKFDUT%QOOCPFPCKFDUTBMMPXUIFVTFPGTUBOEBSEFOVNFSBUJPODPOTUSVDUT
TVDIBTUIFLFZXPSETHQTGCEJJO7JTVBM$PS(QT'CEJJO7JTVBM#BTJD6TJOHUIFTFLFZXPSET
XFDBODSFBUFBNBDSPUPXBMLUIFMJTUPGBMM%QOOCPFPCKFDUT
Sub WalkCommands()
Dim cmd As EnvDTE.Command
For Each cmd In DTE.Commands
'use the EnvDTE.Command object here
Next
End Sub
5IF%QOOCPFDPMMFDUJPOµT+VGONFUIPEXPSLTBCJUEJGGFSFOUMZGSPNUIF+VGONFUIPETPG
PUIFSDPMMFDUJPOPCKFDUT%QOOCPFU+VGOBDDFQUTBTBQBSBNFUFSUIFGBNJMJBSOVNFSJDBMJOEFY
CVUJUBMTPBDDFQUTBOBEEJUJPOBMPQUJPOBMBSHVNFOU*GZPVµSFVTJOHUIFOVNFSJDBMJOEFYJOH
NFUIPEZPVTIPVMETFUUIFTFDPOEBSHVNFOUUP5IJTNFUIPEIBTBOBEEJUJPOBMBSHVNFOU
CFDBVTFBTNFOUJPOFEFBSMJFSB(6*%BOE*%QBJSJTVTFEUPVOJRVFMZJEFOUJGZBDPNNBOE
5IF(6*%JOTUSJOHGPSNBUJTQBTTFEBTUIF¾STUBSHVNFOUBOEUIF*%PGUIFDPNNBOEJT
QBTTFEBTUIFTFDPOEBSHVNFOUXIFOZPVµSFVTJOHUIF(6*%BOE*%UPJOEFYUIF%QOO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SPNUIJTTUSJOH4PUPVTFUIFFBSMJFSFYBNQMFPG¾OEJOHUIF%QOOCPFPCKFDUGPS
UIFPQFO¾MFDPNNBOEBOEDPNCJOFJUXJUIPVSOFXMZGPVOEXBZPGVTJOHBDPNNBOE
OBNFBNBDSPTVDIBTUIFGPMMPXJOHSFTVMUT
Sub FindFileOpenCommandByName()
Dim command As EnvDTE.Command
command = DTE.Commands.Item("File.OpenFile")
End Sub
5P¾OEUIF(6*%BOE*%QBJSPGBDPNNBOEZPVDBOVTFUIF)7+&BOE+&QSPQFSUJFTPGUIF
%QOOCPFPCKFDU8FVTFEUIFTFUXPQSPQFSUJFTUP¾OEUIF(6*%BOE*%QBJSVTFEJOUIF
(KPF(KNG1RGP%QOOCPFNBDSPTIPXOFBSMJFS5IJTJTUIFNBDSPXFVTFEUP¾OEUIFN
Sub FindGuidIDPair()
Dim guid As String
Dim id As Integer
Dim command As EnvDTE.Command
command = DTE.Commands.Item("File.OpenFile")
guid = command.Guid
id = command.ID
MsgBox(guid + ", " + id.ToString())
End Sub
:PVDBOTFFUIFFOUJSFMJTUPGBMMBWBJMBCMFDPNNBOETGSPNXJUIJOUIF0QUJPOTEJBMPHCPY
POUIF&OWJSPONFOU],FZCPBSEQBHF:PVDBOBMTPVTFUIFPCKFDUNPEFMUP¾OEBWBJMBCMF
134
Working with Microsoft Visual Studio 2005
DPNNBOEOBNFT8FµMMEPUIJTXJUIUIF'PX&6'%QOOCPFUDPMMFDUJPOJOUIJTFYBNQMF
NBDSP
Sub CreateCommandList()
Dim command As EnvDTE.Command
Dim output As New OutputWindowPaneEx(DTE, "Create Command List")
For Each command In DTE.Commands
If (command.Name <> Nothing) Then
output.WriteLine(command.Name)
End If
Next
End Sub
8IFOUIFNBDSPJTSVOJUQMBDFTJOUPUIF0VUQVUXJOEPXUIFOBNFPGFBDIDPNNBOE*G
ZPVFYBNJOFUIFNBDSPDMPTFMZFOPVHIZPVµMMOPUJDFBTQFDJBMDIFDLUPWFSJGZUIBU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FEXJUI7JTVBM
4UVEJPUBLFTPOUIFOBNF/CETQU5CORNGU7VKNKVKGU6WTP1P.KPG0WODGTU:PVDBOFOUFSUIJT
OBNFJOUIF$PNNBOE8JOEPXPSDBMMJUGSPNBOPUIFSNBDSPMJLFTP
Sub RunCommand()
DTE.ExecuteCommand("Macros.Samples.Utilities.TurnOnLineNumbers")
End Sub
Creating an Add-In Command
/PXUIBUZPVLOPXIPXDPNNBOETBSFOBNFEGPVOEBOESVOJUµTUJNFUPTFFIPXZPVDBO
DSFBUFZPVSPXODPNNBOET"TXFTBXFBSMJFSXIFOBDPNNBOECVJMUJOUP7JTVBM4UVEJPJT
JOWPLFEUIFBEEPOQSPHSBNGPSUIBUDPNNBOEJTMPDBUFECFDBVTFPGUIF(6*%BTTJHOFE
UPUIFDPNNBOEBOEJUJTBTLFEUPIBOEMFUIFDPNNBOEJOWPDBUJPO-JLFXJTFDPNNBOET
UIBUZPVDSFBUFOFFEBUBSHFUUIBUIBOEMFTUIFDPNNBOEJOWPDBUJPO$PNNBOETDBOCF
EZOBNJDBMMZDSFBUFEBOESFNPWFECVUDSFBUJOHUIFNSFRVJSFTUIBUBOBEEJOCFBTTPDJBUFE
XJUIUIFOFXDPNNBOETP7JTVBM4UVEJPDBO¾OEBOEVTFUIBUBEEJOBTUIFUBSHFU5IF
NFUIPEUPDSFBUFBDPNNBOE#FF0COGF%QOOCPFDBOCFGPVOEPOUIF%QOOCPFU
DPMMFDUJPOPCKFDU)FSFJTJUTTJHOBUVSF
public EnvDTE.Command AddNamedCommand2(EnvDTE.AddIn AddInInstance,
string Name, string ButtonText, string Tooltip, bool MSOButton,
int Bitmap = 0, ref object[] ContextUIGUIDs,
int vsCommandStatusValue = 3, int CommandStyleFlags = 3,
EnvDTE80.vsCommandControlType ControlType = 2)
BOEIFSFBSFUIFBSHVNFOUT
N
"EE*O*OTUBODF 5IF#FF+PPCKFDUUIBUXJMMBDUBTUIFDPNNBOEJOWPDBUJPOUBSHFU
N
/BNF 5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POOFDUUPBOFWFOUIBOEMFS
5IFJOUFSGBDFUPIBOEMFDPNNBOEJOWPDBUJPOTOBNFE+&6%QOOCPF6CTIGVJTNPEFMFE
POUIF+1NG%QOOCPF6CTIGVJOUFSGBDFPGUIF8JO4%,CVU+&6%QOOCPF6CTIGVIBTCFFO
DIBOHFEUPCFFBTJFSUPVTFXJUIMBOHVBHFTTVDIBT$PS7JTVBM#BTJD5IJTJTJUTTJHOBUVSF
public interface IDTCommandTarget
{
public void Exec(string CmdName,
EnvDTE.vsCommandExecOption ExecuteOption, ref object VariantIn,
ref object VariantOut, ref bool Handled);
public void QueryStatus(string CmdName,
EnvDTE.vsCommandStatusTextWanted NeededText,
ref EnvDTE.vsCommandStatus StatusOption,
ref object CommandText);
}
8IFOUIFTFUXPNFUIPETBSFDBMMFEBOEXIJDIWBMVFTBSFQBTTFEUPUIFNEFQFOEPO
UIFUZQFPGDPNNBOEUIBUZPVBSFBEEJOHUP7JTVBM4UVEJP5IFTJNQMFTUDBTFJTXIFO
ZPVQBTTUIFWBMVFXU%QOOCPF%QPVTQN6[RG$WVVQPGPSUIF%QPVTQN6[RGQBSBNFUFSGPS
#FF0COGF%QOOCPFBOEXFXJMM¾STUEJTDVTTUIFTFUXPNFUIPETJOUFSNTPGUIJT
DPNNBOEUZQF8IFOJOWPLFEBMMDPNNBOETUIBUZPVSBEEJODSFBUFTBSFEJTQBUDIFE
138
Working with Microsoft Visual Studio 2005
UISPVHIUIJTJOUFSGBDFQBSUJDVMBSMZUISPVHIUIF'ZGENFUIPE5IF'ZGENFUIPEIBTUIF
GPMMPXJOHBSHVNFOUT
N
$NE/BNF 5IFGVMMOBNFPGUIFDPNNBOE:PVSBEEJOTIPVMEEPBDBTFTFOTJUJWF
DPNQBSFPOUIJTTUSJOHUPEFUFSNJOFXIJDIDPNNBOEJTCFJOHBTLFEUPSVOCFDBVTFBMM
DPNNBOETUIBUUIFBEEJODSFBUFTBSFTFOUUPUIJTNFUIPEGPSIBOEMJOH
N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XU%QOOCPF5VCVWU7PUWRRQTVGFJTQBTTFECBDLUPUIF
DBMMFS*GUIFDPNNBOEOBNFJTJOUIFBQQSPQSJBUFSBOHFJUTFUTUIFEQOOCPF6GZVQBSBNFUFS
UPBTUSJOHUIBUXJMMCFEJTQMBZFEPOUIFNFOVJUFNGPSUIBU.36JUFNTFUTUIFTUBUVT
QBSBNFUFSUPBWBMVFJOEJDBUJOHUIFDPNNBOEJTBWBJMBCMFBOEUIFOSFUVSOT
string []MRUItemNames = new string[]
{"Item 1", "Item 2", "Item 3", "Item 4" };
void QueryStatus(string commandName,
vsCommandStatusTextWanted neededText,
ref vsCommandStatus status, ref object commandText)
{
if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
int index = 0;
string rootCommandName = "MyAddin.Connect.MRUButton";
if (commandName.StartsWith(rootCommandName))
{
//Check for the main command of the group
if ("MyAddin.Connect.MRUButton" == commandName)
{
index = 0;
}
else //Command is of the form MyAddin.Connect.MRUButton_*
{
index = int.Parse(commandName.Substring(rootCommandName.Length + 1));
}
if (index > 3)
//If the command index is out of the
//range of MRU items supported...
{ //then the command is should not show
status = vsCommandStatus.vsCommandStatusUnsupported;
return;
}
else
{ //then show the command, and set the text of the item
status = (vsCommandStatus)
vsCommandStatus.vsCommandStatusSupported |
vsCommandStatus.vsCommandStatusEnabled;
commandText = MRUItemNames[index];
return;
}
}
}
}
7JTVBM4UVEJPNBZBMTPDBMMUIF3WGT[5VCVWUNFUIPEKVTUUPSFUSJFWFUIFUFYUPOUIFNFOV
JUFNBOEOPUSFRVJSFUIFTUBUVTPGUIFDPNNBOE*OUIJTDBTFUIFPGGFGF6GZVQBSBNFUFSXJMM
Chapter 7:
Exploring Commands Programmatically
141
CFTFUUPUIFWBMVFXU%QOOCPF5VCVWU6GZV9CPVGF0COG:PVDBODIBOHFUIFBGPSFNFOUJPOFE
3WGT[5VCVWUUPBMTPDIFDLGPSUIJTWBMVFBOECFDBVTFCPUITUBUVTBOEEQOOCPF6GZVBSFTFU
TFUUJOHPOFWBMVFXIFOJUJTOPUOFFEFEEPFTOPUDBVTFBOZTJEFFGGFDUT±7JTVBM4UVEJPKVTU
JHOPSFTJU
JOUIJTDPEFZPVXJMMIBOEMFCPUIDBTFT
void QueryStatus(string commandName,
vsCommandStatusTextWanted neededText,
ref vsCommandStatus status, ref object commandText)
{
if ((neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone) ||
(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedName))
{
//TODO: Code to handle the QueryStatus
}
}
5IF'ZGENFUIPEGPSUIJTDPOUSPMUZQFJTWFSZNVDIMJLFUIF'ZGENFUIPEGPSB
XU%QOOCPF6[RG$WVVQPDPNNBOEUZQFFYDFQUUIBUZPVXJMMIBWFNVMUJQMFTJNJMBSMZOBNFE
JUFNTCFJOHQBTTFEUPUIF'ZGENFUIPE$PEFUP¾OEXIJDIJUFNXBTFYFDVUFEXJMMMPPL
TJNJMBSUPUIBUJOUIF3WGT[5VCVWUNFUIPEFYDFQUSBUIFSUIBOSFUVSOJOHUIFUFYUBOETUBUVT
PGUIFDPNNBOEZPVXJMMQFSGPSNUIFBQQSPQSJBUFBDUJPOGPSUIBUDPNNBOE
void Exec(string commandName,
vsCommandExecOption executeOption,
ref object varIn, ref object varOut, ref bool handled)
{
string rootCommandName = "MyAddin.Connect.MRUButton";
handled = false;
if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if (commandName == rootCommandName)
{
//Do the operation for MRUItemNames[0]
handled = true;
return;
}
else if (commandName.StartsWith(rootCommandName))
{
int index = int.Parse(commandName.Substring(rootCommandName.Length + 1));
//Do the operation for MRUItemNames[index]
handled = true;
return;
}
}
}
Drop-Down Combo Boxes and MRU Combo Boxes
5IFTFDPNNBOEUZQFTBSFVTFEUPJNQMFNFOUBDPNCPCPYPOBDPNNBOECBS5IF
EJGGFSFODFCFUXFFOUIFUXPDPNNBOEUZQFTJTJOIPXUIFVTFSJOUFSBDUTXJUIU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FF0COGF%QOOCPFNFUIPEJTDBMMFEUIFGVMMZRVBMJ¾FEOBNFPGUIFDMBTTJNQMFNFOUJOH
UIFBEEJOJOUIFGPSNPG0COGURCEG%NCUU0COGJTDPNCJOFEXJUIUIFOBNFPGUIF
DPNNBOEZPVTVQQMZ4PJGUIFDMBTTOBNFJT/[#FFKP%QPPGEVBOEUIFDPNNBOEOBNF
JT&TQR&QYP%QODQUIFSFTVMUJOHOBNFPGUIF¾STUDPNNBOEUIBUJTDSFBUFEJT/[#FFKP
%QPPGEV&TQR&QYP%QODQ5IFTFDPOEDPNNBOEIBTUIJTTBNFOBNFFYDFQUUIFUFYU²@³
JTBQQFOEFENBLJOHUIFDPNNBOEOBNF/[#FFKP%QPPGEV&TQR&QYP%QODQA8IFOUIF
NFUIPE'ZGEJTDBMMFEXJUIBDPNNBOEOBNFPG/[#FFKP%QPPGEV&TQR&QYP%QODQZPVXJMM
OFFEUPDIFDLUIFWBMVFTPGUIFQBSBNFUFSTXCT+PBOEXCT1WVUPEFUFSNJOFJGUIFMJTUPGJUFNT
UP¾MMJOUIFESPQEPXOQPSUJPOJTCFJOHBTLFEGPSPSJGBTFMFDUJPOXBTNBEFXJUIJOUIF
ESPQEPXO*GUIFWBMVFPGXCT+PJTOPUPWNN0QVJKPIBOEJGUIFWBMVFJUDPOUBJOTJTBTUSJOHB
TFMFDUJPOXBTNBEFXJUIJOUIFESPQEPXO)PXFWFSJGUIFXCT1WVWBMVFJTOPUPWNN0QVJKPI
ZPVSBEEJOJTCFJOHRVFSJFEGPSUIFDVSSFOUMZTFMFDUFEJUFNUFYUUPTIPXJOUIFESPQEPXO
BOEZPVTIPVMETFUJUUPBTUSJOH)FSFJTBOFYBNQMFPGUIFDPEFJNQMFNFOUJOHUIJT¾STU
DPNNBOE
void Exec(string commandName,
vsCommandExecOption executeOption,
ref object varIn, ref object varOut, ref bool handled)
{
if (commandName == "MyAddin.Connect.DropDownCombo")
{
if ((varIn != null) && (varIn is string))
{
//The command was executed, retrive the selected text.
string selectedText = varIn as string;
//Perform some operation on the selected text.
}
else
{
//The selected text is being asked for, return that here.
varOut = "Item 1";
}
Chapter 7:
Exploring Commands Programmatically
143
handled = true;
return;
}
}
*GUIF'ZGENFUIPEJTDBMMFEXJUIUIFDPNNBOEOBNFPGUIFTFDPOEDPNNBOEUIFXCT1WV
WBMVFXJMMOPUCFPWNN0QVJKPIBOEZPVOFFEUPQBTTCBDLBOBSSBZDPOUBJOJOHTUSJOHTPG
JUFNTUPTIPX5IJTDPEFXJMM¾MMJOUIFESPQEPXOQPSUJPOXJUIGPVSJUFNT
if (commandName == "MyAddin.Connect.DropDownCombo_1")
{
//Set the list of items the user can select.
varOut = new string[] { "Item 1", "Item 2", "Item 3", "Item 4" };
handled = true;
return;
}
5IFJUFNTJOBO.36DPNCPCPYBSFOPU¾MMFEJOCZDBMMJOHUIFBEEJOSBUIFSUIFJUFNTBSF
¾MMFEJOCZUIFVTFS5IFPOMZDPEFUIBUZPVOFFEUPXSJUFUPIBOEMFUIJTDPNNBOEUZQF
XIJDIJTXU%QOOCPF%QPVTQN6[RG/47%QODQJTUIF3WGT[5VCVWUDPEFUPJOEJDBUFXIFUIFSUIF
DPNNBOEJTFOBCMFEBOEUIF'ZGEDPEFUPIBOEMFTFMFDUJPOXJUIJOUIFESPQEPXO'PSUIF
'ZGENFUIPEUIFXCT+PQBSBNFUFSDPOUBJOTUIFUFYUPGUIFTFMFDUFEJUFNXIFOBTFMFDUJPO
JTNBEF8IFO7JTVBM4UVEJPDMPTFTJUXJMMBVUPNBUJDBMMZTBWFBMMUIFJUFNTUIBUUIFVTFS
FOUFSFEJOUPUIF.36DPNCPCPYBOEUIFOJUXJMMSFTUPSFUIFNUIFOFYUUJNF7JTVBM4UVEJP
JTTUBSUFE
-PDBUFEXJUIJOUIFTBNQMFTUIBUBDDPNQBOZUIJTCPPLUIFBEEJOTBNQMFOBNFE
$PNNBOE5ZQFTEFNPOTUSBUFTIPXUPVTFFBDIPGUIFTFDPNNBOEUZQFT
Programmatically Determining Command State
"UUJNFTZPVNJHIUOFFEUPQSPHSBNNBUJDBMMZEFUFSNJOFXIFUIFSBDPNNBOEJTFOBCMFE
BOEDBOCFJOWPLFETVDIBTXIFOZPVXBOUUPJOWPLFBDPNNBOECZVTJOH&6'
'ZGEWVG%QOOCPF"MMDPNNBOETXIFUIFSBNBDSPDPNNBOEPOFDSFBUFECZBOBEEJO
PSPOFCVJMUJOUP7JTVBM4UVEJPTVQQPSUB3WGT[5VCVWUNFUIPE8IFOZPVJOWPLFUIF&6'
'ZGEWVG%QOOCPFCVUUIFDPNNBOEJTOµUFOBCMFECFDBVTFUIF3WGT[5VCVWUNFUIPESFUVSOFE
BWBMVFJOEJDBUJOHUIBUJUJTOµUDVSSFOUMZBWBJMBCMFZPVµMMHFUBOFYDFQUJPOJGZPVµSFVTJOHB
MBOHVBHFTVQQPSUFECZUIF/&5'SBNFXPSL
5PDIFDLXIFUIFSBDPNNBOEJTFOBCMFEBOEUIVTBWPJEUIJTFSSPSDPOEJUJPOZPVDBOVTF
UIF%QOOCPF+U#XCKNCDNGQSPQFSUZ'PSFYBNQMFUPNBLFTVSFUIBUUIF$WKNF$WKNF5QNWVKQP
DPNNBOEDBOCFDBMMFECFGPSFZPVJOWPLFJUZPVDBOVTFUIFGPMMPXJOHDPEF
Sub CheckAvailability()
If (DTE.Commands.Item("Build.BuildSolution").IsAvailable = True) Then
DTE.ExecuteCommand("Build.BuildSolution")
End If
End Sub
144
Working with Microsoft Visual Studio 2005
How an Add-In Command Handler Is Found
8IFOBVTFSJOWPLFTZPVSDPNNBOE7JTVBM4UVEJPOFFETUPLOPXXIJDIBEEJOIBOEMFTUIBU
DPNNBOETPJUDBODBMMUIFNFUIPETPGUIF+&6%QOOCPF6CTIGVJOUFSGBDF*U¾STUJOTQFDUTUIF
DPNNBOEOBNFBTOPUFEFBSMJFSUIF¾STUQBSUPGUIFGVMMDPNNBOEOBNFJTUIF/BNFTQBDF
$MBTT/BNFPGUIF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FODFBDSPTTBMMPG
UIF0G¾DFBQQMJDBUJPOTBTXFMMBT7JTVBM4UVEJP#FDBVTFUIFDPNNBOECBSTBMTPTVQQPSUBO
PCKFDUNPEFMUIFTFBQQMJDBUJPOTBMTPTIBSFBDPNNPOQSPHSBNNJOHNPEFMGPSBDDFTTJOHUIF
DPNNBOECBSTUSVDUVSF
5IFNBJOQPJOUPGBDDFTTUPUIFDPNNBOECBSPCKFDUTJTUISPVHIUIF&6'%QOOCPF$CTU
QSPQFSUZ5IJTQSPQFSUZSFUVSOTB5[UVGO1DLGEVUZQFXIJDIDBOCFDPOWFSUFEJOUPB/KETQUQHV
8KUWCN5VWFKQ%QOOCPF$CTU%QOOCPF$CTUPCKFDUXIJDIJTEF¾OFEJOUIFBTTFNCMZ.JDSPTPGU
7JTVBM4UVEJP$PNNBOE#BSTEMM5IFGPMMPXJOHNBDSPDPEFEFNPOTUSBUFTSFUSJFWJOHUIJTPCKFDU
Sub GetCommandBars()
Dim commandBars As Microsoft.VisualStudio.CommandBars.CommandBars
commandBars = DTE.CommandBars
End Sub
Chapter 7:
Exploring Commands Programmatically
145
The Command Bar Object Model
5IFDPNNBOECBSPCKFDUNPEFMJTBSSBOHFEJOBUSFFMJLFIJFSBSDIZJOUIFTBNFXBZBTUIF
7JTVBM4UVEJPPCKFDUNPEFM"UUIFUPQPGUIJTUSFFJTBDPMMFDUJPOPG/KETQUQHV8KUWCN5VWFKQ
%QOOCPF$CTU%QOOCPF$CTPCKFDUTUIBUJODMVEFTBMMUIFDPNNBOECBSTTIPSUDVUNFOVT
BOEUIFNBJONFOVCBS&BDIDPNNBOECBSDPOUBJOTBDPMMFDUJPOPGDPOUSPMTUIBUIBWFUIF
UZQF/KETQUQHV8KUWCN5VWFKQ%QOOCPF$CTU%QOOCPF$CT%QPVTQN0ODFB%QOOCPF$CT%QPVTQN
JTSFUSJFWFEJUDBOCFDPOWFSUFEJOUPPOFPGBOVNCFSPGEJGGFSFOUUZQFT0OFUZQFB
%QOOCPF$CT$WVVQPJTBOZNFOVJUFNPOBDPNNBOECBSUIBUUIFVTFSDBODMJDLUP
QFSGPSNBOBDUJPOUIJTJTBOBMPHPVTUPFYFDVUJOHB7JTVBM4UVEJPDPNNBOE5PHFUUPB
%QOOCPF$CT$WVVQPPCKFDUBDBTUNVTUCFQFSGPSNFEGSPNUIF%QOOCPF$CT%QPVTQNPCKFDU
Sub GetCommandBarButton()
Dim commandBarBtn As Microsoft.VisualStudio.CommandBars.CommandBarButton
Dim commandBarCtl As Microsoft.VisualStudio.CommandBars.CommandBarControl
Dim commandBarCtls As Microsoft.VisualStudio.CommandBars.CommandBarControls
'Find the View command bar
commandBarCtls = DTE.CommandBars.Item("View").Controls
'Retrieve the first control on the menu
commandBarCtl = commandBarCtls.Item(1)
'Convert the CommandBarControl to a CommandBarButton object
commandBarBtn = CType(commandBarCtl, _
Microsoft.VisualStudio.CommandBars.CommandBarButton)
MsgBox(commandBarBtn.Caption)
End Sub
5IFPCKFDUSFUVSOFEGSPNUIF%QPVTQNUDPMMFDUJPODBOCFDPOWFSUFEJOUPB%QOOCPF$CT2QRWR
JGUIFJUFNJTUIFSPPUOPEFPGBTVCNFOV"OFYBNQMFPGUIJTJTUIF/FXJUFNPOUIF'JMF
NFOVXIFOUIFVTFSIPMETUIFNPVTFDVSTPSPWFSUIJTNFOVBTVCNFOVBQQFBST:PVDBO
BMTPSFUSJFWFB%QOOCPF$CT2QRWRXIFOUIFJUFNJTPOBTQMJUCVUUPOESPQEPXONFOVTVDI
BTUIF/FX1SPKFDU]/FX#MBOL4PMVUJPOCVUUPOPOUIF4UBOEBSEDPNNBOECBS
Sub GetCommandBarPopup()
Dim commandBar As Microsoft.VisualStudio.CommandBars.CommandBar
Dim cmdBarControl As Microsoft.VisualStudio.CommandBars.CommandBarControl
Dim cmdBarPopup As Microsoft.VisualStudio.CommandBars.CommandBarPopup
'Find the "Standard" command bar
commandBar = DTE.CommandBars.Item("Standard")
'Find the first control on the command bar
cmdBarControl = commandBar.Controls.Item(1)
'Convert the CommandBarControl to a CommandBarPopup
cmdBarPopup = CType(cmdBarControl, _
Microsoft.VisualStudio.CommandBars.CommandBarPopup)
MsgBox(cmdBarPopup.Controls.Item(1).Caption)
End Sub
"QPQVQNFOVJTJUTFMGBDPNNBOECBS:PVDBOµUDBTUEJSFDUMZUPB%QOOCPF$CTPCKFDU
POBQPQVQNFOVCVUUIJTPCKFDUEPFTDPOUBJOB%QOOCPF$CTQSPQFSUZXIJDISFUVSOTB
146
Working with Microsoft Visual Studio 2005
%QOOCPF$CTPCKFDUXIJDIJUTFMGIBTBDPMMFDUJPOPGDPOUSPMTBTZPVDBOTFFJOUIFOFYUUP
MBTUMJOFJOUIFQSFDFEJOHNBDSPDPEF
The Primary Command Bar
5IF&6'%QOOCPF$CTUQSPQFSUZSFUVSOTUIFDPMMFDUJPOPGBMM%QOOCPF$CTPCKFDUTBWBJMBCMF
XJUIJO7JTVBM4UVEJPCVUUIFNPTUDPNNPOMZVTFEDPNNBOECBSJTUIFNBJONFOV
-PPLJOHBUUIFNFOVZPVDBOTFFUIF'JMF&EJUBOE7JFXJUFNTBTXFMMBTBOVNCFSPG
BEEJUJPOBMNFOVJUFNTBMMPGUIFTFBSF%QOOCPF$CTPCKFDUTXJUIJOUIF&6'%QOOCPF$CTU
DPMMFDUJPO#VUCFDBVTFUIFSFNJHIUCFNVMUJQMFJUFNTXJUIJOUIFDPMMFDUJPOXJUIUIFTBNF
OBNFJOEFYJOHUIFDPMMFDUJPOCZVTJOHUIFOBNFNJHIUOPUXPSL'PSFYBNQMFUIFSFBSF
NVMUJQMF%QOOCPF$CTPCKFDUTXJUIUIFUJUMF7JFXBOEZPVNJHIUOPUBMXBZTHFUUIFPOFZPV
XBOUJGZPVJOEFYUIF%QOOCPF$CTUDPMMFDUJPOXJUIUIFTUSJOH8KGY5IFGPMMPXJOHNBDSP
NJHIUSFUVSOUIF7JFXDPNNBOECBSGPSUIF42-FEJUPSBEFQMPZNFOUQSPKFDUQPQVQNFOV
PSUIF7JFXNFOV
Sub GetView()
Dim cmdbars As Microsoft.VisualStudio.CommandBars.CommandBars
Dim commandBar As Microsoft.VisualStudio.CommandBars.CommandBar
cmdbars = DTE.CommandBars
commandBar = cmdbars.Item("View")
End Sub
5PXPSLBSPVOEUIJTZPVDBO¾OEUIF%QOOCPF$CTPCKFDUGPSUIFNFOVCBSDBMMFE/GPW$CT
BOEUIFO¾OEUIF7JFXTVCNFOVDPNNBOECBS
Sub GetMenuCommandBar()
Dim commandBar As Microsoft.VisualStudio.CommandBars.CommandBar
Dim cmdBarControl As Microsoft.VisualStudio.CommandBars.CommandBarControl
Dim cmdBarPopupView As Microsoft.VisualStudio.CommandBars.CommandBarPopup
Dim cmdBarView As Microsoft.VisualStudio.CommandBars.CommandBar
'Retrieve the MenuBar command bar
commandBar = DTE.CommandBars.Item("MenuBar")
'Find the View menu
cmdBarControl = commandBar.Controls.Item("View")
'Convert to a CommandBarPopup
cmdBarPopupView = CType(cmdBarControl, _
Microsoft.VisualStudio.CommandBars.CommandBarPopup)
'Get the CommandBar object for the view menu
cmdBarView = cmdBarPopupView.CommandBar
MsgBox(cmdBarView.Name)
End Sub
#ZEFGBVMUJGUIF"EEJO8J[BSEHFOFSBUFTBOBEEJOBOEUIFPQUJPOJTTFMFDUFEUPQMBDFBO
JUFNPOUIF5PPMTNFOVDPEFJTHFOFSBUFEUPQMBDFBNFOVJUFNPOUIF5PPMTNFOVPGUIF
NFOVCBS*GZPVXBOUUPNPWFUIJTDPNNBOEVTFSJOUFSGBDFUPBEJGGFSFOUNFOVZPVDBO
TJNQMZDIBOHFUIFTUSJOH6QQNUUPBEJGGFSFOUNFOVUJUMFCVUCFDBSFGVMUPTFMFDUUIFDPSSFDU
NFOV*UµTFBTZUPNBLFUIFNJTUBLFPGTFMFDUJOHUIFXSPOHDPNNBOECBSDBVTJOHUIF
Chapter 7:
Exploring Commands Programmatically
147
DPNNBOECVUUPOUPTFFNJOHMZEJTBQQFBSCFDBVTFJU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FJT
JUXJMMCFUIFTFDPOEJUFNBOETPGPSUI
:PVDBOIBSEDPEFBOJOEFYUPQMBDFUIFDPOUSPMCVUUIFDPOUSPMNJHIUOPUBQQFBSXIFSF
ZPVUIJOLJUTIPVMEHPJOSFMBUJPOUPPUIFSDPOUSPMT5IFSFBTPOJTUIBUBDPNNBOECBSNJHIU
IBWFPOFPSNPSFTFQBSBUPSTPSMJOFTESBXOCFUXFFOUXPDPOUSPMT
UIBUEJWJEFDPOUSPMTJOUP
MPHJDBMHSPVQT5IFTFHSPVQTBSFBMTPDPOUSPMTPOUIFDPNNBOECBSBOEUIFZTIPVMECF
DPVOUFEXIFOZPVDBMDVMBUFUIFQPTJUJPO/PUPOMZBSFHSPVQDPOUSPMTDPVOUFEBTJUFNTJO
UIFJOEFYCVUTPBSFDPOUSPMTUIBUBSFOPUWJTJCMFCFDBVTFUIFWBMVFXU%QOOCPF5VCVWU+PXKUKDNG
JTSFUVSOFEGSPNZPVS3WGT[5VCVWUNFUIPE*GUIFDPOUSPMUPCFBEEFETIPVMECFQMBDFEBUUIF
CPUUPNPSFOEPGUIFDPNNBOECBSZPVDBOVTFUIF%QPVTQNU%QWPVQSPQFSUZUPEFUFSNJOF
UIF¾OBMQPTJUJPO
Sub AddControl()
Dim command As EnvDTE.Command
Dim commandBar As Microsoft.VisualStudio.CommandBars.CommandBar
'Find the File.OpenFile command
command = DTE.Commands.Item("File.OpenFile")
'Find the Tools CommandBar
commandBar = DTE.CommandBars.Item("Tools")
'Add a control to the Tools menu that when
' clicked will invoke the File.OpenFile command
command.AddControl(commandBar, commandBar.Controls.Count + 1)
End Sub
/PUFUIBUUIFJOEFYVTFEEPFTOµU¾YBDPOUSPMUPBQBSUJDVMBSQPTJUJPO*GZPVBEEBDPOUSPM
UPQPTJUJPOBOEBTFDPOEDPOUSPMJTBEEFEUPQPTJUJPOUIF¾STUDPOUSPMJTQVTIFEJOUPUIF
TFDPOEQPTJUJPO
148
Working with Microsoft Visual Studio 2005
"UUJ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:PVSVOJOTUBMMQSPHSBNDBOUIFO
JTTVFUIFDPNNBOEMJOFEFWFOWSFTFUBEEJO0COGURCEG%NCUU0COGDPNNBOE'JMF&YJU
XIFSF0COGURCEG%NCUU0COGJTUIFGVMMZRVBMJ¾FEOBNFPGUIFDMBTTJNQMFNFOUJOHUIF
Chapter 7:
Exploring Commands Programmatically
151
BEEJO5IJTXJMMGPSDFBMMUIFDPNNBOETUPCFEFMFUFEGPSUIFBEEJOBOEUIFDPNNBOE
'JMF&YJUDPNNBOEMJOFTXJUDIXJMMGPSDF7JTVBM4UVEJPUPDMPTFXIFOUIFDPNNBOETIBWF
CFFOEFMFUFE#FDBVTFUIFBEEJO¾MFJTOPMPOHFSQSFTFOUUIFDPNNBOETGPSUIBUBEEJOXJMM
OPUCFSFDSFBUFE
Looking Ahead
*OUIFOFYUDIBQUFSXFµMMGPDVTPOVTJOHUIFPCKFDUNPEFMUPDSFBUFBOENPEJGZTPMVUJPOT
BOEQSPKFDUTUIBUBSFMPBEFEJOUP7JTVBM4UVEJP8FµMMBMTPMPPLBUIPXUPXPSLXJUIUIPTF
TPMVUJPOTTVDIBTDIBOHJOHIPXBTPMVUJPOBOEQSPKFDUTXJUIJOUIFTPMVUJPOBSFDPNQJMFE
JOUPBSVOOJOHQSPHSBN
Chapter 8
Managing Solutions and
Projects Programmatically
In this chapter:
Working with Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Project Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Language-Specific Project Objects . . . . . . . . . . . . . . . . . . . . . . . . .
Using Visual Studio Utility Project Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project and Project Item Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Managing Build Configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Persisting Solution and Project Information Across IDE Sessions . . . . . . . . . .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UFECZUIF'PX&6'5QNWVKQP
PCKFDUXIJDIZPVDBOSFUSJFWFVTJOHUIF5QNWVKQPQSPQFSUZPGUIF&6'PCKFDUBTTIPXOJOUIF
GPMMPXJOHNBDSP
Sub GetSolution()
Dim solution As EnvDTE.Solution
solution = DTE.Solution
End Sub
153
154
Working with Microsoft Visual Studio 2005
Creating, Loading, and Unloading Solutions
5PVTFUIF5QNWVKQPPCKFDUBOEJUTNFUIPETBOEQSPQFSUJFTZPVEPOµUOFFEUPDSFBUFPSPQFO
BTPMVUJPO¾MFGSPNEJTL:PVDBOVTFUIF5QNWVKQPPCKFDUFWFOUIPVHIUIFTPMVUJPOOPEFJO
4PMVUJPO&YQMPSFSNJHIUOPUCFWJTJCMF7JTVBM4UVEJPBMXBZTIBTBTPMVUJPOPQFOFWFOJG
JUFYJTUTPOMZJONFNPSZBOEOPUPOEJTL*GZPVPQFOBTPMVUJPO¾MFGSPNEJTLBOEUIFJO
NFNPSZTPMVUJPOJTOPUEJSUZNPEJ¾FECVUOPUTBWFEUPEJTL
UIJTJONFNPSZTPMVUJPOJT
EJTDBSEFEBOEUIFTPMVUJPOPOEJTLJTMPBEFE*GUIFJONFNPSZTPMVUJPOIBTCFFONPEJ¾FE
TVDIBTCZIBWJOHBOFXPSFYJTUJOHQSPKFDUBEEFE
XIFOZPVDMPTFJUZPVµMMCFQSPNQUFE
UPTBWFUIFTPMVUJPOUPEJTL
5PTBWFBTPMVUJPOQSPHSBNNBUJDBMMZZPVDBOVTFUIFNFUIPE5QNWVKQP5CXG#UZPVQBTT
JUUIFGVMMQBUIJODMVEJOHUIF¾MFOBNFBOEUIFTMO¾MFFYUFOTJPOUPXIFSFUIFTPMVUJPO
TIPVMECFTUPSFEPOEJTL)PXFWFSVTJOHUIF5QNWVKQP5CXG#UNFUIPENJHIUOPUBMXBZT
XPSLBOEDBOHFOFSBUFBOFYDFQUJPOCFDBVTFZPVNVTU¾STUTBWFBTPMVUJPO¾MFUPEJTL
PSMPBEJUGSP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SPNUIF&6'+VGO1RGTCVKQPUQSPQFSUZDPOUBJOTWBSJPVT¾MFNBOJQVMBUJPO
NFUIPETBOEQSPQFSUJFT0OFQSPQFSUZPGUIJTPCKFDU2TQORV6Q5CXGEJTQMBZTBEJBMPH
CPYUIBUHJWFTZPVUIFPQUJPOUPTBWFNPEJ¾FE¾MFTBOESFUVSOTBWBMVFJOEJDBUJOHXIJDI
CVUUPOXBTDMJDLFE5IJTQSPQFSUZBMTPTBWFTUIF¾MFTGPSZPVJGUIFBQQSPQSJBUFVTFS
JOUFSGBDFCVUUPOJTTFMFDUFE5IJTQSPQFSUZXPOµUTIPXUIFEJBMPHCPYJGOP¾MFTOFFEUPCF
TBWFE±JUXJMMJNNFEJBUFMZSFUVSOBWBMVFJOEJDBUJOHUIBUZPVDMJDLFEUIF0,CVUUPO:PV
Chapter 8:
Managing Solutions and Projects Programmatically
155
DBODPNCJOFUIF2TQORV6Q5CXGQSPQFSUZXJUIUIF1RGPNFUIPEUPQSPQFSMZTBWFNPEJ¾FE
¾MFTBOEPQFOBTPMVUJPO
Sub OpenSolution()
Dim promptResult As vsPromptResult
'Offer to save any open and modified files:
promptResult = DTE.ItemOperations.PromptToSave
'If the user pressed anything but the Cancel button,
' then open a solution file from disk:
If promptResult <> vsPromptResult.vsPromptResultCancelled Then
DTE.Solution.Open("C:\Solution.sln")
End If
End Sub
:PVµWFMFBSOFEIPXUPDSFBUFTBWFBOEPQFOBTPMVUJPO±UIFPOMZQJFDFPGUIFMJGFDZDMFPG
BTPMVUJPOZPVIBWFOµUMFBSOFEJTIPXUPDMPTFJU5IF5QNWVKQPPCKFDUTVQQPSUTUIFNFUIPE
%NQUGXIJDIZPVDBOVTFUPDMPTFBTPMVUJPO¾MF5IJTNFUIPEBDDFQUTPOFPQUJPOBM$QQNGCP
QBSBNFUFSXIJDIZPVDBOVTFUPEJSFDU7JTVBM4UVEJPUPTBWFUIF¾MFXIFOZPVDMPTFJU*GZPV
QBTTUIFWBMVFVTWGGPS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UIFSXIFOZPVDBMMUIF+VGONFUIPE:PVDBOSFUSJFWFUIFVOJRVFOBNFPGB
QSPKFDUCZVTJOHUIF2TQLGEV7PKSWG0COGQSPQFSUZ5IFGPMMPXJOHNBDSPSFUSJFWFTUIJTWBMVF
GPSBMMUIFQSPKFDUTMPBEFEJOUPBTPMVUJPO
Sub EnumProjects()
Dim project As EnvDTE.Project
For Each project In DTE.Solution
MsgBox(project.UniqueName)
Next
End Sub
5IF5QNWVKQPPCKFDUJTOµUUIFPOMZDPMMFDUJPOPGBMMQSPKFDUTUIBUBSFMPBEFE5IF5QNWVKQP
PCKFDUIBTB2TQLGEVUQSPQFSUZXIJDIBMTPSFUVSOTBDPMMFDUJPOPGUIFBWBJMBCMFQSPKFDUTBOE
156
Working with Microsoft Visual Studio 2005
XPSLTJOUIFTBNFXBZUIBUUIF5QNWVKQPPCKFDUEPFTGPSFOVNFSBUJOHBOEJOEFYJOHQSPKFDUT
*UNJHIUTFFNSFEVOEBOUUPIBWFUIJTTBNFGVODUJPOBMJUZJOUXPQMBDFTCVUUIF7JTVBM4UVEJP
PCKFDUNPEFMUFBNBGUFSQFSGPSNJOHVTBCJMJUZTUVEJFTGPVOEUIBUEFWFMPQFSTEJEOµUSFDPHOJ[F
UIF5QNWVKQPPCKFDUBTBDPMMFDUJPO5IFUFBNUIFSFGPSFBEEFEUIJT2TQLGEVUDPMMFDUJPOUPIFMQ
EFWFMPQFST¾OEUIFMJTUPGQSPKFDUTNPSFFBTJMZ:PVDBOSFXSJUFUIF'PWO2TQLGEVUNBDSPBT
GPMMPXTTPJUDBOVTFUIF2TQLGEVUDPMMFDUJPO
Sub EnumProjects2()
Dim project As EnvDTE.Project
For Each project In DTE.Solution.Projects
MsgBox(project.UniqueName)
Next
End Sub
:PVDBO¾OEUIFMJTUPGQSPKFDUTCZVTJOHUIF5QNWVKQPBOE2TQLGEVUDPMMFDUJPOTCVUBUUJNFT
ZPVµMMOFFEUP¾OEUIFQSPKFDUTUIBUZPVµWFTFMFDUFEXJUIJOUIF4PMVUJPO&YQMPSFSUSFFWJFX
XJOEPX5IF&6'#EVKXG5QNWVKQP2TQLGEVUQSPQFSUZXIFODBMMFEMPPLTBUUIFJUFNTTFMFDUFE
XJUIJO4PMVUJPO&YQMPSFS*GBQSPKFDUOPEFJTTFMFDUFEUIF2TQLGEVPCKFDUGPSUIBUTFMFDUFE
QSPKFDUJTBEEFEUPBMJTUPGPCKFDUTUIBUXJMMCFSFUVSOFE*GBQSPKFDUJUFNJTTFMFDUFEUIF
QSPKFDUDPOUBJOJOHUIBUJUFNJTBMTPBEEFEUPUIFMJTUSFUVSOFE'JOBMMZBOZEVQMJDBUFTBSF
SFNPWFEGSPNUIFMJTUBOEUIFMJTUJTSFUVSOFE5IFGPMMPXJOHNBDSPEFNPOTUSBUFTUIFVTFPG
UIJTQSPQFSUZ
Sub FindSelectedProjects()
Dim selectedProjects As Object()
Dim project As EnvDTE.Project
selectedProjects = DTE.ActiveSolutionProjects
For Each project In selectedProjects
MsgBox(project.UniqueName)
Next
End Sub
Adding Projects to a Solution
8IJMFZPVDBOVTFUIFPCKFDUNPEFMUPFOVNFSBUFQSPKFDUTXJUIJOBTPMVUJPOUIFSFNBZ
CFUJNFTXIFOZPVOFFEUPBEEBOFXPSFYJTUJOHQSPKFDUUPUIFTPMVUJPO:PVBEEQSPKFDUT
UPUIFTPMVUJPOCZVTJOHUIF#FF(TQO6GORNCVGBOE#FF(TQO(KNGNFUIPETPOUIF5QNWVKQP
PCKFDU#FF(TQO(KNGUBLFTBQBUIUPBOFYJTUJOHQSPKFDUPOEJTLBOEJOTFSUTUIBUQSPKFDUJOUP
UIFTPMVUJPO#FF(TQO6GORNCVGXJMMDSFBUFBOFXQSPKFDUXJUIJOUIFTPMVUJPOCBTFEVQPO
B745FNQMBUF¾MF±UIFTBNFUFNQMBUFTUIBUXFTIPXFEZPVIPXUPDSFBUFJO$IBQUFS
$BMMJOHUIJTNFUIPEXJMMJOWPLFUIFUFNQMBUFXJ[BSEDBVTJOHUIFQSPKFDUUPCFDPQJFEJOUP
BEFTUJOBUJPOGPMEFSUIBUZPVTQFDJGZBOEDBVTJOHBMMSFQMBDFNFOUUPLFOTXJUIJOUIF¾MFTGPS
UIBUQSPKFDUUPCFSFQMBDFEXJUIUIFBQQSPQSJBUFWBMVFT5IFTJHOBUVSFGPS#FF(TQO6GORNCVGJT
public EnvDTE.Project AddFromTemplate(string FileName,
string Destination, string ProjectName,
bool Exclusive = false)
Chapter 8:
Managing Solutions and Projects Programmatically
157
)FSFBSFUIFBSHVNFOUT
N
'JMF/BNF 5IFGVMMQBUIUPUIFQSPKFDUUFNQMBUF
N
%FTUJOBUJPO 5IFMPDBUJPOPOEJTLUPXIJDIUIFQSPKFDUBOEUIF¾MFTJUSFGFSFODFTBSF
DPQJFE5IFXJ[BSETIPVMEDSFBUFUIJTEFTUJOBUJPOQBUICFGPSF#FF(TQO6GORNCVGJT
DBMMFE
N
1SPKFDU/BNF 5IFOBNFBTTJHOFEUPUIFQSPKFDU¾MFBOEUIFOBNFJO4PMVUJPO
&YQMPSFSXIFSFJUIBTCFFODPQJFE%POµUBUUBDIUIFFYUFOTJPOPGUIFQSPKFDUUZQFUP
UIJTBSHVNFOU
N
&YDMVTJWF *GUIJTQBSBNFUFSJTTFUUPVTWGUIFDVSSFOUTPMVUJPOJTDMPTFEBOEBOFXPOF
JTDSFBUFECFGPSFUIFUFNQMBUFQSPKFDUJTBEEFE*GUIJTQBSBNFUFSJTHCNUGUIFTPMVUJPO
JTOµUDMPTFEBOEUIFOFXMZDSFBUFEQSPKFDUJTBEEFEUPUIFDVSSFOUMZPQFOTPMVUJPO
Note If the Exclusive parameter is set to true when AddFromFile or AddFromTemplate
is called, the existing solution is closed without the user being given the option to
save any modified files. You should give the user the option to save by calling the
ItemOperations.PromptToSave property before calling AddFromTemplate or AddFromFile.
:PVDBO¾OEUIFQBUIUPBUFNQMBUFUIBUJTJOTUBMMFEGPSBMMVTFSTXJUIUIFNFUIPE
5QNWVKQP)GV2TQLGEV6GORNCVG5P¾OEUIFQBUIUPUIFUFNQMBUFZPVOFFEUPTVQQMZUIF
UFNQMBUFOBNFUIBUZPVXBOUUP¾OEBTXFMMBTUIFQSPHSBNNJOHMBOHVBHFPGUIFUFNQMBUF
UIBUZPVXBOUUPBEE5IFUFNQMBUFOBNFJTUIF[JQ¾MFUIBUDPOUBJOTBMMUIF¾MFTOFDFTTBSZ
UPSFDSFBUFBQSPKFDU'PSFYBNQMFUIF.JDSPTPGU7JTVBM$ˆDPOTPMFBQQMJDBUJPOUFNQMBUFJT
OBNFE$POTPMF"QQMJDBUJPO[JQBOEUIFMBOHVBHFOBNFGPS7JTVBM$JT$4IBSQTPBNBDSP
UIBU¾OETUIJTUFNQMBUFXPVMEMPPLMJLFTP
Sub ProjectTemplatePath()
Dim solution2 As EnvDTE80.Solution2
Dim CSConsoleTemplatePath As String
solution2 = CType(DTE.Solution, EnvDTE80.Solution2)
CSConsoleTemplatePath = solution2.GetProjectTemplate(_
ConsoleApplication.zip", "CSharp")
MsgBox(CSConsoleTemplatePath)
End Sub
:PVDBODSFBUFBQSPKFDUCBTFEVQPOBUFNQMBUFTUPSFEJOUIF.Z%PDVNFOUTGPMEFSCVU
CFDBVTFUIFSFJTOPBVUPNBUFEXBZPGDBMDVMBUJOHUIFQBUIUPUIJTUFNQMBUFZPVOFFEUP
DBMDVMBUFJUZPVSTFMG8IFOB[JQ¾MFJTQMBDFEJOUPUIFBQQSPQSJBUFGPMEFSVOEFSUIF.Z
%PDVNFOUT=7JTVBM4UVEJP=1SPKFDU5FNQMBUFTGPMEFSBOE7JTVBM4UVEJPOPUJDFTUIFOFX
UFNQMBUFCFDBVTFUIF/FX1SPKFDUEJBMPHCPYIBTCFFOTIPXO
JUXJMMFYUSBDUUIBUUFNQMBUF
JOUPBDBDIFGPMEFSBU$=%PDVNFOUTBOE4FUUJOHT=WUGTPCOG="QQMJDBUJPO%BUB=.JDSPTPGU=
7JTVBM4UVEJP==1SPKFDU5FNQMBUFT$BDIF8FDBOVTFUIF/&5'SBNFXPSLNFUIPE
158
Working with Microsoft Visual Studio 2005
'PXKTQPOGPV)GV(QNFGT2CVJUP¾OEUIF¾STUQPSUJPOPGUIJTQBUICVUXFXJMMOFFEUPDPOTUSVDU
UIFSFTUBOEBEEUPJUUIFMBOHVBHFBOEUFNQMBUFOBNFPVSTFMWFTUP¾OEUIFGVMMQBUIPGUIF
WTUFNQMBUF¾MF*GZPVIBWFB.JDSPTPGU7JTVBM#BTJDˆQSPKFDUUFNQMBUFOBNFE.Z5FNQMBUF[JQ
JOUIFDPSSFDUQMBDFZPVDBO¾OEUIFQBUIUPUIFWTUFNQMBUF¾MFXJUIDPEFTVDIBTUIJT
Sub UserProjectTemplatePath()
Dim projectTemplatePath As String
projectTemplatePath = System.Environment.GetFolderPath( _
System.Environment.SpecialFolder.ApplicationData)
projectTemplatePath = System.IO.Path.Combine(projectTemplatesPath, _
"Microsoft\VisualStudio\8.0\ProjectTemplatesCache")
'projectTemplatesPath contains the path for all templates, now add to
' the path using information specific to the template to find:
projectTemplatePath = System.IO.Path.Combine(projectTemplatesPath, _
"Visual Basic\MyTemplate.zip\MyTemplate.vstemplate")
End Sub
Capturing Solution Events
"TZPVJOUFSBDUXJUIBTPMVUJPO7JTVBM4UVEJP¾SFTFWFOUTUIBUBMMPXBOBEEJOPSBNBDSPUP
SFDFJWFOPUJ¾DBUJPOTBCPVUXIJDIBDUJPOTZPVQFSGPSN5IFTFFWFOUTBSF¾SFEUISPVHIUIF
5QNWVKQP'XGPVUPCKFDUXIJDIZPVDBOBDDFTTUISPVHIUIF'XGPVU5QNWVKQP'XGPVUQSPQFSUZ:PV
DBODBQUVSFTPMVUJPOFWFOUTJOUIFVTVBMXBZ±CZPQFOJOHUIF'PXKTQPOGPV'XGPVUNPEVMFPG
BOZNBDSPQSPKFDUTFMFDUJOHUIF5QNWVKQP'XGPVUPCKFDUJOUIFMFGUESPQEPXOMJTUBUUIFUPQ
PGUIFDPE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PTFDIBOHFTXJMMCFEJTDBSEFE
N
WPJE"GUFS$MPTJOH
5IJTFWFOUJT¾SFEKVTUBGUFSUIFTPMVUJPO¾MFIBT¾OJTIFEDMPTJOH
5IFTBNQMFOBNFE4PMVUJPO&WFOUTXIJDIJTBNPOHUIFCPPLµTTBNQMF¾MFTEFNPOTUSBUFT
DPOOFDUJOHUPFBDIPGUIFTFFWFOUT0ODFZPVMPBEUIJTTBNQMFBTFBDIFWFOUJT¾SFEUIF
BEEJOEJTQMBZTBNFTTBHFCPYTIPXJOHBCJUPGJOGPSNBUJPOBCPVUUIFFWFOUUIBUXBT¾SFE
5IF3WGT[%NQUG5QNWVKQPFWFOUIBOEMFSBMTPPGGFSTUIFPQUJPOPGDBODFMJOHUIFDMPTJOHPGUIF
TPMVUJPO5IFTPVSDFDPEFGPSUIJTBEEJOTBNQMFJTTIPXOJO-JTUJOH
Listing 8-1
SolutionEvents.cs, the source code for the solution events add-in
namespace
{
using
using
using
using
using
using
SolutionEvents
System;
Microsoft.VisualStudio.CommandBars;
Extensibility;
EnvDTE;
EnvDTE80;
System.Windows.Forms;
public class Connect : Object, IDTExtensibility2
{
public Connect()
{
}
public void OnConnection(object application, ext_ConnectMode connectMode, _
object addInInst, ref Array custom)
{
applicationObject = (DTE2)application;
addInInstance = (AddIn)addInInst;
160
Working with Microsoft Visual Studio 2005
//Set the solutionEvents delegate variable using the
// DTE.Events.SolutionEvents property:
solutionEvents =
(EnvDTE.SolutionEvents)applicationObject.
Events.SolutionEvents;
//Setup all available event handlers by creating a new
// instance of the appropriate delegates:
solutionEvents.AfterClosing += new
_dispSolutionEvents_AfterClosingEventHandler(AfterClosing);
solutionEvents.BeforeClosing += new
_dispSolutionEvents_BeforeClosingEventHandler
(BeforeClosing);
solutionEvents.Opened += new
_dispSolutionEvents_OpenedEventHandler(Opened);
solutionEvents.ProjectAdded += new
_dispSolutionEvents_ProjectAddedEventHandler(ProjectAdded);
solutionEvents.ProjectRemoved += new
_dispSolutionEvents_ProjectRemovedEventHandler
(ProjectRemoved);
solutionEvents.ProjectRenamed += new
_dispSolutionEvents_ProjectRenamedEventHandler
(ProjectRenamed);
solutionEvents.QueryCloseSolution += new
_dispSolutionEvents_QueryCloseSolutionEventHandler
(QueryCloseSolution);
solutionEvents.Renamed += new
_dispSolutionEvents_RenamedEventHandler(Renamed);
}
public void OnDisconnection(ext_DisconnectMode disconnectMode,
ref Array custom)
{
//The Add-in is closing. Disconnect the event handlers:
solutionEvents.AfterClosing -= new
_dispSolutionEvents_AfterClosingEventHandler
(AfterClosing);
solutionEvents.BeforeClosing -= new
_dispSolutionEvents_BeforeClosingEventHandler
(BeforeClosing);
solutionEvents.Opened -= new
_dispSolutionEvents_OpenedEventHandler(Opened);
solutionEvents.ProjectAdded -= new
_dispSolutionEvents_ProjectAddedEventHandler(ProjectAdded);
solutionEvents.ProjectRemoved -= new
_dispSolutionEvents_ProjectRemovedEventHandler
(ProjectRemoved);
solutionEvents.ProjectRenamed -= new
_dispSolutionEvents_ProjectRenamedEventHandler
(ProjectRenamed);
solutionEvents.QueryCloseSolution -= new
_dispSolutionEvents_QueryCloseSolutionEventHandler
(QueryCloseSolution);
solutionEvents.Renamed -= new
Chapter 8:
Managing Solutions and Projects Programmatically
_dispSolutionEvents_RenamedEventHandler(Renamed);
}
public void OnAddInsUpdate(ref Array custom)
{
}
public void OnStartupComplete(ref Array custom)
{
}
public void OnBeginShutdown(ref Array custom)
{
}
//SolutionEvents.AfterClosing delegate handler:
public void AfterClosing()
{
MessageBox.Show("SolutionEvents.AfterClosing", "Solution Events");
}
//SolutionEvents.BeforeClosing delegate handler:
public void BeforeClosing()
{
MessageBox.Show("SolutionEvents.BeforeClosing", "Solution Events");
}
//SolutionEvents.Opened delegate handler:
public void Opened()
{
MessageBox.Show("SolutionEvents.Opened", "Solution Events");
}
//SolutionEvents.ProjectAdded delegate handler.
//Display the UniqueName of the project that has been added.
public void ProjectAdded(EnvDTE.Project project)
{
MessageBox.Show("SolutionEvents.ProjectAdded\nProject: " +
project.UniqueName, "Solution Events");
}
//SolutionEvents.ProjectRemoved delegate handler.
//Display the UniqueName of the project that has been added.
public void ProjectRemoved(EnvDTE.Project project)
{
MessageBox.Show("SolutionEvents.ProjectRemoved\nProject: " +
project.UniqueName, "Solution Events");
}
//SolutionEvents.ProjectRemoved delegate handler.
//Display the UniqueName of the project that has been renamed,
// and the full path file before it was renamed.
public void ProjectRenamed(EnvDTE.Project project, string oldName)
161
162
Working with Microsoft Visual Studio 2005
{
MessageBox.Show("SolutionEvents.ProjectRenamed\nProject: " +
project.UniqueName + "\nOld project name: " + oldName,
"Solution Events");
}
//SolutionEvents.QueryCloseSolution delegate handler.
//Asks if closing the solution should be canceled.
public void QueryCloseSolution(ref bool cancel)
{
if (MessageBox.Show(
"SolutionEvents.QueryCloseSolution\nContinue with close?",
"Solution Events", MessageBoxButtons.YesNo) ==
DialogResult.Yes)
cancel = false;
else
cancel = true;
}
//SolutionEvents.QueryCloseSolution delegate handler.
//Displays the full path the solution before and after it was renamed.
public void Renamed(string oldName)
{
MessageBox.Show(
"SolutionEvents.Renamed\nNew solution name: " +
applicationObject.Solution.FullName +
"\nOld solution name: " + oldName, "Solution Events");
}
private DTE2 applicationObject;
private AddIn addInInstance;
//The delegate handler variable:
private EnvDTE.SolutionEvents solutionEvents;
}
}
Is It a Bug When My Events Are Being Disconnected?
0WFSUIFZFBST*µWFPGUFOCFFOBTLFEJGUIFSFJTBCVHXJUIFWFOUTCFDBVTFFWFOUTDBOCF
VOFYQFDUFEMZMPTUBOEOPMPOHFS¾SFFWFOJGDPEFUPEJTDPOOFDUBOFWFOUJTOFWFSSVO
5IJTQSPCMFNJTCFDBVTFPGBDPNNPOQSPHSBNNJOHNJTUBLFUIBUSFWFBMTJUTFMGCFDBVTF
PGIPXUIFHBSCBHFDPMMFDUPSXPSLTJOUIF/&5'SBNFXPSL-PPLBUUIFGPMMPXJOHDPEF
XIJDIDPOOFDUTUPUIFTPMVUJPOµT4GPCOGFFWFOU
public void ConnectSolutionEvents()
{
EnvDTE.SolutionEvents solutionEvents;
solutionEvents = (EnvDTE.SolutionEvents)
applicationObject.Events.SolutionEvents;
Chapter 8:
Managing Solutions and Projects Programmatically
163
solutionEvents.Renamed += new
_dispSolutionEvents_RenamedEventHandler(Renamed);
}
8IFOUIJTNFUIPEJTDBMMFEUPDPOOFDUUPUIF4GPCOGFFWFOUUIFUQNWVKQP'XGPVUWBSJBCMF
JTBTTJHOFEUPBOJOTUBODFPGUIF5QNWVKQP'XGPVUPCKFDU#VUUIFUQNWVKQP'XGPVUWBSJBCMFJT
MPDBMUPUIF%QPPGEV5QNWVKQP'XGPVUNFUIPEBOEBTBSFTVMUXIFO%QPPGEV5QNWVKQP'XGPVU
SFUVSOT UP UIF DBMMFS UQNWVKQP'XGPVU JT NBSLFE BT BWBJMBCMF UP CF HBSCBHF DPMMFDUFE
6TVBMMZUIFFWFOU¾SFTPODFPSUXJDFCVUXIFOUIFHBSCBHFDPMMFDUPSTUBSUTXPSLJOHJU
TFFTUIBUUIJTWBSJBCMFDBOCFSFNPWFEGSPNNFNPSZBOESFNPWFTJUUIVTEJTDPOOFDUJOH
UIFFWFOUIBOEMFS5PNBLFZPVSFWFOUIBOEMFSDPEFXPSLDPSSFDUMZZPVTIPVMENPWF
UIFUQNWVKQP'XGPVUWBSJBCMFEFDMBSBUJPOPVUTJEFUIFNFUIPEBOEUPUIFDMBTTTDPQF5IJT
XJMMFOTVSFUIBUUIFFWFOUIBOEMFSJTOµUDPMMFDUFEVOUJMUIFDMBTTJTVOMPBEFE"MTPOPUF
UIBUUIJTCFIBWJPSBQQMJFTUPBMMFWFOUIBOEMFSTXIFOUIFZµSFDPOOFDUFEVTJOHUIF/&5
'SBNFXPSLOPUKVTUUIF5QNWVKQP4GPCOGFFWFOU
Working with Project Items
4PMVUJPOTNBOBHFBOVNCFSPGQSPKFDUTBOEFBDIQSPKFDUNBOBHFTUIF¾MFTUIBUBSFCVJMUJOUP
BQSPHSBN&BDIQSPKFDUDPOUBJOT¾MFTUIBUDBOCFFOVNFSBUFEBOEQSPHSBNNFE
Enumerating Project Items
'JMFTXJUIJOBQSPKFDUBSFBSSBOHFEIJFSBSDIJDBMMZ"QSPKFDUDBODPOUBJOBOZOVNCFSPG¾MFT
BOEPOFPSNPSFGPMEFSTXIJDIUIFNTFMWFTDBODPOUBJOBEEJUJPOBM¾MFTBOEGPMEFST5P
NBUDIUIJTQSPKFDUIJFSBSDIZUIFQSPKFDUPCKFDUNPEFMJTBMTPBSSBOHFEIJFSBSDIJDBMMZXJUI
UIF2TQLGEV+VGOUDPMMFDUJPOSFQSFTFOUJOHUIFOPEFTUIBUDPOUBJOJUFNTBOEUIF2TQLGEV+VGO
PCKFDUSFQSFTFOUJOHFBDIJUFNXJUIJOUIJTDPMMFDUJPO5PFOVNFSBUFUIJTIJFSBSDIZZPVVTF
UIF2TQLGEV+VGOUBOE2TQLGEV+VGOPCKFDUT5IFGPMMPXJOHNBDSPXBMLTUIF¾STUMFWFMPGUIF
IJFSBSDIZPGUIF2TQLGEV+VGOUBOE2TQLGEV+VGOPCKFDUTCZVTJOHUIF2TQLGEV2TQLGEV+VGOUQSPQFSUZ
UPPCUBJOUIFUPQMFWFM2TQLGEV+VGOUPCKFDU
Sub EnumTopLevelProjectItems()
Dim projItem As EnvDTE.ProjectItem
Dim projectProjectItems As EnvDTE.ProjectItems
Dim project As EnvDTE.Project
'Find the first project in a solution:
project = DTE.Solution.Projects.Item(1)
'Retrieve the collection of project items:
projectProjectItems = project.ProjectItems
'Walk the list of items in the collection:
For Each projItem In projectProjectItems
MsgBox(projItem.Name)
Next
End Sub
164
Working with Microsoft Visual Studio 2005
4PNFJUFNTXJUIJOBQSPKFDUTVDIBTBGPMEFSBSFCPUIBOJUFNXJUIJOUIFQSPKFDUIJFSBSDIZ
BOEBDPOUBJOFSPGPUIFS¾MFTBOEGPMEFST#FDBVTFUIFTFGPMEFSTBSFCPUIJUFNTBOE
DPMMFDUJPOTPGJUFNTBGPMEFSJTSFQSFTFOUFEJOUIFQSPKFDUNPEFMIJFSBSDIZXJUICPUIB
2TQLGEV+VGOPCKFDUBOEB2TQLGEV+VGOUPCKFDU:PVDBOEFUFSNJOFXIFUIFSB2TQLGEV+VGOOPEF
JTBMTPBDPOUBJOFSPGNPSF2TQLGEV+VGOOPEFTCZDBMMJOHUIF2TQLGEV+VGO2TQLGEV+VGOUQSPQFSUZ
XIJDISFUVSOTB2TQLGEV+VGOUDPMMFDUJPOJGUIFOPEFDBODPOUBJOTVCJUFNT:PVDBOFOVNFSBUF
BMMUIF2TQLGEV+VGOBOE2TQLGEV+VGOUPCKFDUTXJUIJOBQSPKFDUCZXSJUJOHBSFDVSTJWFNBDSP
GVODUJPOTVDIBTUIJT
Sub EnumProjectItems(ByVal projItems As EnvDTE.ProjectItems)
Dim projItem As EnvDTE.ProjectItem
'Find all the ProjectItem objects in the given collection:
For Each projItem In projItems
MsgBox(projItem.Name)
'And walk any items the current item may contain:
EnumProjectItems(projItem.ProjectItems)
Next
End Sub
Sub EnumProject()
Dim project As EnvDTE.Project
'Find the first project in a solution:
project = DTE.Solution.Projects.Item(1)
EnumProjectItems(project.ProjectItems)
End Sub
5IF'PWO2TQLGEVNBDSP¾STU¾OETUIF2TQLGEV+VGOUDPMMFDUJPOPGBHJWFOQSPKFDUBOEUIFOJU
DBMMTUIF'PWO2TQLGEV+VGOUTVCSPVUJOFXIJDIXJMM¾OEBMMUIF2TQLGEV+VGOPCKFDUTUIBUUIF
DPMMFDUJPODPOUBJOT*GUIF2TQLGEV+VGOPCKFDUJTJUTFMGBDPMMFDUJPOJUXJMMSFDVSTJWFMZDBMMUIF
'PWO2TQLGEV+VGOUTVCSPVUJOFUPEJTQMBZUIFJUFNTJUDPOUBJOT
'PMEFSTBSFOµUUIFPOMZJUFNTUIBUDBODPOUBJOBDPMMFDUJPOPG2TQLGEV+VGOPCKFDUT'JMFTTVDI
BT8JOEPXT'PSNTBOE8FC'PSNTBSFBMTPDPMMFDUJP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OHUPMPDBUFUIF#JUNBQCNQ¾MF
Sub FindBitmap()
Dim project As EnvDTE.Project
Dim projectProjectItems As EnvDTE.ProjectItems
Dim resourcesProjectItem As EnvDTE.ProjectItem
Dim resourcesProjectItems As EnvDTE.ProjectItems
Dim bitmapsProjectItem As EnvDTE.ProjectItem
Dim bitmapsProjectItems As EnvDTE.ProjectItems
Dim bitmapProjectItem As EnvDTE.ProjectItem
'Get the project:
project = DTE.Solution.Item(1)
'Get the list of items in the project:
projectProjectItems = project.ProjectItems
'Get the item for the Resources folder:
resourcesProjectItem = projectProjectItems.Item("Resources")
'Get the collection of items in the Resources folder:
resourcesProjectItems = resourcesProjectItem.ProjectItems
'Get the item for the Bitmaps folder:
bitmapsProjectItem = resourcesProjectItems.Item("Bitmaps")
'Get the collection of items in the Bitmaps folder:
bitmapsProjectItems = bitmapsProjectItem.ProjectItems
'Get the item for the Bitmap1.bmp file:
bitmapProjectItem = bitmapsProjectItems.Item("Bitmap1.bmp")
MsgBox(bitmapProjectItem.Name)
End Sub
:PVDBOXBMLEPXOUIFUSFFPGUIF2TQLGEV+VGOBOE2TQLGEV+VGOUIJFSBSDIZUP¾OEBTQFDJ¾D¾MF
CVUTPNFUJNFTZPVNJHIUOFFEBRVJDLFSBOEFBTJFSXBZPGMPDBUJOHUIF2TQLGEV+VGOPCKFDU
GPSB¾MFXJUIBTQFDJ¾D¾MFOBNFJOBQSPKFDU:PVDBOVTFUIF(KPF2TQLGEV+VGONFUIPEPGUIF
5QNWVKQPPCKFDUUP¾OEBOJUFNCZQBTTJOHBQPSUJPOPGUIF¾MFQBUIUPXIFSFUIF¾MFJTMPDBUFE
POEJTL'PSFYBNQMFTVQQPTFUXPBEEJOQSPKFDUTIBWFCFFODSFBUFEVTJOHUIF"EEJO
8J[BSE
JOBGPMEFSZPVDSFBUFEDBMMFE"EEJOTMPDBUFEJOUIFSPPUPGESJWF$&BDIPGUIFTF
166
Working with Microsoft Visual Studio 2005
UXPBEEJOT.Z"EEJOBOE.Z"EEJODPOUBJOTB¾MFOBNFE$POOFDUDT:PVDPVMEVTFUIF
GPMMPXJOHNBDSPUPMPDBUFUIF$POOFDUDT¾MFJOFJUIFSQSPKFDU
Sub FindItem()
Dim projectItem As EnvDTE.ProjectItem
projectItem = DTE.Solution.FindProjectItem("Connect.cs")
End Sub
)PXFWFSCFDBVTF(KPF2TQLGEV+VGOSFUVSOTBOZ¾MFUIBUNBUDIFTUIJT¾MFOBNFZPVDBOµUUFMM
XIJDI2TQLGEV+VGOXJMMCFSFUVSOFE±UIF2TQLGEV+VGOPCKFDUGPSUIF$POOFDUDTJO.Z"EEJOPS
UIF2TQLGEV+VGOPCKFDUGPS$POOFDUDTJO.Z"EEJO5PSF¾OFUIFTFBSDIZPVDBOTVQQMZBCJU
NPSFPGUIF¾MFQBUIBTUIFTQFDJ¾FE¾MFOBNFBTTIPXOJOUIFGPMMPXJOHNBDSPXIJDIBEET
UIFOBNFPGUIFGPMEFSPOEJTLUIBUDPOUBJOTUIF.Z"EEJOWFSTJPOPG$POOFDUDT
Sub FindItemWithFolder()
Dim projectItem As EnvDTE.ProjectItem
projectItem = DTE.Solution.FindProjectItem("MyAddin1\Connect.cs")
End Sub
0GDPVSTFKVTUBTZPVDBOTQFDJGZBQPSUJPOPGUIFQBUIUP¾OEUIF2TQLGEV+VGOZPVDBOVTF
UIFXIPMFQBUIUP[FSPJOPOUIFFYBDUJUFNZPVXBOU
Sub FindItemWithFullPath()
Dim projectItem As EnvDTE.ProjectItem
projectItem = _
DTE.Solution.FindProjectItem("C:\Addins\MyAddin1\Connect.cs")
End Sub
Adding and Removing Project Items
:PVDBOBEEOFX¾MFTUPBQSPKFDUJOUXPXBZT5IF¾STUXBZJTUPVTFUIF#FF(TQO&KTGEVQT[
#FF(TQO(KNG#FF(TQO(KNG%QR[BOE#FF(TQO6GORNCVGNFUIPETPGUIF2TQLGEV+VGOUJOUFSGBDF
XIJDIXFµMMEJTDVTTTIPSUMZ
5IFTFDPOEXBZJTUPV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BM#BTJD
Chapter 8:
Managing Solutions and Projects Programmatically
167
BOE7JTVBM$QSPKFDUTBSFGPMEFSCBTFEXIJDINFBOTUIBUUIFQSPKFDUIJFSBSDIZTIPXOJO
4PMVUJPO&YQMPSFSJTNJSSPSFEPOEJTLBOEBOZ¾MFTXJUIJOUIFQSPKFDUNVTUCFJOUIFGPMEFS
DPOUBJOJOHUIFQSPKFDUPSJOPOFPGJUTTVCGPMEFST.JDSPTPGU7JTVBM$ˆQSPKFDUTXPSL
BMJUUMFEJGGFSFOUMZB¾MFUIBUJTQBSUPGUIFQSPKFDUDBOCFMPDBUFEBOZXIFSFPOEJTLBOEJU
EPFTOµUOFFEUPCFXJUIJOUIFGPMEFSDPOUBJOJOHUIFQSPKFDUPSBDIJMEGPMEFSPGUIBUGPMEFS
'PSJOTUBODFTVQQPTFB¾MFOBNFE¾MFUYUJTMPDBUFEJOUIFSPPUEJSFDUPSZPGUIF$ESJWF*G
XFSVOUIFNBDSP
Sub AddExistingItem()
DTE.ItemOperations.AddExistingItem("C:\file.txt")
End Sub
BOEUIFJUFNTFMFDUFEJO4PMVUJPO&YQMPSFSJTB7JTVBM$PSB7JTVBM#BTJDQSPKFDUPSPOFPG
JUTDIJMESFO¾MFUYUXJMMCFDPQJFEJOUPUIFGPMEFSPSTVCGPMEFSDPOUBJOJOHUIFQSPKFDU¾MFBOE
UIFOBEEFEUPUIFQSPKFDU#VUJGUIFTFMFDUFEJUFNJTB7JTVBM$QSPKFDUUIF¾MFXJMMCFMFGU
JOQMBDFBOEBSFGFSFODFXJMMCFBEEFEUPUIJT¾MF
8IFSFBT#FF'ZKUVKPI+VGOJOTFSUTB¾MFGSPNEJTLJOUPBQSPKFDU#FF0GY+VGODSFBUFTBOFX¾MF
BOEBEETJUUPUIFQSPKFDU5IJTNFUIPEUBLFTUXPBSHVNFOUTBOEIBTUIFGPMMPXJOHNFUIPE
TJHOBUVSF
public EnvDTE.ProjectItem AddNewItem(string Item = "General\Text File",
string Name = "")
:PV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O6GORNCVG#FF(TQO&KTGEVQT[BDDFQUTBTBQBSBNFUFSUIFQBUIUPBGPMEFSPOEJTL
UIJTGPMEFSJTTFBSDIFESFDVSTJWFMZDBVTJOHBMMJUTDPOUBJOFE¾MFTBOETVCGPMEFSTUPCFBEEFE
UPUIFQSPKFDU#FF(TQO(KNG%QR[BOE#FF(TQO(KNGCPUIQFSGPSNUIFTBNFCBTJDPQFSBUJPO
BEEJOHBSFGFSFODFUPUIFTQFDJ¾FE¾MFPOEJTLUPUIFQSPKFDU)PXFWFS#FF(TQO(KNG%QR[
DPQJFTUIF¾MFJOUPUIFQSPKFDUµTEJSFDUPSZTUSVDUVSFCFGPSFBEEJOHUIJT¾MFUPUIFQSPKFDU
XIJMF#FF(TQO(KNGBEETBMJOLUPUIFJUFNJOXIJDIFWFSGPMEFSUIF¾MFJTDPOUBJOFE
#FF(TQO(KNG%QR[EJGGFSTGSPNUIF#FF(TQO6GORNCVGNFUIPEPGUIF2TQLGEV+VGOUDPMMFDUJPO
OPUUPCFDPOGVTFEXJUIUIF#FF(TQO6GORNCVGNFUIPEPGUIF5QNWVKQPPCKFDU
JOUIBU
Chapter 8:
Managing Solutions and Projects Programmatically
169
#FF(TQO6GORNCVGDPQJFTUIF¾MFJOUPUIFGPMEFSPOEJTLGPSUIFQSPKFDUBOEUIFOUIFQSPKFDU
NJHIUNBLFTPNFNPEJ¾DBUJPOTUPUIF¾MFBGUFSUIF¾MFTBSFBEEFEJGUIFUFNQMBUF¾MFIBTUIF
WTUFNQMBUFFYUFOTJPOUIFUFNQMBUFXJ[BSEJTTUBSUFEBOEDBMMFEUPBEEUIF¾MFUPUIFQSPKFDU
)FSFBSFUIFTJHOBUVSFTUIFQBSBNFUFSTBOEUIFNFBOJOHTPGUIFQBSBNFUFSTGPSUIFTF
NFUIPET
EnvDTE.ProjectItem
EnvDTE.ProjectItem
EnvDTE.ProjectItem
EnvDTE.ProjectItem
AddFromDirectory(string Directory)
AddFromFileCopy(string FilePath)
AddFromFile(string FileName)
AddFromTemplate(string FileName, string Name)
N
%JSFDUPSZ 5IFTPVSDFGPMEFSPOEJTL4FBSDIFTGPS¾MFTBOETVCGPMEFSTCFHJOXJUIUIJT
GPMEFS
N
'JMF1BUI'JMF/BNF 5IFMPDBUJPOPGUIF¾MFUPDPQZPSBEEBSFGFSFODFUP
N
/BNF 5IFSFTVMUJOHOBNFPGUIF¾MF5IJTOBNFTIPVMEIBWFUIFFYUFOTJPOPGUIF¾MF
UZQF
&BDIPGUIFTFNFUIPETSFUVSOTB2TQLGEV+VGOBOPCKFDUUIBUDBOCFVTFEUPQFSGPSNPQFSBUJPOT
POUIF¾MFUIBUXBTBEEFETVDIBTPQFOJOHUIF¾MFPSBDDFTTJOHUIF¾MFµTDPOUFOUT
Note All of the Add methods on both the Solution and ProjectItems objects will return
null or Nothing (depending upon the programming language you are using) if you pass a
.vstemplate file as a template name. This is because a wizard could add 0, 1, or many items
to a solution or project. If a wizard does not add any items, null or Nothing is a logical value
to return. If multiple items are added, because the Add methods can return only one Project
or ProjectItem object, null or Nothing is the correct value to return because the wizard would
need to select one item arbitrarily to return. If the template wizard were to add one item, it
would be confusing if a Project or ProjectItem object were returned, so, in this case, null or
Nothing is also returned. Also, some of the Add methods will allow you to specify a target file
name, but the wizard does not necessarily honor that target file name, so finding the item
that you suggested a name for may not work. There is another reason for this value to be
returned if you pass a .vstemplate as a template file: wizards do not have a mechanism for
returning an item to Visual Studio indicating which item has been added, and without the
ability to pass back an object, there is nothing to return other than null or Nothing.
'JOEJOHUIFQBUIUPBOJOTUBMMFEUFNQMBUFGPSBEEJOHBOJUFNUPBQSPKFDUJTTJNJMBSUP¾OEJOH
BUFNQMBUFGPSBEEJOHBQSPKFDU'PSUFNQMBUFTJOTUBMMFEGPSBMMVTFSTSBUIFSUIBOVTJOHUIF
)GV6GORNCVG2CVJNFUIPEBTZPVEPGPSQSPKFDUTZPVVTFUIF)GV2TQLGEV+VGO6GORNCVGGPS
QSPKFDUJUFNT5IJTDPEFVTFTUIF)GV2TQLGEV+VGO6GORNCVGNFUIPEUP¾OEUIFQBUIUPUIF
WTUFNQMBUF¾MFGPSB7JTVBM#BTJDNPEVMF
Sub ProjectItemTemplatePath()
Dim solution2 As EnvDTE80.Solution2
Dim VBModuleTemplatePath As String
170
Working with Microsoft Visual Studio 2005
solution2 = CType(DTE.Solution, EnvDTE80.Solution2)
VBModuleTemplatePath = _
solution2.GetProjectItemTemplate("Module.zip",_
"VisualBasic")
MsgBox(VBModuleTemplatePath)
End Sub
"OEKVTUBTGPSQSPKFDUUFNQMBUFTJOTUBMMFECZUIFVTFSZPVXJMMOFFEUPVTFBCSVUFGPSDF
TUSBUFHZUP¾OEUIFQBUIUPUIFVTFSJOTUBMMFEQSPKFDUJUFNUFNQMBUF1SPKFDUJUFNUFNQMBUFT
BSFFYUSBDUFEJOUPBGPMEFSOBNFE*UFN5FNQMBUFT$BDIFXIFOUIFBQQSPQSJBUF"EE/FX
*UFNEJBMPHCPYJTTIPXO5IJTNBDSPDPEFXJMM¾MFBOFXQSPKFDUJUFNUFNQMBUFOBNFE
.Z5FNQMBUFGPSUIF7JTVBM#BTJDMBOHVBHF
Sub UserProjectItemTemplatePath()
Dim projectItemTemplatePath As String
projectItemTemplatePath = System.Environment.GetFolderPath _
(System.Environment.SpecialFolder.ApplicationData)
projectItemTemplatePath = System.IO.Path.Combine _
(projectItemTemplatePath, _
"Microsoft\VisualStudio\8.0\ItemTemplatesCache")
'projectItemTemplatePath contains the path for all templates,
'now add to the path
' using information specific to the template to find:
projectItemTemplatePath = System.IO.Path.Combine _
(projectItemTemplatePath, _
"Visual Basic\MyTemplate.zip\MyTemplate.vstemplate")
End Sub
:PVNJHIUPDDBTJPOBMMZOFFEUPSFNPWFBOJUFNUIBUIBTCFFOBEEFEUPBQSPKFDUCFDBVTF
ZPVOPMPOHFSOFFEJU5IF2TQLGEV+VGOPCKFDUTVQQPSUTUXPNFUIPETGPSSFNPWJOHJUFNTGSPN
UIFQSPKFDU4G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ject Projects
85.CPI2TQL852TQLGEVJTUIFJOUFSGBDFUIBUEF¾OFTFYUFOTJPOTUPUIF'PX&6'2TQLGEVPCKFDU
GPS7JTVBM+7JTVBM#BTJDPS7JTVBM$QSPKFDUT0ODFZPVµWFSFUSJFWFEUIF'PX&6'2TQLGEV
JOUFSGBDFGPSPOFPGUIFTFQSPKFDUUZQFTZPVDBOHFUUPUIF85.CPI2TQL852TQLGEVJOUFSGBDFCZ
DBMMJOHUIF2TQLGEV1DLGEVQSPQFSUZ5IFGPMMPXJOHNBDSPDPEFXIJDIBTTVNFTUIBUUIF¾STU
QSPKFDUJOUIF2TQLGEVUDPMMFDUJPOJTB7JTVBM#BTJD7JTVBM$PS7JTVBM+QSPKFDUSFUSJFWFTUIF
852TQLGEVPCKFDUGPSUIBUQSPKFDU
Sub GetVSProject()
Dim project As EnvDTE.Project
Dim vsproject As VSLangProj.VSProject
project = DTE.Solution.Projects.Item(1)
vsproject = CType(project.Object, VSLangProj.VSProject)
End Sub
References
3FGFSFODFTBSFQPJOUFSTUPTPGUXBSFDPNQPOFOUTUIBUBQSPKFDUDBOVTFUPSFEVDFUIFBNPVOU
PGDPEFBQSPHSBNNFSOFFETUPXSJUF"QSPKFDUVTFTUIFUZQFPGJOGPSNBUJPODPOUBJOFE
XJUIJOBSFGFSFODFUPEJTQMBZJOGPSNBUJPOJOUIFGPSNPG*OUFMMJ4FOTFˆTUBUFNFOUDPNQMFUJPO
"SFGFSFODFBMTPQSPWJEFTJOGPSNBUJPOUPUIFDPNQJMFSGPSSFTPMWJOHTZNCPMTVTFEJO
QSPHSBNNJOHDPEF"SFGFSFODFDBOCFBOBTTFNCMZPSBOPUIFSQSPKFDUMPBEFEJOUPUIF
TPMVUJPOBOEZPVDBODSFBUFSFGFSFODFTUP$0.DPNQPOFOUTCZXSBQQJOHUIF$0.PCKFDU
UZQFJOGPSNBUJPOMJCSBSZXJUIBOJOUFSPQBTTFNCMZ:PVDBOBEESFGFSFODFTUISPVHIUIFVTFS
JOUFSGBDFCZSJHIUDMJDLJOHPOUIF3FGFSFODFTOPEFJOB7JTVBM#BTJDPS7JTVBM$QSPKFDU
DIPPTJOH"EE3FGFSFODFGSPNUIFTIPSUDVUNFOVBOEUIFOTFMFDUJOHBDPNQPOFOUJOUIF
EJBMPHCPYUIBUBQQFBST
6TJOHUIF85.CPI2TQL4GHGTGPEGUPCKFDUZPVDBOFOVNFSBUFBEEPSSFNPWFSFGFSFODFT5P
HFUUPUIF4GHGTGPEGUPCKFDUVTFUIF852TQLGEV4GHGTGPEGUQSPQFSUZ'PSFYBNQMFUIFGPMMPXJOH
DPEFSFUSJFWFTUIF4GHGTGPEGUPCKFDUBOEUIFOFOVNFSBUFTUIFSFGFSFODFTUIBUIBWFCFFOBEEFE
UPBQSPKFDU
Sub EnumReferences()
Dim proj As EnvDTE.Project
Dim vsproj As VSLangProj.VSProject
Dim references As VSLangProj.References
Dim reference As VSLangProj.Reference
proj = DTE.Solution.Projects.Item(1)
vsproj = proj.Object
references = vsproj.References
For Each reference In references
MsgBox(reference.Name)
Next
End Sub
:PVBEEBSFGFSFODFUPBOBTTFNCMZCZDBMMJOHUIF4GHGTGPEGU#FFNFUIPEBOEQBTTJOHUIF
QBUIUPUIFBTTFNCMZ5IF#FFNFUIPEDPQJFTUIFBTTFNCMZJOUPUIFQSPKFDUPVUQVUGPMEFS
172
Working with Microsoft Visual Studio 2005
VOMFTTBDPQZPGUIFBTTFNCMZXJUIUIFTBNFWFSTJPOBOEQVCMJDLFZJOGPSNBUJPOJTTUPSFE
JOUIFHMPCBMBTTFNCMZDBDIF("$
5IJTJTEPOFTPUIBUXIFOUIFQSPKFDUPVUQVUJTSVOPS
MPBEFECZBOPUIFSBTTFNCMZUIFDPSSFDUSFGFSFODFEBTTFNCMZDBOCFMPBEFE5IFGPMMPXJOH
NBDSPDPEFBEETBSFGFSFODFUPBOBTTFNCMZ
Sub AddReferenceToAssembly()
Dim vsproj As VSLangProj.VSProject
Dim proj As EnvDTE.Project
proj = DTE.Solution.Projects.Item(1)
vsproj = CType(proj.Object, VSLangProj.VSProject)
vsproj.References.Add("C:\Program Files\Common Files\" & _
"Microsoft Shared\MSEnv\PublicAssemblies\extensibility.dll")
End Sub
5IJTDPEF¾OETUIF852TQLGEVPCKFDUGPSBQSPKFDUBOEUIFOBEETBSFGFSFODFUPUIF
&YUFOTJCJMJUZEMMNFUBEBUBBTTFNCMZ±UIFTBNFBTTFNCMZUIBUDPOUBJOTUIFEF¾OJUJPOPGUIF
+&6'ZVGPUKDKNKV[JOUFSGBDFXIJDIJTVTFEGPSCVJMEJOHBEEJOT:PVDBOµUBEEBTTFNCMJFT
MPDBUFEXJUIJOUIF("$BTSFGFSFODFTUPBQSPKFDUCFDBVTFUIF7JTVBM#BTJDBOE7JTVBM$
QSPKFDUTZTUFNTNBJOUBJOBTFQBSBUJPOCFUXFFOUIF¾MFTUIBUBSFSFGFSFODFEGPSCVJMEJOH
BHBJOTUBOE¾MFTUIBUBSFVTFEEVSJOHBDPNQPOFOUµTSVOUJNF
%VSJOHEFWFMPQNFOUBDPNQPOFOUUIBUJTDPNQJMFECZPOFQSPKFDUJOBTPMVUJPONJHIUCF
OFFEFECZBDPNQPOFOUJOBOPUIFSQSPKFDU:PVDBODSFBUFBSFGFSFODFGSPNPOFQSPKFDUUP
BOPUIFSQSPKFDUCZVTJOHUIF4GHGTGPEGU#FF2TQLGEVNFUIPE5IJTNFUIPEBDDFQUTB2TQLGEV
PCKFDUBOEBEETBSFGFSFODFUPUIBUQSPKFDUBTTIPXOIFSF
Sub AddProjectReference()
Dim vsproj As VSLangProj.VSProject
Dim proj As EnvDTE.Project
'Find the project the reference will be added to:
proj = DTE.Solution.Projects.Item(1)
vsproj = CType(proj.Object, VSLangProj.VSProject)
'Find the referenced project:
proj = DTE.Solution.Projects.Item(2)
'Make the project to project reference:
vsproj.References.AddProject(proj)
End Sub
"EEJOHBSFGFSFODFUPB$0.PCKFDUSFRVJSFTWBMVFTUIBUBSF$0.DFOUS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TIPVMEEF¾OFB1*"CFDBVTFFWFOUIPVHIZPVDPVME
HFOFSBUFNVMUJQMF*"TCZXSBQQJOHUIFTBNFUZQFMJCSBSZUIFUZQFTJOPOF*"BSFDPOTJEFSFE
EJTUJODUGSPNUIFUZQFTJOBOPUIFS*"BOEBOFSSPSXJMMCFHFOFSBUFE5IFGPMMPXJOHNBDSP
DPEFBEETBSFGFSFODFUPUIFUZQFMJCSBSZGPS.JDSPTPGU8JOEPXT.FEJBˆ1MBZFS
Sub AddCOMReference()
Dim vsproj As VSLangProj.VSProject
Dim proj As EnvDTE.Project
proj = DTE.Solution.Projects.Item(1)
vsproj = CType(proj.Object, VSLangProj.VSProject)
vsproj.References.AddActiveX( _
"{22D6F304-B0F6-11D0-94AB-0080C74C7E95}", 1, 0)
End Sub
Web References
5IF/&5'SBNFXPSLOPUPOMZNBLFTUSBEJUJPOBMTPGUXBSFEFWFMPQNFOUFBTJFSCVUJUBMTP
NBLFTOFXTPGUXBSFEFWFMPQNFOUNFUIPEPMPHJFTQPTTJCMF0OFPGUIFTFOFXNFUIPEPMPHJFT
JOWPMWFT9.-8FCTFSWJDFT9.-8FCTFSWJDFTFOBCMFTPGUXBSFEFWFMPQNFOUBDSPTTUIF
*OUFSOFUCZQMBDJOHTPGUXBSFDPEFPOBTFSWFSXIJDIDBOUIFOCFBDDFTTFECZTPGUXBSFUIBU
JTSVOPOUIFVTFSµTDPNQVUFS7JTVBM4UVEJPNBLFTDPOOFDUJOHEFTLUPQTPGUXBSFUP9.-
8FCTFSWJDFTBTFBTZBTBEEJOHB8FCSFGFSFODF8IFOBSFGFSFODFUPBO9.-8FCTFSWJDF
JTNBEFBTQFDJBM¾MFXSJUUFOVTJOHUIF8FC4FSWJDFT%FTDSJQUJPO-BOHVBHF84%-
9.-
TDIFNBJTEPXOMPBEFEGSPNUIFTFSWFSDPNQVUFSBOEBQSPYZDMBTTBDMBTTUIBUDPOUBJOTUIF
MPHJDUPUSBOTMBUFBNFUIPEPSQSPQFSUZDBMMGSPNUIFDMJFOUDPNQVUFSBDSPTTUIF*OUFSOFUUP
UIFTFSWFSDPNQVUFS
JTHFOFSBUFEGSPNUIF84%-¾MF5IJTQSPYZDMBTTDBOUIFOCFVTFEUP
DBMMUPUIF9.-8FCTFSWJDF
5IFGPMMPXJOHNBDSPBEETB8FCSFGFSFODFUPBQSPKFDU*USFUSJFWFTUIF852TQLGEVPCKFDUGPSB
QSPKFDUBOEUIFODBMMTUIF#FF9GD4GHGTGPEGNFUIPEXJUIUIF63-GPSUIF9.-8FCTFSWJDF
5IJTFYBNQMFVTFTUIF5FSSB4FSWFS8FCTFSWJDFQSPWJEFECZ.JDSPTPGUXIJDIPGGFSTEFUBJMFE
HFPHSBQIJDJOGPSNBUJPOBOETBUFMMJUFJNBHFTGPSUIF6OJUFE4UBUFT5IJT8FCTFSWJDFJT
MPDBUFEBUJVVRVGTTCUGTXGTOKETQUQHVEQO6GTTC5GTXKEGCUOZ
Sub AddTerraServerWebRef()
Dim vsProj As VSLangProj.VSProject
Dim serviceURL As String
'Set the URL to the TerraServer web service
serviceURL = "http://terraserver.microsoft.com/TerraService2.asmx"
'Find the VSProject for a project
vsProj = DTE.Solution.Projects.Item(1).Object
'Add the web reference
vsProj.AddWebReference(serviceURL)
End Sub
174
Working with Microsoft Visual Studio 2005
8IFOUIJT8FCSFGFSFODFJTNBEFUIF84%-¾MFEFTDSJCJOHUIF9.-8FCTFSWJDFJT
EPXOMPBEFEGSPNUIFTFSWFSDPNQVUFSBOEUIFQSPYZDMBTTGPSUIFTFSWJDFJTHFOFSBUFEBOE
BVUPNBUJDBMMZBEEFEUPUIFQSPKFDU5IJTDMBTTJTQMBDFEJOBOBNFTQBDFEF¾OFECZUIF8FC
QSPUPDPMBOETFSWFSOBNFCVUJOSFWFSTFPSEFS4PGPSFYBNQMFJGUIF9.-8FCTFSWJDF
XFSFMPDBUFEBUYYYOKETQUQHVEQOUIFOBNFTQBDFGPSUIFTFSWJDFXPVMECFEQOOKETQUQ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UBWBJMBCMFGPSBMMUIF¾MFTXJUIJOUIFQSPKFDUOPUKVTUUIF¾MFUIBUVTFTUIF+ORQTVU
TUBUFNFOU6TJOHUIF852TQLGEV+ORQTVUDPMMFDUJPOZPVDBOFOVNFSBUFSFNPWFBOEBEE
JNQPSUTGPSUIFFOUJSFQSPKFDU5IFGPMMPXJOHNBDSPBEETUIF5[UVGO:/.OBNFTQBDFUPB
7JTVBM#BTJDQSPKFDU
Sub AddSystemXMLImport()
Dim vsProj As VSLangProj.VSProject
vsProj = DTE.Solution.Projects.Item(1).Object
vsProj.Imports.Add("System.Xml")
End Sub
Note The Imports object is valid only for the Visual Basic project type. Any attempt to
access this object for a Visual C# or Visual J# project will return a null or Nothing value.
Chapter 8:
Managing Solutions and Projects Programmatically
175
ProjectProperties
&BDIQSPKFDUIBTBOVNCFSPGPQUJPOTBTTPDJBUFEXJUIJUUIBUBMMPXZPVUPDPOUSPMIPXZPV
JOUFSBDUXJUIUIBUQSPKFDU:PVDBOTFUUIFTFPQUJPOTVOEFSUIF$PNNPO1SPQFSUJFTOPEFPG
BQSPKFDUµT1SPQFSUZ1BHFTEJBMPHCPY5IFPQUJPOTJODMVEFUIFOBNFPGUIFDPNQPOFOUUIBU
UIFDPNQJMFSTIPVMECVJMEUIFLJOEPGQSPKFDUUPCFHFOFSBUFEBOFYFPSBEMM
BOEMBZPVU
PQUJPOTGPSUIF)5.-EFTJHOFS:PVDBOBMTPTFUUIFTFPQUJPOTQSPHSBNNBUJDBMMZCZVTJOH
UIF2TQRGTVKGUQSPQFSUZPGUIF2TQLGEVPCKFDU5IJTQSPQFSUZSFUVSOTUIFTBNF2TQRGTVKGUPCKFDU
UIBUµTVTFEUISPVHIPVU7JTVBM4UVEJPUPTFUPQUJPOT5IFGPMMPXJOHNBDSPXBMLTUIFMJTUPG
QSPQFSUJFTBWBJMBCMFUPBQSPKFDUBTXFMMBTUIFWBMVFTBOEUZQFTPGFBDIQSPQFSUZ
Sub WalkVSProjectProperties()
Dim project As EnvDTE.Project
Dim properties As EnvDTE.Properties
Dim [property] As EnvDTE.Property
Dim owp As InsideVSNET.Utilities.OutputWindowPaneEx
owp = New InsideVSNET.Utilities.OutputWindowPaneEx(DTE, _
"Project properties")
project = DTE.Solution.Projects.Item(1)
properties = project.Properties
For Each [property] In properties
owp.WriteLine("Name: " + [property].Name)
owp.WriteLine("Value: " + [property].Value.ToString())
owp.WriteLine("Type: " + [property].Value.GetType().FullName)
owp.WriteLine()
Next
End Sub
:PVDBOVTFUIJT2TQRGTV[PCKFDUUPSFBEUIFWBMVFTPGQSPQFSUJFTBOEUPTFUUIFQSPQFSUJFT
GPSBQSPKFDU5IFGPMMPXJOHNBDSPEFNPOTUSBUFTUIJT*UTFUTUIFJDPOUPVTFGPSBQSPKFDU
XIFOJUJTDPNQJMFE5IJTDPEFBTTVNFTUIBUBOJDPOOBNFE*DPOJDPJTMPDBUFEJOUIFGPMEFS
DPOUBJOJOHUIFQSPKFDU¾MF
Sub SetProjectIcon()
Dim project As EnvDTE.Project
Dim [property] As EnvDTE.Property
Dim projectPath As String
project = DTE.Solution.Projects.Item(1)
'Get the Property object for the icon:
[property] = project.Properties.Item("ApplicationIcon")
'Construct the path to the icon based off of the
' project path:
projectPath = project.FullName
projectPath = System.IO.Path.GetDirectoryName(projectPath)
projectPath = projectPath + "\Icon.ico"
'Set the icon for the project:
[property].Value = projectPath
End Sub
176
Working with Microsoft Visual Studio 2005
Using Visual Studio Utility Project Types
5PIFMQZPVNPSFFBTJMZNBJOUBJO¾MFTXJUIJOBTPMVUJPO7JTVBM4UVEJPNBLFTBWBJMBCMF
WBSJPVTVUJMJUZQSPKFDUT5IFTFVUJMJUZQSPKFDUTBMMPXZPVUPLFFQUSBDLPG¾MFTUIBUBSFOPUQBSU
PGBOZPUIFSQSPKFDUUIBUJTMPBEFEJOUPBTPMVUJPO#FDBVTFBOZ¾MFUZQFDBOCFTUPSFEXJUIJO
UIFTFQSPKFDUTTVDIBTQSPHSBNTPVSDF¾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FDLCPY
5IF.JTDFMMBOFPVT'JMFTQSPKFDUIBTBVOJRVFOBNFBTTPDJBUFEXJUIJUUIBUVOMJLFXJUI
PUIFSQSPKFDUTEPFTOµUDIBOHFPWFSUJNF5IJTOBNF.JTD'JMFTJTEF¾OFECZUIFDPOTUBOU
XU/KUE(KNGU2TQLGEV7PKSWG0COG5IFGPMMPXJOHNBDSPSFUSJFWFTUIF2TQLGEVPCKFDUGPSUIF
.JTDFMMBOFPVT'JMFTQSPKFDU
Sub FindMiscFilesProject()
Dim project As EnvDTE.Project
Dim projects As EnvDTE.Projects
projects = DTE.Solution.Projects
project = projects.Item(EnvDTE.Constants.vsMiscFilesProjectUniqueName)
End Sub
8IFOUIF¾STU¾MFJTPQFOFEXJUIJOUIF.JTDFMMBOFPVT'JMFTQSPKFDUBOJUFNJTBEEFEUP
UIF5QNWVKQP2TQLGEVUDPMMFDUJPOUIBUJNQMFNFOUTUIF2TQLGEVJOUFSGBDF*UXPSLTKVTUBTUIF
2TQLGEVJOUFSGBDFJNQMFNFOUFECZQSPKFDUTTVDIBT7JTVBM#BTJDPS7JTVBM$FYDFQUUIBUB
GFXPGUIFQSPQFSUJFTXJMMSFUVSOPWNNPS0QVJKPIPSUISPXB5[UVGO0QV+ORNGOGPVGF'ZEGRVKQP
XIFODBMMFE5BCMFMJTUTUIFNFUIPETBOEQSPQFSUJFTPGUIF2TQLGEVPCKFDUUIBUSFUVSOB
NFBOJOHGVMWBMVFGPSUIF.JTDFMMBOFPVT'JMFTQSPKFDUBOEUIF2TQLGEV+VGOBOE2TQLGEV+VGOU
PCKFDUTDPOUBJOFEXJUIJOUIJTQSPKFDU
Chapter 8:
Table 8-1
Objects
Managing Solutions and Projects Programmatically
177
Methods and Properties of the Project, ProjectItems, and ProjectItem
Project
ProjectItems
ProjectItem
DTE
DTE
DTE
ProjectItems
Parent
Collection
Name (read-only)
Item
Name (read-only)
UniqueName
GetEnumerator / _NewEnum
FileCount
Kind
Kind
Kind
FullName
Count
FileNames
ContainingProject
SaveAs
Save
IsOpen
Open
Delete
Remove
ExpandView
ContainingProject
IsDirty
:PVDBOBEEOFX¾MFTUPUIF.JTDFMMBOFPVT'JMFTQSPKFDUCZVTJOHUIF+VGO1RGTCVKQPU0GY(KNG
NFUIPEXIJDIIBTUIFGPMMPXJOHNFUIPETJHOBUVSF
public EnvDTE.Window NewFile(string Item = "General\Text File",
string Name = "", string ViewKind =
"{00000000-0000-0000-0000-000000000000}")
#ZBQQMZJOHUIFUFDIOJRVFTXFVTFEFBSMJFSUPDBMDVMBUFUIF¾STUQBSBNFUFSGPSUIF
+VGO1RGTCVKQPU#FF0GY+VGONFUIPEXFDBO¾OEUIFWBMVFUIBUTIPVMECFQBTTFEUPUIF
0GY(KNGNFUIPE5IFTFDPOEQBSBNFUFSBMTPIBTUIFTBNFNFBOJOHBTUIFTFDPOEQBSBNFUFS
PGUIF+VGO1RGTCVKQPU#FF0GY+VGONFUIPE±UIFOBNFPGUIF¾MFXJUIFYUFOTJPO
UIBUJTUP
CFBEEFE±BOEJGUIFFNQUZTUSJOHJTQBTTFEBEFGBVMUOBNFJTDBMDVMBUFE5IFMBTUBSHVNFOU
TQFDJ¾FTXIJDIWJFXUIF¾MFTIPVMECFPQFOFEJOXIFOJUJTBEEFE5IFTFWBMVFTDBOCF
GPVOEXJUIJOUIF'PX&6'%QPUVCPVUDMBTTBOECFHJOXJUIUIFOBNFXU8KGY-KPF
Solution Folders
5IF.JTDFMMBOFPVT'JMFTQSPKFDUMJTUT¾MFTUIBUBSFUFNQPSBSJMZPQFOJOBOFEJUPSBOEXIFO
UIPTF¾MFTBSFDMPTFEUIFZBSFSFNPWFEGSPNUIBUQSPKFDU#VUXIBUJGZPVXBOUUPBTTPDJBUFB
¾MFXJUIBTPMVUJPOOPUIBWFUIBU¾MFCVJMUBTQBSUPGBQSPKFDUBOEIBWFUIBU¾MFTUBZXJUIJO
178
Working with Microsoft Visual Studio 2005
ZPVSTPMVUJPOXIFOUIFFEJUPSXJOE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BOKVTUESBHUIFQSPKFDUJOUPUIFBQQSPQSJBUFTPMVUJPOGPMEFS
"4PMVUJPO'PMEFSBMTPIBTBQSPHSBNNBUJDJOUFSGBDFUPUIJTGVODUJPOBMJUZ5PDSFBUFBTPMVUJPO
GPMEFSXJUIJOBTPMVUJPOTJNQMZDBMMUIF5QNWVKQP#FF5QNWVKQP(QNFGTNFUIPETVQQMZJOHB
OBNFGPSUIFOFXGPMEFS
Sub CreateSolutionFolder()
Dim solution2 As EnvDTE80.Solution2
solution2 = CType(DTE.Solution, EnvDTE80.Solution2)
solution2.AddSolutionFolder("My Folder")
End Sub
5IF#FF5QNWVKQP(QNFGTNFUIPESFUVSOTBO'PX&6'2TQLGEVPCKFDUXIJDIXPSLTBTBOZPUIFS
2TQLGEVPCKFDUTVDIBTUIBUBWBJMBCMFGSPN7JTVBM$PS7JTVBM#BTJDQSPKFDUTFYDFQUZPV
XJMM¾OEUIBUNBOZNFUIPETTVDIBTUIF5CXGNFUIPEXJMMOPUXPSLGPSBTPMVUJPOGPMEFS
CFDBVTFJUIBTOPNFBOJOHGPSUIBUQSPKFDUUZQF5IF1DLGEVQSPQFSUZPOUIF2TQLGEVJOUFSGBDF
GPSBTPMVUJPOGPMEFSSFUVSOTBOPCKFDUUIBUZPVDBOUIFODBTUJOUPUIFJOUFSGBDF'PX&6'
5QNWVKQP(QNFGT5IF5QNWVKQP(QNFGTJOUFSGBDFBMTPIBTBNFUIPEOBNFE#FF5QNWVKQP(QNFGT
XIJDIXJMMBMMPXZPVUPDSFBUFBOFTUFEGPMEFS5IJTNBDSPJTBNPEJ¾FEWFSTJPOPGUIFPOFUP
BEEBGPMEFSUPUIFTPMVUJPOCVUJUXJMMBMTPDSFBUFBOFTUFEGPMEFS
Sub CreateSolutionFolder()
Dim solution2 As EnvDTE80.Solution2
Dim project As EnvDTE.Project
Dim solutionFolder As EnvDTE80.SolutionFolder
solution2 = CType(DTE.Solution, EnvDTE80.Solution2)
project = solution2.AddSolutionFolder("MyFolder")
solutionFolder = CType(project.Object, EnvDTE80.SolutionFolder)
solutionFolder.AddSolutionFolder("My Other Folder")
End Sub
5IF5QNWVKQP(QNFGTJOUFSGBDFXJMMBMTPBMMPXZPVUPQSPHSBNNBUJDBMMZBEEQSPKFDUTPS¾MFT5IF
NFUIPE#FF(TQO(KNGXJMMUBLFUIFQBUIUPBOFYJTUJOHQSPKFDU¾MFB¾MFXJUIBOFYUFOTJPO
TVDIBTDTQSPKWCQSPKPSWDQSPK
BOEBEEJUUPBTPMVUJPOGPMEFS)FSFJTBOFYBNQMFPGUIF
Chapter 8:
Managing Solutions and Projects Programmatically
179
VTFPGUIJTNFUIPEXIJDIXJMM¾STUDSFBUFBTPMVUJPOGPMEFSOBNFE.Z'PMEFSBOEUIFOXJMM
BEEBOFYJTUJOH7JTVBM$QSPKFDUOBNFE$POTPMF"QQMJDBUJPOUPUIBUTPMVUJPOGPMEFS
Sub ProjectAdd()
Dim project As EnvDTE.Project
Dim solutionFolder As EnvDTE80.SolutionFolder
Dim solution2 As EnvDTE80.Solution2
solution2 = CType(DTE.Solution, EnvDTE80.Solution2)
project = solution2.AddSolutionFolder("MyFolder")
solutionFolder = project.Object
solutionFolder.AddFromFile("C:\Project\ConsoleApplication1.csproj")
End Sub
5IF#FF(TQO6GORNCVGNFUIPEUBLFTBQBUIUPBWTUFNQMBUF¾MFBWT[¾MFPSBOFYJTUJOH
QSPKFDUUPDMPOFBQBUIUPTUPSFUIFQSPKFDUJOBOEBOBNFPGUIFOFXQSPKFDUUPDSFBUF
#FF(TQO6GORNCVGUIFOHFOFSBUFTBOFXQSPKFDUCBTFEVQPOUIFXJ[BSEPSUIFFYJTUJOH
QSPKFDU5IJTNBDSPVTFTUIF)GV2TQLGEV6GORNCVGNFUIPEPGUIF5QNWVKQPJOUFSGBDFUP¾OEUIF
QBUIUPUIF7JTVBM$$POTPMF"QQMJDBUJPOUFNQMBUFBOEUIFODSFBUFTBQSPKFDUCBTFEVQPO
UIJTUFNQMBUFXJUIJOBOFXTPMVUJPOGPMEFS
Sub NewProjectAdd()
Dim project As EnvDTE.Project
Dim solutionFolder As EnvDTE80.SolutionFolder
Dim solution2 As EnvDTE80.Solution2
Dim CSConsoleTemplatePath As String
solution2 = CType(DTE.Solution, EnvDTE80.Solution2)
project = solution2.AddSolutionFolder("MyFolder")
solutionFolder = project.Object
CSConsoleTemplatePath = solution2.GetProjectTemplate _
("ConsoleApplication.zip", "CSharp")
solutionFolder.AddFromTemplate(CSConsoleTemplatePath, _
"C:\Projects\TestProject", "NewProject")
End Sub
"EEJOHBOFYJTUJOHPSOFX¾MFUPBTPMVUJPOGPMEFSJTEPOFXJUIJOUIF2TQLGEVPCKFDUCZVTJOH
UIF#FF(TQONFUIPETKVTUBTZPVXPVMEBEEBOJUFNUPB7JTVBM$PS7JTVBM+QSPKFDU
0ODFZPVIBWFBTPMVUJPOGPMEFSXJUIJUFNTMPDBUFEXJUIJOUIBUGPMEFSZPVDBOVTFUIFGPMEFS
BTB2TQLGEVPCKFDUUPMPDBUFBOJUFNDPOUBJOFEXJUIJOJU'PSFYBNQMFTVQQPTFZPVIBWFB
TPMVUJPOGPMEFSXJUIBQSPKFDUMPBEFEJOUPJU5IFGPMMPXJOHNBDSP¾OETUIF2TQLGEVGPSUIF
TPMVUJPOGPMEFSBOEUIFOPCUBJOTUIFDPMMFDUJPOPG2TQLGEV+VGOUGPSUIFTPMVUJPOGPMEFS&WFO
UIPVHIBQSPKFDUXJUIJOBTPMVUJPOGPMEFSJTBQSPKFDUJUJTTUJMMBOJUFNBOETPUIFTPMVUJPO
GPMEFSBMMPXTZPVUPHFUUPBMJTUPG2TQLGEV+VGOPCKFDUTCZVTJOHUIF2TQLGEV+VGOUQSPQFSUZ
0ODFZPVIBWFUIJT2TQLGEV+VGOPCKFDUZPVDBODBMMUIF1DLGEVQSPQFSUZUPSFUSJFWFUIFPCKFDU
TQFDJ¾DUPUIBUQSPKFDUJUFNOPEFXIJDIJTBQSPKFDU
Sub FindProjectInSolutionFolder()
Dim slnFolderProject As Project
180
Working with Microsoft Visual Studio 2005
Dim containedProject As Project
Dim projectAsAProjectItem As ProjectItem
'Find the solution folder. This code expects one folder and
' no other items within the solution
slnFolderProject = DTE.Solution.Projects.Item(1)
'Find the project item for the project in the folder
projectAsAProjectItem = slnFolderProject.ProjectItems.Item(1)
'Convert the project item into a Project
containedProject = projectAsAProjectItem.Object
MsgBox(containedProject.Name)
End Sub
5IJTJTUIFPOMZXBZUIBUZPVDBOHFUUPB2TQLGEVPCKFDUGPSBQSPKFDUUIBUJTMPDBUFEXJUIJO
BTPMVUJPOGPMEFS5IF5QNWVKQP2TQLGEVU+VGONFUIPEXJMMOPUSFUVSOBQSPKFDUµTPCKFDUGPS
BQSPKFDUJOBTPMVUJPOGPMEFSCFDBVTFUIF2TQLGEVU+VGONFUIPEMPPLTPOMZBUUIFQSPKFDUT
EJSFDUMZVOEFSOFBUIUIFTPMVUJPOOPEFJO4PMVUJPO&YQMPSFS
*GZPVIBWFBSFGFSFODFUPB2TQLGEVPCKFDUFJUIFSBQSPKFDUTVDIBTB7JTVBM+QSPKFDUPSB
2TQLGEVPCKFDUGPSBTPMVUJPOGPMEFSBOEUIBUQSPKFDUJTDPOUBJOFEXJUIJOBTPMVUJPOGPMEFS
ZPVDBO¾OEUIF2TQLGEV+VGOPCKFDUPGUIBUQSPKFDUXJUIJOUIFTPMVUJPOGPMEFSCZVTJOHUIF
2CTGPV2TQLGEV+VGOQSPQFSUZ5IJTNBDSPXJMM¾OEB2TQLGEVPCKFDUXJUIJOBTPMVUJPOGPMEFSBOE
UIFOXBMLCBDLVQUIFIJFSBSDIZUP¾OEUIFTPMVUJPOGPMEFSUIBUDPOUBJOTJU
Sub FindParentProject()
'First, find the project nested in the solution folder:
Dim nestedProject As Project
Dim solutionFolder As Project
solutionFolder = DTE.Solution.Projects.Item(1)
nestedProject = solutionFolder.ProjectItems.Item(1).Object
'Now, find the solution folder parent of the nested project
Dim parentProjectItem As ProjectItem
Dim parentProject As Project
parentProjectItem = nestedProject.ParentProjectItem
parentProject = parentProjectItem.ContainingProject
'Make sure the parent project and the solution folder are the same
MsgBox(parentProject.UniqueName = solutionFolder.UniqueName)
End Sub
Unmodeled Projects
"MMUIFQSPKFDUUZQFTXFµWFEJTDVTTFETPGBSIBWFJNQMFNFOUFEB2TQLGEVPCKFDUUIBUDBOCF
VTFECZBNBDSPPSBOBEEJO)PXFWFSBGFXQSPKFDUUZQFTTVDIBTBEBUBCBTFQSPKFDUPS
BQSPKFDUUIBUIBTCFFOVOMPBEFECZVTJOHUIF1SPKFDU]6OMPBE1SPKFDUDPNNBOEEPOµU
JNQMFNFOUUIF2TQLGEVPCKFDUUIFNTFMWFT5PBMMPXTPNFQSPHSBNNBCJMJUZGPSUIFTFQSPKFDU
UZQFT7JTVBM4UVEJPTVQQPSUTUIFWPOQFGNGFQSPKFDUUZQF"OVONPEFMFEQSPKFDUQSPWJEFT
Chapter 8:
Managing Solutions and Projects Programmatically
181
BOJNQMFNFOUBUJPOPGUIF2TQLGEVPCKFDUUIBUTVQQPSUTPOMZUIFQSPQFSUJFTDPNNPOBNPOH
BMMQSPKFDUUZQFTXIJDIBSF&6'-KPFBOE0COG"MMPUIFSQSPQFSUJFTBOENFUIPETPO
UIJTJNQMFNFOUBUJPOPGUIF2TQLGEVPCKFDUSFUVSOWBMVFTUIBUIBWFOPVTFGVMNFBOJOHPS
HFOFSBUFBOFYDFQUJPOXIFODBMMFEBOETIPVMEOµUCFVTFECZBNBDSPPSBOBEEJO:PVDBO
EJTUJOHVJTIBOVONPEFMFEQSPKFDUGSPNPUIFSQSPKFDUUZQFTCZDIFDLJOHUIF2TQLGEV-KPF
QSPQFSUZXIJDISFUVSOTUIFDPOTUBOU'PX&6'%QPUVCPVUXU2TQLGEV-KPF7POQFGNGFJGUIFQSPKFDU
JTBOVONPEFMFEQSPKFDU5IFGPMMPXJOHNBDSPFOVNFSBUFTBMMUIFQSPKFDUTMPBEFEJOUPB
TPMVUJPOBOEEFUFSNJOFTXIJDIPOFTBSFVONPEFMFE
Sub FindUnmodeledProjects()
Dim project As EnvDTE.Project
For Each project In DTE.Solution.Projects
If (project.Kind = EnvDTE.Constants.vsProjectKindUnmodeled) Then
MsgBox(project.Name + " is unmodeled")
End If
Next
End Sub
Project and Project Item Events
+VTUBTBTPMVUJPO¾SFTFWFOUTUPBMMPXBOBEEJOPSNBDSPUPSFTQPOEUPUIFBDUJPOTUIFVTFS
JTQFSGPSNJOHUIFWBSJPVTQSPKFDUUZQFTBMTP¾SFFWFOUTTPUIBUBOBEEJOPSBNBDSPDBOCF
JOGPSNFEPGXIBUUIFVTFSJTEPJOH:PVDPOOFDUUPUIFFWFOUT¾SFECZUIFEJGGFSFOUQSPKFDU
UZQFTJOEJGGFSFOUXBZTCVUFBDIQSPKFDUUZQFTVQQPSUTUIFTBNFJOUFSGBDFTVTFEUPIBOEMFUIF
FWFOUJOWPDBUJPOT&BDIQSPKFDU¾SFTUXPDMBTTFTPGFWFOUTBDUJPOTQFSGPSNFEXJUIUIFQSPKFDU
BOEBDUJPOTQFSGPSNFEXJUIUIFJUFNTXJUIJOUIPTFQSPKFDUT)FSFBSFUIFFWFOUTBOEUIF
TJHOBUVSFTUIBUBSFDBMMFEXIFOBQSPKFDUJTBEEFESFNPWFEPSSFOBNFEXJUIJOBTPMVUJPO
void ItemAdded(ByVal Project As EnvDTE.Project)
void ItemRemoved(ByVal Project As EnvDTE.Project)
void ItemRenamed(ByVal Project As EnvDTE.Project,
ByVal OldName As String)
5IFTFBSFUIFTJHOBUVSFTPGFWFOUT¾SFEXIFOBQSPKFDUJUFNJTBEEFEUPSFNPWFEGSPNPS
SFOBNFEXJUIJOBQSPKFDU
void ItemAdded(ByVal ProjectItem As EnvDTE.ProjectItem)
void ItemRemoved(ByVal ProjectItem As EnvDTE.ProjectItem)
void ItemRenamed(ByVal ProjectItem As EnvDTE.ProjectItem, _
ByVal OldName As String)
5PDPOOFDUUPUIFTFFWFOUTZPVDBOVTFUIF'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EVNFUIPE
QBTTJOHJOBTQFDJBMTUSJOHJOEJDBUJOHUIFQSPKFDUUZQFZPVXBOUUPDPOOFDUUP#VU¾STUZPV
OFFEUPTFUVQUIFFWFOUWBSJBCMFTUPIBOEMFUIFFWFOU:PVEFDMBSFUIFFWFOUWBSJBCMFXJUIJO
BNBDSPQSPKFDUCZBEEJOHUIFGPMMPXJOHDPEFUPUIF'PXKTQPOGPV'XGPVUNPEVMF5IJT
FYBNQMFDPOOFDUTUPUIF7JTVBM$QSPKFDUFWFOUT
<System.ContextStaticAttribute()> _
Public WithEvents csharpProjectItemsEvents As EnvDTE.ProjectItemsEvents
8IFOZPVFOUFSUIJTDPEFBOFOUSZBQQFBSTJOUIFMFGUESPQEPXOMJTUBUUIFUPQPGUIF
DPEFGPSUIF'PXKTQPOGPV'XGPVUNBDSPNPEVMF4FMFDUUIFFOUSZUP¾MMUIFSJHIUESPQEPXO
MJTUXJUIUIFFWFOUTGPSUIJTPCKFDUBOETFMFDUFBDIFWFOUUPDSFBUFUIFDPEFOFDFTTBSZGPS
DBQUVSJOHUIBUFWFOU"UUIJTQPJOUUIFFWFOUIBOEMFSXPOµUCFJOWPLFEGPS7JTVBM$QSPKFDUT
CFDBVTFUIFFWFOUWBSJBCMFEUJCTR2TQLGEV+VGOU'XGPVUIBTZFUUPCFTFUUPBOJOTUBODFPGB
2TQLGEV+VGOU'XGPVUPCKFDU5PTFUUIJTWBSJBCMFUPBOJOTUBODFPGUIFDPSSFDUFWFOUPCKFDUDSFBUF
BIBOEMFSGPS&6''XGPVU1P5VCTVWR%QORNGVGBOEQMBDFXJUIJOJUUIFDPEFUPDPOOFDUUPUIF
FWFOUNVDIBTZPVXPVMEXJUIJOBOBEEJO
Private Sub DTEEvents_OnStartupComplete() _
Handles DTEEvents.OnStartupComplete
csharpProjectItemsEvents = _
DTE.Events.GetObject("CSharpProjectItemsEvents")
End Sub
8JUIUIJTFWFOUIBOEMFSJOQMBDFXIFO7JTVBM4UVEJPJTDMPTF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CVHTPMVUJPODPO¾HVSBUJPOUPCVJME
UIFSFMFBTFWFSTJPOPGBQSPKFDUUIBUJTMPBEFEJOUPUIFTPMVUJPO
Sub ChangeProjectConfiguration()
Dim solutionBuild As EnvDTE.SolutionBuild
Dim solutionCfgs As EnvDTE.SolutionConfigurations
Dim solutionCfg As EnvDTE.SolutionConfiguration
Dim solutionContext As EnvDTE.SolutionContext
'Find the debug solution configuration:
solutionBuild = DTE.Solution.SolutionBuild
solutionCfgs = solutionBuild.SolutionConfigurations
Chapter 8:
Managing Solutions and Projects Programmatically
185
solutionCfg = solutionCfgs.Item("Debug")
'Retrieve the solution context for the first project:
solutionContext = solutionCfg.SolutionContexts.Item(1)
'Change the debug solution context to build the
' Release project configuration:
solutionContext.ConfigurationName = "Release"
'Reset the build flag for this context:
solutionContext.ShouldBuild = True
End Sub
:PVDBONPEJGZB5QNWVKQP%QPVGZVUPTFUUIFQSPKFDUDPO¾HVSBUJPOUIBUTIPVMECFCVJMUGPS
BQBSUJDVMBSTPMVUJPODPO¾HVSBUJPOBOEZPVDBOBMTPTFUWBMVFTTVDIBTUIBUTQFDJGZJOH
XIFUIFSUIFQSPKFDUDPO¾HVSBUJPOTIPVMECFCVJMU5IJTJTEPOFJOUIFOFYUUPMBTUMJOFPGUIF
QSFDFEJOHNBDSPXIFSFUIF5JQWNF$WKNFQSPQFSUZJTTFUUPVTWG*OUIJTNBDSPUIJTQSPQFSUZ
NVTUCFTFUCFDBVTFBTJTFYQFDUFEXIFOUIFEFCVHTPMVUJPODPO¾HVSBUJPOJT¾STUDSFBUFE
JUEPFTOµUDPOUBJOUIFSFMFBTFQSPKFDUDPO¾HVSBUJPO*UUIFSFGPSFJTOµUTFUUPCVJMEGPSUIBU
TPMVUJPODPO¾HVSBUJPOTPXIFOUIFEFCVHTPMVUJPODPO¾HVSBUJPOJTTFUUPCVJMEUIFSFMFBTF
QSPKFDUDPO¾HVSBUJPOUIBU²EPOPUCVJME³TUBUFJTDBSSJFEBMPOHXJ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QMFTVQQPTFBOPQFOTPMVUJPODPOUBJOTUXPQSPKFDUTFBDIPGUIFNUPCFEFTJHOBUFE
BTBTUBSUVQQSPKFDU:PVDBOVTFDPEFTVDIBTUIFGPMMPXJOHUPTFUUIFTFQSPKFDUTBTTUBSUVQ
QSPKFDUT
Sub SetStartupProjects()
Dim startupProjects(1) As Object
startupProjects(0) = DTE.Solution.Projects.Item(1).UniqueName
startupProjects(1) = DTE.Solution.Projects.Item(2).UniqueName
DTE.Solution.SolutionBuild.StartupProjects = startupProjects
End Sub
*GPOMZPOFQSPKFDUTIPVMECFTFUBTBTUBSUVQQSPKFDUUIFDPEFMPPLTMJLFUIJT
Sub SetStartupProject()
Dim startupProject As String
startupProject = DTE.Solution.Projects.Item(1).UniqueName
DTE.Solution.SolutionBuild.StartupProjects = startupProject
End Sub
8IFOZPVTFUUIFTUBSUVQQSPKFDUTZPVNVTUCFDBSFGVMUPTVQQMZPOMZCVJMEBCMFQSPKFDUT
*GPOFPGUIFQSPKFDUTTVQQMJFEUP5QNWVKQP$WKNF5VCTVWR2TQLGEVUJTGPSFYBNQMFUIFVOJRVF
OBNFGPSUIF.JTDFMMBOFPVT'JMFTQSPKFDUPSUIF4PMVUJPO*UFNTQSPKFDUBOFSSPSJT
HFOFSBUFE
Project Dependencies
8IFOZPVXPSLXJUIBTPMVUJPOUIBUDPOUBJOTNVMUJQMFQSPKFDUTUIFDPNQPOFOUTCVJMUCZ
POFQSPKFDUNJHIUSFMZPOUIFPVUQVUPGBOPUIFSQSPKFDU"OFYBNQMFPGUIJTJTBDPOUSPM
QSPKFDUDBMMFE6TFS$POUSPMXIJDIJTQMBDFEPOUIFGPSNPGB8JOEPXT'PSNTBQQMJDBUJPO
DBMMFE8JO'PSN#FDBVTFDIBOHFTUPUIF6TFS$POUSPMQSPKFDUNJHIUBGGFDUIPXUIBUDPOUSPM
JTVTFECZUIF8JOEPXT'PSNTQSPKFDUUIF6TFS$POUSPMQSPKFDUNVTUCFDPNQJMFECFGPSF
UIF8JO'PSNQSPKFDUJTDPNQJMFE5PFOGPSDFUIJTSFMBUJPOTIJQCFUXFFOUIFUXPQSPKFDUTZPV
DBODSFBUFBQSPKFDUEFQFOEFODZ5IFEFQFOEFODJFTCFUXFFOUXPPSNPSFQSPKFDUTDBOCF
EFQJDUFEVTJOHBEFQFOEFODZHSBQIUIFEFQFOEFODZHSBQIGPSUIFQSPKFDUT8JO'PSNBOE
6TFS$POUSPMJTTIPXOJO'JHVSF5IFBSSPXJTQPJOUJOHUPUIFQSPKFDUUIBUBOPUIFSQSPKFDU
JTEFQFOEFOUPO
WinForm
Figure 8-4
UserControl
A dependency graph showing a WinForm project dependent on a UserControl project
4VQQPTFXFBEEBOFXQSPKFDUUPUIFTPMVUJPO±BDMBTTMJCSBSZDBMMFE$MBTT-JCUIBUJNQMFNFOUT
GVODUJPOBMJUZVTFECZCPUIUIF8JO'PSNBOEUIF6TFS$POUSPMQSPKFDUT"EFQFOEFODZHSBQI
GPSUIJTTPMVUJPOJTTIPXOJO'JHVSF
Chapter 8:
Managing Solutions and Projects Programmatically
187
UserControl
WinForm
ClassLib
Figure 8-5
The dependency graph for three projects
:PVDBOTFFJOUIJTEFQFOEFODZHSBQIUIBUUIF8JO'PSNQSPKFDUDBOµUCFCVJMUVOUJMUIF
6TFS$POUSPMBOE$MBTT-JCQSPKFDUTIBWFCFFOCVJMU5IF6TFS$POUSPMQSPKFDUSFMJFTPO
POMZUIF$MBTT-JCQSPKFDUCFJOHCVJMU¾STU8IFOBCVJMEPGUIJTTPMVUJPOJTTUBSUFEJGUIF
CVJMETZTUFNDIPPTFTUIF6TFS$POUSPMQSPKFDUUPTUBSUCVJMEJOH¾STUUIF$MBTT-JCQSPKFDU
CVJMET*GUIFCVJMETZTUFNDIPPTFTUIF$MBTT-JCQSPKFDU¾STUCFDBVTFJUEPFTOPUIBWFBOZ
EFQFOEFODJFTJUDBOCVJMEJNNFEJBUFMZXJUIPVUOFFEJOHUPCVJMEBOZPUIFSQSPK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FOEFODJFTXIJDIXJMMEJTQMBZUIF1SPKFDU%FQFOEFODJFTEJBMPHCPY
TIPXOJO'JHVSF
5IFEJBMPHCPYTIPXTBMMUIFQSPKFDUTUIBUDBOCFTFUBTBEFQFOEFODZ
GPSUIF6TFS$POUSPMQSPKFDU5IF8JO'PSNDIFDLCPYJTTIBEFECFDBVTFBEFQFOEFODZJTTFU
GSPNUIF8JO'PSNQSPKFDUUPUIF6TFS$POUSPMQSPKFDUBOE7JTVBM4UVEJPXPOµUBMMPXBDZDMF
CFUXFFOUIF8JO'PSNQSPKFDUBOEUIF6TFS$POUSPMQSPKFDUUPCFDSFBUFE
:PVDBOBMTPTFUCVJMEEFQFOEFODJFTUISPVHIUIFPCKFDUNPEFM5IF5QNWVKQP$WKNF
$WKNF&GRGPFGPEKGUQSPQFSUZSFUVSOTB$WKNF&GRGPFGPEKGUPCKFDUXIJDIJTBDPMMFDUJPOPG
$WKNF&GRGPFGPE[PCKFDUT:PVDBOJOEFYUIJTDPMMFDUJPOCZVTJOHUIF+VGONFUIPE±ZPVDBO
188
Working with Microsoft Visual Studio 2005
QBTTBOVNFSJDJOEFYBO'PX&6'2TQLGEVPCKFDUPSUIFVOJRVFOBNFPGBQSPKFDU&BDI
QSPKFDUJOUIFTPMVUJPOIBTJUTPXO'PX&6'$WKNF&GRGPFGPE[PCKFDUXIPTF4GSWKTGF2TQLGEVU
QSPQFSUZZPVDBOVTFUPBEESFNPWFPSSFUSJFWFEFQFOEFODJFTGPSBQSPKFDU5IFGPMMPXJOH
NBDSPEJTQMBZTJOUIF0VUQVUXJOEPXUIFBWBJMBCMFQSPKFDUTJOUIFPQFOTPMVUJPOBTXFMMBT
BMMUIFQSPKFDUTJUEFQFOETPO
Figure 8-7
Setting project dependencies
Sub Depends()
Dim projectDep As EnvDTE.BuildDependency
Dim project As EnvDTE.Project
Dim owp As New InsideVSNET.Utilities.OutputWindowPaneEx(DTE,
"Build dependencies")
For Each projectDep In DTE.Solution.SolutionBuild.BuildDependencies
Dim reqProjects As Object()
owp.Write("The project ")
owp.Write(projectDep.Project.Name)
owp.WriteLine(" relies on:")
reqProjects = projectDep.RequiredProjects
If (reqProjects.Length = 0) Then
owp.WriteLine(vbTab + "<None>")
Else
For Each project In reqProjects
owp.WriteLine(vbTab + project.Name)
Next
End If
owp.WriteLine()
Next
End Sub
6TJOHUIF$WKNF&GRGPFGPE[PCKFDUZPVDBODSFBUFBNBDSPPSBOBEEJOUIBUTFUTVQUIF
EFQFOEFODJFTCFUXFFOUXPPSNPSFQSPKFDUT4VQQPTFVTJOHPVSDVSSFOUFYBNQMFUIBU
Chapter 8:
Managing Solutions and Projects Programmatically
189
BTPMVUJPOXJUIUIFQSPKFDUT8JO'PSN6TFS$POUSPMBOE$MBTT-JCJTMPBEFEBOEOP
EFQFOEFODJFTIBWFCFFOTFU5IF$WKNF&GRGPFGPE[PCKFDUTVQQPSUTUISFFNFUIPETGPS
NPEJGZJOHUIFQSPKFDUTUIBUBQSPKFDUJTEFQFOEFOUPO#FF2TQLGEV4GOQXG2TQLGEVBOE
4GOQXG#NN2TQLGEVU#FF2TQLGEVBOE4GOQXG2TQLGEVBDDFQUUIFVOJRVFOBNFPGBQSPKFDUUIBU
TIPVMECFBEEFEPSSFNPWFEBTBEFQFOEFODZGPSBTQFDJ¾DQSPKFDU4GOQXG#NN2TQLGEVUUBLFT
OPBSHVNFOUTBOESFNPWFTBMMQSPKFDUEFQFOEFODJFT5IFGPMMPXJOHNBDSP5GV&GRGPFGPEKGU
CVJMETUIFDPSSFDUEFQFOEFODJFTGPSUIFUISFFQSPKFDUTPMVUJPOUPDPOGPSNUPUIFEFQFOEFODZ
HSBQITIPXOJO'JHVSF
Sub SetDependencies()
Dim buildDependencies As EnvDTE.BuildDependencies
Dim buildDependency As EnvDTE.BuildDependency
Dim project As EnvDTE.Project
Dim winFormUniqueName As String
Dim userControlUniqueName As String
Dim classLibUniqueName As String
'Gather up the unique name of each project
For Each project In DTE.Solution.Projects
If (project.Name = "WinForm") Then
winFormUniqueName = project.UniqueName
ElseIf (project.Name = "UserControl") Then
userControlUniqueName = project.UniqueName
ElseIf (project.Name = "ClassLib") Then
classLibUniqueName = project.UniqueName
End If
Next
buildDependencies = DTE.Solution.SolutionBuild.BuildDependencies
For Each buildDependency In buildDependencies
If (buildDependency.Project.Name = "WinForm") Then
buildDependency.RemoveAllProjects()
'Add all projects except the WinForm
' project as a dependency:
buildDependency.AddProject(userControlUniqueName)
buildDependency.AddProject(classLibUniqueName)
ElseIf (buildDependency.Project.Name = "UserControl") Then
buildDependency.RemoveAllProjects()
'Add a dependency to the ClassLib project:
buildDependency.AddProject(classLibUniqueName)
End If
Next
End Sub
Manipulating Project Settings
4PMVUJPODPO¾HVSBUJPOTBSFVTFEUPHSPVQUPHFUIFSQSPKFDUDPO¾HVSBUJPOT&BDIQSPKFDU
DPOUBJOTBOVNCFSPGDPO¾HVSBUJPOTUIBUDPOUSPMIPXUIFDPNQJMFSTIPVMEDSFBUFUIF
QSPHSBNDPEFGPSUIBUQSPKFDU#FDBVTFBQSPKFDUDBOIBWFNVMUJQMFQSPKFDUDPO¾HVSBUJPOT
BTTPDJBUFEXJUIJUZPVDBOHFOFSBUFEJGGFSFOUWFSTJPOTPGBQSPHSBN
190
Working with Microsoft Visual Studio 2005
ConfigurationManager Object
:PVNBOBHFQSPKFDUDPO¾HVSBUJPOTUISPVHIUIF%QP¿IWTCVKQP/CPCIGTPCKFDUXIJDIIBTB
DPMMFDUJPOPG%QP¿IWTCVKQPPCKFDUTBOEMFUTZPVDSFBUFOFXDPO¾HVSBUJPOT$PO¾HVSBUJPOT
GPSBQSPKFDUBSFBSSBOHFEJOBHSJEQBUUFSOXJUIUIFDPO¾HVSBUJPOUZQFTVDIBTEFCVHPS
SFMFBTFBMPOHPOFBYJTPGUIFHSJEBOEUIFQMBUGPSNPOXIJDIUIFDPO¾HVSBUJPOXJMMCFCVJMU
GPSPOUIFPUIFSBYJT5IFQMBUGPSNTUIBU7JTVBM4UVEJPDVSSFOUMZTVQQPSUTBSF8JOˆGPS
CJU.JDSPTPGU8JOEPXTˆSVOOJOHPOUIFYQSPDFTTPSBOE"OZ$16UIFDPO¾HVSBUJPO
OBNF"OZ$16JTHJWFOUPQSPHSBNTUIBUXJMMSVOPOUIF/&5'SBNFXPSL
JGUIFQSPKFDU
JTCFJOHDPNQJMFEGPSUIF.JDSPTPGU/&5QMBUGPSN±JODMVEJOH/&5BQQMJDBUJPOTGPSUIF
EFTLUPQPSTNBSUEFWJDF#FDBVTFQSPKFDUTDBOCVJMEPOMZPOFQMBUGPSNUZQFBUBUJNFUIF
TFDPOEBYJTXJMMBMXBZTIBWFPOFEJNFOTJPO
Note Previous versions of Visual Studio used the name .NET for the platform name when
compiling to MSIL bytecode. If you are upgrading an add-in or macro to Visual Studio, you
will need to change your code to use the new name, Any CPU.
:PVDBO¾OEBQBSUJDVMBSQSPKFDUDPO¾HVSBUJPOJOTFWFSBMXBZT5IF¾STUXBZJTUPVTF
UIFGBNJMJBS+VGONFUIPEUIBUµTBWBJMBCMFPOBMMDPMMFDUJPOPCKFDUT)PXFWFSVOMJLFNPTU
PUIFS+VGONFUIPETPODPMMFDUJPOPCKFDUTUIF%QP¿IWTCVKQP/CPCIGT+VGONFUIPESFRVJSFT
UXPQBSBNFUFST5IF¾STUQBSBNFUFSDBOCFBOVNFSJDBMJOEFYBOETQBOTUIFFOUJSFHSJE
PGQMBUGPSNTBOEDPO¾HVSBUJPOT:PVDBOBMTPVTF+VGOUPEJSFDUMZMPDBUFB%QP¿IWTCVKQP
CZQBTTJOHUIFDPO¾HVSBUJPOOBNFBTUIF¾STUQBSBNFUFSBOEUIFQMBUGPSNOBNFBTUIF
TFDPOEQBSBNFUFS4VQQPTFB7JTVBM$QSPKFDUJTPQFOJO4PMVUJPO&YQMPSFS5P¾OEUIF
%QP¿IWTCVKQPPCKFDUGPSUIF8JOEFCVHCVJMEZPVDBOVTFDPEFTVDIBTUIFGPMMPXJOH
Sub RetrieveDebugWin32Configuration()
Dim config As Configuration
Dim project As EnvDTE.Project
project = DTE.Solution.Projects.Item(1)
config = project.ConfigurationManager.Item("Debug", "Win32")
End Sub
"OPUIFSXBZUPSFUSJFWFTQFDJ¾DDPO¾HVSBUJPOTJTUPVTFUIF%QP¿IWTCVKQP/CPCIGT
%QP¿IWTCVKQP4QYBOE%QP¿IWTCVKQP/CPCIGT2NCVHQTONFUIPETXIJDIUBLFUIFCVJMEUZQF
BOEUIFQMBUGPSNOBNFSFTQFDUJWFMZ5IFTFNFUIPETSFUVSOBDPMMFDUJPOPG%QP¿IWTCVKQP
PCKFDUTUIBUZPVDBOJUFSBUFUISPVHIUP¾OEBTQFDJ¾DJUFN5IF%QP¿IWTCVKQP4QYNFUIPE
SFUVSOTBMJTUPGBMMDPO¾HVSBUJPOTXJUIUIFQBTTFEOBNFUIF2NCVHQTONFUIPESFUVSOTBMJTU
PGBMMDPO¾HVSBUJPOTCFMPOHJOHUPBTQFDJ¾DQMBUGPSN5IFTFNFUIPETBSFNPTUVTFGVMJGZPV
XBOUUPNPEJGZUIFTFUUJOHTPGDPO¾HVSBUJPOTUIBUBSFDMPTFMZSFMBUFEUPPOFBOPUIFSTVDI
BTXBMLJOHBMMUIF8JODPO¾HVSBUJPOTPGB7JTVBM$QSPKFDUBOEFOBCMJOHNBOBHFE
FYUFOTJPOTUIVTBMMPXJOHZPVSQSPHSBNUPVTFUIF/&5'SBNFXPSLJO$DPEF5IF
GPMMPXJOHDPEFTBNQMFEPFTKVTUUIBU"GUFS¾OEJOHUIF8JODPO¾HVSBUJPOTBWBJMBCMFUPB
Chapter 8:
Managing Solutions and Projects Programmatically
191
QSPKFDUJUSFUSJFWFTUIF2TQRGTVKGUPCKFDUGPSUIBUDPO¾HVSBUJPOBOETFUTUIF/CPCIGF'ZVGPUKQP
QSPQFSUZUPVTWGBMMPXJOHUIFDPNQJMFSUPHFOFSBUFDPEFUIBUDBOXPSLXJUIUIF/&5
'SBNFXPSL
Sub SetManagedExtensionsProperty()
Dim configManager As ConfigurationManager
Dim configs As Configurations
Dim config As Configuration
Dim project As EnvDTE.Project
project = DTE.Solution.Projects.Item(1)
configManager = project.ConfigurationManager
configs = configManager.Platform("Win32")
For Each config In configs
Dim prop As EnvDTE.Property
prop = config.Properties.Item("ManagedExtensions")
prop.Value = True
Next
End Sub
:PVDBODSFBUFOFXDPO¾HVSBUJPOTCBTFEPOBOFYJTUJOHDPO¾HVSBUJPOJOUIFTBNF
XBZUIBUZPVDBODSFBUFOFXTPMVUJPODPO¾HVSBUJPOTCZDPQZJOHBOFYJTUJOHTPMVUJPO
DPO¾HVSBUJPO:PVDSFBUFOFXQSPKFDUDPO¾HVSBUJPOTCZVTJOHUIF%QP¿IWTCVKQP/CPCIGT
#FF%QP¿IWTCVKQP4QYNFUIPE5IJTNFUIPEUBLFTBTJUTQBSBNFUFSTUIFOBNFPGUIF
OFXDPO¾HVSBUJPOBOEBOFYJTUJOHDPO¾HVSBUJPOOBNFXIJDIJTVTFEBTBUFNQMBUF
GPSDSFBUJOHUIFOFXDPO¾HVSBUJPO#FF%QP¿IWTCVKQP4QYBMTPBDDFQUTBTBOBSHVNFOU
B$QQNGCPWBMVF5IJTQBSBNFUFSOBNFE2TQRCICVGXPSLTJOUIFTBNFXBZBTUIF
2TQRCICVGQBSBNFUFSPGUIF5QNWVKQP%QP¿IWTCVKQPU#FFNFUIPECVUJOSFWFSTF8IFOUIF
5QNWVKQP%QP¿IWTCVKQPU#FFNFUIPEJTDBMMFEXJUIUIF2TQRCICVGQBSBNFUFSTFUUPVTWGBDPQZ
PGUIFTPMVUJPODPO¾HVSBUJPOBOEBMMUIFQSPKFDUDPO¾HVSBUJPOTJUDPOUBJOTJTNBEF*GUIF
#FF%QP¿IWTCVKQP4QYNFUIPEJTDBMMFEXJUIJUT2TQRCICVGQBSBNFUFSTFUUPVTWGUIFDVSSFOUMZ
BDUJWFTPMVUJPODPO¾HVSBUJPOJTDPQJFEJUTOBNFJTTFUUPUIFOBNFQBTTFEBTUIFOFXQSPKFDU
DPO¾HVSBUJPOBOEUIFOFXTPMVUJPODPO¾HVSBUJPOJTNPEJ¾FEUPDPOUBJOUIFOFXMZDSFBUFE
QSPKFDUDPO¾HVSBUJPO
Note The ConfigurationManager object contains the method AddPlatform, which
works much the same as the AddConfigurationRow method but adds a platform row to the
build type configuration grid. If you call this method for any of the current versions of the
Microsoft-language products, an exception will be generated because new platforms can’t be
added for these project types. This doesn’t mean that this method won’t work for third-party
programming language projects or future versions of Microsoft programming languages.
.PTUQSPKFDUUZQFTTVQQPSUPOMZPOFQMBUGPSNUZQFCVUTPNFQSPKFDUTTVDIBTTFUVQ
QSPKFDUTBSFOPUBTTPDJBUFEXJUIBOZQMBUGPSN±XIBUJTCVJMUJTQMBUGPSNBHOPTUJD"TFUVQ
QSPKFDUEPFTOµUDBSFXIFUIFSJUTDPOUFOUTBSFJOUFOEFEGPS8JOPS/&5QMBUGPSNTJUT
SPMFJTUPDPOUBJO¾MFTUPCFJOTUBMMFEPOUPUIFVTFSµTDPNQVUFSTPBQMBUGPSNJTOPUB
DPOTJEFSBUJPOXIFOZPVCVJMEBTFUVQQSPKFDU#FDBVTFUIFCVJMEUZQFDPO¾HVSBUJPOHSJE
192
Working with Microsoft Visual Studio 2005
DBOµUCFPOFEJNFOTJPOBMBQTFVEPQMBUGPSNJTHFOFSBUFEGPSUIFTFQSPKFDUUZQFTBOEJUT
OBNFJTTFUUP0# Project Configuration Properties
1SPKFDUDPO¾HVSBUJPOTEJGGFSJOUIFQSPQFSUZWBMVFTUIBUBSFTFU'PSFYBNQMFPOFEJGGFSFODF
CFUXFFOUIFEFCVHBOESFMFBTFDPO¾HVSBUJPOTJTUIBUUIFEFCVHDPO¾HVSBUJPOEPFTOµU
PQUJNJ[FUIFDPEFXIJDINBLFTEFCVHHJOHFBTJFSUPQFSGPSNBOEPQUJNJ[BUJPOJTUVSOFE
POGPSUIFSFMFBTFDPO¾HVSBUJPOUPNBLFUIFDPEFSVOGBTUFS4VDIQSPQFSUJFTBSFTFUUISPVHI
UIFPCKFDUSFUVSOFECZDBMMJOHUIF%QP¿IWTCVKQP2TQRGTVKGUQSPQFSUZ"TZPVTBXFBSMJFSJOUIF
5GV/CPCIGF'ZVGPUKQPU2TQRGTV[NBDSPFYBNQMFUIJTQSPQFSUZSFUVSOTBO'PX&6'2TQRGTVKGU
PCKFDU±UIFTBNFPCKFDUUIBUJTVTFEUISPVHIPVU7JTVBM4UVEJPUPTFUQSPQFSUZWBMVFTPO
WBSJPVTPCKFDUT5IFGPMMPXJOHNBDSPSFUSJFWFTUIFEFCVHBOESFMFBTFDPO¾HVSBUJPOTGPSB
QSPKFDUSFBETUIF$QQNGCP1RVKOK\GDPO¾HVSBUJPOQSPQFSUZOFHBUFTJUBOEUIFOTUPSFTJU
CBDLJOUPUIFDPO¾HVSBUJPO5IJTNFBOTUIBUUIF1RVKOK\GQSPQFSUZJTJOWFSUFEGPSBMMUIFTF
DPO¾HVSBUJPOT
Sub SwapOptimizationSettings()
Dim project As EnvDTE.Project
Dim configManager As EnvDTE.ConfigurationManager
Dim configs As EnvDTE.Configurations
Dim config As EnvDTE.Configuration
Dim props As EnvDTE.Properties
'Find the ConfigurationManager for the project:
project = DTE.Solution.Projects.Item(1)
configManager = project.ConfigurationManager
'Get the debug configuration manager
configs = configManager.ConfigurationRow("Debug")
'Walk each configuration in the debug configuration row
For Each config In configs
Dim optimize As Boolean
'Get the Optimize property for the configuration
props = config.Properties
optimize = props.Item("Optimize").Value
'Negate the value
props.Item("Optimize").Value = Not optimize
Next
'Repeat for the release configuration
configs = configManager.ConfigurationRow("Release")
For Each config In configs
Dim optimize As Boolean
'Get the Optimize property for the configuration
props = config.Properties
optimize = props.Item("Optimize").Value
'Negate the value
props.Item("Optimize").Value = Not optimize
Next
End Sub
Chapter 8:
Managing Solutions and Projects Programmatically
193
Build Events
"TFBDITUBHFPGBCVJMEJTQFSGPSNFE7JTVBM4UVEJP¾SFTBOFWFOUUIBUDBOCFDBQUVSFECZ
BNBDSPPSBEEJOBMMPXJOHDVTUPNDPEFUPCFSVO'PVSFWFOUTBSFEF¾OFE)FSFBSFUIFJS
TJHOBUVSFT
void OnBuildBegin(EnvDTE.vsBuildScope Scope, EnvDTE.vsBuildAction Action);
void OnBuildProjConfigBegin(string Project, string ProjectConfig,
string Platform, string SolutionConfig);
void OnBuildProjConfigDone(string Project, string ProjectConfig,
string Platform, string SolutionConfig, bool Success);
void OnBuildDone(EnvDTE.vsBuildScope Scope, EnvDTE.vsBuildAction Action);
5IFTFFWFOUIBOEMFSTIBWFUIFGPMMPXJOHNFBOJOHT
N
0O#VJME#FHJO 5IJTFWFOUJT¾SFEKVTUCFGPSFBCVJMEJTTUBSUFE5XPBSHVNFOUTBSF
QBTTFEUPUIFIBOEMFSPGUIJTFWFOU5IF¾STUBSHVNFOUJTBOFOVNFSBUJPOPGUZQF
'PX&6'XU$WKNF5EQRGXIJDIDBOCFFJUIFSXU$WKNF5EQRG$CVEJJGZPVDIPTFUPTUBSU
BCBUDICVJMEPGPOFPSNPSFQSPKFDUT
XU$WKNF5EQRG2TQLGEVJGZPVTFMFDUFEBTJOHMF
QSPKFDUUPCVJMECZSJHIUDMJDLJOHBQSPKFDUBOEDIPPTJOH#VJME
PSXU$WKNF5EQRG5QNWVKQP
JGZPVDIPTFUIFBDUJWFTPMVUJPODPO¾HVSBUJPOUPCVJME
5IFTFDPOEBSHVNFOUJT
PGUZQF'PX&6'XU$WKNF#EVKQPBOEDBOCFFJUIFSXU$WKNF#EVKQP$WKNFJGUIFQSPKFDU
PSTPMVUJPODPO¾HVSBUJPOJTUPCFDPNQJMFE
XU$WKNF#EVKQP%NGCPJGUIFQSPKFDUPS
TPMVUJPODPO¾HVSBUJPOµTCVJMEPVUQVUJTUPCFEFMFUFEGSPNEJTL
XU$WKNF#EVKQP&GRNQ[
JGUIFQSPKFDUPSTPMVUJPODPO¾HVSBUJPOJTUPCFEFQMPZFEUPJUTUBSHFU
PS
XU$WKNF#EVKQP4GDWKNF#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TFEUPFBDIIBOEMFS'PSFYBNQMFJG
XFXFSFUPDSFBUFBTPMVUJPODPOUBJOJOHUXPQSPKFDUT$MBTT-JCSBSZBOE$MBTT-JCSBSZMPBE
194
Working with Microsoft Visual Studio 2005
UIFTBNQMFBEEJOBOEQFSGPSNBCVJMEPOUIFTPMVUJPOCZDIPPTJOH#VJME]#VJME4PMVUJPO
UIFGPMMPXJOHJOGPSNBUJPOXPVMECFEJTQMBZFE
OnBuildBegin
Scope: vsBuildScopeSolution
Action: vsBuildActionBuild
OnBuildProjConfigBegin
Project: ClassLibrary1.csproj
Platform: Any CPU
Solution Configuration: Debug
OnBuildProjConfigDone
Project: ClassLibrary1.csproj
Platform: Any CPU
Solution Configuration: Debug
Success: True
OnBuildProjConfigBegin
Project: ..\ClassLibrary2\ClassLibrary2.csproj
Platform: Any CPU
Solution Configuration: Debug
OnBuildProjConfigDone
Project: ..\ClassLibrary2\ClassLibrary2.csproj
Platform: Any CPU
Solution Configuration: Debug
Success: True
OnBuildDone
Scope: vsBuildScopeSolution
Action: vsBuildActionBuild
5IJTPVUQVUPVUMJOFTUIFTUFQTQFSGPSNFEUPCVJMEUIJTUXPTPMVUJPOQSPKFDU*UTUBSUTXJUIB
DBMMUPUIF1P$WKNF$GIKPFWFOUIBOEMFSBOEUIFOCVJMETFBDIQSPKFDUDPO¾HVSBUJPODPOUBJOFE
XJUIJOUIFTPMVUJPODPO¾HVSBUJPOPOFBGUFSBOPUIFSXJUIUIF1P$WKNF&QPGFWFOUIBOEMFS
CFJOH¾SFEUPTJHOBMUIBUUIFCVJMEQSPDFTTIBTCFFODPNQMFUFE8JUI7JTVBM4UVEJPUIF
1P$WKNF2TQL%QP¿I$GIKPBOE1P$WKNF2TQL%QP¿I'PFFWFOUTBSF¾SFEPOFBGUFSBOPUIFSXJUIOP
PUIFSCVJMEFWFOUT¾SFECFUXFFOUIFN)PXFWFSBNBDSPPSBEEJOTIPVMEOPUUBLFBEWBOUBHF
PGUIJTPSEFSPGFWFOUTJGZPVQMBOUPQPSUUIJTDPEFUPBGVUVSFWFSTJPOPG7JTVBM4UVEJPCFDBVTF
GVUVSFWFSTJPOTNJHIUUBLFBEWBOUBHFPGNVMUJQSPDFTTPSDPNQVUFSTCVJMEJOHPOFQSPKFDU
DPO¾HVSBUJPOPOPOFQSPDFTTPSBOEBOPUIFSQSPKFDUDPO¾HVSBUJPOPOBOPUIFSQSPDFTTPS*GB
NBDSPPSBOBEEJOXFSFUPSFMZPOUIJTPSEFSPGFWFOUTUIFDPEFNJHIUOPUXPSLQSPQFSMZ
Persisting Solution and Project Information
Across IDE Sessions
"UUJNFTZPVSBEEJOPSNBDSPNJHIUOFFEUPTBWFTPNFEBUBUIBUTIPVMECFDBSSJFEBMPOH
XJUIUIFTPMVUJPOPSQSPKFDU¾MF5IFPCKFDUNPEFMTVQQPSUTTBWJOHJOGPSNBUJPOJOUPUIFTF
¾MFTXJUIUIF'PX&6')NQDCNUPCKFDU:PVDBO¾OEUIJTPCKFDUCZDBMMJOHUIF)NQDCNUQSPQFSUZ
PGCPUIPGUIFTFPCKFDUT
Chapter 8:
Managing Solutions and Projects Programmatically
195
Sub SolutionGlobals()
Dim globals As EnvDTE.Globals
globals = DTE.Solution.Globals
End Sub
Sub ProjectGlobals()
Dim globals As EnvDTE.Globals
globals = DTE.Solution.Projects.Item(1).Globals
End Sub
5IF)NQDCNUPCKFDUPGUIF5QNWVKQPBOE2TQLGEVPCKFDUTXPSLTJONVDIUIFTBNFXBZBTUIF
)NQDCNUPCKFDUGPVOEPOUIF%5&PCKFDUXJUIBGFXNJOPSEJGGFSFODFT'JSTUJGBNBDSPPSBO
BEEJOTUPSFTEBUBJOUPUIFTPMVUJPOPSQSPKFDU¾MFFWFOJGUIF8CTKCDNG2GTUKUVU¿BHJTTFUGPS
UIBUWBSJBCMFUIFEBUBNJHIUOPUCFXSJUUFOJOUPUIFTPMVUJPOPSQSPKFDU¾MF5IJTJTCFDBVTF
NBLJOHBDIBOHFUPBWBSJBC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FSIBT
BOZTJHOJ¾DBODFCVUJUJTKVTUBOJOUFSFTUJOHGBDU5IFGPMMPXJOHNBDSPTBNQMFJTBO
JNQMFNFOUBUJPOPGUIF1P$WKNF&QPGFWFOU"TFBDI1P$WKNF&QPGFWFOUJT¾SFEUIFTBNQMF
DIFDLTGPSUIFFYJTUFODFPGUIF$WKNF%QWPVGTWBSJBCMFXJUIJOUIFTPMVUJPO)NQDCNUPCKFDU
*GUIJTWBMVFFYJTUTJUJTJODSFNFOUFEBOETUPSFECBDLJOUPUIF)NQDCNUPCKFDU*GUIJTWBMVF
EPFTOµUFYJTUUIFWBMVFJTTUPSFE5IFDPEFGPSUIF1P$WKNF&QPGFWFOUJTTIPXOIFSF
Private Sub BuildEvents_OnBuildDone(ByVal Scope As _
EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) _
Handles BuildEvents.OnBuildDone
'Increment the build counter by storing a value in the
' solution file through the Globals object:
Dim globals As Globals
Dim int32 As System.Int32
globals = DTE.Solution.Globals
If (globals.VariableExists("BuildCounter")) Then
'A counter has been set, increment it:
int32 = System.Int32.Parse _
(globals.VariableValue("BuildCounter").ToString())
int32 = int32 + 1
196
Working with Microsoft Visual Studio 2005
globals.VariableValue("BuildCounter") = int32.ToString()
globals.VariablePersists("BuildCounter") = True
Else
'The variable has never been set, seed the counter:
globals.VariableValue("BuildCounter") = 1.ToString()
globals.VariablePersists("BuildCounter") = True
End If
End Sub
Looking Ahead
*OUIJTDIBQUFSXFMPPLFEBUIPXUIFQJFDFTPGUIFPCKFDUNPEFM¾UUPHFUIFSUP
QSPHSBNNBUJDBMMZNBOBHFUIFNBOZQSPKFDUUZQFTUIBUDBOCFMPBEFEJOUPBTPMVUJPO*OUIF
OFYUDIBQUFSXFXJMMTFFIPXUPQSPHSBNUIFVTFSJOUFSGBDFFMFNFOUTPG7JTVBM4UVEJPTVDI
BTUIFNBOZEJGGFSFOUUPPMBOEEPDVNFOUXJOEPXT
Chapter 9
Programming the Visual Studio
User Interface
In this chapter:
Window Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Explorer Windows and the UIHierarchy Object . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Toolbox Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Task List Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Error List Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Output Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Forms Designer Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Custom Tool Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Options Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
197
203
207
210
221
221
224
227
233
239
.JDSPTPGUˆ7JTVBM4UVEJPˆJTNBEFVQPGNBOZEJGGFSFOUXJOEPXTUIBUTIPXEBUBUPUIFVTFS
JODMVEJOHUIF5BTL-JTU4PMVUJPO&YQMPSFSBOEUIF.JDSPTPGU8JOEPXTˆ'PSNTEFTJHOFS
:PVDBONBOJQVMBUFUIFTFXJOEPXTOPUPOMZCZVTJOHUIFNPVTFBOELFZCPBSECVUBMTP
UISPVHIUIFPCKFDUNPEFMCZVTJOHBNBDSPPSBOBEEJO*OUIJTDIBQUFSXFµMMEJTDVTTUIF
NBOZPCKFDUTZPVDBOQSPHSBNJOUIFVTFSJOUFSGBDFPG7JTVBM4UVEJP
Window Basics
5IFVTFSJOUFSGBDFGPSFBDIXJOEPXJO7JTVBM4UVEJPJTEJGGFSFOUGSPNUIBUPGPUIFSXJOEPXT
CVUUIFZBMMTIBSFBGFXCBTJDNFUIPETBOEQSPQFSUJFT-FUµTMPPLBUUIFDPNNPOQBSUTPGUIF
PCKFDUNPEFM
The Windows Collection
7JTVBM4UVEJPDPOUBJOTBOVNCFSPGUPPMBOEEPDVNFOUXJOEPXTUIBUZPVDBOBDDFTT
UISPVHIUIFBVUPNBUJPONPEFM&BDIPGUIFTFXJOEPXTJTSFQSFTFOUFEJOUIFPCKFDUNPEFM
CZB9KPFQYPCKFDUBOEDBOCFGPVOEJOUIF9KPFQYUDPMMFDUJPOXIJDIJTBDDFTTJCMFUISPVHI
UIF&6'9KPFQYUQSPQFSUZ
197
198
Working with Microsoft Visual Studio 2005
:PVDBOSFUSJFWFB9KPFQYPCKFDUGSPNUIF9KPFQYUDPMMFDUJPOJOBOVNCFSPGXBZT0OFXBZ
JTUPVTFUIFFOVNFSBUPSUPXBMLUIFMJTUPGBMMBWBJMBCMFXJOEPXTBTTIPXOIFSF
Sub EnumWindows()
Dim window As EnvDTE.Window
For Each window In DTE.Windows
MsgBox(window.Caption)
Next
End Sub
0SZPVDBOVTFUIFOVNFSJDBMJOEFYJOHNFUIPE
Sub EnumWindows2()
Dim window As EnvDTE.Window
Dim i As Integer
For i = 1 To DTE.Windows.Count
MsgBox(DTE.Windows.Item(1).Caption)
Next
End Sub
)PXFWFSVTJOHUIFTFGPSNBUTGPS¾OEJOHBXJOEPXJTOµUPQUJNBMCFDBVTFZPVVTVBMMZXBOUUP
¾OEPOFTQFDJ¾DXJOEPXBOEMPPLJOHBUBMMUIFXJOEPXTUP¾OEJUJTBXBTUFPG$16DZDMFT
5IFOVNFSJDBMJOEFYJOHNFUIPEJTOµUBMXBZTCFTUCFDBVTFUIFQPTJUJPOPGBXJOEPXGSPNPOF
JOTUBODFPG7JTVBM4UVEJPUPUIFOFYUNJHIUDIBOHFTPZPVDBOµUSFMZPOVTJOHBOJOEFYUP
SFUVSOBTQFDJ¾D9KPFQYPCKFDU*OGBDUZPVIBWFOPHVBSBOUFFUI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QYUDPMMFDUJPODIBOHJOH0OFPUIFSCFOF¾UPGVTJOHUIF(6*%JTUIBUFWFOJGUIF
XJOEPXZPVµSFTFBSDIJOHGPSIBTOµUZFUCFFODSFBUFE7JTVBM4UVEJPJTBEWBODFEFOPVHIUP
DSFBUFUIFUPPMXJOEPXXIFOJUµTSFRVFTUFE
:PVNJHIUPDDBTJPOBMMZSVOBDSPTTBXJOEPXUIBUEPFTOµUIBWFBDPOTUBOU(6*%EF¾OFE
GPSJU5IF4PVSDF$POUSPM&YQMPSFSXJOEPXJTBOFYBNQMF8IFOZPVOFFEUP¾OETVDI
BXJOEPXZPVDBOVTFUIF(6*%JOUIFGPSNPGBTUSJOHJOQMBDFPGPOFPGUIFQSFEF¾OFE
DPOTUBOUTBTTIPXOJOUIFGPMMPXJOHFYBNQMFXIJDISFUSJFWFTUIF9KPFQYPCKFDUGPSUIF
4PVSDF$POUSPM&YQMPSFSXJOEPX
Sub FindTheSourceControlExplorerWindow()
Dim window As EnvDTE.Window
window = DTE.Windows.Item("{99B8FA2F-AB90-4F57-9C32-949F146F1914}")
End Sub
200
Working with Microsoft Visual Studio 2005
:PVDBO¾OEUIF(6*%UIBUDBOCFQBTTFEUPUIF+VGONFUIPECZVTJOHUIF1DLGEV-KPF
QSPQFSUZ5IFGPMMPXJOHNBDSPUBLFTUIJTBQQSPBDIUPEJTQMBZUIF(6*%GPSUIF'BWPSJUFT
XJOEPX
Sub FindTheSourceControlExplorerWindow2()
Dim window As EnvDTE.Window
'You should show the Source Control Explorer window
' before calling this code!
window = DTE.Windows.Item("Source Control Explorer")
MsgBox(window.ObjectKind)
End Sub
8IFOZPVSVOUIJTNBDSPUIF(6*%GPSUIF4PVSDF$POUSPM&YQMPSFSXJOEPXJTEJTQMBZFE
JOBNFTTBHFCPY:PVDBOUIFOEF¾OFBDPOTUBOUTFUUPUIJT(6*%BOEVTFUIJTDPOTUBOU
JOBOZDPEFUIBUOFFETUP¾OEUIJTXJOEPX5IJTJTIPXXFGPVOEUIF(6*%GPSUIF
(KPF6JG5QWTEG%QPVTQN'ZRNQTGT9KPFQYNBDSP
Using the Object Property
.BOZXJOEPXTJO7JTVBM4UVEJPIBWFBOPCKFDUNPEFMUIBUZPVDBOVTFUPNBOJQVMBUFUIF
EBUBDPOUBJOFEJOUIBUXJOEPX:PVDBO¾OEUIFTFXJOEPXTQFDJ¾DPCKFDUTCZVTJOHUIF
1DLGEVQSPQFSUZPGUIF9KPFQYPCKFDU'PSFYBNQMFDBMMJOHUIF1DLGEVQSPQFSUZPGUIF9KPFQY
PCKFDUGPSUIF5BTL-JTUXJOEPXSFUVSOTUIF6CUM.KUVPCKFDUXIJDIBMMPXTZPVUPFOVNFSBUF
BEESFNPWFBOEDIBOHFQSPQFSUJFTPGUBTLJUFNTJOUIF5BTL-JTUXJOEPX5IFGPMMPXJOH
NBDSPSFUSJFWFTUIF6CUM.KUVPCKFDU
Sub GetTaskListObject()
Dim window As EnvDTE.Window
Dim taskList As EnvDTE.TaskList
window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindTaskList)
taskList = CType(window.Object, EnvDTE.TaskList)
End Sub
"OVNCFSPGUZQFTBSFBWBJMBCMFBTUIFQSPHSBNNBCMFPCKFDUGPSUIFEJGGFSFOUXJOEPXTOPUKVTU
UIF6CUM.KUVPCKFDUBTTIPXOJOUIFNBDSP5BCMFMJTUTUIF(6*%DPOTUBOUZPVQBTTUPUIF
+VGONFUIPEUP¾OEB9KPFQYPCKFDUBTXFMMBTUIFQSPHSBNNBCMFPCKFDUGPSUIBUXJOEPX
Table 9-1
Windows and Their Programmable Objects
Window
GUID Constant
Object Type
Command Window
vsWindowKindCommandWindow
EnvDTE.CommandWindow
Macro Explorer
vsWindowKindMacroExplorer
EnvDTE.UIHierarchy
Output window
vsWindowKindOutput
EnvDTE.OutputWindow
Server Explorer
vsWindowKindServerExplorer
EnvDTE.UIHierarchy
Solution Explorer
vsWindowKindSolutionExplorer
EnvDTE.UIHierarchy
Error List
vsWindowKindErrorList
EnvDTE80.ErrorList
Chapter 9:
Table 9-1
Programming the Visual Studio User Interface
201
Windows and Their Programmable Objects
Window
GUID Constant
Object Type
Task List
vsWindowKindTaskList
EnvDTE.TaskList
Toolbox
vsWindowKindToolbox
EnvDTE.ToolBox
Web browser window
vsWindowKindWebBrowser
SHDocVw.WebBrowser
Text editor
<None>
EnvDTE.TextWindow
Forms designer
<None>
System.ComponentModel.
Design.IDesignerHost
HTML designer
<None>
EnvDTE.HTMLWindow
/PUPOMZEPTPNFPGUIFUPPMXJOEPXTJO7JTVBM4UVEJPIBWFBOPCKFDUNPEFMCVUBDPVQMF
PGUIFEPDVNFOUXJOEPXTIBWFBOPCKFDUNPEFMBTXFMM5IF9KPFQY1DLGEVQSPQFSUZPG
UIFUFYUFEJUPS/&5'PSNTEFTJHOFSBOE)5.-EFTJHOFSXJOEPXTSFUVSOTBOPCKFDU
BQQSPQSJBUFGPSQSPHSBNNJOHUIBUXJOEPXPCKFDU5IFPCKFDUGPSQSPHSBNNJOHUIF/&5
'PSNTEFTJHOFSXJOEPXTJTEJTDVTTFEMBUFSJOUIJTDIBQUFSUIFPCKFDUTGPSQSPHSBNNJOHUIF
UFYUFEJUPSBOE)5.-FEJUPSXJOEPXTBSFEJTDVTTFEJO$IBQUFS
Shortcuts to Common Tool Windows
"MUIPVHIBDRVJSJOHUIFTQFDJ¾DPCKFDUCFIJOEBUPPMXJOEPXJTOPUUPPUFSSJCMZDPNQMJDBUFE
7JTVBM4UVEJPNBLFTJUFBTZUPHFUUPTPNFPGUIFNPTUDPNNPOUPPMXJOEPXPCKFDUTCZ
VTJOHUIF6QQN9KPFQYUPCKFDU5IJTPCKFDUHJWFTZPVEJSFDUBDDFTTUPUIFPCKFDUCFIJOEUIF
$PNNBOE8JOEPXUIF0VUQVUXJOEPX4PMVUJPO&YQMPSFS&SSPS-JTU5BTL-JTUBOE
5PPMCPY5IF6QQN9KPFQYUPCKFDUDBOCFGPVOEPOUIF&6'PCKFDUXJUIDPEFTVDIBTUIJT
WFSZTJNQMFNBDSP
Sub FindTaskList()
Dim taskList As TaskList
taskList = CType(DTE, DTE2).ToolWindows.TaskList
End Sub
5IF6QQN9KPFQYUPCKFDUBMTPHJWFTZPVRVJDLBDDFTTUPUIFTQFDJ¾DPCKFDUPGPUIFSUPPM
XJOEPXTXJUIUIF)GV6QQN9KPFQYQSPQFSUZ5IJTQSPQFSUZBDDFQUTBTBQBSBNFUFSUIF(6*%
GPSBUPPMXJOEPX±UIFTBNF(6*%ZPVXPVMEQBTTUPUIF9KPFQYU+VGONFUIPEBOESFUVSOT
UIFTBNFWBMVFSFUVSOFEGSPNBXJOEPXµT1DLGEVQSPQFSUZNFBOJOHUIBUUIJTMJOFPGDPEF
taskList = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindTaskList).Object
JTFRVJWBMFOUUPUIJTMJOFPGDPEF
taskList = CType(DTE, DTE2).ToolWindows.GetToolWindow _
(EnvDTE.Constants.vsWindowKindTaskList)
202
Working with Microsoft Visual Studio 2005
The Main Window
&BDIUPPMBOEEPDVNFOUXJOEPXJO7JTVBM4UVEJPIBTB9KPFQYPCKFDUBWBJMBCMF)PXFWFS
7JTVBM4UVEJPJTBMTPBXJOEPXTPJUµTPOMZGBJSUIBUB9KPFQYPCKFDUCFBWBJMBCMFGPSUIBU
XJOEPXBTXFMM3BUIFSUIBOJOEFYJOHUIF'PX&6'9KPFQYUDPMMFDUJPOUP¾OEUIJT9KPFQY
PCKFDUZPVVTFUIF/CKP9KPFQYQSPQFSUZPGUIF&6'PCKFDU
Sub FindTheMainWindow()
Dim mainWindow As EnvDTE.Window
mainWindow = DTE.MainWindow
End Sub
8IFOZPVXPSLXJUIUIF9KPFQYPCKFDUGPSUIF7JTVBM4UVEJPNBJOXJOEPXBGFXNFUIPET
BOEQSPQFSUJFTEPOµUXPSLBTUIFZEPXIFOZPVXPSLXJUIUPPMPSEPDVNFOU9KPFQY
PCKFDUT5IFEJGGFSFODFTCFUXFFOUPPMBOEEPDVNF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CFXSBQQFECZBDMBTTUIBUJNQMFNFOUTBOJOUFSGBDFUIBUUIF/&5MJCSBSZDBO
BDDFQUBTBQBSFOU:PVDBOJNQMFNFOUUIJTJOUFSGBDF5[UVGO9KPFQYU(QTOU+9KP9KPFQYPO
ZPVSBEEJODMBTTPSPOBTFQBSBUFDMBTTXJUIJOBNBDSPQSPKFDU5IF+9KP9KPFQYJOUFSGBDF
IBTPOFQSPQFSUZOBNFE*CPFNGUIJTQSPQFSUZSFUVSOTB5[UVGO+PV2VTXIJDIDPOUBJOTUIF
IBOEMFUPBQBSFOUXJOEPXBOEJOUIJTDBTFJTUIFWBMVFSFUVSOFEGSPNUIF9KPFQY*9PF
QSPQFSUZ8IFOJUµTUJNFUPTIPXBGPSNCZVTJOHUIF(QTO5JQY&KCNQINFUIPEZPVDBOQBTT
UIFDMBTTUIBUJNQMFNFOUTUIF+9KP9KPFQYBTBOBSHVNFOUUPUIJTNFUIPE
5PJNQMFNFOU+9KP9KPFQYGPSBOBEEJOZPVNVTU¾STUBEEJUUPUIFJOUFSGBDFMJTUGPSZPVS
BEEJOBTTIPXOIFSF
public class Connect : Object, Extensibility.IDTExtensibility2,
System.Windows.Forms.IWin32Window
Chapter 9:
Programming the Visual Studio User Interface
203
/FYUZPVBEEUIFJNQMFNFOUBUJPOPGUIF*CPFNGQSPQFSUZ
//Implementation of the IWin32Window.Handle property:
public System.IntPtr Handle
{
get
{
return new System.IntPtr (applicationObject.MainWindow.HWnd);
}
}
'JOBMMZZPVDBOEJTQMBZBGPSNBTTVNJOHUIBUBGPSNDMBTTOBNFE(QTOFYJTUTXJUIJOBO
BEEJOQSPKFDU
CZVTJOHDPEFTVDIBTUIJT
Form1 form1 = new Form1();
form1.ShowDialog(this);
*NQMFNFOUJOHUIJTJOUFSGBDFXJUIJOBNBDSPJTFWFOFBTJFSUIFNBDSPTBNQMFTQSPKFDU
UIBUJTJOTUBMMFEXJUI7JTVBM4UVEJPBMSFBEZDPOUBJOTUIFDPEFGPSB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JTVTFEUP¾OEUIF7+*KGTCTEJ[+VGOUDPMMFDUJPOXIJDIDPOUBJOTBMMUIF
SPPUJUFNTPGUIFUSFFXJUIJOB6*IJFSBSDIZXJOEPX&BDISPPUUSFFJUFNJTSFQSFTFOUFECZ
B7+*KGTCTEJ[+VGOPCKFDUXJUIJOUIF7+*KGTCTEJ[+VGOUDPMMFDUJPOBOECFDBVTFBMMPGUIFTF
USFFJUFNTDBOUIFNTFMWFTDPOUBJOTVCJUFNTUIF7+*KGTCTEJ[+VGO7+*KGTCTEJ[+VGOUQSPQFSUZ
204
Working with Microsoft Visual Studio 2005
SFUVSOTB7+*KGTCTEJ[+VGOUDPMMFDUJPO5IJTQBUUFSOPGUSFFOPEFTSFUVSOJOHBDPMMFDUJPOPG
PUIFSOPEFTDPOUJOVFTVOUJMUIBUCSBODIPGUIFUSFFFOET5IFGPMMPXJOHNBDSPVTFTUIF
7+*KGTCTEJ[PCKFDUUP¾OEBOEEJTQMBZUIFOBNFPGUIFUPQMFWFMOPEFPG.BDSP&YQMPSFS
Sub GetTopLevelUIHierItems()
Dim macroExplWin As Window
Dim uiHierarchy As EnvDTE.UIHierarchy
Dim uiHierarchyItems As EnvDTE.UIHierarchyItems
'Find the macro explorer window, and the UIHierarchy
' object for this window:
macroExplWin = DTE.Windows.Item(Constants.vsWindowKindMacroExplorer)
uiHierarchy = macroExplWin.Object
'Get the top level collection of items:
uiHierarchyItems = uiHierarchy.UIHierarchyItems
'Display the name of the first node in this collection:
MsgBox(uiHierarchyItems.Item(1).Name)
End Sub
)FSF.BDSP&YQMPSFSµT7+*KGTCTEJ[PCKFDUJTGPVOEBOEUIFDPMMFDUJPOPG7+*KGTCTEJ[+VGOUJT
SFUSJFWFE5IFOBNFEJTQMBZFEJTUIBUPGUIF¾STUJUFNJOUIFDPMMFDUJPOXIJDIJOUIJTDBTFJT
/CETQUCFDBVTFUIFUPQMFWFMOPEFJO.BDSP&YQMPSFSJTBMXBZTUIF.BDSPTOPEF
$POUJOVJOHXJUIPVSFYBNQMFUIF.BDSPTOPEFJOUIF.BDSP&YQMPSFSXJOEPXDPOUBJOT
BOVNCFSPGNBDSPQSPKFDUT#FDBVTFUIJTOPEFDBOIBWFTVCJUFNTJUJTBDPOUBJOFSPG
7+*KGTCTEJ[+VGOPCKFDUTTPUIF7+*KGTCTEJ[+VGO7+*KGTCTEJ[+VGOUQSPQFSUZSFUVSOTB
DPMMFDUJPOPCKFDU5IJT7+*KGTCTEJ[+VGOUDPMMFDUJPODPOUBJOTBMJTUPGBMMUIFNBDSPQSPKFDUT
BOEJGXFNPEJGZUIFFBSMJFSNBDSPXFDBOXBMLUIFMJTUPGUIFNBDSPQSPKFDUT
Sub WalkMacroProjects()
Dim macroExplWin As Window
Dim uiHierarchy As EnvDTE.UIHierarchy
Dim uiHierarchyItems As EnvDTE.UIHierarchyItems
Dim uiHierarchyItem As EnvDTE.UIHierarchyItem
Dim uiHierarchyItem2 As EnvDTE.UIHierarchyItem
'Find the Macro Explorer window, and the UIHierarchy
' object for this window:
macroExplWin = DTE.Windows.Item(Constants.vsWindowKindMacroExplorer)
uiHierarchy = macroExplWin.Object
'Get the first node in this collection, the Macros node:
uiHierarchyItem = uiHierarchy.UIHierarchyItems.Item(1)
'Walk all the items in this collection, which is
' the list of macro projects:
For Each uiHierarchyItem2 In uiHierarchyItem.UIHierarchyItems
MsgBox(uiHierarchyItem2.Name)
Next
End Sub
5IFTFTBNQMFNBDSPTTIPXIPXUPXBMLUIFIJFSBSDIZTIPXOJOUIF.BDSP&YQMPSFS
XJOEPX5PVTFUIJTDPEFUPMPPLBUXIBUJTDPOUBJOFEJOUIF4PMVUJPO&YQMPSFSBOE4FSWFS
&YQMPSFSXJOEPXTZPVDBOTJNQMZDIBOHFUIFWBMVFQBTTFEUPUIF9KPFQYU+VGONFUIPEUP
%QPUVCPVUXU9KPFQY-KPF5QNWVKQP'ZRNQTGTPS%QPUVCPVUXU9KPFQY-KPF5GTXGT'ZRNQTGT
Chapter 9:
Programming the Visual Studio User Interface
205
Note Do the UIHierarchy objects seem familiar? Walking the UIHierarchy, UIHierarchyItems,
and UIHierarchyItem objects to find an item in a UI hierarchy window is similar to using
ProjectItems and ProjectItem to walk a project to find a project item. The reason for this
similarity is that the UIHierarchy objects were designed to reflect how you would use the
ProjectItem and ProjectItems objects.
The UIHierarchy Object
'JOEJOHBTQFDJ¾DOPEFXJUIJOB6*IJFSBSDIZXJOEPXDBOJOWPMWFBHSFBUEFBMPGDPEF
FTQFDJBMMZJGUIFEFTJSFEOPEFJTOFTUFENPSFUIBOUXPMFWFMTEFFQ6TJOHUIF7+*KGTCTEJ[
)GV+VGONFUIPEZPVDBOEJSFDUMZ¾OEB7+*KGTCTEJ[+VGOPCKFDUPGBOPEFSBUIFSUIBO
XSJUJOHBMPUPGDPEFUPUSBWFSTFUIFUSFFPGOPEFT'PSFYBNQMFJGZPVXBOUUPHFUUPUIF
7+*KGTCTEJ[+VGOPCKFDUPGUIF+PUGTV&CVGNBDSPMPDBUFEJOUIF74&EJUPSNPEVMFPGUIF
4BNQMFTNBDSPQSPKFDUZPVDBOXSJUFDPEFTVDIBTUIJT
Sub FindUIHierItemForInsertDateMacro()
Dim macroExplWin As Window
Dim uiHierarchy As EnvDTE.UIHierarchy
Dim uiHierarchyItem As EnvDTE.UIHierarchyItem
Dim uiHierarchyItems As EnvDTE.UIHierarchyItems
macroExplWin = DTE.Windows.Item(Constants.vsWindowKindMacroExplorer)
uiHierarchy = macroExplWin.Object
uiHierarchyItems = uiHierarchy.UIHierarchyItems
uiHierarchyItem = uiHierarchyItems.Item("Macros")
uiHierarchyItems = uiHierarchyItem.UIHierarchyItems
uiHierarchyItem = uiHierarchyItems.Item("Samples")
uiHierarchyItems = uiHierarchyItem.UIHierarchyItems
uiHierarchyItem = uiHierarchyItems.Item("VSEditor")
uiHierarchyItems = uiHierarchyItem.UIHierarchyItems
uiHierarchyItem = uiHierarchyItems.Item("InsertDate")
MsgBox(uiHierarchyItem.Name)
End Sub
5IJTCJUPGDPEFJTRVJUFWFSCPTFIPXFWFSBOEXFDBOTIPSUFOJUCZVTJOHUIF
7+*KGTCTEJ[)GV+VGONFUIPE
Sub FindUIHierItemForInsertDateMacro2()
Dim macroExplWin As Window
Dim uiHierarchy As EnvDTE.UIHierarchy
Dim uiHierarchyItem As EnvDTE.UIHierarchyItem
macroExplWin = DTE.Windows.Item(Constants.vsWindowKindMacroExplorer)
uiHierarchy = macroExplWin.Object
uiHierarchyItem = _
uiHierarchy.GetItem("Macros\Samples\VSEditor\InsertDate")
MsgBox(uiHierarchyItem.Name)
End Sub
7+*KGTCTEJ[)GV+VGOBDDFQUTBTUSJOHXIJDIJTUIFQBUIUPBOJUFNUIBUQJOQPJOUTBOPEF
XJUIJOUIFIJFSBSDIZ5IJTQBUIJTDBMDVMBUFECZUBLJOHUIFOBNFTPGFBDIOPEFJOUIFCSBODI
UPUIFUSFFOPEFUIBUZPVXBOUUP¾OETFQBSBUFECZUIFGPSXBSETMBTI=
DIBSBDUFS
206
Working with Microsoft Visual Studio 2005
5IF7+*KGTCTEJ[5GNGEVGF+VGOUQSPQFSUZSFUVSOTBOBSSBZPG7+*KGTCTEJ[+VGOPCKFDUTGPSJUFNT
UIBUBSFTFMFDUFEXJUIJOUIF6*IJFSBSDIZUSFF"TEPPUIFSBSSBZTSFUVSOFECZUIFPCKFDU
NPEFMXIFOZPVµSFVTJOHBMBOHVBHFTVQQPSUFECZ/&5UIJTQSPQFSUZSFUVSOTBOBSSBZPG
VOUZQFEPCKFDUT±BOBSSBZPG5[UVGO1DLGEV
Sub GetUIHierSelectedItems()
Dim macroExplWin As Window
Dim uiHierarchy As EnvDTE.UIHierarchy
Dim selectedItems As Object()
Dim uiHierarchyItem As EnvDTE.UIHierarchyItem
macroExplWin = DTE.Windows.Item(Constants.vsWindowKindMacroExplorer)
uiHierarchy = macroExplWin.Object
selectedItems = uiHierarchy.SelectedItems
For Each uiHierarchyItem In selectedItems
MsgBox(uiHierarchyItem.Name)
Next
End S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5GNGEV5IJTNFUIPEJTTJNJMBSUPUIF7+*KGTCTEJ[5GNGEV7RBOE7+*KGTCTEJ[5GNGEV&QYP
NFUIPETFYDFQUUIBUJUXPSLTPOPOMZPOFOPEFBUBUJNF±UIF7+*KGTCTEJ[+VGOUIBUUIF5GNGEV
NFUIPEXBTDBMMFEPO#FDBVTFUIF5GNGEVNFUIPENPEJ¾FTPOMZUIFDVSSFOU7+*KGTCTEJ[+VGO
JUEPFTOµUBDDFQUBOVNCFSPGJUFNTUPTFMFDU
$BMMJOHUIF7+*KGTCTEJ[+VGO1DLGEVQSPQFSUZSFUVSOTUIFFYUFOTJCJMJUZPCKFDUJGPOFJT
BWBJMBCMFGPSUIBUOPEF'PSFYBNQMFXIFOZPVµSFVTJOH4PMVUJPO&YQMPSFSZPVDBOSFUSJFWF
BO'PX&6'2TQLGEVPS'PX&6'2TQLGEV+VGOPCKFDUCFIJOEUIBUOPEFCZVTJOHUIF1DLGEV
QSPQFSUZ5IFGPMMPXJOHDPEF¾OETUIF7+*KGTCTEJ[+VGOGPSUIF¾STUQSPKFDUBOETFDPOE
JUFNXJUIJOUIBUQSPKFDUUIFTFDPOEJUFNJTTFBSDIFEGPSCFDBVTFUIF¾STUJUFNXIFOB
/&5QSPKFDUJTMPBEFEJTUIF3FGFSFODFTOPEF
BOEHFUTUIF'PX&6'2TQLGEVBOE'PX&6'
2TQLGEV+VGOPCKFDUTGPSUIPTFOPEFT
Sub GetUIHierItemObject()
Dim uihier As EnvDTE.UIHierarchy
Dim uihierProj As EnvDTE.UIHierarchyItem
Dim uihierProjItem As EnvDTE.UIHierarchyItem
Dim project As EnvDTE.Project
Dim projItem As EnvDTE.ProjectItem
uihier = DTE.Windows.Item( _
Constants.vsWindowKindSolutionExplorer).Object
uihierProj = uihier.UIHierarchyItems.Item(1).UIHierarchyItems.Item(1)
project = uihierProj.Object
uihierProjItem = uihierProj.UIHierarchyItems.Item(2)
projItem = uihierProjItem.Object
End Sub
The Toolbox Window
5IF5PPMCPYTUPSFTDPOUSPMTBOEDPEFTOJQQFUTUIBUZPVDBOESBHPOUPUIF'PSNT%FTJHOFS
XJOEPXUFYUFEJUPSXJOEPXTBOEOFBSMZBOZUIJOHFMTFUIBUDBOCFBESBHBOEESPQUBSHFU
5IF5PPMCPYJTNBEFVQPGBTFUPGQBHFTPSUBCTXIFSFJUFNTDBOCFTUPSFEBOEHSPVQFE
JOUPSFMBUFEDBUFHPSJFT
Tabs and Items
5P¾OEUIF5PPMCPYXJOEPXZPVDBOQBTTUIFDPOTUBOUXU9KPFQY-KPF6QQNDQZUPUIF
9KPFQYU+VGONFUIPEXIJDISFUVSOTB9KPFQYPCKFDU5IF6QQN$QZPCKFDUJTUIFOGPVOECZ
DBMMJOHUIFSFUVSOFEPCKFDUµT9KPFQY1DLGEVQSPQFSUZBTTIPXOIFSF
Sub FindTheToolBox()
Dim toolBoxWindow As EnvDTE.Window
Dim toolBox As EnvDTE.ToolBox
208
Working with Microsoft Visual Studio 2005
toolBoxWindow = DTE.Windows.Item(Constants.vsWindowKindToolbox)
toolBox = toolBoxWindow.Object
End Sub
#FDBVTFUIF5PPMCPYDBODPOUBJONPSFUIBOPOFUBCBDPMMFDUJPOJTBWBJMBCMFUPFOVNFSBUF
BMMUIFTFUBCT:PV¾OEUIJTDPMMFDUJPOUIF6QQN$QZ6CDUPCKFDUCZDBMMJOHUIF6QQN$QZ
6QQN$QZ6CDUQSPQFSUZ6TJOHUIF6QQN$QZ6CDUDPMMFDUJPOZPVDBOFOVNFSBUFFBDI6QQN$QZ6CD
PCKFDUJOUIF5PPMCPYBOEFWFODSFBUFOFXUBCTUPIPVTFDPNQPOFOUTPSUFYUGSBHNFOUTPG
ZPVSDIPPTJOH5PDSFBUFBOFXUBCZPVVTFUIF6QQN$QZ6CDU#FFNFUIPEXIJDIUBLFTBTBO
BSHVNFOUUIFOBNFPGUIFOFXUBCUPDSFBUFBOESFUVSOTB6QQN$QZ6CDPCKFDUGPSUIFOFXMZ
DSFBUFEUBC5IFGPMMPXJOHNBDSPBEETBOFX5PPMCPYUBC
Sub AddNewToolBoxTab()
Dim toolBoxWindow As EnvDTE.Window
Dim toolBox As EnvDTE.ToolBox
toolBoxWindow = DTE.Windows.Item(Constants.vsWindowKindToolbox)
toolBox = toolBoxWindow.Object
toolBox.ToolBoxTabs.Add("My commonly used items").Activate()
End Sub
5IJTDPEFDSFBUFTBOFXUBCDBMMFE.Z$PNNPOMZ6TFE*UFNTBOEUIF#EVKXCVGNFUIPEPG
UIF6QQN$QZ6CDPCKFDUNBLFTTVSFJUµTUIFTFMFDUFEUBC
/PUPOMZJTUIF5PPMCPYBDPMMFDUJPOPGUBCTCVUFBDIUBCJTBMTPBDPMMFDUJPOPGJUFNT
&BDIDPMMFDUJPOJUFNJTSFQSFTFOUFEJOUIFPCKFDUNPEFMCZB6QQN$QZ+VGOPCKFDUBOEDBO
CFFOVNFSBUFECZVTJOHUIF6QQN$QZ+VGOUPCKFDUXIJDIJTGPVOECZDBMMJOHUIF6QQN$QZ6CD
6QQN$QZ+VGOUQSPQFSUZ:PVDBOXBMLUIFFOUJSFDPOUFOUTPGUIF5PPMCPYCZVTJOHUIF
'PWOGTCVG6QQN$QZ%QPVGPVUNBDSPTIPXOIFSF
Sub EnumerateToolBoxContents()
Dim toolBoxWindow As EnvDTE.Window
Dim toolBox As EnvDTE.ToolBox
Dim toolBoxTab As ToolBoxTab
Dim outputWindow As New _
InsideVSNET.Utilities.OutputWindowPaneEx(DTE, "Toolbox contents")
toolBoxWindow = DTE.Windows.Item(Constants.vsWindowKindToolbox)
toolBox = toolBoxWindow.Object
For Each toolBoxTab In toolBox.ToolBoxTabs
Dim toolBoxItem As ToolBoxItem
outputWindow.WriteLine(toolBoxTab.Name)
For Each toolBoxItem In toolBoxTab.ToolBoxItems
outputWindow.WriteLine(vbTab + toolBoxItem.Name)
Next
Next
End Sub
0ODFZPV¾OEB6QQN$QZ+VGOPCKFDUZPVµMMTFFUIBUZPVDBOµUEPNVDIXJUIJU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UPUIBUXJOEPX5PBEEBUFYUGSBHNFOUZPVDBOVTFDPEFMJLFUIJT
Sub AddTextToTheToolBox()
Dim toolBoxWindow As EnvDTE.Window
Dim toolBox As EnvDTE.ToolBox
Dim toolBoxTab As EnvDTE.ToolBoxTab
Dim toolBoxItems As EnvDTE.ToolBoxItems
toolBoxWindow = DTE.Windows.Item(Constants.vsWindowKindToolbox)
toolBox = toolBoxWindow.Object
toolBoxTab = toolBox.ToolBoxTabs.Item("General")
toolBoxItems = toolBoxTab.ToolBoxItems
toolBoxItems.Add("My Text", "This is some text", _
vsToolBoxItemFormat.vsToolBoxItemFormatText)
End Sub
5IJTDPEFTUBSUTCZXBMLJOHUIFPCKFDUNPEFMBOE¾OEJOHUIF(FOFSBMUBCPGUIF5PPMCPY*U
FOETCZDBMMJOHUIF6QQN$QZ+VGOU#FFNFUIPEBOEBEEJOHBOJUFNMBCFMFE/[6GZVXJUIUIF
UFYU6JKUKUUQOGVGZVUIBUIBTUIF$MJQCPBSEGPSNBUPGUZQFVGZV
"EEJOHUFYUJOUIF)5.-GPSNBUJTTJNJMBSUPBEEJOHQMBJOUFYU±UIFEJGGFSFODFTBSFUIBU
SBUIFSUIBOQBTTJOHSBXUFYUZPVOFFEUPQBTTBGSBHNFOUPG)5.-DPEFBOEUIFGPSNBUPG
UIFEBUBJTNBSLFEBT)5.-CZVTJOHXU6QQN$QZ+VGO(QTOCV*6/.
Sub AddHTMLToTheToolBox()
Dim toolBoxWindow As EnvDTE.Window
Dim toolBox As EnvDTE.ToolBox
Dim toolBoxTab As EnvDTE.ToolBoxTab
Dim toolBoxItems As EnvDTE.ToolBoxItems
toolBoxWindow = DTE.Windows.Item(Constants.vsWindowKindToolbox)
toolBox = toolBoxWindow.Object
toolBoxTab = toolBox.ToolBoxTabs.Item("General")
toolBoxItems = toolBoxTab.ToolBoxItems
toolBoxItems.Add("My HTML", "<b>This is bold HTML</b>", _
vsToolBoxItemFormat.vsToolBoxItemFormatHTML)
End Sub
"GUFSZPVSVOUIJTDPEFBGSBHNFOUPG)5.-JTQMBDFEPOUPUIF5PPMCPYJGZPVESBHUIBU
5PPMCPYJUFNJOUPUIF)5.-EFTJHOFSUFYUXJMMBQQFBSJOCPMETUZMF
210
Working with Microsoft Visual Studio 2005
Note Remember that HTML is really just an application of XML that follows a particular
schema. Because HTML is XML, you can also store XML fragments as HTML on the Toolbox.
Visual Studio not only lets you drag-and-drop these HTML/XML fragments into an HTML
document, but it also allows you to drag them into an XML document. In fact, a better name
for the vsToolBoxItemFormatHTML value would have been vsToolBoxItemFormatXML.
"MPOHXJUIUIFTFUXPUFYUGPSNBUTUIF5PPMCPYDBOBMTPTUPSF"DUJWF9ˆDPOUSPMTXIJDIDBO
CFESBHHFEJOUP)5.-EFTJHOFSPS8JOˆBQQMJDBUJPOTTVDIBTB.JDSPTPGU'PVOEBUJPO
$MBTTFT<.'$>EJBMPHCPY
UIBUTVQQPSUIPTUJOH"DUJWF9DPOUSPMT5PBEEBO"DUJWF9DPOUSPM
TVQQMZUIFXU6QQN$QZ+VGO(QTOCV)7+&EBUBUZQF5IFGPSNBUPGUIF&CVCBSHVNFOUJTUIF
$MBTT*EFOUJ¾FS$-4*%
(6*%PGUIF"DUJWF9DPOUSPMPSEFTQJUFUIFOBNFPGUIFGPSNBU
UZQF
UIF1SPH*%PGUIFDPOUSPM5IFGPMMPXJOHNBDSPBEETUXPDPQJFTPGUIF8JOEPXT
.FEJBˆ1MBZFSDPOUSPMUPUIF5PPMCPY5IF¾STUPOFJTBEEFECZVTJOHUIF$-4*%PGUIF
DPOUSPMBOEUIFTFDPOEJTBEEFECBTFEPOJUT1SPH*%
Sub AddCOMObjectToTheToolBox()
Dim toolBoxWindow As EnvDTE.Window
Dim toolBox As EnvDTE.ToolBox
Dim toolBoxTab As EnvDTE.ToolBoxTab
Dim toolBoxItems As EnvDTE.ToolBoxItems
toolBoxWindow = DTE.Windows.Item(Constants.vsWindowKindToolbox)
toolBox = toolBoxWindow.Object
toolBoxTab = toolBox.ToolBoxTabs.Item("General")
toolBoxItems = toolBoxTab.ToolBoxItems
toolBoxItems.Add("Name", "{22D6F312-B0F6-11D0-94AB- 0080C74C7E95}", _
vsToolBoxItemFormat.vsToolBoxItemFormatGUID)
toolBoxItems.Add("Name", "MediaPlayer.MediaPlayer.1", _
vsToolBoxItemFormat.vsToolBoxItemFormatGUID)
End Sub
8IFOZPVSVOUIJTDPEFZPVµMMOPUJDFUIBUUIF0COGQBSBNFUFSJTJHOPSFE5IJTJTCFDBVTF
UIF5PPMCPYFYUSBDUTUIFOBNFGSPNUIFDPOUSPM
Note .NET User Controls can be added to the toolbox by using the vsToolBoxItemFormat.
vsToolBoxItemFormatDotNETComponent enumerated value. However, this method has been
deprecated in favor of using the Content Installer to install controls, as we discussed in
Chapter 4. The Content Installer gives you much better control over where items are installed
into the toolbox and makes installing and uninstalling controls much easier.
The Task List Window
"TZPVTBXFBSMJFSUIFQSPHSBNNBCMFPCKFDUCFIJOEUIF5BTL-JTUXJOEPXJTUIF'PX&6'
6CUM.KUVPCKFDU6TJOHUIF6CUM.KUVPCKFDUZPVDBOBEEOFXUBTLJUFNTUPQSPWJEFJOGPSNBUJPO
UPUIFVTFSBCPVUXPSLUIBUOFFETUPCFQFSGPSNFEBTXFMMBTFYBNJOFUBTLTBEEFECZB
DPNQJMFSPSPUIFSUPPM
Chapter 9:
Programming the Visual Studio User Interface
211
Task List Items
5IF'PX&6'6CUM.KUVPCKFDUMFUTZPVHFUUPUIFJUFNTJOUIF5BTL-JTUXJOEPXCZDBMMJOHUIF
6CUM+VGOUQSPQFSUZXIJDISFUVSOTB6CUM+VGOUDPMMFDUJPODPOUBJOJOHPOFJUFNGPSFBDIUBTL
JUFNJOUIF5BTL-JTUXJOEPX:PVDBOWJFXTVCTFUTPGUIFJUFNTJOUIF5BTL-JTUXJOEPXCZ
¾MUFSJOHPVUJUFNTUIBUEPOµUCFMPOHUPBQBSUJDVMBSHSPVQJOHPSDBUFHPSZCVUJUFNTUIBUBS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PMUP¾YUIJTBVUPNBUJDBMMZ
N
"NBDSPUIBUTZODISPOJ[FTZPVSDBMFOEBSJO.JDSPTPGU0VUMPPLˆXJUIUIF7JTVBM4UVEJP
5BTL-JTUXJOEPXSFNJOEJOHZPVBNPOHPUIFSUIJOHTUPQJDLVQBHJGUPOZPVSXBZ
IPNFGSPNXPSLGPSBOBOOJWFSTBSZPSBCJSUIEBZ4VDIBUPPMDBOTBWFZPVBMPUPGHSJFG
:PVDBOCVJMETVDIUPPMTCFDBVTFZPVDBOJOTFSUOFXUBTLJUFNTJOUPUIF5BTL-JTUXJOEPXCZ
VTJOHUIF6CUM+VGOU#FFNFUIPE6CUM+VGOU#FFPGGFSTBHSFBUEFBMPG¿FYJCJMJUZJOUIFFMFNFOUT
UIBUBSFEJTQMBZFEGPSOFXUBTLJUFNTBOEIPXUIFZµSFEJTQMBZFE"TBSFTVMUUIJTNFUIPEIBT
POFPGUIFNPTUDPNQMFYBSHVNFOUTJHOBUVSFTPGBMMUIFNFUIPETJO7JTVBM4UVEJP
public EnvDTE.TaskItem Add(string Category,
string SubCategory,
string Description,
EnvDTE.vsTaskPriority Priority = vsTaskPriorityMedium,
object Icon,
bool Checkable = false,
string File = "",
int Line = -1,
bool CanUserDelete = true,
bool FlushItem = true)
212
Working with Microsoft Visual Studio 2005
:PVDBOVTFUIFTBNQMFBEEJO"EE5BTL-JTU*UFNTUPTFFUIFPVUQVUHFOFSBUFECZUIFNBOZ
DPNCJOBUJPOTPGUIFTFQBSBNFUFST8FµMMMPPLBUFBDIQBSBNFUFSJOUVSOPWFSUIFOFYUGFX
TFDUJPOT
Category and SubCategory
"MMUBTLTXIFUIFSUIFZµSFDSFBUFECZUIFBVUPNBUJPOPCKFDUNPEFMPSCZ7JTVBM4UVEJP
JUTFMGCFMPOHUPBDBUFHPSZ%CVGIQTKGUBSFVTFETJNQMZUPHSPVQUBTLTBOESFMBUFUIFNUP
POFBOPUIFS$PNNPODBUFHPSZUZQFTBSFDPNQJMFFSSPSTVTFSUBTLTBOETIPSUDVUT:PV
DBODSFBUFOFXDBUFHPSZHSPVQTCZVTJOHUIF%CVGIQT[QBSBNFUFSPGUIF#FFNFUIPE8IFO
ZPVDBMMUIFNFUIPEUIFMJTUPGDVSSFOUMZLOPXODBUFHPSJFTJTTFBSDIFEGPSBDBUFHPSZXJUIB
OBNFUIBUNBUDIFTUIJTBSHVNFOU*GPOFJTOPUGPVOEBOFXDBUFHPSZJTBEEFEBOEUIFOFX
UBTLJUFNJTBEEFEUPUIJTDBUFHPSZ*GBDBUFHPSZXJUIBNBUDIJOHOBNFJTGPVOEUIFOFX
UBTLJTBEEFEUPUIBUFYJTUJOHDBUFHPSZHSPVQ
7JTVBM4UVEJPEPFTOµUDVSSFOUMZVTFUIF5WD%CVGIQT[BSHVNFOUPGUIF#FFNFUIPEZPVSBEE
JOPSNBDSPDBOMFBWFJUCMBOLPSVTFJUGPSJOUFSOBMCPPLLFFQJOH
Description
5IFEFTDSJQUJPOPGBUBTLBQQFBSTJOUIF%FTDSJQUJPODPMVNOPGUIF5BTL-JTUXJOEPXBOE
UIF&GUETKRVKQPBSHVNFOUPGUIF#FFNFUIPETFUTUIJTDPMVNO5IJTQBSBNFUFSPGUIF#FF
NFUIPEBOEUIF%CVGIQT[BOE5WD%CVGIQT[QBSBNFUFSTBSFUIFPOMZSFRVJSFEQBSBNFUFST
*HOPSJOHUIFPQUJPOBMQBSBNFUFSTGPSOPXXFµMMDSFBUFPVS¾STU5BTL-JTUJUFNCZVTJOHUIF
GPMMPXJOHNBDSPDPEF
Sub TLAddItems()
Dim taskList As EnvDTE.TaskList
taskList = DTE.Windows.Item(Constants.vsWindowKindTaskList).Object
taskList.TaskItems.Add("Category", "", "Description2")
taskList.TaskItems.Add("Category", "", "Description1")
End Sub
Priority
5IFOFYUBSHVNFOUZPVDBOQBTTUPUIF#FFNFUIPEJTUIF2TKQTKV[BSHVNFOU5IJTBSHVNFOU
JTPQUJPOBMXIFOZPVVTFUIF7JTVBM#BTJDQSPHSBNNJOHMBOHVBHFCVUJGJUµTTVQQMJFEBO
JDPOBQQFBSTJOUIF¾STUDPMVNOPGUIF5BTL-JTU±UIFQSJPSJUZDPMVNO±UPSFNJOEUIFVTFS
PGUIFJNQPSUBODFPGDPNQMFUJOHUIBUUBTL"IJHIQSJPSJUZUBTLIBTBSFEFYDMBNBUJPOQPJOU
OFYUUPJUBMPXQSJPSJUZUBTLIBTBCMVFEPXOXBSEQPJOUJOHBSSPXBOEBNFEJVNQSJPSJUZ
UBTLIBTOPQSJPSJUZJDPO5IFGPMMPXJOHNBDSPBEETOFXUBTLJUFNTUPUIF5BTL-JTUFBDI
XJUIBEJGGFSFOUQSJPSJUZ
Sub TLAddItemsPriority()
Dim taskList As EnvDTE.TaskList
taskList = DTE.Windows.Item(Constants.vsWindowKindTaskList).Object
taskList.TaskItems.Add("Category", "", _
Description1", vsTaskPriority.vsTaskPriorityHigh)
Chapter 9:
Programming the Visual Studio User Interface
213
taskList.TaskItems.Add("Category", "", _
"Description2", vsTaskPriority.vsTaskPriorityLow)
taskList.TaskItems.Add("Category", "", _
"Description3", vsTaskPriority.vsTaskPriorityMedium)
End Sub
Icon
5IF+EQPQBSBNFUFSBMMPXTZPVUPQMBDFBOJDPOOFYUUPBOFXMZBEEFEUBTLJUFNUPJEFOUJGZ
UIBUUBTLJUFN5IF¾WFQSFEF¾OFEJDPOTBSFEFTDSJCFEJO5BCMF
Table 9-2
Icon Image
Predefined Icons for Task List Items
Constant
vsTaskIcon.vsTaskIconComment
vsTaskIcon.vsTaskIconUser
vsTaskIcon.vsTaskIconSquiggle
vsTaskIcon.vsTaskIconShortcut
vsTaskIcon.vsTaskIconCompile
-FGUPVUPGUIJTUBCMFJTUIFEFGBVMUJDPOXU6CUM+EQP0QPG²BCMBOLJDPO±XIJDIBQQFBSTPSJO
UIJTDBTFEPFTOPUBQQFBS
JGUIJTQBSBNFUFSJTOPUTQFDJ¾FE
Note If you call the TaskItems.Add method and supply the value vsTaskIconShortcut as the
icon, a shortcut in a file isn’t created. The icon is used for display purposes only. This applies to
the other values that can be passed as the Icon parameter; using vsTaskIconCompile doesn’t
create a compiler error, vsTaskIconComment doesn’t add a comment to a source file, and so
forth.
*GUIFTFQSFEF¾OFEJNBHFTEPOµUTVJUUIFUBTLJUFNZPVµSFDSFBUJOHZPVDBODSFBUFZPVS
PXOJNBHFUPEJTQMBZOFYUUPUIFUBTL:PVOFFEBCZQJYFMCJUNBQJNBHFXJUIBDPMPS
EFQUIPGFJUIFSDPMPSTPSCJU
DPMPST"OZQJYFMJOUIFJNBHFUIBUIBTB
CBDLHSPVOE3(#DPMPSPGXJMMCMFFEUISPVHIUIFJNBHFTIPXJOHUIFDPMPSPGUIF
5BTL-JTUXJOEPX5IF+EQPQBSBNBUFSDBOCFTFUUPPOFPGUISFFGPSNBUTJOBEEJUJPOUP
UIFQSFWJPVTMZMJTUFEDPOTUBOUT:PVDBOQBTTUIFIBOEMFPGB5[UVGO&TCYKPI$KVOCRPCKFDU
XIJDIJTSFUSJFWFEGSPNUIF)GV*DKVOCRNFUIPE:PVDBOBMTPQBTTUIFQBUIBTBTUSJOHUP
BCJUNBQ¾MFPOEJTL5IF¾OBMXBZJTUPMPBEUIFCJUNBQJOUPBO+2KEVWTG&KURJOTUBODFBOE
UIFOQBTTJUBTUIF+EQPQBSBNFUFS"O+2KEVWTG&KURJOUFSGBDFJTUIF$0.XBZPGQBTTJOH
BSPVOEBCJUNBQPCKFDU5PDSFBUFBO+2KEVWTG&KURPCKFDUJOB/&5BEEJOZPVNVTUXSJUFB
TNBMMBNPVOUPG1*OWPLFDPEFUPDSFBUFUIJTPCKFDUUZQF1*OWPLFJTUIFUFDIOPMPHZUIF
/&5'SBNFXPSLVTFTUPDBMMVONBOBHFEDPEFGSPN/&5QSPHSBNT
5IFTZTUFN%--
PMFBVUEMMFYQPSUTBNFUIPEDBMMFE1NG.QCF2KEVWTG(KNGUIBUUBLFTBQBUIUPBCJUNBQ¾MF
214
Working with Microsoft Visual Studio 2005
XIJDIDBOCFUIFCJUNBQUPTIPXJOUIF5BTL-JTUBOESFUVSOTUIFOFDFTTBSZ+2KEVWTG&KUR
PCKFDU#FGPSFZPVDBMMUIF1NG.QCF2KEVWTG(KNGNFUIPEZPVNVTUBEETPNFDPEFUIBUNJHIU
TFFNNBHJDBMUPUIFDMBTTUIBUJNQMFNFOUTZPVSBEEJO
[DllImport("oleaut32.dll",
CharSet=System.Runtime.InteropServices.CharSet.Auto,
SetLastError=true)]
internal extern static int OleLoadPictureFile(object fileName,
[MarshalAsAttribute(UnmanagedType.IDispatch)] ref object ipictrueDisp);
5IJTDPEFEF¾OFTUIFNFUIPETJHOBUVSFGPSDPEFUIBUµTFYQPSUFEGSPNUIF$0.%--
0MF"VUEMMBOEXJUIUIJT1*OWPLFNFUIPEEFDMBSFEZPVDBODBMMUIF1NG.QCF2KEVWTG(KNG
NFUIPEXJUIUIF¾MFOBNFPGUIFDVTUPNCJUNBQ
object objIPictureDisp = null;
string filename = "C:\SomeImage.bmp";
int nret = OleLoadPictureFile(fileName, ref objIPictureDisp);
8IFOUIJTNFUIPEDBMMSFUVSOTUIFQDL+2KEVWTG&KURWBSJBCMFJTTFUUPBO+2KEVWTG&KURPCKFDU
UIBUDBOCFQBTTFEBTUIF+EQPQBSBNFUFSPGUIF6CUM+VGOU#FFNFUIPE
*GZPVUSZUPDBMMUIF#FFNFUIPEGSPNXJUIJOBNBDSPBOEQBTTBTUIF+EQPQBSBNFUFSBO
+2KEVWTG&KURPCKFDUPSUIFIBOEMFUPBCJUNBQBOFYDFQUJPOJTHFOFSBUFE5IJTIBQQFOT
CFDBVTFZPVSNBDSPTSVOJOBTFQBSBUFQSPDFTT8IFOBNFUIPEPSQSPQFSUZJTDBMMFEPO
UIF7JTVBM4UVEJPPCKFDUNPEFMBMMEBUBNVTUCFNBSTIBMFEPSUSBOTMBUFEGSPNUIFNFNPSZ
CFJOHVTFECZUIF.BDSPTFEJUPSQSPHSBNUPUIFNFNPSZVTF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SPQFSUJFT%CVGIQT[5WD%CVGIQT[%JGEMGF&GUETKRVKQP(KNG0COG.KPG
BOE2TKQTKV[FBDIDBOCFSFBEQSPHSBNNBUJDBMMZUPTFFXIBUEBUBJTTUPSFEGPSUIPTFDPMVNOT
PGUIF5BTL-JTU:PVDBOBMTPTFUUIFTFQSPQFSUJFTBTMPOHBTUIFZµSFOPUSFBEPOMZ4PNFUBTL
JUFNTUIBU7JTVBM4UVEJPDSFBUFTIBWFUIFJSDPMVNOTNBSLFEBTSFBEPOMZTPUIFZDBOµUCF
NPEJ¾FE5PUFTUXIFUIFSBQBSUJDVMBSDPMVNODBOCFTFUZPVDBONBLFBDBMMUPUIF+U5GVVCDNG
QSPQFSUZXIJDIBDDFQUTBTBQBSBNFUFSUIFDPMVNOXJUIJOUIF5BTL-JTUBXU6CUM.KUV%QNWOP
FOVNFSBUJPOWBMVF
BOEJGUIFDPMVNODBOCFNPEJ¾FEUIF+U5GVVCDNGQSPQFSUZSFUVSOTUSVF
PUIFSXJTFJUSFUVSOTGBMTF'PSFYBNQMFUPDIBOHFBUBTLJUFNµTEFTDSJQUJPOWBMVFZPVDBO
XSJUFDPEFTVDIBTUIJTXIJDI¾STUWFSJ¾FTUIBUUIFEFTDSJQUJPODBOCFDIBOHFE
Sub ModifyTaskDescription()
Dim taskList As EnvDTE.TaskList
Dim task As EnvDTE.TaskItem
taskList = DTE.Windows.Item(Constants.vsWindowKindTaskList).Object
task = taskList.TaskItems.Item(1)
If (task.IsSettable(vsTaskListColumn.vsTaskListColumnDescription)) Then
task.Description = "A new description"
End If
End Sub
&GNGVGEFMFUFTUIFJUFNJGEFMFUJPOJTQPTTJCMFGSPNUIFMJTUPGJUFNT"TNFOUJPOFEFBSMJFSBMM
JUFNTBEEFEUISPVHIUIFPCKFDUNPEFMDBOCFEFMFUFEXIFUIFSUIF%CP7UGT&GNGVGQBSBNFUFS
JTVTWGPSHCNUGXIFOZPVDBMMUIF6CUM+VGOU#FFNFUIPE0UIFSUBT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FUBTLJUFN*GUIFWBMVFHCNUGJTQBTTFECBDLUISPVHIUIF
0CXKICVG*CPFNGFBSHVNFOUBOEOPPOFFMTFIBOEMFTUIFOBWJHBUJPOPGUIFUBTL7JTVBM4UVEJP
QMBZTBCFMMTPVOEGPSUIFVTFS
$POOFDUJOHUPUIJTFWFOUXJUIJOBNBDSPQSPKFDUJTBTTJNQMFBTPQFOJOHUIF'PXKTQPOGPV'XGPVU
NBDSPNPEVMFTFMFDUJOHUIF5BTL-JTU&WFOUTJUFNGSPNUIFESPQEPXOMJTUBUUIFUPQMFGUPG
UIFFEJUPSXJOEPXGPSUIJTNPEVMFBOEUIFOTFMFDUJOHUIF6CUM0CXKICVGFFWFOUGSPNUIFUPQ
SJHIUESPQEPXOMJTU6TJOHUIJTFWFOUBOEUIFBSHVNFOUTUIBUBSFQBTTFEUPJUZPVDBOXSJUF
BNBDSPFWFOUIBOEMFSGPSUIF0CXKICVG*CPFNGFFWFOUUIBUPQFOTUIF¾MFJGTQFDJ¾FE
UIBUUIF
UBTLJUFNSFGFSTUPBOETFMFDUUIFMJOFJOUIFTPVSDF¾MFUIBUUIFUBTLJUFNQPJOUTUP5IFDPEF
GPSUIJTFWFOUIBOEMFSXPVMEMPPLMJLFUIJT
Private Sub TaskListEvents_ TaskNavigated(ByVal TaskItem As EnvDTE.TaskItem, _
ByRef NavigateHandled As Boolean) Handles TaskListEvents.TaskNavigated
'If the file argument has been specified for this task...
Chapter 9:
Programming the Visual Studio User Interface
217
If (TaskItem.FileName <> "") Then
Dim fileWindow As EnvDTE.Window
Dim textWindow As EnvDTE.TextWindow
Dim textPane As EnvDTE.TextPane
'Then open the file, find the TextWindow and TextPane objects...
fileWindow = DTE.ItemOperations.OpenFile(TaskItem.FileName, _
EnvDTE.Constants.vsViewKindTextView)
textWindow = CType(fileWindow.Object, EnvDTE.TextWindow)
textPane = CType(textWindow.ActivePane, EnvDTE.TextPane)
'Then move the caret to the correct line:
textPane.Selection.MoveTo(TaskItem.Line, 1, False)
textPane.Selection.SelectLine()
NavigateHandled = True
End If
End Sub
$POOFDUJOHUPUIJTFWFOUXJUIJOBOBEEJOJTBMNPTUBTTJNQMFBTDPOOFDUJOHUPJUXJUIJOB
NBDSPCVUBMJUUMFNPSFDPEFJTOFFEFE5IF¾STUTUFQJTUPEFDMBSFBWBSJBCMFUPDPOOFDUUIF
FWFOUIBOEMFSUP*OUIJTDBTFXFµSFDPOOFDUJOHUP5BTL-JTUFWFOUTTPXFµMMVTFUIF'PX&6'
6CUM.KUV'XGPVUDMBTT
private EnvDTE.TaskListEvents taskListEvents;
/FYUZPVEFDMBSFUIFFWFOUIBOEMFSNFUIPEXIJDINVTUGPMMPXUIFNFUIPETJHOBUVSFBT
EFDMBSFEJOUIF0CKFDU#SPXTFS:PVDBOBMTPDPOWFSUUIFNBDSPDPEFTIPXOFBSMJFSJOUP$
GPSBOBEEJO
public void TaskNavigated(EnvDTE.TaskItem taskItem,
ref bool navigateHandled)
{
//If the file argument has been specified for this task...
if(taskItem.FileName != "")
{
EnvDTE.Window fileWindow;
EnvDTE.TextWindow textWindow;
EnvDTE.TextPane textPane;
// Then open the file, find the TextWindow and TextPane objects...
fileWindow = applicationObject.ItemOperations.OpenFile(
taskItem.FileName, EnvDTE.Constants.vsViewKindTextView);
textWindow = (EnvDTE.TextWindow)fileWindow.Object;
textPane = (EnvDTE.TextPane)textWindow.ActivePane;
//Then move the caret to the correct line:
textPane.Selection.MoveTo(taskItem.Line, 1, false);
textPane.Selection.SelectLine();
navigateHandled = true;
}
}
'JOBMMZZPVNVTUTFUUIFVCUM.KUV'XGPVUWBSJBCMFUPBOJOTUBODFPGB6CUM.KUV'XGPVUPCKFDU
XIJDIZPV¾OECZDBMMJOHUIF'XGPVU6CUM.KUV'XGPVUQSPQFSUZ5IJTQSPQFSUZUBLFTPOF
218
Working with Microsoft Visual Studio 2005
BSHVNFOU±BDBUFHPSZUIBUµTVTFEBTB¾MUFS*GZPVQBTTUIFFNQUZTUSJOHBTBOBSHVNFOU
ZPVSFWFOUIBOEMFSJTDBMMFEXIFOBOZUBTLJUFNHFOFSBUFTBOFWFOU±XIFUIFSUIFJUFNXBT
BEEFECZBOBEEJOPSNBDSPPSCZ7JTVBM4UVEJPJUTFMG#VUJGZPVTQFDJGZBDBUFHPSZGPSUIJT
BSHVNFOU±UIFTBNFDBUFHPSZTUSJOHZPVDBOQBTTBTUIF¾STUBSHVNFOUUPUIF6CUM+VGOU
#FFNFUIPE±POMZFWFOUTGPSBUBTLJUFNUIBUIBWFUIJTTBNFDBUFHPSZBSFTFOUUPZPVSFWFOU
IBOEMFS5IJT¾MUFSJOHNFDIBOJTNDBOIFMQDVUEPXOPOUIFOVNCFSPGFWFOUTUIBUBSF¾SFE
UIFSFCZJODSFBTJOHUIFQFSGPSNBODFPGZPVSDPEF#FDBVTFXFXBOUPVSDPEFUPIBOEMF
FWFOUTGPSBMMUBTLJUFNTXFµMMQBTTUIFFNQUZTUSJOHUPUIF'XGPVU6CUM.KUV'XGPVUQSPQFSUZ
EnvDTE.Events events = applicationObject.Events;
taskListEvents = (EnvDTE.TaskListEvents)events.get_TaskListEvents("");
5IFMBTUTUFQJTUPBTTPDJBUFUIFFWFOUPCKFDUXJUIUIFFWFOUIBOEMFS:PVEPUIJTCZDSFBUJOH
BOFX'PX&6'AFKUR6CUM.KUV'XGPVUA6CUM0CXKICVGF'XGPV*CPFNGTPCKFDUBOEBEEJOHJUUPUIF
VCUM.KUV'XGPVU6CUM0CXKICVGFDPMMFDUJPOPGFWFOUIBOEMFST
taskListEvents.TaskNavigated += new
_dispTaskListEvents_TaskNavigatedEventHandler(this.TaskNavigated);
6CUM0CXKICVGFJTOµUUIFPOMZ5BTL-JTUFWFOUZPVSDPEFDBODBQUVSF6CUM#FFGFBOE
6CUM4GOQXGFFWFOUTBSF¾SFEXIFOBOFXUBTLJUFNJTBEEFEPSKVTUCFGPSFJUµTSFNPWFE
SFTQFDUJWFMZ5IFMBTUFWFOU6CUM/QFK¿GFJT¾S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TDSFBUFTB5BTL-JTUJUFNGPS.JDSPTPGU7JTVBM
$BOE$XJUIUIFEFTDSJQUJPO(KZVJKUNCVGTBOEUIFDPNNFOUµ61&1(KZVJKUNCVGT¶EPFT
UIFTBNFGPSB7JTVBM#BTJD¾MF
5IFTQFDJBMUPLFOTUIBUUIF5BTL-JTUTFBSDIFTGPSBSFEF¾OFEJOUIF0QUJPOTEJBMPHCPY
XIFSFZPVDBOBEEOFXUPLFOTBOESFNPWFPSNPEJGZFYJTUJOHUPLFOT5IFEFGBVMUDPNNFOU
UPLFOTBSF)"$,50%06/%0/&BOE6OSFTPMWFE.FSHF$PO¿JDU
Chapter 9:
Programming the Visual Studio User Interface
219
:PVDBOBEEBOFXUPLFOCZUZQJOHBUPLFOOBNFJOUIF/BNFCPYTFMFDUJOHBQSJPSJUZBOE
UIFODMJDLJOHUIF"EECVUUPO:PVDBOBMTPBEESFNPWFBOENPEJGZUIFTFUPLFOTCZVTJOH
UIFPCKFDUNPEFM5PQSPHSBNUIFTFUPLFOTVTFUIF2TQRGTVKGUDPMMFDUJPO:PVµMM¾OENPSF
EFUBJMTBCPVUUIF2TQRGTVKGUDPMMFDUJPOMBUFSJOUIJTDIBQUFS±GPSOPXXFµMMPWFSMPPLUIFEFUBJMT
PGIPXUPVTFUIF2TQRGTVKGUDPMMFDUJPOBOEMPPLPOMZBUIPXUPDIBOHFUIFUPLFOTCZVTJOH
UIJTPCKFDU5IF¾STUTUFQJTUP¾OEUIF%QOOGPV6QMGPUQSPQFSUZCZVTJOHDPEFTVDIBTUIF
GPMMPXJOH
Sub GetTokensArray()
Dim tokens As Object()
Dim prop As EnvDTE.Property
Dim props As EnvDTE.Properties
props = DTE.Properties("Environment", "TaskList")
prop = props.Item("CommentTokens")
tokens = prop.Value
End Sub
5IF%QOOGPV6QMGPUQSPQFSUZSFUVSOTBOBSSBZPGTUSJOHTUIBUIBWFBTQFDJBMGPSNBUBOE
XIFOUIJTNBDSPJTSVOJU¾OETBMMUIFBWBJMBCMFUPLFOTJOUIFGPSNBU6QMGP0COG2TKQTKV[
XIFSF6QMGP0COGJTXIBUTIPVMEBQQFBSBGUFSUIFDPNNFOUOPUBUJPOGPSUIFHJWFOMBOHVBHF
BOE2TKQTKV[JTUIFOVNFSJDBMWBMVFPGBOJUFNJOUIF'PX&6'XU6CUM2TKQTKV[FOVNFSBUJPO*O
UIFQSFDFEJOHNBDSPUIFTUSJOHGPSUIF50%0UPLFOJT61&1CFDBVTFUIFTUSJOHUPTFBSDI
GPSJOUIFUFYUFEJUPSJT61&1BOEUIFQSJPSJUZUIBUBQQFBSTJOUIF5BTL-JTUGPSUIJTUPLFOJT
XU6CUM2TKQTKV[/GFKWOXIPTFOVNFSJDBMWBMVFJT
"EEJOHZPVSPXOUPLFOUPUIFMJTUPGUPLFOTJTBUISFFTUFQQSPDFTT4FUUJOHUIFMJTUPGUPLFOT
DMFBSTUIFDVSSFOUMJTUBUMFBTUUIFUPLFOTUIBUBSFOPUSFBEPOMZ
TPZPVOFFEUPQSFTFSWFUIF
LOPXOUPLFOTTPZPVEPOµUPWFSXSJUFUIFLOPXOUPLFOTUIBUUIFVTFSNJHIUIBWFDSFBUFE
'JSTUZPVOFFEUPSFUSJFWFUIFMJTUPGDVSSFOU5BTL-JTUUPLFOT"EEZPVSPXOUPLFOUPUIF
BSSBZPGFYJTUJOHUPLFOTBOEUIFOTFUUIFQSPQFSUZXJUIUIFFYQBOEFEBSSBZ:PVDBOTFFUIJT
JOUIFGPMMPXJOHNBDSPXIJDIBEETBIJHIQSJPSJUZ4&$63*5:UPLFOUPUIFMJTUPGDPNNFOU
UPLFOT
Sub AddSecurityToken()
Dim tokens As Object()
Dim token As String
Dim prop As EnvDTE.Property
Dim props As EnvDTE.Properties
'Find the property holding the known tokens
props = DTE.Properties("Environment", "TaskList")
prop = props.Item("CommentTokens")
tokens = prop.Value
Add one to the list of known tokens to hold
' the new SECURITY token
ReDim Preserve tokens(tokens.Length)
'Add the new token
tokens(tokens.Length - 1) = "SECURITY:3"
'Set the list of known tokens
prop.Value = tokens
End Sub
220
Working with Microsoft Visual Studio 2005
5PEFMFUFBUPLFOZPVSVOTJNJMBSDPEFCVUJOTUFBEPGBEEJOHBOFMFNFOUUPUIFBSSBZZPV
SFNPWFBOFMFNFOU
Sub RemoveSecurityToken()
Dim tokens As Object()
Dim newTokens As Object()
Dim token As String
Dim i As Integer = 0
Dim found As Boolean = False
Dim prop As EnvDTE.Property
Dim props As EnvDTE.Properties
props = DTE.Properties("Environment", "TaskList")
prop = props.Item("CommentTokens")
tokens = prop.Value
'Don't want to shrink the array if
' the token is not available
For Each token In tokens
If token = "SECURITY:3" Then
found = True
Exit For
End If
Next
'If the SECURITY token was not found, then
' there is nothing to remove so we can exit
If found = False Then
Exit Sub
End If
'Resize the newTokens array
ReDim newTokens(tokens.Length - 2)
'Copy the list of tokens into the newTokens array
' skipping the SECURITY token
For Each token In tokens
If token <> "SECURITY:3" Then
newTokens(i) = token
i = i + 1
End If
Next
'Set the list of tokens
prop.Value = newTokens
End Sub
*GZPVSBEEJOHFOFSBUFTDPEFUPQMBDFJOUIFUFYUCVGGFSBOEZPVXBOUUPJOTFSUBDPNNFOU
UPLFOUIBUHJWFTUIFVTFSBEEJUJPOBMJOGPSNBUJPOBCPVUIPXUPNPEJGZUIFDPEFZPVDBOVTF
UIF6CUM.KUV&GHCWNV%QOOGPV6QMGPQSPQFSUZUP¾OEXIJDIUPLFOUPJOTFSU5IFGPMMPXJOHDPEF
DSFBUFTBTUSJOHDPOUBJOJOHBDMBTTXJUIUIFEFGBVMUDPNNFOUUPLFOEJSFDUJOHUIFVTFSUP
XIFSFUPJOTFSUDPEF
Sub InsertTLTokenCode()
Dim classString As String
Dim taskList As EnvDTE.TaskList
taskList = DTE.Windows.Item(Constants.vsWindowKindTaskList).Object
classString = "Public Class AClass" + Chr(13)
classString = classString + Chr(9) + "'" + taskList.DefaultCommentToken
Chapter 9:
Programming the Visual Studio User Interface
221
classString = classString + ": Insert your code here" + Chr(13)
classString = classString + "End Class"
End Sub
The Error List Window
5IFFSSPSMJTUXJOEPXMPPLTMJLFBOEBDUTWFSZNVDIMJLFUIFUBTLMJTUFYDFQUUIJTXJOEPXJT
SFBEPOMZNFBOJOHZPVDBOOPUBEEZPVSPXOJUFNTUPJU±BEEJOHJUFNTJTSFTFSWFEPOMZGPS
DPNQJMFST8IFOUIFDPEFJTDPNQJMFEXJUIJO7JTVBM4UVEJPUIFPVUQVUPGUIFDPNQJMFSTBSF
TDBOOFEBOEPOFJUFNJTJOTFSUFEJOUPUIFFSSPSMJTUGPSFBDIFSSPSXBSOJOHPSNFTTBHFGPVOE
*GZPVXFSFUPCSPXTFUIFPCKFDUTNFUIPETBOEQSPQFSUJFTPGUIFFSSPSMJTUZPVXJMM¾OEUIBU
NBOZPGUIFNFUIPETBOEQSPQFSUJFTPOUIF'TTQT.KUV'TTQT+VGOUBOE'TTQT+VGOPCKFDUTXPSL
NVDIMJLFUIFNFUIPETBOEQSPQFSUJFTPGUIF6CUM.KUV6CUM+VGOUBOE6CUM+VGOPCKFDUT
5IF'TTQT.KUVPCKFDUUIFPCKFDUSFQSFTFOUJOHUIFFSSPSMJTUUPPMXJOEPXJOUIFVTFSJOUFSGBDF
DBOCFGPVOEFJUIFSCZVTJOHUIF6QQN9KPFQYU'TTQT.KUVQSPQFSUZPSUIF9KPFQYU+VGO
NFUIPEQBTTJOHJOUIF'PX&6'9KPFQY-KPFUXU9KPFQY-KPF'TTQT.KUVDPOTUBOUBOEUIFO
DBTUJOHUIF9KPFQY1DLGEVQSPQFSUZJOUPBO'TTQT.KUVUZQF
Sub FindErrorList()
Dim errorList As ErrorList
'Find the ErrorList object using the Windows.Item(...).Object style
errorList = DTE.Windows.Item _
(EnvDTE80.WindowKinds.vsWindowKindErrorList).Object
'Find the ErrorList object using the ToolWindows style
errorList = CType(DTE, DTE2).ToolWindows.ErrorList
End Sub
0ODFZPVIBWFUIF'TTQT.KUVPCKFDUZPVDBOHFUUPUIF'TTQT+V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vsWindowKindOutput)
outputWindow = CType(window.Object, EnvDTE.OutputWindow)
End Sub
Output Window Panes
5IFVTFSJOUFSGBDFPGUIF0VUQVUXJOEPXDPOTJTUTPGBOVNCFSPGWJFXQPSUTPSQBOFTFBDI
PGXIJDIEJTQMBZTUFYU:PVDBOTXJUDICFUXFFOUIFTFQBOFTCZTFMFDUJOHBQBOFCZOBNF
GSPNUIFESPQEPXOMJTUBUUIFUPQPGUIF0VUQVUXJOEPX:PVDBOFOVNFSBUFUIFQBOFTCZ
VTJOHUIF1WVRWV9KPFQY2CPGUPCKFDUBTTIPXOIFSF
Sub EnumOutputWindowPanes()
Dim window As EnvDTE.Window
Dim outputWindow As EnvDTE.OutputWindow
Dim outputWindowPanes As EnvDTE.OutputWindowPanes
Dim outputWindowPane As EnvDTE.OutputWindowPane
'Find the OutputWindow object
window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
outputWindow = CType(window.Object, EnvDTE.OutputWindow)
'Retrieve the OutputWindowPanes object
outputWindowPanes = outputWindow.OutputWindowPanes
'Enumerate each OutputWindowPane
For Each outputWindowPane In outputWindowPanes
MsgBox(outputWindowPane.Name)
Next
End Sub
:PVDBOBMTPVTFUIF1WVRWV9KPFQY2CPGUPCKFDUUPDSFBUFOFXQBOFT5IFNFUIPE#FFUBLFT
BTJUTPOMZBSHVNFOUUIFOBNFPGUIFOFXQBOFUPDSFBUF
Sub CreateOutputWindowPane()
Dim window As EnvDTE.Window
Dim outputWindow As EnvDTE.OutputWindow
Dim outputWindowPanes As EnvDTE.OutputWindowPanes
Dim outputWindowPane As EnvDTE.OutputWindowPane
'Find the OutputWindow object
window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
outputWindow = CType(window.Object, EnvDTE.OutputWindow)
'Retrieve the OutputWindowPanes object
outputWindowPanes = outputWindow.OutputWindowPanes
'Add a new pane:
outputWindowPane = outputWindowPanes.Add("My New Pane")
End Sub
5IJTNBDSPDSFBUFTBOFXPVUQVUXJOEPXQBOFOBNFE.Z/FX1BOFUIBUµTSFBEZUPCF¾MMFE
XJUIUIFUFYUPVUQVUPGZPVSBEEJOPSNBDSPDPEF:PVDBOJOKFDUUFYUJOUPUIJTXJOEPXCZ
Chapter 9:
Programming the Visual Studio User Interface
223
VTJOHUIF1WVRWV9KPFQY2CPG1WVRWV5VTKPINFUIPEXIJDIUBLFTBTUSJOHUIBUµTBQQFOEFEUP
UIFFOEPGPUIFSUFYUJOUIFBQQSPQSJBUFQBOF"TTUSJOHTBSFQMBDFEJOUPUIF0VUQVUXJOEPX
QBOFUIFZµSFJOKFDUFEXJUIPVUBMJOFCSFBLCFUXFFOUIFNUIJTNFBOTUIBUJGBOFXMJOF
DIBSBDUFSOFFETUPCFQMBDFECFUXFFOFBDITUSJOHZPVNVTUXSJUFUIFDPEFUPEPUIJT5IF
GPMMPXJOHNBDSPTBNQMFEJTQMBZTUIFDPOUFOUTPGUIFGPMEFSDPOUBJOJOHUIFTPMVUJPO¾MFUIBUµT
DVSSFOUMZPQFOBTFBDI¾MFQBUIJTEJTQMBZFEJOUIF0VUQVUXJOEPXQBOFBMJOFCSFBLPS
"4$**WBMVF
JTJOTFSUFE
Sub DisplaySolutionDirectory()
Dim files As String()
Dim file As String
Dim directoryOutputWindowPane As OutputWindowPane
Dim fullName As String
Dim outputWindow As OutputWindow
outputWindow = DTE.Windows.Item(Constants.vsWindowKindOutput).Object
'Find the folder the solution is in, as well as the files that are
' in that folder:
fullName = System.IO.Path.GetDirectoryName(DTE.Solution.FullName)
files = System.IO.Directory.GetFiles(fullName)
'Try to find a "Solution Directory" pane, if one does not exist,
' create it:
With outputWindow.OutputWindowPanes
Try
directoryOutputWindowPane = .Item("Solution Directory")
'Show the pane:
directoryOutputWindowPane.Activate()
Catch
directoryOutputWindowPane = .Add("Solution Directory")
End Try
End With
'Clear the pane:
directoryOutputWindowPane.Clear()
For Each file In files
'Display the file path, with a line break between each line
directoryOutputWindowPane.OutputString(file + Chr(13))
Next
End Sub
5IJTN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(QTOUBTTFNCMZJTVTFEQSPHSBNNJOHBGPSNJOUIFEFTJHOFSJTTJNJMBSUPQSPHSBNNJOHB
GPSNBTJUFYFDVUFTBUSVOUJNF
The IDesignerHost Interface
"'PSNTEFTJHOFSXJOEPXFYQPTFTBOPCKFDUNPEFMBTNBOZPGUIFPUIFSXJOEPXT
EP5IFPCKFDUIJFSBSDIZSFUVSOFEGSPNDBMMJOHUIF9KPFQY1DLGEVQSPQFSUZPGB'PSNT
EFTJHOFSXJOEPXJTPGUZQF5[UVGO%QORQPGPV/QFGN&GUKIP+&GUKIPGT*QUV5PFYBNJOF
BOENPEJGZBGPSNXJUIJOUIFEFTJHOFSZPVNVTU¾OEUIF5[UVGO9KPFQYU(QTOU%QPVTQN
PCKFDUGPSUIBUGPSN:PVDBOEPUIJTCZDBMMJOHUIF+&GUKIPGT*QUV4QQV%QORQPGPV
QSPQFSUZBOEDBTUJOHUIFPCKFDUSFUVSOFEJOUPB(QTOU%QPVTQNPCKFDUBTTIPXOJOUIJT
DPEFTOJQQFU
System.Windows.Forms.Control control;
System.ComponentModel.Design.IDesignerHost designerHost;
designerHost =(System.ComponentModel.Design.IDesignerHost)
applicationObject.ActiveWindow.Object;
control = (System.Windows.Forms.Control)designerHost.RootComponent;
Note The System.Windows.Forms.Form class derives from the System.Windows.Forms.
Control class. The code shown here demonstrates how to manipulate a user control, but you
can use the same unmodified code to program a Windows Form.
6TJOHUIF5[UVGO9KPFQYU(QTOU%QPVTQNPCKFDUZPVDBODPOOFDUFWFOUTUPEFUFSNJOFXIFO
UIFGPSNXBTNPEJ¾FEUP¾OEBOENPEJGZQSPQFSUJFTTVDIBTUIFEJNFOTJPOTPGUIFGPSN
BOEUPQMBDFNPEJGZBOESFNPWFDPOUSPMTGSPNUIFGPSN
Marshaling
*GZPVUSZUPVTFUIF+&GUKIPGT*QUVJOUFSGBDFGSPNXJUIJOBNBDSPB5[UVGO4WPVKOG4GOQVKPI
4GOQVKPI'ZEGRVKQPFYDFQUJPOJTUISPXO5IJTJTCFDBVTFVTFSJOUFSGBDFFMFNFOUTTVDIBTUIF
5[UVGO9KPFQYU%QPVTQNPCKFDUDBOOPUCFSFNPUFEBDSPTTQSPDFTTCPVOEBSJFT3FNFNCFS
UIBUUIF.BDSPT*%&SVOTJOBQSPDFTTTFQBSBUFGSPNUIF7JTVBM4UVEJPQSPDFTT#FDBVTFPG
UIJTSFTUSJDUJPOUIFEFTJHOFSPCKFDUNPEFMDBOCFVTFEPOMZXJUIJOBOBEEJOBOEOPUGSPNB
NBDSP
Chapter 9:
Programming the Visual Studio User Interface
225
Adding Controls to a Form
0ODFZPV¾OEUIF+&GUKIPGT*QUVJOUFSGBDFGPSB'PSNTEFTJHOFSZPVDBOFBTJMZBEEOFX
DPOUSPMTUPUIFGPSN5PBEEBDPOUSPMZPVOFFEUIF5[UVGO6[RGPCKFDUUIBUEFTDSJCFTUIF
DPOUSPM:PVDBO¾OEUIJTPCKFDUCZVTJOHUIF6[RG)GV6[RGTUBUJDNFUIPEXIJDIJTQBTTFE
UIFGVMMDMBTTOBNFPGBDPOUSPM'PSFYBNQMFUPBEEBMJTUWJFXDPOUSPMUPBGPSNZPVDBOVTF
DPEFTVDIBTUIJT
System.Type type;
type = System.Type.GetType("System.Windows.Forms.ListView");
:PVDBOUIFOQBTTUIJT6[RGPCKFDUUPUIF+&GUKIPGT*QUV%TGCVG%QORQPGPVNFUIPEUPDSFBUF
UIFDPOUSPM5IJTNFUIPEIBTUXPPWFSMPBETUIF¾STUPGXIJDIUBLFTUXPQBSBNFUFST5IF
¾STUQBSBNFUFSJTUIF6[RGPCKFDUXFKVTUGPVOEBOEUIFTFDPOEQBSBNFUFSJTUIFWBSJBCMF
OBNFPGUIFDPOUSPMXFXBOUUPDSFBUF5IJTWBSJBCMFOBNFNVTUCFVOJRVFBNPOHWBSJBCMFT
DPOUBJOFEXJUIJOUIFGPSNµTDMBTTPUIFSXJTFBOBNFDPMMJTJPOXJMMPDDVSBOEBOFYDFQUJPO
XJMMCFHFOFSBUFE5IFTFDPOEPWFSMPBEPGUIJTNFUIPEUBLFTBTBOBSHVNFOUPOMZUIF6[RG
PCKFDUUIF'PSNTEFTJHOFSFYBNJOFTUIFGPSNDPEFUP¾OEBVOJRVFWBSJBCMFOBNFUPVTF
#PUIPGUIFTFPWFSMPBETFNJUUIFBQQSPQSJBUFDPEFUPJOTUBOUJBUFBDPOUSPMPGUIFTQFDJ¾FE
UZQF5IFGPMMPXJOHDPEFDSFBUFTBMJTUWJFXDPOUSPMXJUIUIFWBSJBCMFOBNFNKUV8KGY%QPVTQN
System.ComponentModel.IComponent component;
component = designerHost.CreateComponent(type, "listViewControl");
*GZPVXFSFUPBEEUIJTDPEFUPBOBEEJOBOEFYFDVUFJUZPVXPVMEOµUTFFUIFDPOUSPMBQQFBS
POUIFGPSN5IJTJTCFDBVTFUIFDPOUSPMXIJMFJOTUBOUJBUFEIBTOµUCFFOQBSFOUFEUPUIF
GPSNBOEBEEFEUPUIFGPSNµT%QPVTQNUDPMMFDUJPO5PBEEUIFDPOUSPMUPUIFGPSNµT%QPVTQNU
DPMMFDUJPOZPVNVTUTFUUIF2CTGPVQSPQFSUZPGUIFDPOUSPMUPUIFGPSNUIBUTIPVMEDPOUBJO
UIFDPOUSPM:PVDBOTFUUIF2CTGPVQSPQFSUZPSBOZQSPQFSUZPGBDPOUSPMGPSUIBUNBUUFS
CZ
VTJOHUIF5[UVGO%QORQPGPV/QFGN2TQRGTV[&GUETKRVQT%QNNGEVKQPPCKFDU5IJTPCKFDUDPOUBJOT
BDPMMFDUJPOPGQSPQFSUJFTBWBJMBCMFGPSBDPOUSPMBTWBMVFTBSFTFUGPSUIFQSPQFSUJFTUIFZ
DPOUBJODPEFJTHFOFSBUFEXJUIJOUIFGPSNµTDMBTTUIBUDPSSFTQPOETUPUIFQSPQFSUZZPVTFU
:PVDBOTFUUIF2CTGPVQSPQFSUZBTGPMMPXT
System.ComponentModel.PropertyDescriptorCollection props;
System.ComponentModel.PropertyDescriptor propParent;
//Find the properties for the listViewControl control:
props = System.ComponentModel.TypeDescriptor.GetProperties(component);
//Get the Parent property
propParent = props["Parent"];
//Set the Parent property to the form:
propParent.SetValue(newControl, designerHost.RootComponent);
Finding Existing Controls
:PVOPXLOPXIPXUPDSFBUFDPOUSPMTBOEQMBDFUIFNPOBGPSN#VUIPXEPZPV¾OE
FYJTUJOHDPOUSPMTPOBGPSN "TNFOUJPOFEFBSMJFSUIF+&GUKIPGT*QUV4QQV%QORQPGPV
226
Working with Microsoft Visual Studio 2005
QSPQFSUZSFUVSOTBOPCKFDUUIBUDBOCFDBTUJOUPB5[UVGO9KPFQYU(QTOU%QPVTQNPCKFDU
6TJOHUIJTPCKFDUZPVDBODBMMNFUIPETBOEQSPQFSUJFTKVTUBTZPVXPVMEBUSVOUJNFUP
¾OEJOGPSNBUJPOBCPVUBGPSN'PSFYBNQMFUIFGPMMPXJOHDPEFXBMLTUIFMJTUPGDPOUSPMT
DPOUBJOFEJOB5[UVGO9KPFQYU(QTOU%QPVTQNPCKFDU
System.ComponentModel.Design.IDesignerHost designer;
System.Windows.Forms.Control rootControl;
//Set the designer variable here from the Window.Object property
rootControl = (System.Windows.Forms.Control)designer.RootComponent
foreach (System.Windows.Forms.Control control in rootControl.Controls)
{
//Retrieve desired control information
}
:PVDBOVTFUIF2TQRGTV[&GUETKRVQT%QNNGEVKQPPCKFDUUP¾OEQSPQFSUJFTPGBDPOUSPMNVDIBT
ZPVXPVMEUPTFUQSPQFSUJFTPOUIFGPSNFYDFQUZPVVTFUIF2TQRGTV[&GUETKRVQT)GV8CNWG
NFUIPE
System.ComponentModel.Design.IDesignerHost designer;
System.ComponentModel.PropertyDescriptor propControls;
System.ComponentModel.PropertyDescriptorCollection props;
System.ComponentModel.IComponent component;
System.Windows.Forms.Form form;
System.Drawing.Size size;
designer = (System.ComponentModel.Design.IDesignerHost) _
applicationObject.ActiveWindow.Object;
component = designer.RootComponent;
//Get the Size property using the forms designer:
props = System.ComponentModel.TypeDescriptor.GetProperties(component);
propControls = props["Size"];
size = (System.Drawing.Size)propControls.GetValue(component);
//Get the Size property directly from the form:
form = (System.Windows.Forms.Form)component;
size = form.Size;
A Form Layout Sample
.JDSPTPGU7JTVBM#BTJDWFSTJPOTBOEFBSMJFSIBWFBUPPMXJOEPXDBMMFE'PSN-BZPVU
UIBUTIPXTUIFTJ[FPGBGPSNCFJOHEFTJHOFEBTJUXPVMEBQQFBSPOUIFEFTLUPQPGZPVS
DPNQVUFS7JTVBM4UVEJPEPFTOµUIBWFUIJTGFBUVSFCVUZPVDBOFBTJMZDSFBUFPOFCZVTJOH
UIFBVUPNBUJPONPEFMPGUIF'PSNTEFTJHOFS:PVDBO¾OEUIFTPVSDFDPEFGPSUIJTTBNQMF
DBMMFE'PSN7JFXBNPOHUIFCPPLµTTBNQMF¾MFT
8IFOUIFBEEJOTUBSUTJUDSFBUFTBUPPMXJOEPXUIBUESBXTBWJSUVBMNPOJUPSSFQSFTFOUJOH
ZPVSDPNQVUFSNPOJUPS5IFTDSFFOPGUIFWJSUVBMNPOJUPSNBUDIFTUIFEJTQMBZSFTPMVUJPO
PGZPVSNPOJUPS*GZPVSDPNQVUFSVTFTNVMUJQMFNPOJUPSTUIFSFTPMVUJPOPGUIFQSJNBSZ
NPOJUPSJTVTFE
"GUFSDPOOFDUJOHUPUIF9KPFQY#EVKXCVGFFWFOUJUXBJUTGPSB'PSNT
Chapter 9:
Programming the Visual Studio User Interface
227
EFTJHOFSXJOEPXUPCFDPNFBDUJWFBOEUIFOJUMPPLTBUUIFBWBJMBCMFDPOUSPMTJOUIFGPSN
BOEESBXTUIFGPSNBOEJUTDPOUSPMTPOUIFWJSUVBMTDSFFO'PSFYBNQMFJGZPVDSFBUFBGPSN
UIBUIBTUIFDBMFOEBSBOECVUUPODPOUSPMTPOJUBTTIPXOJO'JHVSFUIF'PSNTEFTJHOFS
XJOEPXTIPXOJO'JHVSFBQQFBST5IJTTBNQMFBMTPEFNPOTUSBUFTPVSOFYUUPQJD
DSFBUJOHZPVSPXOUPPMXJOEPXTUIBUBSFIPTUFECZ7JTVBM4UVEJP
Figure 9-1 A Windows Form with a calendar control and a button control
Figure 9-2
The Form Layout window showing the form from Figure 9-1
Creating Custom Tool Windows
"TZPVLOPXNPTUPGUIFXJOEPXTJO7JTVBM4UVEJPIBWFBOPCKFDUNPEFMUIBUZPVDBOVTF
UPQSPHSBNUIFDPOUFOUTBOEQSFTFOUEBUBUIBUZPVSDPEFHFOFSBUFT)PXFWFSBUUJNFTZPV
NJHIUOFFEUPEJTQMBZEBUBJOBXBZUIBUUIFFYJTUJOHUPPMXJOEPXTDBOOPUIBOEMF5PBMMPX
ZPVUPEJTQMBZEBUBJOBXBZUIBUJTNPTUTVJUBCMFGPSZPVSBEEJOUIF7JTVBM4UVEJPPCKFDU
NPEFMBMMPXTDSFBUJPOPGDVTUPNUPPMXJOEPXT
5IFSFBSFUXPXBZTUPDSFBUFBUPPMXJOEPX±POFXBZJTXJUIBO"DUJWF9DPOUSPMBOEUIF
PUIFSJTXJUIB/&56TFS$POUSPM5PDSFBUFBUPPMXJOEPXBMMZPVOFFEJTBO"DUJWF9
228
Working with Microsoft Visual Studio 2005
DPOUSPMBOEBOBEEJOUIBUNBLFTBDBMMUPUIF9KPFQYU%TGCVG6QQN9KPFQYNFUIPE
%TGCVG6QQN9KPFQYIBTUIFGPMMPXJOHNFUIPETJHOBUVSF
public EnvDTE.Window CreateToolWindow(EnvDTE.AddIn AddInInst,
string ProgID,
string Caption,
string GuidPosition,
ref object DocObj)
5IJTNFUIPESFUVSOTB9KPFQYPCKFDUUIBUCFIBWFTBTBOZUPPMXJOEPXUIBU7JTVBM4UVEJP
DSFBUFT)FSFBSFUIFBSHVNFOUTGPSUIJTNFUIPE
N
"EE*O*OTU "OBEEJOPCKFDUUIBUµTUIFTQPOTPSPGUIFUPPMXJOEPX8IFOUIFTQPOTPS
BEEJOJTVOMPBEFEBMMUPPMXJOEPXTBTTPDJBUFEXJUIUIBUBEEJOB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CVG6QQN9KPFQYNFUIPEUIFTBNQMF¾MFTUIBUBDDPNQBOZ
UIJTCPPLJODMVEFBOBEEJOQSPKFDUDBMMFE74.FEJB1MBZFS5IJTTBNQMFDSFBUFTBUPPMXJOEPX
IPTUJOHUIF"DUJWF9DPOUSPMGPS8JOEPXT.FEJB1MBZFSBOEUIFOCZVTJOHUIFQSPHSBNNBCMF
PCKFDUPGUIFDPOUSPMQMBZTBOBVEJP¾MF5IFDPEFUIBUEPFTUIFXPSLPGDSFBUJOHUIFUPPM
XJOEPXMPPLTMJLFUIJT
void CreateMediaPlayerToolWindow()
{
EnvDTE.Windows windows;
EnvDTE.Window mediaPlayerWindow;
object controlObject = null;
string mediaPlayerProgID = "MediaPlayer.MediaPlayer";
string toolWindowCaption = "Windows Media Player";
string toolWindowGuid = "{AB5E549E-F823-44BB-8161-BE2BD5D698D8}";
//Create and show a tool window that hosts the
// Windows Media Player control:
Chapter 9:
Programming the Visual Studio User Interface
229
windows = applicationObject.Windows;
mediaPlayerWindow = windows.CreateToolWindow(addInInstance,
mediaPlayerProgID,
toolWindowCaption,
toolWindowGuid,
ref controlObject);
mediaPlayerWindow.Visible = true;
//Play the Windows "Tada" sound:
//Can get only the system directory (Eg: C:\windows\system32),
// need to change this to the Windows install dir
string mediaFile = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.System);
mediaFile += "\\..\\media\\tada.wav";
MediaPlayer.IMediaPlayer2 mediaPlayer =
(MediaPlayer.IMediaPlayer2)controlObject;
mediaPlayer.AutoStart = true;
mediaPlayer.FileName = mediaFile;
}
5IF%TGCVG/GFKC2NC[GT6QQN9KPFQYNFUIPEJTDBMMFEJOUXPQMBDFTJOUIFTBNQMFBEEJO±PODF
JOUIF1P%QPPGEVKQPNFUIPEBOEPODFJOUIF1P5VCTVWR%QORNGVGNFUIPE*UNVTUCFDBMMFE
UXJDFCFDBVTFPGUIFXBZBEEJOTBSFMPBEFECZ7JTVBM4UVEJP*GBOBEEJOJTTFUUPMPBEPO
TUBSUVQXIFO7JTVBM4UVEJPTUBSUTUIFBEEJOTUBSUTMPBEJOH5IJTMPBEJOHQSPDFTTJODMVEFT
DBMMJOHUIF1P%QPPGEVKQPNFUIPE#VUUIF1P%QPPGEVKQPNFUIPEJTDBMMFEKVTUCFGPSFUIF
7JTVBM4UVEJPNBJOXJOEPXJTDSFBUFEBOETIPXO*GZPVDBMMUIF%TGCVG6QQN9KPFQYNFUIPE
XJUIJO1P%QPPGEVKQPCFGPSFUIFNBJOXJOEPXJTTIPXODSFBUJOHUIFUPPMXJOEPXXJMM
GBJMCFDBVTFDSFBUJOHBO"DUJWF9DPOUSPMSFRVJSFTJUTQBSFOUXJOEPXUPCFWJTJCMF:PVDBO
DIFDLUPNBLFTVSFUIBUUIFNBJOXJOEPXIBTCFFODSFBUFECZFYBNJOJOHUIFEQPPGEV/QFG
BSHVNFOUQBTTFEUPUIF1P%QPPGEVKQPNFUIPE*GUIJTJTTFUUPGZVAEOA#HVGT5VCTVWRUIFBEE
JOXBTMPBEFEUISPVHIUIF"EEJO.BOBHFSPSCZNFBOTPUIFSUIBOUIFNQCFQPUVCTVWR¿BH
CFJOHTFUBOE7JTVBM4UVEJPCFJOHTUBSUFE5IFSFGPSFUIFUPPMXJOEPXDBOCFTIPXOXIFO
BOBEEJOJTMPBEFECZVTJOHBO1P%QPPGEVKQPJNQMFNFOUBUJPOTVDIBTUIJT
public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst,
ref System.Array custom)
{
applicationObject = (_DTE)application;
addInInstance = (AddIn)addInInst;
//If the add-in is loaded from the Add-in Manager dialog, then
// create and show the tool window:
if(connectMode == Extensibility.ext_ConnectMode.ext_cm_AfterStartup)
{
CreateMediaPlayerToolWindow();
}
}
*GUIFNQCFQPUVCTVWR¿BHJTTFUBOEZPVXBOUUPTIPXUIFUPPMXJOEPXXIFOBOBEEJOJT
MPBEFEZPVDBODSFBUFUIFXJOEPXJOUIF1P5VCTVWR%QORNGVGNFUIPE5IJTNFUIPEJTDBMMFE
230
Working with Microsoft Visual Studio 2005
XIFOJOJUJBMJ[BUJPOPG7JTVBM4UVEJPJTDPNQMFUFXIJDIJODMVEFTDSFBUJOHBOETIPXJOHUIF
NBJOXJOEPX*UµTBTTJNQMFBTUIJTDPEFTOJQQFU
public void OnStartupComplete(ref System.Array custom)
{
//If the add-in is loaded at startup, then
// create and show the tool window:
CreateMediaPlayerToolWindow();
}
5PDSFBUFBUPPMXJOEPXCZVTJOHB/&5VTFSDPOUSPMZPVXJMMOFFEUPDBMMUIF
9KPFQYU%TGCVG6QQN9KPFQYNFUIPE5IJTNFUIPEIBTBTJHOBUVSFUIBUJTTJNJMBS
UPUIF9KPFQYU%TGCVG6QQN9KPFQYNFUIPECVUSBUIFSUIBOUBLJOHB$0.1SPH*%UP
JEFOUJGZBDPOUSPM%TGCVG6QQN9KPFQYUBLFTCPUIBOBNFPGB/&5BTTFNCMZBOEUIFGVMM
OBNFPGBDMBTTXJUIJOUIBUBTTFNCMZUIBUEFSJWFTGSPNUIF/&5'SBNFXPSLµT5[UVGO
9KPFQYU(QTOU7UGT%QPVTQNDMBTT5IFTJHOBUVSFPGUIF%TGCVG6QQN9KPFQYNFUIPEJT
public EnvDTE.Window CreateToolWindow2(EnvDTE.AddIn AddInInst,
string Assembly,
string Class,
string Caption,
string GuidPosition,
ref object DocObj)
5IFBSHVNFOUTUIBUEJGGFSGSPNUIF%TGCVG6QQN9KPFQYNFUIPEBSF
N
"TTFNCMZ 5IFOBNFPGBOBTTFNCMZ5IJTDBOCFFJUIFSBGVMMQBUIUPBOBTTFNCMZ
TVDIBT%>/[%QPVTQN#UUGODN[FNNPSBTUSPOHOBNFGPSBOBTTFNCMZTVDIBT/KETQUQHV
8KUWCN5VWFKQ9KPFQYU(QTOU8GTUKQP%WNVWTGPGWVTCN2WDNKE-G[6QMGPDHHH
FCCRTQEGUUQT#TEJKVGEVWTG/5+.XIJDIJTUIFOBNFPGUIFBTTFNCMZJNQMFNFOUJOH
GPSNTXJUIJOUIF/&5'SBNFXPSLT5IFBTTFNCMZOBNFDBOBMTPCFB63-NFBOJOH
UIFQSF¾Y¿NGBOEFWFOJVVRDBOCFVTFEUPMPDBUFUIFBTTFNCMZ
N
$MBTT 5IFGVMMZRVBMJ¾FEOBNFPGBDMBTTJNQMFNFOUJOH5[UVGO9KPFQYU(QTOU
7UGT%QPVTQN5IFOBNFTQBDFBTXFMMBTUIFDMBTTOBNFNVTUCFHJWFOVTJOHUIFEPUUFE
GPSNBUTVDIBT5[UVGO9KPFQYU(QTOU2TQRGTV[)TKFXIJDIJTUIFOBNFGPSUIFQSPQFSUZ
HSJEUIFTBNFHSJEVTFEXJUIJOUIF1SPQFSUJFTXJOEPXJOTJEFPG7JTVBM4UVEJP
Note When using the CreateToolWindow or CreateToolWindow2 method, you should not
use code such as Guid.NewGuid().ToString(“B”) to pass the GUID string. This code will create a new GUID each time the method is called. Rather, you should declare a variable at
class scope such as string toolWindowGuid = “{6b74173d-c3e0-4d95-a6bc-877e660c319d}”;
to create one GUID for each window type. If you ever need to find the Window object
for the tool window again, you can locate that window directly with the DTE.Windows.
Item(toolWindowGuid) method. Using the same GUID each time also will allow Visual Studio
to save the position of the tool window wherever the user has placed the window, so the next
time you create the tool window it can be placed back in the same position. You should use
this GUID only for a tool window of the same type, and you should never use the same GUID
to create multiple or different tool windows.
Chapter 9:
Programming the Visual Studio User Interface
231
5IF'PSN-BZPVUTBNQMFVTFTUIF%TGCVG6QQN9KPFQYNFUIPEUPDSFBUFJUTVTFSJOUFSGBDF
#FDBVTFUIFVTFSDPOUSPMJNQMFNFOUJOHUIFVTFSJOUFSGBDFJTDPOUBJOFEXJUIJOUIFTBNF
BTTFNCMZBTUIFBEEJODPEFUIFBTTFNCMZOBNFDBOCFGPVOECZVTJOHUIF.QECVKQPQSPQFSUZPG
UIF5[UVGO4GÀGEVKQP#UUGODN[DMBTT5IJT#UUGODN[DMBTTJTGPVOEXJUIUIF)GV'ZGEWVKPI#UUGODN[
TUBUJDNFUIPEBOE.QECVKQPSFUVSOTUIFGVMMQBUIXJUIUIF¿NGQSPUPDPM-BTUMZCFDBVTFUIF
VTFSDPOUSPMUPCFIPTUFEJTOBNFE'PSN-BZPVU$UMBOEJTDPOUBJOFEXJUIJOUIF(QTO.C[QWV
BTTFNCMZUIFDMBTTOBNFQBTTFEUP%TGCVG6QQN9KPFQYJT(QTO.C[QWV(QTO.C[QWV%VN
:PVDBOBMTPDSFBUFBQSPHSBNNBCMFPCKFDUGPSBUPPMXJOEPXDSFBUFEGSPNB/&56TFS
$POUSPM5IJTXJMMBMMPXUIFVTFSTPGZPVSBEEJOUPQSPHSBNZPVSUPPMXJOEPXKVTUBT
UIFZQSPHSBNUIF5BTL-JTUXJOEPXPS5PPMCPY5PFYQPTFBQSPHSBNNBCMFPCKFDUBMMZPV
OFFEUPEPJTUPEF¾OFBOJOUFSGBDFBOEUIFOJNQMFNFOUUIBUJOUFSGBDFPOUIFDMBTTUIBU
EFSJWFTGSPN5[UVGO9KPFQYU(QTOU7UGT%QPVTQN5IJTCJUPGDPEFTIPXTBOJOUFSGBDFOBNFE
+2TQITCOOCDNG1DLGEVQSPWJEJOHBQSPHSBNNBCMFPCKFDUPOBDPOUSPMOBNFE7UGT%QPVTQN
public interface IProgrammableObject
{
void Method();
}
public partial class UserControl1 : UserControl, IProgrammableObject
{
public UserControl1()
{
InitializeComponent();
}
public void Method()
{
System.Windows.Forms.MessageBox.Show("Method");
}
}
8JUIUIJTDPEFUPJNQMFNFOUBQSPHSBNNBCMFPCKFDUZPVDBODSFBUFUIFUPPMXJOEPXBOE
DBMM/GVJQFMJLFUIJT
object obj = null;
EnvDTE80.Windows2 window2 = (EnvDTE80.Windows2)applicationObject.Windows;
string thisAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location;
EnvDTE.Window win = window2.CreateToolWindow2
(addInInstance, thisAssembly, "MyAddin.UserControl1",
"Window Caption", "{0d3619e3-b0b4-4af3-9053-95a29222159b}",
ref obj);
win.Visible = true;
IProgrammableObject programmableObj = (IProgrammableObject)obj;
programmableObj.Method();
Setting the Tab Picture of a Custom Tool Window
8IFOUXPPSNPSFUPPMXJOEPXTBSFUBCMJOLFEUPHFUIFSBOJNBHFJTEJTQMBZFETPUIF
VTFSDBORVJDLMZSFDPHOJ[FUIFUPPMXJOEPXTUIBUBSFMJOLFEUPHFUIFS5PTFUUIFUBC
232
Working with Microsoft Visual Studio 2005
QJDUVSFGPSBUPPMXJOEPXUIBUµTDSFBUFECZBOBEEJOZPVVTFUIF9KPFQY5GV6CD2KEVWTG
NFUIPE5GV6CD2KEVWTGUBLFTBTJUTBSHVNFOUB$0.+2KEVWTG&KURUZQFBCJUNBQIBOEMF
PSBQBUIUPBCJUNBQ¾MFB¾MFXJUIUIFFYUFOTJPOCNQ
TVDIBT$=UQOGDKVOCRCNQ5P
DSFBUFBO+2KEVWTG&KURPCKFDUZPVDBOVTFUIFTBNFUFDIOJRVFEFTDSJCFEFBSMJFSPGDBMMJOH
UIF1NG.QCF2KEVWTG(KNGNFUIPEBOEUIFOQBTTJOHUIFSFUVSOFE+2KEVWTG&KURPCKFDUUPUIF
5GV6CD2KEVWTGNFUIPE#JUNBQIBOEMFTDBOCFSFUSJFWFECZMPBEJOHBCJUNBQ¾MFCZVTJOHBOZ
PGUIFWBSJPVTXBZTUIBUBOJNBHFDBOCFMPBEFEJOUPBOJOTUBODFPGUIF/&5'SBNFXPSLµT
$KVOCRDMBTTTVDIBTGSPNBSFTPVSDFFNCFEEFEXJUIJOUIFBEEJOµTBTTFNCMZ
UIFOUIF
$KVOCR*CPFNGQSPQFSUZJTDBMMFEUPSFUSJFWFUIFIBOEMF
5IFCJUNBQUPQMBDFPOUPBUPPMXJOEPXUBCNVTUIBWFPOFPGUXPTQFDJ¾DGPSNBUTBOEBOZ
EFWJBUJPOGSPNUIFTFGPSNBUTDBODBVTFUIFCJUNBQUPBQQFBSXJUIJODPSSFDUDPMPSTPSOPU
BQQFBSBUBMM5IF¾STUGPSNBU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SUJFTJTNBEFBWBJMBCMF±TVDIBTUIF¾MFQBUIXIFOUIF¾MFXBT
NPEJ¾FEPSIPXUIF¾MFTIPVMECFCVJMU8IFOZPVDSFBUFBUPPMXJOEPXZPVNJHIUBMTP
XBOUUPIBWFQSPQFSUJFTGPSZPVSUPPMXJOEPXBQQFBSJOUIF1SPQFSUJFTXJOEPX:PVTFU
JUFNTUPBQQFBSJOUIF1SPQFSUJFTXJOEPXCZVTJOHUIF9KPFQY5GV5GNGEVKQP%QPVCKPGTNFUIPE
XIJDIUBLFTBTBQBSBNFUFSBOBSSBZPGUZQF5[UVGO1DLGEV5IFTFJUFNTBSFEJTQMBZFEJOUIF
1SPQFSUJFTXJOEPXXIFOUIFXJOEPXUIBUIBTUIJTNFUIPEDBMMFEPOJUCFDPNFTUIFBDUJWF
XJOEPX5IFTBNQMF74.FEJB1MBZFS"EWBOFYUFOTJPOUPUIF74.FEJB1MBZFSTBNQMFEJTQMBZT
BQSPQFSUZTFUJOUIF1SPQFSUJFTXJOEPXCZDBMMJOHUIF5GV5GNGEVKQP%QPVCKPGTNFUIPEXJUIUIF
QSPHSBNNBCMFPCKFDUPG8JOEPXT.FEJB1MBZFSXIJDIXBTSFUVSOFEUISPVHIUIF&QE1DL
QBSBNFUFSPGUIF%TGCVG6QQN9KPFQYNFUIPE5IJTQPSUJPOPGDPEFTIPXTIPXUIJTJTEPOF
object []propertiesWindowObjects = {mediaPlayer};
mediaPlayerWindow.SetSelectionContainer(ref propertiesWindowObjects);
Chapter 9:
Programming the Visual Studio User Interface
233
:PVDBODBMMUIF5GV5GNGEVKQP%QPVCKPGTNFUIPEPOMZPOUPPMXJOEPXTUIBUZPVDSFBUF*G
ZPVDBMMUIJTNFUIPEPOB9KPFQYPCKFDUGPSTBZUIF4PMVUJPO&YQMPSFSUPPMXJOEPXBO
FYDFQUJPOXJMMCFHFOFSBUFE
The Options Dialog Box
%FWFMPQFSTDBOCFB¾OJDLZCVODI±UIFZXBOU7JTVBM4UVEJP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OETVCDBUFHPSZ5IFDBUFHPSZBOETVCDBUFHPSZVTFE
UP¾OEB2TQRGTVKGUDPMMFDUJPOBSFCBTFEPOUIFDBUFHPSZBOETVCDBUFHPSZEJTQMBZFEJOUIF
0QUJPOTEJBMPHCPYVTFSJOUFSGBDFCVUUIFJSOBNFTNJHIUCFTMJHIUMZEJGGFSFOUGSPNUIF
DBUFHPSZBOETVCDBUFHPSZOBNFT5P¾OEUIFMJTUPGDBUFHPSJFTBOETVCDBUFHPSJFTZPVNVTU
VTFUIF3FHJTUSZ&EJUPS'JSTUZPV¾OEUIFJUFNJOUIF0QUJPOTEJBMPHCPYUIBUZPVXBOUUP
FEJU'PSPVSFYBNQMFXFµMMNPEJGZUIFUBCJOEFOUTJ[FPGUIF7JTVBM#BTJDTPVSDFDPEFFEJUPS
XIJDIJTGPVOEPOUIFQBHFPGUIF5FYU&EJUPSDBUFHPSZBOE#BTJDTVCDBUFHPSZ
Note The Text Editor category is a bit different from other categories in the Options dialog
box in that it has three levels, with the third level being a sub-subcategory. However, in the
automation model, the General and Tabs sub-subcategories are combined into one and have
the same name as they do in the programming language.
"GUFSSVOOJOHSFHFEJUFYFOBWJHBUFUPUIFLFZ*-';A.1%#.A/#%*+0'>51(69#4'>
/KETQUQHV>8KUWCN5VWFKQ>>#WVQOCVKQP2TQRGTVKGU6OEFSOFBUIUIJTLFZJTBMJTUPGBMMUIF
QSPQFSUZDBUFHPSJFTBDDFTTJCMFUPBNBDSPPSBOBEEJO8FµSFMPPLJOHGPSUIF5FYU&EJUPS
DBUFHPSZ±UIFLFZXIPTFOBNFNPTUDMPTFMZNBUDIFTUIJTDBUFHPSZOBNFJOUIFVTFSJOUFSGBDF
JT5FYU&EJUPSXJUIPVUBTQBDF
"GUFSFYQBOEJOHUIJTJUFNJOUIF3FHJTUSZ&EJUPSZPVµMMTFFB
234
Working with Microsoft Visual Studio 2005
MJTUPGTVCDBUFHPSJFTPOFPGUIPTFTVCDBUFHPSJFT#BTJDNBUDIFT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key As Microsoft.Win32.RegistryKey
Dim newDocument As Document
Dim selection As TextSelection
'Open a new document to store the information
newDocument = DTE.ItemOperations.NewFile("General\Text File").Document
selection = newDocument.Selection
'Open the registry key that holds the list of categories:
key = Microsoft.Win32.Registry.LocalMachine
key = key.OpenSubKey( _
"SOFTWARE\Microsoft\VisualStudio\8.0\AutomationProperties")
'Enumerate the categories:
For Each categoryName In key.GetSubKeyNames()
Dim subcategoryName As String
selection.Insert(categoryName + vbLf)
'Enumerate the subcategories:
For Each subcategoryName In _
key.OpenSubKey(categoryName).GetSubKeyNames()
Dim properties As Properties
Dim prop As [Property]
selection.Insert(" " + subcategoryName + vbLf)
Try
'Enumerate each property:
properties = DTE.Properties(categoryName, subcategoryName)
For Each prop In properties
selection.Insert("
" + prop.Name + vbLf)
Next
Catch
End Try
Chapter 9:
Programming the Visual Studio User Interface
235
Next
Next
End Sub
6TJOHUIFPVUQVUGSPNUIJTNBDSPXFDBO¾OEUIF5FYU&EJUPSDBUFHPSZBOEUIF#BTJD
TVCDBUFHPSZBOEUIFOMPPLJOUIF0QUJPOTEJBMPHCPYGPSTPNFUIJOHUIBUMPPLTMJLFUIFOBNF
5BC4J[F5IFDMPTFTUNBUDIJT5BC4J[F6TJOHUIJTOBNFXFDBO¾OEUIF2TQRGTV[PCKFDUGPS
UIF7JTVBM#BTJDUFYUFEJUPS5BC4J[F
Sub GetVBTabSizeProperty()
Dim properties As Properties
Dim prop As [Property]
properties = DTE.Properties("TextEditor", "Basic")
prop = properties.Item("TabSize")
End Sub
/PXBMMUIBUµTMFGUUPEPJTSFUSJFWFUIFWBMVFPGUIJTQSPQFSUZCZVTJOHUIF2TQRGTV[8CNWG
QSPQFSUZ
Sub GetVBTabSize()
Dim properties As Properties
properties = DTE.Properties("TextEditor", "Basic")
MsgBox(properties.Item("TabSize").Value)
End Sub
5IJTNBDSPEJTQMBZTUIFWBMVFXIJDIJTUIFTBNFWBMVFJOUIF5PPMT0QUJPOTEJBMPHCPY
GPSUIF5BC4J[FPQUJPOPGUIF#BTJDTVCDBUFHPSZPGUIF5FYU&EJUPSDBUFHPSZ:PVTFUUIJTWBMVF
UIFTBNFXBZZPVSFUSJFWFUIFWBMVFFYDFQUUIF8CNWGQSPQFSUZJTXSJUUFOUPSBUIFSUIBOSFBE
Sub SetVBTabSize()
Dim properties As Properties
properties = DTE.Properties("TextEditor", "Basic")
properties.Item("TabSize").Value = 4
End Sub
#ZTJNQMZDIBOHJOHUIFDBUFHPSZBOETVCDBUFHPSJFTQBTTFEUPUIF&6'2TQRGTVKGUQSPQFSUZ
BOEMPPLJOHBUUIFMJTUPGQSPQFSUZOBNFTHFOFSBUFECZUIF9CNM2TQRGTV[0COGUNBDSPZPV
DBONPEJGZNBOZPGUIFPQUJPOTTIPXOJOUIF5PPMT0QUJPOTEJBMPHCPY
Is It What It Says It Is?
8IFOZPVVTFUIF7JTVBM4UVEJPPCKFDUNPEFMZPVNJHIUVTFUIF7JTVBM#BTJD+UPQFSBUPS
PSUIF/&5'SBNFXPSL1DLGEV'SWCNUNFUIPEUPUSZUPEFUFSNJOFXIFUIFSUXPPCKFDUT
BSFUIFTBNF#VUUIF+UPQFSBUPSBOEUIF'SWCNUNFUIPENJHIUOPUBMXBZTSFUVSOXIBU
ZPVFYQFDUCFDBVTFPGIPXUIF7JTVBM4UVEJPPCKFDUNPEFMXBTCVJMU*GZPVSVOBNBDSP
TVDIBTUIJT
Sub CompareWindowsObjects()
Dim window1 As Window
Dim window2 As Window
236
Working with Microsoft Visual Studio 2005
window1 = DTE.Windows.Item(Constants.vsWindowKindTaskList)
window2 = DTE.Windows.Item(Constants.vsWindowKindTaskList)
MsgBox(window1 Is window2)
End Sub
BNFTTBHFCPYXJUIUIFWBMVF6TWGJTEJTQMBZFE8IFOZPVBTLGPSB9KPFQYPCKFDUUIF
PCKFDUNPEFMDIFDLTUPTFFXIFUIFSB9KPFQYPCKFDUIBTCFFODSFBUFEGPSUIFTQFDJ¾D
XJOEPXJGOPUBOFX9KPFQYPCKFDUJTDPOTUSVDUFEBOESFUVSOFEUPUIFDBMMJOHDPEF
*GB9KPFQYPCKFDUIBTBMSFBEZCFFODSFBUFEUIBUPCKFDUJTSFDZDMFEBOESFUVSOFEUPUIF
DBMMFS5IJTJTCPUIBQFSGPSNBODFBOENFNPSZDPOTVNQUJPOPQUJNJ[BUJPOCFDBVTFOFX
PCKFDUTBSFOPUVOOFDFTTBSJMZDSFBUFEXIJDIDPOTVNFTNFNPSZ
BOEJOJUJBMJ[FEXIJDI
DPOTVNFTQSPDFTTPSUJNF
#VUJGZPVSVODPEFTVDIBTUIJT
Sub ComparePropertyObjects()
Dim props1 As Properties
Dim props2 As Properties
props1 = DTE.Properties("Environment", "General")
props2 = DTE.Properties("Environment", "General")
MsgBox(props1 Is props2)
End Sub
UIFNFTTBHFCPYEJTQMBZT(CNUGCFDBVTFUIF2TQRGTVKGUDPMMFDUJPONVTUCFSFDPOTUSVDUFE
FBDI UJNF ZPV DBMM UIF &6'2TQRGTVKGU QSPQFSUZ UP CF TVSF JU IBT UIF NPTU VQUPEBUF
JOGPSNBUJPO
$BMMJOHUIF&6'2TQRGTVKGUQSPQFSUZNVMUJQMFUJNFTDBODBVTFIVHFNFNPSZDPOTVNQUJPO
QSPCMFNT4VQQPTFZPVDBMMUIF&6'2TQRGTVKGUQSPQFSUZSFQFBUFEMZJOBUJHIUMPPQFWFSZ
UJNFUIFQSPQFSUZJTDBMMFEBOFX2TQRGTVKGUDPMMFDUJPOJTDSFBUFEJOJUJBMJ[FEBOEUIFO
SFUVSOFEUPUIFDBMMJOHDPEF5IJTPCKFDUDPOTVNFTNFNPSZGPSUIF$0.PCKFDUUIBU
7JTVBM4UVEJPDSFBUFTBOEJGZPVµSFVTJOHBQSPHSBNNJOHMBOHVBHFTVQQPSUFECZUIF/&5
'SBNFXPSLB/&5XSBQQFSDMBTTUIBUBMMPXTZPVUPQSPHSBNUIJTPCKFDUJTDPOTUSVDUFE
:PVDBOTFFUIFNFNPSZDPOTVNQUJPOHSPXBMNPTUCPVOEMFTTMZJGZPVSVOUIFGPMMPXJOH
NBDSPBOEXBUDIUIFWTNTWSFYFQSPDFTTUIFQSPDFTTUIBUIPTUTUIFJOTUBODFPGUIF/&5
'SBNFXPSLBOESVOTNBDSPDPEF
POUIF1SPDFTTFTUBCPG8JOEPXT5BTL.BOBHFS
Sub RepeatedConstruct()
Dim i As Long
Dim props As Properties
For i = 1 To Long.MaxValue
props = DTE.Properties("Environment", "General")
Next
End Sub
8IFOZPVSVOUIJTNBDSPUIFMPPQOFWFSBMMPXTBHBSCBHFDPMMFDUJPOUPPDDVSCFDBVTFUIF
/&5'SBNFXPSLJTGPDVTFEPOSVOOJOHZPVSDPEFOPUTFBSDIJOHBOESFNPWJOHVOVTFE
PCKFDUT5PNBLFTVSFZPVSQSPHSBNEPFTOµUDPOTVNFNPSFNFNPSZUIBOJUTIPVMEZPV
TIPVMECFTVSFOPUUPDSFBUFNPSFPCKFDUTUIBOOFDFTTBSZ:PVDBOEPTPCZVTJOHUIF
+UPQFSBUPSPSUIF1DLGEV'SWCNUNFUIPEBOEPQUJNJ[JOHBDDPSEJOHMZ'PSFYBNQMFZPV
DBOSFXSJUFUIF4GRGCVGF%QPUVTWEVNBDSPBTGPMMPXTBOEBWPJETZTUFNNFNPSZTUSFTTCZ
Chapter 9:
Programming the Visual Studio User Interface
237
TJNQMZNPWJOHUIFDBMMUP&6'2TQRGTVKGUPVUTJEFPGUIFMPPQ
Sub OptimizedRepeatedConstruct()
Dim i As Long
Dim props As Properties
Dim showStatusbar As Boolean
props = DTE.Properties("Environment", "General")
For i = 1 To Long.MaxValue
showStatusbar = props.Item("ShowStatusBar").Value
Next
End Sub
"O VOTDJFOUJ¾D NFBTVSFNFOU DPOTJTUJOH PG PQFOJOH 8JOEPXT 5BTL .BOBHFS BOE
OPUJOHUIFBNPVOUPGNFNPSZDPOTVNFECFGPSFBOEBGUFSSVOOJOHUIFNBDSP
TIPXT
UIBUNPWJOHUIFPOFMJOFPV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µTMPPLBUUIFDPEFUIBUUIF
TUBSUFSLJUXJMMHFOFSBUFGPSZPV
Declaring the XML for a Tools Options Page
5IF9.-JOUIFBEEJO¾MFUPEFDMBSFBUPPMTPQUJPOTQBHFJTRVJUFTJNQMFIFSFJTBTOJQQFU
GSPNBOBEEJO¾MFUIBUEFDMBSFTBQBHF
<ToolsOptionsPage>
<Category Name="My Custom Category">
<SubCategory Name="My Custom Subcategory">
<Assembly>MyCustomPage.dll</Assembly>
<FullClassName>MyCustomPage.UserControl1</FullClassName>
</SubCategory>
</Category>
</ToolsOptionsPage>
238
Working with Microsoft Visual Studio 2005
5IJTTOJQQFUPG9.-EFDMBSFTBQBHFUIBUXJMMDSFBUFBOPEFJOUIFUSFFPOUIFMFGUTJEFPGUIF
5PPMT0QUJPOTEJBMPHCPYOBNFE.Z$VTUPN$BUFHPSZ*UXJMMBMTPDSFBUFBOPEFVOEFS.Z
$VTUPN$BUFHPSZOBNFE.Z$VTUPN4VCDBUFHPSZ*GUIFVTFSXFSFUPTFMFDUUIJTOPEFUIF
BTTFNCMZ.Z$VTUPN1BHFEMMXJMMCFMPBEFEBOEUIFOUIFDMBTT/[%WUVQO2CIG7UGT%QPVTQN
JTJOTUBOUJBUFEBOEUIFOEJTQMBZFE*GZPVVTFEBOBNFTVDIBT&OWJSPONFOUBTUIFDBUFHPSZ
UIJTQBHFJTNFSHFEJOUPUIF&OWJSPONFOUOPEFJOUIFUSFFPGUIF5PPMT0QUJPOTEJBMPHCPY
:PVDBOTQFDJGZNVMUJQMF4VC$BUFHPSZUBHTXJUIJOB$BUFHPSZUBHBOEBMMUIPTFQBHFTBSF
HSPVQFEUPHFUIFSVOEFSPOFUPQMFWFMOPEF
The IDTToolsOptionsPage Interface
"O0QUJPOTQBHFIBTUISFFTUBHFTJOJUTMJGFUJNFDSFBUJPOJOUFSBDUJPOBOEEJTNJTTBM5P
BMMPXZPVSQBHFUPLOPXBCPVUUIFTFUISFFTUBHFTUIFVTFSDPOUSPMOFFETUPJNQMFNFOUUIF
'PX&6'+&66QQNU1RVKQPU2CIGJOUFSGBDF5IJTJOUFSGBDFIBTUIFGPMMPXJOHTJHOBUVSF
public interface IDTToolsOptionsPage
{
public void GetProperties(ref object PropertiesObject);
public void OnAfterCreated(EnvDTE.DTE DTEObject);
public void OnCancel();
public void OnHelp();
public void OnOK();
}
8IFOUIFVTFS¾STUEJTQMBZTUIF0QUJPOTEJBMPHCPY7JTVBM4UVEJPTFFTJOUIFBEEJO¾MF
UIBUZPVµWFEFDMBSFEBQBHFBOEJUDSFBUFTBOJOTUBODFPGZPVS/&5VTFSDPOUSPM*GUIF
+&66QQNU1RVKQPU2CIGJOUFSGBDFJTJNQMFNFOUFEPOUIBUDPOUSPMUIF1P#HVGT%TGCVGFNFUIPE
JTDBMMFEBOEJTQBTTFEUIF&6'PCKFDUGPSUIFJOTUBODFPG7JTVBM4UVEJPUIBUJTDSFBUJOHUIF
DPOUSPM5IFJNQMFNFOUBUJPOPGUIJTNFUIPEDBOQFSGPSNBOZJOJUJBMJ[BUJPOTUFQTOFFEFE
TVDIBTSFBEJOHWBMVFTGSPNUIFTZTUFNSFHJTUSZBOEVTJOHUIFTFWBMVFTUPTFUVQUIFVTFS
JOUFSGBDFPGUIFDPOUSPM
5IF0QUJPOTEJBMPHCPYIBTUISFFCVUUPOTUIFVTFSDBODMJDL0,$BODFMBOE)FMQ*GUIF
VTFSDMJDLT0,UIF+&66QQNU1RVKQPU2CIG1P1-NFUIPEJTDBMMFEHJWJOHZPVSQBHFBDIBODF
UPTUPSFCBDLJOUPUIFTZTUFNSFHJTUSZBOZWBMVFTUIBUUIFVTFSNJHIUIBWFTFMFDUFE:PV
TIPVMEBMTPQFSGPSNBOZDMFBOVQXPSLJOUIF1P1-NFUIPECFDBVTFUIF0QUJPOTQBHFJT
BCPVUUPCFEJTNJTTFE*GUIFVTFSDMJDLTUIF$BODFMCVUUPOUIF1P%CPEGNNFUIPEJTDBMMFE
/PWBMVFTUIBUUIFVTFSTF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TTJNQMFNFOUJOHUIFQSPQFSUJFT
BOEJUTIPVMEMPPLMJLFUIJT
[System.Runtime.InteropServices.ClassInterface
(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)]
0GDPVSTFBMMUIFTFBUUSJCVUFTBSFTFUVQGPSZPVCZUIFTUBSUFSLJUTPZPVTIPVMEOPUOFFEUP
NBLFBOZPGUIFTFDIBOHFTZPVSTFMG
5P¾OEUIFQSPQFSUZFYQPTFECZUIJTPCKFDUZPVXJMMQBTTUIFDBUFHPSZBOETVCDBUFHPSZ
WBMVFTGSPNUIFBEEJO¾MFUPUIF%5&1SPQFSUJFTQSPQFSUZBOEUIFOVTFUIFSFUVSOFE
2TQRGTVKGUDPMMFDUJPOKVTUBTZPVXPVMEGPSBQSPQFSUZCVJMUJOUP7JTVBM4UVEJP5IJTNBDSP
XJMMEJTQMBZUIF5CORNG2TQRGTV[QSPQFSUZGPSUIF9.-TOJQQFUHJWFOFBSMJFS
Sub ShowSamplePropertyValue()
Dim props As Properties
props = DTE.Properties("My Custom Category", "My Custom Subcategory")
MsgBox(props.Item("SampleProperty").Value)
End Sub
Looking Ahead
*OUIJTDIBQUFSZPVMFBSOFEIPXZPVDBOQSPHSBNNBOZPGUIFXJOEPXTBWBJMBCMFJO7JTVBM
4UVEJP*OUIFOFYUDIBQUFSXFµMMTIPXZPVIPXUPQSPHSBNUIFEBUBJOPOFTQFDJ¾DXJOEPX
UZQFUIFUFYUFEJUPSXJOEPX
Chapter 10
Text-Editing Objects and Events
In this chapter:
Editor Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Point Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The TextSelection Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Undo Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Text Editor Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
241
246
250
253
256
259
262
.VDIPGXIBUBQSPHSBNNFSEPFTEVSJOHUIFXPSLEBZBOEXPSLOJHIU
JOWPMWFTFEJUJOH
UFYU*OGBDUFEJUJOHUFYUJTTPNVDIBQBSUPGQSPHSBNNJOHUIBUNBOZBTVDDFTTGVMCVTJOFTT
IBTCFFOCVJMUBSPVOEDSFBUJOHBCFUUFS/PUFQBEBOEUIFQPQVMBSJUZPGUIFTFFEJUPSTIBT
HSPXOJOEJSFDUQSPQPSUJPOUPUIFOVNCFSPGNVOEBOFUBTLTUIBUUIFZBVUPNBUFBOEUIF
FYUFOUUPXIJDIUIFZDBOCFDVTUPNJ[FE"TZPVMFBSOFEJO$IBQUFS.JDSPTPGUˆ7JTVBM
4UVEJPˆCPBTUTB¾STUDMBTTDPEFFEJUPSBOEUIFBVUPNBUJPOPCKFDUNPEFMMFUTZPVUBLF
BEWBOUBHFPGUIFFEJUPSµTGVODUJPOBMJUZUPDSFBUFBMMUIFFEJUJOHGFBUVSFTUIBUXPVMEIBWFCFFO
JODMVEFEIBEZPVCFFOJODIBSHFPGEFWFMPQNFOU
Editor Windows
*GZPVXBOUUPFEJUUFYUJOUIFJOUFHSBUFEEFWFMPQNFOUFOWJSPONFOU*%&
ZPVOFFEB
EPDVNFOUJGZPVIBWFBEPDVNFOUZPVBMTPIBWFBOFEJUPSXJOEPX*O7JTVBM4UVEJP
EPDVNFOUTBOEFEJUPSXJOEPXTBSFJOTFQBSBCMFTPJUQBZTUPLOPXBMJUUMFBCPVUFEJUPS
XJOEPXTFWFOJGFEJUJOHUFYUJOEPDVNFOUTJTZPVSVMUJNBUFHPBM'JHVSFHJWFTBTOFBL
QSFWJFXPGUIFFEJUPSXJOEPXTPGJOUFSFTUUPVTJOUIJTDIBQUFS
The Window Object
5IFSFµTOPUNVDIUPTBZBCPVUUIF9KPFQYPCKFDU±JUµTKVTUBTIPSUTUPQPOUIFXBZUPNPSF
TQFDJBMJ[FEXJOEPXT'JOEJOHBXJOEPXJTTUSBJHIUGPSXBSEJGZPVXBOUUIFXJOEPXUIBUIBT
UIFGPDVTUIF&6'#EVKXG9KPFQYQSPQFSUZSFUVSOTJUUPZPV*GZPVXBOUTPNFPUIFSXJOEPX
BOEZPVLOPXJUTDBQUJPOVTF&6'9KPFQYU+VGO
ECRVKQP 'JHVSFTIPXTUIFDPEFGPS
SFUSJFWJOHUIF$POOFDUDTBOE)5.-1BHFIUNXJOEPXT
"GUFSZPVIBWFB9KPFQYPCKFDUUIFNPTUJNQPSUBOUQSPQFSUZGPS¾OEJOHPUIFSXJOEPXT
JT1DLGEVXIJDISFUVSOTUIFDPSSFTQPOEJOH6GZV9KPFQYPS*6/.9KPFQYPCKFDUGPSFEJUPS
241
242
Working with Microsoft Visual Studio 2005
XJOEPXT*GZPVEPOµULOPXGPSDFSUBJOXIJDIUZQFUIF1DLGEVQSPQFSUZIPMETZPVµMMIBWFUP
DIFDLCZVTJOHUIF6[RG1Hª+U.JDSPTPGU7JTVBM#BTJDˆ
PSKU$
LFZXPSE
If TypeOf DTE.ActiveWindow.Object Is TextWindow Then
§
End If
Window Object
DTE.ActiveWindow
DTE.Windows.Item(”Connect.cs”)
Window Object
DTE.Windows.Item(”HTMLPage1.htm”)
TextWindow Object
DTE.ActiveWindow.Object
DTE.Windows.Item
(”Connect.cs”).Object
HTMLWindow Object
DTE.Windows.Item
(”HTMLPage1.htm”).Object
TextPane Object
DTE.ActiveWindow.Object.ActivePane
DTE.ActiveWindow.Selection.TextPane
DTE.Windows.Item(”Connect.cs”).Object.ActivePane
DTE.Windows.Item(”Connect.cs”).Selection.TextPane
Figure 10-1
Editor windows
0GDPVSTFJGZPVEPOµUDIFDLBOEZPVVTFUIFXSPOHPCKFDUZPVµMMSFDFJWFBOFYDFQUJPO
DPVSUFTZPGUIFDPNNPOMBOHVBHFSVOUJNF$-3
The TextWindow and HTMLWindow Objects
5IF6GZV9KPFQYBOE*6/.9KPFQYPCKFDUTSFQSFTFOUUIFFEJUPSXJOEPXTJOUIF*%&&BDI
UZQFPGGFSTBTNBMMTFUPGQSPQFSUJFTUIBUHJWFZPVBDDFTTUPFEJUPSXJOEPXTQFDJ¾DGFBUVSFT
Chapter 10:
Text-Editing Objects and Events
243
5BCMFMJTUTUIF6GZV9KPFQYQSPQFSUJFT5IFUXPQSPQFSUJFTPGOPUFBSF#EVKXG2CPGBOE
2CPGUXIJDIHJWFZPVBDDFTTUPUIFQBOFTJOBHJWFOFEJUPSXJOEPX
Table 10-1 TextWindow Properties
Property
Description
ActivePane
Returns the TextPane object associated with the active pane.
DTE
Returns the top-level DTE object.
Panes
Returns a TextPanes collection containing the panes in the window.
Parent
Returns the parent Window object.
Selection
Returns the TextSelection object for the active pane. (It is equivalent to
Parent.Selection.)
&TTFOUJBMMZBO*6/.9KPFQYPCKFDUJTKVTUB6GZV9KPFQYPCKFDU±FYDFQUXIFOJUJTOµU8FµMM
HPJOUPNPSFEFUBJMPOUIBUBMJUUMFCJUMBUFS
5BCMFTIPXTUIF*6/.9KPFQYQSPQFSUJFT
Table 10-2 HTMLWindow Properties
Property
Description
CurrentTab
Sets or returns the currently selected tab (HTML or Design)
CurrentTabObject
Returns a TextWindow object when the HTML tab is selected; returns an
IHTMLDocument2 interface when the Design tab is selected
DTE
Returns the top-level DTE object
Parent
Returns the parent Window object
5IF%WTTGPV6CDQSPQFSUZVTFTWBMVFTGSPNUIF'PX&6'XU*6/.6CDUFOVNFSBUJPO
XU*6/.6CDU5QWTEGXIFOTFUUJOHPSSFUVSOJOHUIF)5.-UBCBOEXU*6/.6CDU&GUKIPXIFO
TFUUJOHPSSFUVSOJOHUIF%FTJHOUBC5IF%WTTGPV6CD1DLGEVQSPQFSUZSFUVSOTB6GZV9KPFQY
PCKFDUXIFOUIF)5.-UBCJTTFMFDUFEXIJDIJTXIZXFTVHHFTUFEFBSMJFSUIBUBO
*6/.9KPFQYJTKVTUB6GZV9KPFQYJOEJTHVJTF8IFOUIF%FTJHOUBCJTTFMFDUFEIPXFWFS
%WTTGPV6CD1DLGEVSFUVSOTBOOUJVON+*6/.&QEWOGPVJOUFSGBDFXIJDIQSPWJEFTBDDFTTUP
UIF%ZOBNJD)5.-%)5.-
PCKFDUNPEFMPGUIFVOEFSMZJOHEPDVNFOU#FBXBSFUIBUUIF
WJFXTPGGFSFECZUIF%FTJHOUBCBOE)5.-UBCBSFOµUTZODISPOJ[FEDIBOHFTJOPOFWJFX
XPOµUQSPQBHBUFUPUIFPUIFSVOUJMZPVTXJUDIWJFXT*OQSBDUJDBMUFSNTUIJTNFBOTUIBUZPV
TIPVMEVTFSFGFSFODFTPOMZUPUIFDVSSFOUWJFX
Note To use the mshtml namespace, you need its primary interop assembly: Microsoft.
mshtml.dll. You can find this assembly at Program Files\Microsoft.NET\Primary Interop
Assemblies. Add-in writers can add a reference to this assembly by browsing to it from the
Add Reference dialog box; macro writers first need to copy the DLL file to the Visual Studio
2005 PublicAssemblies folder before they can access the assembly.
244
Working with Microsoft Visual Studio 2005
"TZPVOPXLOPXJUUBLFTTFWFSBMTUFQTUPEJTDPWFSXIFUIFSBUFYUXJOEPXIJEFTJOTJEFBO
BSCJUSBSZXJOEPX*GZPVUIJOLJUXPVMECFOJDFUPIBWFBGVODUJPOUIBUUBLFTDBSFPGUIFTF
TUFQTGPSZPVZPVµSFJOMVDL
Function GetTextWindow(ByVal win As Window) As TextWindow
' Description: Returns the TextWindow object for a given window,
'
or Nothing if not a text window
Dim txtWin As TextWindow = Nothing
' Check for TextWindow
If TypeOf win.Object Is TextWindow Then
txtWin = win.Object
' Otherwise, check for HTMLWindow, then TextWindow
ElseIf TypeOf win.Object Is HTMLWindow Then
Dim htmlWin As HTMLWindow = win.Object
If htmlWin.CurrentTab = vsHTMLTabs.vsHTMLTabsSource Then
txtWin = htmlWin.CurrentTabObject
End If
End If
Return txtWin
End Function
The TextPane Object
5IF6GZV2CPGPCKFDUSFQSFTFOUTBQBOFJOBOFEJUPSXJOEPX&WFSZFEJUPSXJOEPXDBOCFTQMJU
JOUPUXPQBOFTUPBMMPXZPVUPKVYUBQPTFUXPMPDBUJPOTJOBUFYU¾MF:PVDBOTQMJUUIFWJFX
NBOVBMMZFJUIFSCZEPVCMFDMJDLJOHUIFTQMJUUFSCBS±UIFUIJOSFDUBOHMFBUUIFUPQPGUIFTDSPMM
CBS±PSCZESBHHJOHUIFTQMJUUFSCBSUPUIFEFTJSFEMPDBUJPO"GUFSXBSEZPVDBONBLFDIBOHFT
UPUIFTBNFEPDVNFOUUISPVHIFJUIFSQBOF
Finding TextPane Objects
5IFBVUPNBUJPOPCKFDUNPEFMNBLFTJUFBTZUP¾OE6GZV2CPGPCKFDUTJGZPVBMSFBEZIBWFB
6GZV9KPFQYPCKFDUKVTUVTFUIF#EVKXG2CPGQSPQFSUZPSJUFSBUFUISPVHIUIF2CPGUDPMMFDUJPO
VOUJMZPV¾OEUIF6GZV2CPGZPVXBOU6OGPSUVOBUFMZUIF*6/.9KPFQYPCKFDUEPFTOµUPGGFS
TJNJMBSQSPQFSUJFTEJSFDUMZTPZPV¾STUIBWFUPVTFMPHJDMJLFUIBUGPVOEJOUIF)GV6GZV9KPFQY
GVODUJPOFBSMJFSUPFYUSBDUB6GZV9KPFQYGSPNBO*6/.9KPFQY
"OBMUFSOBUJWFXBZPGSFUSJFWJOHB6GZV2CPGJTUISPVHIUIF6GZV5GNGEVKQPPCKFDU6GZV5GNGEVKQP
IBTB6GZV2CPGQSPQFSUZUIBUSFUVSOTUIFQBOFUPXIJDIUIFTFMFDUJPOCFMPOHT6GZV2CPGIBT
BOPSUIPHPOBMQSPQFSUZ5GNGEVKQPUIBUSFUVSOTUIF6GZV5GNGEVKQPJOUIFQBOF
6GZV9KPFQY
BOE*6/.9KPFQYCPUIIBWFB5GNGEVKQPQSPQFSUZBTEPFT9KPFQYXIJDINFBOTUIFSFµT
BOJOEJSFDUQBUIUP6GZV2CPGPWFSXIJDIBMMXJOEPXPCKFDUTDBOUSBWFM'PSNPTUQVSQPTFT
IPXFWFSVTJOHB6GZV9KPFQYUP¾OEB6GZV2CPGXPSLTKVTU¾OF
Chapter 10:
Text-Editing Objects and Events
245
0OFQBOFSFMBUFERVFTUJPOZPVNJHIUBTLJTXIFUIFSBTFDPOEQBOFJTPQFOJOBOFEJUPS
XJOEPX5IFGPMMPXJOHDPEFHJWFTZPVUIFBOTXFS
Function IsSecondPaneOpen(ByVal txtWin As TextWindow) As Boolean
' Description: Returns whether a second pane is open in a text window
Return (txtWin.Panes.Count = 2)
End Function
5IF6GZV2CPGUDPMMFDUJPOSFUVSOFECZ2CPGUIBTPOF6GZV2CPGPCKFDUGPSFBDIQBOFJOUIF
XJOEPXTPJUT%QWPVQSPQFSUZSFUVSOTXIFOBTFDPOEQBOFJTPQFO
)FSFµTBNPSFJOUFSFTUJOHQSPCMFN±¾OEJOHUIFUPQPSCPUUPNQBOFJOBXJOEPX5IFQSPCMFN
XPVMECFJOUSBDUBCMFFYDFQUGPSUIFGBDUUIBUUIFCPUUPNQBOFJTBMXBZTBUJOEFYPGJUT6GZV2CPGU
DPMMFDUJPO(JWFOUIBUCJUPGJOGPSNBUJPOIFSFBSFUXPGVODUJPOTUIBUSFUVSOUIFBQQSPQSJBUFQBOF
Function GetTopPane(ByVal txtWin As TextWindow) As TextPane
' Description: Returns the top pane in the text window
Dim txtPane As TextPane = Nothing
If txtWin.Panes.Count = 1 Then
' Only one pane, so return it
txtPane = txtWin.ActivePane
Else
' Top pane is always index 2
txtPane = txtWin.Panes.Item(2)
End If
Return txtPane
End Function
Function GetBottomPane(ByVal txtWin As TextWindow) As TextPane
' Description: Returns the bottom pane in a text window. Returns
'
top pane if only one pane is open
' Bottom pane is always index 1
Return txtWin.Panes.Item(1)
End Function
5IF#EVKXCVG6QR2CPGBOE#EVKXCVG$QVVQO2CPGNBDSPTJODMVEFEXJUIUIFCPPLµTTBNQMF¾MFTMFU
ZPVUFTUUIFQSFWJPVTDPEFPOMJWFXJOEPXT
0OFMBTURVFTUJPOZPVNJHIUIBWFJTXIJDIQBOFBHJWFO6GZV2CPGCFMPOHTUP"U¾STUJUNJHIU
TFFNFBTZFOPVHIUPDPNQBSFUIFHJWFO6GZV2CPGXJUIJUTDPSSFTQPOEJOHNFNCFSJOUIF
6GZV2CPGUDPMMFDUJPOCVUGPSUIFSFBTPOTHJWFOJOUIF$IBQUFSTJEFCBS²+U*U8IBU*U4BZT
*U+U ³
ZPVDBOµUDPNQBSF6GZV2CPGSFGFSFODFTGPSFRVBMJUZ'PSUVOBUFMZZPVDBODPNQBSF
6GZV5GNGEVKQPSFGFSFODFTTVDDFTTGVMMZBOPQFSBUJPOUIBUQSPWJEFTBMMUIFJOGPSNBUJPOZPVOFFE
UPXSJUFUIFGPMMPXJOHGVODUJPOT
Function IsTopPane(ByVal txtPane As TextPane) As Boolean
' Description: Returns whether the given TextPane is the top pane
246
Working with Microsoft Visual Studio 2005
Dim result As Boolean = False
If txtPane.Collection.Count = 1 Then
result = True
Else
If txtPane.Selection Is txtPane.Collection.Item(2).Selection Then
result = True
End If
End If
Return result
End Function
Function IsBottomPane(ByVal txtPane As TextPane) As Boolean
' Description: Returns whether the given TextPane is the bottom pane
Dim result As Boolean = False
If txtPane.Collection.Count = 2 Then
result = _
(txtPane.Selection Is txtPane.Collection.Item(1).Selection)
End If
Return result
End Function
Documents
"UUIFSJTLPGTUBUJOHUIFPCWJPVTBOEQPTTJCMZUIFQBJOGVMMZPCWJPVT
UIF7JTVBM4UVEJP
UFYUFEJUPSPQFSBUFTPOEPDVNFOUT8IFOZPVQSPHSBNJUµTFBTZUPUIJOLUIBUZPVµSF
UZQJOHJOB¾MFZPVMPBETPVSDFDPEFGSPNB¾MFXIFOZPVCFHJOFEJUJOHBOEZPVTBWFUIF
DIBOHFTUPB¾MFXIFOZPV¾OJTITPJUµTOBUVSBMUPBTTVNFUIBUBMMUIFUJNFJOCFUXFFOJT
TQFOUXPSLJOHPOB¾MF)PXFWFSB¾MFJTTPNFUIJOHUIBUFYJTUTPOEJTL±UIFEPDVNFOUZPV
XPSLXJUIJOUIFUFYUFEJUPSJTTPNFUIJOHMFTTQFSNBOFOUCVUJO¾OJUFMZNPSFNBMMFBCMF5IJT
TFDUJPOJOUSPEVDFTZPVUPUIFUXPPCKFDUTUIBUDBQUVSFUIFTFRVBMJUJFTPGEPDVNFOUTBOE
NBLFUIFNBWBJMBCMFUPZPVUISPVHIBVUPNBUJPOUIF&QEWOGPVBOE6GZV&QEWOGPVPCKFDUT
The Document Object
5IF&QEWOGPVPCKFDUTFSWFTBTBHFOFSBMQVSQPTFXSBQQFSGPSUFYUEBUBJUQSPWJEFTNFUIPET
BOEQSPQFSUJFTUIBUHJWFZPVIJHIMFWFMDPOUSPMPWFSCPUIUIFEBUBBOEUIFXJOEPXTJOXIJDI
UIBUEBUBBQQFBST
Creating and Finding Documents
:PVDBODSFBUFBEPDVNFOUQSPHSBNNBUJDBMMZCZVTJOHNFUIPETPGUIF+VGO1RGTCVKQPUPCKFDU
XIJDIJTDPWFSFEJO$IBQUFSBOEUIF2TQLGEV+VGOUPCKFDUXIJDIJTDPWFSFEJO$IBQUFS
'PSFYBNQMFUIF+VGO1RGTCVKQPU0GY(KNGNFUIPEXIJDIDPSSFTQPOETUPUIF/FXDPNNBOE
POUIF'JMFNFOVMFUTZPVDSFBUFB¾MFUIBUJTOµUBTTPDJBUFEXJUIBQBSUJDVMBSQSPKFDU5IF
Chapter 10:
Text-Editing Objects and Events
247
GPMMPXJOHNBDSPTIPXTIPXUPDSFBUFBUFYU¾MFCZVTJOHUIF0GY(KNGNFUIPE
Sub CreateNewTextFile()
' Description: Shows how to use the ItemOperations.NewFile method
'
to create a new text file
Dim
Dim
Dim
Dim
Item As String = "General\Text File"
Name As String = "MyTextFile"
ViewKind As String = Constants.vsViewKindPrimary
win As Window
win = DTE.ItemOperations.NewFile(Item, Name, ViewKind)
End Sub
0OFQFDVMJBSJUZPGUIF0GY(KNGNFUIPEµT0COGQBSBNFUFSJTUIBUJUTQFDJ¾FTUIFOBNFPG
UIFOFXEPDVNFOUJOEJSFDUMZ8JUIBOFYJTUJOH¾MFUIFEPDVNFOUOBNFBOEUIFXJOEPX
DBQUJPOCPUIDPSSFTQPOEUPUIF¾MFOBNF8JUIBEPDVNFOUDSFBUFECZ0GY(KNGIPXFWFSUIF
0COGQBSBNFUFSTFSWFTBTUIFDBQUJPOPGUIFOFXEPDVNFOUµTXJOEPXPOMZ±UIFEPDVNFOU
BDRVJSFTUIFOBNFPGUIFUFNQPSBSZ¾MFDSFBUFECZ7JTVBM4UVEJPUPTUPSFUIFOFX
EPDVNFOU5IF²JOEJSFDUMZ³QBSUIBQQFOTXIFOZPVTBWFUIFEPDVNFOU7JTVBM4UVEJP
EJTQMBZTUIF0COGWBMVFBTUIFEFGBVMUOBNFPGUIF¾MFUPTBWF
:PVIBWFUISFFNBJOXBZTPG¾OEJOHBOESFUSJFWJOHBOFYJTUJOH&QEWOGPVPCKFDUUIF&6'
&QEWOGPVUDPMMFDUJPOUIF9KPFQY&QEWOGPVQSPQFSUZBOEUIF&6'#EVKXG&QEWOGPVQSPQFSUZ
5IF&6'&QEWOGPVUDPMMFDUJPODPOUBJOTBSFGFSFODFUPFWFSZPQFO&QEWOGPVPCKFDU+VTU
BTXJUIBOZPUIFSDPMMFDUJPOJOUIFBVUPNBUJPOPCKFDUNPEFMZPVDBOJUFSBUFUISPVHIUIF
&QEWOGPVPCKFDUTJOUIFDPMMFDUJPOMPPLJOHGPSUIFPOFZPVXBOUPSJGZPVLOPXUIFOBNFPG
UIFEPDVNFOUZPVDBOSFUSJFWFJUCZVTJOHUIF&QEWOGPVU+VGONFUIPEMJLFTP
Dim doc As Document = DTE.Documents.Item("MyFile.cs")
*GZPVIBWFB9KPFQYPCKFDUJUT&QEWOGPVQSPQFSUZSFUVSOTUIFBTTPDJBUFE&QEWOGPVPCKFDU
4PNFPGUIFUFTUTGPSUIJTDIBQUFSVTFUIFGPMMPXJOHNBDSPUPSFUSJFWFUIF&QEWOGPVPCKFDUPG
B9KPFQYJGUIFXJOEPXEPFTOµUFYJTUUIFNBDSPDSFBUFTBOFXUFYU¾MFXJUIUIFSFRVFTUFE
DBQUJPOBOESFUVSOTJUT&QEWOGPVPCKFDU
Function GetDocument(ByVal caption As String) As Document
' Description: Retrieves the Document object associated with
'
the specified window, or creates a text file in
'
a new window and returns its Document object
Dim win As Window
Try
win = DTE.Windows.Item(caption)
Catch ex As System.Exception
win = DTE.ItemOperations.NewFile("General\Text File", caption)
End Try
Return win.Document
End Function
248
Working with Microsoft Visual Studio 2005
Managing Document Windows
5IFSFMBUJPOTIJQPG&QEWOGPVPCKFDUTUPXJOEPXTJTPOFUPNBOZBXJOEPXBMXBZTIBTPOF
BTTPDJBUFE&QEWOGPVCVUB&QEWOGPVDBOCFPQFOJONBOZXJOEPXT:PVDBOPQFOBOFX
XJOEPXPOBEPDVNFOUCZVTJOHUIF&QEWOGPV0GY9KPFQYNFUIPEXIJDIXPSLTJOUIFTBNF
XBZBTUIF/FX8JOEPXDPNNBOEPOUIF8JOEPXNFOV&BDIPGUIFXJOEPXTBTTPDJBUFEXJUI
BQBSUJDVMBSEPDVNFOUXJMMIBWFBTJUTDBQUJPOUIFEPDVNFOUOBNFGPMMPXFECZBDPMPO
BOE
UIFXJOEPXOVNCFSGPSFYBNQMF$POOFDUDT$POOFDUDTBOETPPO#FDBVTFUIFXJOEPXT
IBWFUIFTBNFVOEFSMZJOHEBUBDIBOHFTJOPOFXJOEPXXJMMBQQFBSJOBMMPUIFSSFMBUFEXJOEPXT
Warning Visual Basic files don’t support Document.NewWindow, so they throw a “not
implemented” exception when you call this method.
5IFBCJMJUZUPIBWFNVMUJQMFXJOEPXTNFBOTUIBUZPVXPOµU¾OEB&QEWOGPV2CTGPVQSPQFSUZ
UIBUSFUVSOTUIFDPOUBJOJOHXJOEPX8IJDIXJOEPXXPVMEJUSFUVSO *OTUFBEZPVDBO¾OE
BMMUIFXJOEPXTBTTPDJBUFEXJUIBQBSUJDVMBSEPDVNFOUCZJUFSBUJOHUISPVHIUIF&QEWOGPV
9KPFQYUDPMMFDUJPOBTTIPXOCZUIFGPMMPXJOHNBDSP
Sub ListDocumentWindows()
' Description: Lists all the windows associated with
'
each open document
Dim pane As OutputWindowPane = _
GetOutputWindowPane("List Document Windows")
pane.Clear()
Dim doc As Document
For Each doc In DTE.Documents
Dim win As Window
pane.OutputString(doc.Name & " windows:" & vbCrLf)
For Each win In doc.Windows
pane.OutputString("
" & win.Caption & vbCrLf)
Next
pane.OutputString(vbCrLf)
Next
End Sub
:PVDBO¾OEUIFBDUJWFXJOEPXGPSUIF&QEWOGPVPCKFDUCZVTJOHJUT#EVKXG9KPFQYQSPQFSUZ
XIJDISFUVSOTUIFBDUJWFXJOEPXJGBQQMJDBCMFPSUIFUPQNPTUXJOEPXBTTPDJBUFEXJUIUIF
EPDVNFOUJGOPOFPGUIFEPDVNFOUµTXJOEPXTJTBDUJWF
Warning The Document.ActiveWindow property has a bug—it always returns the first
document window, regardless of which window has the focus.
Chapter 10:
Text-Editing Objects and Events
249
Managing Document Changes
5IFDPBSTFTUNFBOTBWBJMBCMFUPUIF&QEWOGPVPCKFDUGPSNBOBHJOHDIBOHFTJTJUT4GCF1PN[
QSPQFSUZXIJDIBMMPXTZPVUPHFUPSTFUUIFEPDVNFOUµTSFBEPOMZTUBUF.FUIPETUIBU
NPEJGZBEPDVNFOUµTUFYUUISPXBOFYDFQUJPOJGUIFEPDVNFOUJTSFBEPOMZTPJUµTXPSUI
DIFDLJOHUIF4GCF1PN[QSPQFSUZCFGPSFZPVNBLFUFYUDIBOHFT
:PVDBOVOEPBOESFEPDIBOHFTUPBEPDVNFOUCZVTJOHUIF&QEWOGPV7PFQBOE&QEWOGPV
4GFQNFUIPETSFTQFDUJWFMZ5IFTFUXPNFUIPETPGGFSUIFTBNFGVODUJPOBMJUZBTUIFJS&EJU
NFOVDPVOUFSQBSUT5IF7PFQBOE4GFQNFUIPETCPUISFUVSOB#PPMFBOWBMVFJOEJDBUJOH
XIFUIFSUIFPQFSBUJPOUPPLQMBDF
Warning You can call Document.Undo or Document.Redo on a read-only document as
many times as you want to, so long as the corresponding undo or redo stack is empty; in
such cases, the method returns False. The problem is that you can change the Document.
ReadOnly property on the fly, which means you can have undoable (or redoable) changes in
your document when you switch from read-write to read-only. If you call Undo or Redo on a
nonempty undo or redo stack of a read-only document, you get an exception.
5IF&QEWOGPV6GZV5GNGEVKQPQSPQFSUZSFUVSOTUIF6GZV5GNGEVKQPPCKFDUBTTPDJBUFEXJUIUIF
BDUJWFXJOEPXPSUIFUPQNPTUXJOEPXJGOPOFPGUIFEPDVNFOUµTXJOEPXTIBTUIFGPDVT
:PVDBOVTFUIF6GZV5GNGEVKQPPCKFDUµTNZSJBEFEJUJOHNFUIPETBOEQSPQFSUJFTUPBVUPNBUF
KVTUBCPVUBOZFEJUJOHUBTL:PVµMMMFBSOBMMBCPVU6GZV5GNGEVKQPPCKFDUTJOUIFVQDPNJOH
TFDUJPOUJUMFE²5IF6GZV5GNGEVKQP0CKFDU³
Saving and Closing Documents
5IF&QEWOGPV5CXGNFUIPETBWFTUIFEPDVNFOUBOEPQUJPOBMMZMFUTZPVDIPPTFUIFOBNFBOE
UIFMPDBUJPOUPTBWFUP5IF5CXGNFUIPEUISPXTBOFYDFQUJPOJGUIFMPDBUJPOZPVTQFDJGZEPFTOµU
BMSFBEZFYJTUJGZPVHJWFBDPSSFDUMPDBUJPOCVUOPOBNFUIF5CXGNFUIPEVTFTUIFDVSSFOUOBNF
PGUIFEPDVNFOU"CVHJOUIF.JDSPTPGU7JTVBM$ˆJNQMFNFOUBUJPODBVTFTUIF5CXGNFUIPEUP
JHOPSFBOZOFX¾MFOBNFUIBUZPVH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CKFDUDBOSFQSFTFOUBOZEPDVNFOUJOUIF*%&UIF6GZV&QEWOGPVPCKFDU
SFQSFTFOUTUFYUEPDVNFOUTPOMZ:PVSFUSJFWFB6GZV&QEWOGPVPCKFDUCZVTJOHUIF&QEWOGPV
1DLGEVNFUIPEBOEQBTTJOHJOBOFNQUZTUSJOHPSBWBMVFPG²6GZV&QEWOGPV´UIFNFUIPE
SFUVSOTPWNNPS0QVJKPIGPSOPOUFYUEPDVNFOUT
5IFNPTUJNQPSUBOU6GZV&QEWOGPVQSPQFSUJFTBOENFUIPETBSFUIPTFSFMBUFEUPUIF6GZV2QKPV
'FKV2QKPVBOE6GZV5GNGEVKQPFEJUJOHPCKFDUT5IF6GZV&QEWOGPV5GNGEVKQPQSPQFSUZSFUVSOTUIF
UFYUEPDVNFOUµTTFMFDUJPOBOECFIBWFTUIFTBNFBTUIF&QEWOGPV5GNGEVKQPQSPQFSUZ5IF
5VCTV2QKPVBOE'PF2QKPVQSPQFSUJFTSFUVSO6GZV2QKPVPCKFDUTUIBUNBSLUIFCFHJOOJOHBOEFOE
SFTQFDUJWFMZPGUIFUFYUEPDVNFOUCVGGFS5IF%TGCVG'FKV2QKPVNFUIPESFUVSOTBO'FKV2QKPV
PCKFDUBUUIFMPDBUJPOPGUIF6GZV2QKPVQBTTFEJOUPUIFNFUIPEQBTTJOHJOPWNNPS0QVJKPI
DSFBUFTBO'FKV2QKPVBUUIFCFHJOOJOHPGUIFEPDVNFOU
Note It makes little sense to call TextDocument.CreateEditPoint with a TextPoint parameter
because a TextPoint object already has its own CreateEditPoint method. However, passing null
to TextDocument.CreateEditPoint is the only way to create an EditPoint without first creating
an intermediary point object.
Point Objects
"TZPVNJHIUHVFTTBQPJOUPCKFDUSFQSFTFOUTBQPTJUJPOJOBUFYUEPDVNFOU5IF
BVUPNBUJPOPCKFDUNPEFMHJWFTZPVUISFFQPJOUPCKFDUTUPDIPPTFGSPN6GZV2QKPV
8KTVWCN2QKPVBOE'FKV2QKPV
The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VDBOUIFOVTFUPFEJUUFYU5BCMFTIPXTZPVUIF
EJGGFSFOUXBZTUP¾OEB6GZV2QKPVPCKFDU
Table 10-3
How to Retrieve a TextPoint Object
Returned By
Applies To
StartPoint property
TextDocument
TextPane
TextRange
EndPoint property
TextDocument
TextRange
The VirtualPoint Object
"8KTVWCN2QKPVPCKFDUSFQSFTFOUTBQPJOUJOXKTVWCNURCEGXIJDIJTBUFYUFEJUPSGFBUVSFUIBU
BMMPXTUIFJOTFSUJPOQPJOUUPNPWFJOEF¾OJUFMZQBTUUIFFOEPGBMJOF8IFOZPVUZQFB
DIBSBDUFSBUBQPJOUJOWJSUVBMTQBDFUIFFEJUPSBVUPNBUJDBMMZ¾MMTJOUIFTQBDFCFUXFFOUIF
DVSSFOUFOEPGUIFMJOFBOEUIFOFXDIBSBDUFS:PVDBOFOBCMFWJSUVBMTQBDFGPSBMMMBOHVBHFT
CZPQFOJOHUIF5PPMT±0QUJPOTEJBMPHCPYTFMFDUJOH5FYU&EJUPS±"MM-BOHVBHFT±(FOFSBM
BOETFMFDUJOHUIF&OBCMF7JSUVBM4QBDFDIFDLCPYJOUIF4FUUJOHTBSFB4FF'JHVSF
Figure 10-2
Enabling virtual space
5BCMFTIPXTUIFEJGGFSFOUXBZTZPVDBO¾OEB8KTVWCN2QKPVPCKFDU"TZPVDBOTFFGSPN
UIFUBCMF8KTVWCN2QKPVPCKFDUTTQSJOHGSPN6GZV5GNGEVKQPPCKFDUTXIJDIHJWFTZPVBDMVFUP
UIFJSGVODUJPOTFMFDUJPOTDBOFYUFOEJOUPWJSUVBMTQBDFTPUIF6GZV5GNGEVKQPPCKFDUOFFET
8KTVWCN2QKPVPCKFDUTUPLFFQUSBDLPGFOEQPJOUTUIBUGBMMPVUTJEFUIFUFYUCVGGFS
252
Working with Microsoft Visual Studio 2005
Table 10-4
How to Retrieve a VirtualPoint Object
Returned By
Applies To
ActivePoint property
TextSelection
AnchorPoint property
BottomPoint property
TopPoint property
"TXJUIUIF6GZV2QKPVPCKFDUPOFPGUIF8KTVWCN2QKPVPCKFDUµTNBJOVTFTJOWPMWFTUIFDSFBUJPO
PG'FKV2QKPVPCKFDUT#FBXBSFIPXFWFSUIBUB8KTVWCN2QKPVPCKFDUDBOµUDSFBUFBO'FKV2QKPV
PCKFDUJOWJSUVBMTQBDF±JGZPVUSZUIF'FKV2QKPVPCKFDUHFUTDSFBUFEBUUIFFOEPGUIFDVSSFOU
MJOFJOTUFBE:PVDBOBWPJEUIPTFTJUVBUJPOTCZVTJOHUIFGPMMPXJOHGVODUJPOXIJDIUFMMTZPV
XIFOB8KTVWCN2QKPVPCKFDUIBTTUSBZFEJOUPWJSUVBMTQBDF
Function IsVirtualSpace(ByVal vrtPoint As VirtualPoint) As Boolean
' Description: Returns whether the VirtualPoint lies in virtual space
Return vrtPoint.LineCharOffset <> vrtPoint.VirtualCharOffset
End Function
5IF8KTVWCN2QKPVPCKFDUEF¾OFTBQSPQFSUZOBNFE8KTVWCN%JCT1HHUGVUIBUSFUVSOTUIFEJTUBODF
CFUXFFOUIFQPJOUBOEUIFCFHJOOJOHPGUIFMJOF5IF8KTVWCN%JCT1HHUGVQSPQFSUZBMXBZTIBT
UIFTBNFWBMVFBTUIF.KPG%JCT1HHUGVQSPQFSUZFYDFQUXIFOUIFQPJOUJTJOWJSUVBMTQBDF
Lab: Exploring Virtual Space
5IFCFTUXBZUPVOEFSTUBOEWJSUVBMTQBDFBOEJUTFGGFDUTPOQPJOUPCKFDUTJTUPUFTUJUGPS
ZPVSTFMG)FSFµTBRVJDLFYQFSJNFOU
5VSOPGGWJSUVBMTQBDFJOUIFFEJUPS
0QFOBOFXUFYU¾MFBOEUZQF*BNB¾TIXJUIPVUQSFTTJOH&OUFS
4FMFDUUIFFOUJSFTFOUFODFCZESBHHJOHUIFNPVTFGSPNMFGUUPSJHIU/PUJDFUIBU
UIFFEJUPSXPOµUFYUFOEUIFTFMFDUJPOCFZPOEUIFQFSJPE
0QFOUIF0VUQVUXJOEPXBO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FUFYUCVGGFSJUBMTPTIPXTUIBU
WJSUVBMTQBDFEPFTOµUDPVOUBTTFMFDUFEUFYU*OTUFBEWJSUVBMTQBDFBMMPXTGPSXIBUZPV
NJHIUDBMM8:4*/8:(FEJUJOH±XIBUZPVTFFJTOµUOFDFTTBSJMZXIBUZPVHFU
The EditPoint Object
5IF'FKV2QKPVJTUIFXPSLIPSTFPGUIFQPJOUPCKFDUT*OBEEJUJPOUPUIF6GZV2QKPVNFUIPETBOE
QSPQFSUJFT'FKV2QKPVIBTNFUIPETUIBUMFUZPVBVUPNBUFFWFSZQPTTJCMFNPEJ¾DBUJPOPGUIF
UFYUCVGGFS5BCMFTIPXTUIFEJGGFSFOUXBZTZPVDBOSFUSJFWFBO'FKV2QKPVPCKFDU
Table 10-5
How to Retrieve an EditPoint Object
Returned By
Applies To
CreateEditPoint method
EditPoint
TextPoint
VirtualPoint
TextDocument
8FµMMFYBNJOFUIF'FKV2QKPVPCKFDUµTNFUIPETTIPSUMZJOUIFTFDUJPOUJUMFE²"$PNQBSJTPOPG
UIF6GZV5GNGEVKQPBOE'FKV2QKPVPCKFDUT³
The TextSelection Object
5IF6GZV5GNGEVKQPPCKFDUQVMMTEPVCMFEVUZBTBSFQSFTFOUBUJPOPGUIFDBSFUJOUIFFEJUPS
XJOEPXBTXFMMBTBSFQSFTFOUBUJPOPGUIFDVSSFOUMZTFMFDUFEUFYU:PVDBOUIJOLPGUIFDBSFU
BTB[FSPMFOHUITFMFDUJPO
#FDBVTFUIFSFDBOCFPOMZPOFTFMFDUJPOJOBOFEJUPSXJOEPX
UIFSFDBOCFPOMZPOF6GZV5GNGEVKQPPCKFDUQFSEPDVNFOU'JHVSFCSFBLTEPXOB
6GZV5GNGEVKQPJOUPJUTDPOTUJUVFOUQBSUT
"TZPVDBOTFFJO'JHVSFGPVSQSPQFSUJFTEFMJOFBUFB6GZV5GNGEVKQP6QR2QKPV$QVVQO2QKPV
#PEJQT2QKPVBOE#EVKXG2QKPV&BDIPGUI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WJSUVBMTQBDFTFMFDUJPO8IFOBTFMFDUJPOTQBOTNVMUJQMFMJOFTUIF6GZV4CPIGUQSPQFSUZ
IPMETBDPMMFDUJPOPG6GZV4CPIGPCKFDUTPOFGPSFBDIMJOFPGUIFTFMFDUJPO
5BCMFMJTUTUIFEJGGFSFOUXBZTZPVDBOSFUSJFWFB6GZV5GNGEVKQPPCKFDU
Table 10-6
Properties That Return a TextSelection Object
Property
Applies To
Selection
Document
TextDocument
TextPane and TextPane2
TextWindow
Window and Window2
A Comparison of the TextSelection and EditPoint Objects
5IF6GZV5GNGEVKQPBOE'FKV2QKPVPCKFDUTPGGFSBCFXJMEFSJOHBSSBZPGFEJUJOHNFUIPETXIJDI
BSFMJTUFEJO5BCMF-PPLJOHBUUIFUBCMFZPVµMMTFFUIBU6GZV5GNGEVKQPBOE'FKV2QKPVTIBSF
UIFNBKPSJUZPGUIFJSNFUIPETBOEIBWFPOMZBGFXTFFNJOHMZNJOPSEJGGFSFODFTXIJDI
Chapter 10:
Text-Editing Objects and Events
255
NBLFTDIPPTJOHPOFPWFSUIFPUIFSBLJOUPDIPPTJOHCFUXFFOUIFGFBUVSF4XJTT"SNZ
LOJGFUIBUDPNFTXJUITDJTTPSTBOEUIFGFBUVSF4XJTT"SNZLOJGFUIBUDPNFTXJUIBTBX
*ONPTUDJSDVNTUBODFTFJUIFSLOJGFXJMMEPKVTU¾OF±JUµTPOMZJOUIPTFQBSUJDVMBSNPNFOUT
XIFOZPVOFFEUPHBUIFS¾SFXPPEPSEPBMJUUMFQFSTPOBMHSPPNJOHUIBUZPVTVEEFOMZSFBMJ[F
UIBUZPVDBOµUDVUEPXOCSBODIFTXJUITDJTTPSTBOEZPVDBOµUUSJNOPTFIBJSTXJUIBTBX
6TJOHUIFFEJUJOHPCKFDUTJTNVDIUIFTBNFJOUIBUZPVXPOµULOPXXIFUIFSZPVµWFDIPTFO
UIFSJHIUPOFGPSUIFKPCVOUJMJUGBJMTZPV
Table 10-7
TextSelection and EditPoint Methods
Task
Methods in Common
TextSelection Only
Moving the
insertion point
CharLeft, CharRight,
EndOfDocument, EndOfLine,
LineDown, LineUp,
MoveToAbsoluteOffset,
MoveToLineAndOffset,
MoveToPoint,
StartOfDocument,
StartOfLine, WordLeft,
WordRight
Collapse, GoToLine,
MoveToDisplayColumn,
PageDown, PageUp
Finding and
retrieving text
FindPattern
FindText
Selecting text
EditPoint Only
GetLines, GetText
SelectAll, SelectLine
Modifying text
ChangeCase, Copy, Cut,
Delete, DeleteWhitespace,
Indent, Insert, InsertFromFile,
PadToColumn, Paste,
ReplacePattern,
SmartFormat, Unindent
Managing
bookmarks
ClearBookmark,
NextBookmark,
PreviousBookmark,
SetBookmark
Miscellaneous
OutlineSection
DeleteLeft,
DestructiveInsert,
NewLine, Tabify,
Untabify
InsertNewLine1,
ReplaceText
SwapAnchor
ReadOnly
1
Defined by EditPoint2
5IFGVOEBNFOUBMEJGGFSFODFCFUXFFOUIFUXPPCKFDUTJTUIBUUIF6GZV5GNGEVKQPPCKFDUJTWJFX
CBTFEBOEUIF'FKV2QKPVPCKFDUJTCVGGFSCBTFE5IF6GZV5GNGEVKQPPCKFDUFYJTUTQSJNBSJMZUP
NPEFMVTFSBDUJPOTXJUIJOUIFUFYUFEJUPS±JGZPVDBOEPJUCZIBOEJOUIFFEJUPSZPVDBO
EPJUXJUIUIF6GZV5GNGEVKQPPCKFDU:PVDBOTFFUIJTEFNPOTUSBUFEFWFSZUJNFZPVSFDPSE
BNBDSPUIF.BDSP3FDPSEFSUSBOTMBUFTDIBOHFTUIBUZPVNBLFUPUFYUEPDVNFOUTJOUP
TFRVFODFTPG6GZV5GNGEVKQPTUBUFNFOUT
5IJTFNQIBTJTPO8:4*8:(GVODUJPOBMJUZIPXFWFS
NFBOTUIBUUIFHMPCBMWJFXTUBUFDBOBGGFDUUIFCFIBWJPSPGB6GZV5GNGEVKQPNFUIPE'PS
256
Working with Microsoft Visual Studio 2005
FYBNQMFXIFOMJOFXSBQQJOHJTFOB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PGEZOBNJDFEJUJOHGFBUVSFT
5IF$GHQTG-G[2TGUUBOE#HVGT-G[2TGUUFWFOUTIBWFUISFFQBSBNFUFSTJODPNNPO-G[2TGUU
5GNGEVKQPBOE+P5VCVGOGPV%QORNGVKQP5IF-G[2TGUUQBSBNFUFSHJWFZPVBTUSJOHSFQSFTFOUBUJPO
PGUIFDIBSBDUFSUIBU¾SFEUIFFWFOU5IFLFZQSFTTFWFOUT¾SFJOSFTQPOTFUPBOZBMQIBOVNFSJD
DIBSBDUFSTUIBUUIFVTFSUZQFTJOBEEJUJPOUIFFWFOUT¾SFGPSUIF#BDLTQBDF%FMFUF4QBDF5BC
BOE&OUFSLFZTBOE$USM&OUFSLFZDPNCJOBUJPO
Warning The keypress events don’t fire for just keyboard input—they also fire once for
each character added through the TextSelection.Text property. If you’re tempted to use the
TextSelection.Text property inside a keypress event handler, take care to guard against a
runaway recursion.
5IF5GNGEVKQPQBSBNFUFSHJWFTZPVBSFGFSFODFUPUIFBDUJWFEPDVNFOUµT6GZV5GNGEVKQPPCKFDU
XIJDIZPVDBOVTFUPEFUFSNJOFUIFMPDBUJPOPGUIFLFZQSFTTBOEXIFUIFSUIFLFZQSFTT
SFQMBDFTFYJTUJOHUFYU'JOBMMZUIF+P5VCVGOGPV%QORNGVKQPQBSBNFUFSXBSOTZPVXIFOUIF
LFZQSFTTDPJODJEFTXJUIBO*OUFMMJ4FOTFˆTUBUFNFOUDPNQMFUJPO
260
Working with Microsoft Visual Studio 2005
*OBEEJUJPOUPUIFBGPSFNFOUJPOFEFWFOUQBSBNFUFSTUIF$GHQTG-G[2TGUUFWFOUIBTB
%CPEGN-G[RTGUUQBSBNFUFS"TZPVNJHIUHVFTTTFUUJOHUIJTQBSBNFUFSUP6TWGTUPQT
UIFLFZQSFTTGSPNFWFSSFBDIJOHUIFFEJUPSXJOEPXBOEDPOTFRVFOUMZQSFWFOUTUIF
#HVGT-G[2TGUUFWFOUGSPN¾SJOH
5PHFUBGFFMGPSIPXUIFTFLFZQSFTTFWFOUTXPSL-JTUJOHTIPXTIPXUPJNQMFNFOU
LFZQSFTTFWFOUIBOEMFSTBTNBDSPT
Listing 10-1
Handling the BeforeKeyPress and AfterKeyPress events
<System.ContextStaticAttribute()> Public WithEvents _
TextDocumentKeyPressEvents As EnvDTE80.TextDocumentKeyPressEvents
Private DTE2 As EnvDTE80.DTE2 = CType(DTE, EnvDTE80.DTE2)
Private output As OutputWindowPane
Public Sub EnableKeyPressEventMacros()
' Description: Creates an Output window pane and initializes
'
the TextDocumentKeyPressEvents variable.
Try
output = _
DTE2.ToolWindows.OutputWindow.OutputWindowPanes.Item( _
"KeyPress Events")
Catch ex As System.Exception
output = _
DTE2.ToolWindows.OutputWindow.OutputWindowPanes.Add( _
"KeyPress Events")
Finally
output.Activate()
End Try
TextDocumentKeyPressEvents = _
DTE2.Events.GetObject("TextDocumentKeyPressEvents")
End Sub
Public Sub DisableKeyPressEventMacros()
' Description: Clears the Output window pane and resets the
'
TextDocumentKeyPressEvents variable.
TextDocumentKeyPressEvents = Nothing
If Not IsNothing(output) Then
output.Clear()
output = Nothing
End If
End Sub
Private Sub TextDocumentKeyPressEvents_BeforeKeyPress( _
ByVal Keypress As String, _
ByVal Selection As EnvDTE.TextSelection, _
ByVal InStatementCompletion As Boolean, _
ByRef CancelKeypress As Boolean) _
Handles TextDocumentKeyPressEvents.BeforeKeyPress
Chapter 10:
Text-Editing Objects and Events
261
' Description: Handles the BeforeKeyPress event by displaying
'
the event’s parameters.
output.OutputString("BeforeKeyPress: ")
output.OutputString("KeyPress = " & Keypress & ", ")
output.OutputString("Selection = '" & Selection.Text & "', ")
output.OutputString("InStatementCompletion = " & _
InStatementCompletion.ToString)
output.OutputString(vbCrLf)
If KeyPress.ToUpper() = "W" Then
CancelKeypress = True
output.OutputString(vbCrLf)
End If
End Sub
Private Sub TextDocumentKeyPressEvents_AfterKeyPress( _
ByVal Keypress As String, _
ByVal Selection As EnvDTE.TextSelection, _
ByVal InStatementCompletion As Boolean) _
Handles TextDocumentKeyPressEvents.AfterKeyPress
' Description: Handles the AfterKeyPress event by displaying
'
the event's parameters.
output.OutputString("AfterKeyPress: ")
output.OutputString("KeyPress = " & Keypress & ", ")
output.OutputString("Selection = '" & Selection.Text & "', ")
output.OutputString("InStatementCompletion = " & _
InStatementCompletion.ToString)
output.OutputString(vbCrLf & vbCrLf)
End Sub
*GZPVSVOUIF'PCDNG-G[2TGUU'XGPV/CETQUJO-JTUJOHBOETUBSUUZQJOHJOBOFEJUPS
XJOEPXZPVµMMTFFUIFWBMVFTPGUIF$GHQTG-G[2TGUUBOE#HVGT-G[2TGUUFWFOUQBSBNFUFST
EJTQMBZFEJOUIF0VUQVUXJOEPX5PTIPXIPX%CPEGN-G[RTGUUNJHIUCFVTFEUPNBLF
NJTDIJFGUIF$GHQTG-G[2TGUUFWFOUIBOEMFSFGGFDUJWFMZSFNPWFTUIF8LFZXJUIPVUIBWJOHUP
QSZJUMPPTFGSPNUIFLFZCPBSE
The LineChanged Event
5IFBVUPNBUJPOPCKFDUNPEFMEF¾OFTBUIJSEFWFOUTQFDJ¾DUPFEJUJOHUIF.KPG%JCPIGFFWFOU
*GZPVSBQQMJDBUJPOOFFETPOMZUPQSPDFTTUFYUMJOFCZMJOFTBNQMJOHFWFSZLFZTUSPLFNJHIU
CFPWFSLJMM*OUIPTFJOTUBODFTUIF.KPG%JCPIGFFWFOUPGGFSTBMPXPWFSIFBEBMUFSOBUJWFUPUIF
LFZTUSPLFFWFOUT
5IF.KPG%JCPIGFFWFOUIBTUISFFQBSBNFUFSTUPIFMQZPV¾HVSFPVUXIZUIFFWFOU¾SFE5IF
¾STUUXPQBSBNFUFST5VCTV2QKPVBOE'PF2QKPVNBSLUIFCFHJOOJOHBOEFOEPGUIFDIBOHFTUP
UIFUFYUCVGGFS:PVDBOVTFUIFTF6GZV2QKPVWBMVFTUPSFUSJFWFUIFDIBOHFTMJLFTP
Dim text As String
text = StartPoint.CreateEditPoint.GetText(EndPoint)
262
Working with Microsoft Visual Studio 2005
5IFUIJSEQBSBNFUFS*KPVJTBCJU¿BHUIBUIPMETWBMVFTGSPNUIFXU6GZV%JCPIGFFOVNFSBUJPO
TIPXOJO5BCMF
5IF¿BHTTFUJO*KPVBSFFWJEFODFUIBUZPVDBOQJFDFUPHFUIFSUPSF
DSFBUFUIFBDUJPOTMFBEJOHVQUPUIFFWFOU*OQSBDUJDFUIF*KPVQBSBNFUFSEPFTOµUHJWFZPV
RVJUFFOPVHIJOGPSNBUJPOUP¾HVSFPVUFYBDUMZXIBUMFEUPUIFFWFOU±CVUUIFOJGJUEJEJU
XPVMEOµUCFDBMMFEBIJOU
Table 10-8
The vsTextChanged Enumeration
Field
Description
vsTextChangedMultiLine
The changes affected multiple lines of text.
vsTextChangedSave
The changes were saved to disk.
vsTextChangedCaretMoved
The insertion point moved off the line containing the changes.
vsTextChangedReplaceAll
The entire text buffer was replaced by an insertion.
vsTextChangedNewLine
A new line was entered.
vsTextChangedFindStarting
A find operation moved the insertion point off the line
containing changes.
5IF.KPG%JCPIGFFWFOUEPFTOµUSFBMMZ¾SFXIFOUIFMJOFDIBOHFT±UIBUJTJUEPFTOµU¾SFGPS
FBDIOFXDIBSBDUFSBEEFEUPPSEFMFUFEGSPNBMJOF*OTUFBEUIFFWFOU¾SFTXIFODIBOHFT
UPBMJOFBSFDPNNJUUFEJOTPNFXBZTVDIBTXIFOUIFJOTFSUJPOQPJOUNPWFTPGGUIF
MJOFDIBOHFTBSFTBWFEUPEJTLPSUIFEPDVNFOUXJOEPXMPTFTGPDVT"OVOEPDPOUFYU
FGGFDUJWFMZEJTBCMFTUIJTFWFOUVOUJMUIFVOEPDPOUFYUDMPTFTBGUFSXBSEUIFFWFOU¾SFTJGBOZ
PGUIFDIBOHFTNBEFXJUIJOUIFVOEPDPOUFYUXPVMEIBWFDBVTFEJUUP¾SFVOEFSOPSNBM
DJSDVNTUBODFTUIFJOTFSUJPOQPJOUNPWFTPGGBDIBOHFEMJOFUIFFOUJSF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M4UVEJP6TFS&EVDBUJPOHSPVQ.BSDMJWFTJO4FBUUMFXJUIIJTCFBVUJGVMXJGF
+VMJBDVUFBTBCVUUPOTPO.BYBOEIFMMPOXIFFMTEBVHIUFS#SJHJUUF
Additional Resources for Web Developers
Published and Forthcoming Titles from Microsoft Press
Microsoft® Visual Web Developer™ 2005
Express Edition: Build a Web Site Now!
Programming Microsoft ASP.NET 2.0
Core Reference
Jim Buyens
Dino Esposito ● ISBN 0-7356-2176-4
●
ISBN 0-7356-2212-4
With this lively, eye-opening, and hands-on book, all you need
is a computer and the desire to learn how to create Web pages
now using Visual Web Developer Express Edition! Featuring a
full working edition of the software, this fun and highly visual
guide walks you through a complete Web page project from
set-up to launch. You’ll get an introduction to the Microsoft
Visual Studio® environment and learn how to put the lightweight, easy-to-use tools in Visual Web Developer Express to
work right away—building your first, dynamic Web pages with
Microsoft ASP.NET 2.0. You’ll get expert tips, coaching, and
visual examples at each step of the way, along with pointers
to additional learning resources.
Microsoft ASP.NET 2.0 Programming
Step by Step
George Shepherd
●
ISBN 0-7356-2201-9
With dramatic improvements in performance, productivity, and
security features, Visual Studio 2005 and ASP.NET 2.0 deliver a
simplified, high-performance, and powerful Web development
experience. ASP.NET 2.0 features a new set of controls and
infrastructure that simplify Web-based data access and include
functionality that facilitates code reuse, visual consistency, and
aesthetic appeal. Now you can teach yourself the essentials of
working with ASP.NET 2.0 in the Visual Studio environment—
one step at a time. With Step by Step, you work at your own
pace through hands-on, learn-by-doing exercises. Whether
you’re a beginning programmer or new to this version of the
technology, you’ll understand the core capabilities and
fundamental techniques for ASP.NET 2.0. Each chapter puts
you to work, showing you how, when, and why to use specific
features of the ASP.NET 2.0 rapid application development
environment and guiding you as you create actual components
and working applications for the Web, including advanced
features such as personalization.
Programming Microsoft Windows® Forms
Charles Petzold ● ISBN 0-7356-2153-5
Programming Microsoft Web Forms
Douglas J. Reilly ● ISBN 0-7356-2179-9
CLR via C++
Jeffrey Richter with Stanley B. Lippman
ISBN 0-7356-2248-5
Delve into the core topics for
ASP.NET 2.0 programming,
mastering the essential skills and
capabilities needed to build highperformance Web applications
successfully. Well-known ASP.NET
author Dino Esposito deftly builds
your expertise with Web forms,
Visual Studio, core controls,
master pages, data access, data
binding, state management,
security services, and other mustknow topics—combining definitive reference with practical, hands-on programming instruction. Packed with expert guidance and pragmatic examples, this
Core Reference delivers the key resources that you need to
develop professional-level Web programming skills.
Programming Microsoft ASP.NET 2.0
Applications: Advanced Topics
Dino Esposito ● ISBN 0-7356-2177-2
Master advanced topics in ASP.NET
2.0 programming—gaining the
essential insights and in-depth
understanding that you need to
build sophisticated, highly functional Web applications successfully. Topics include Web forms,
Visual Studio 2005, core controls,
master pages, data access, data
binding, state management,
and security considerations.
Developers often discover that
the more they use ASP.NET, the
more they need to know. With expert guidance from ASP.NET
authority Dino Esposito, you get the in-depth, comprehensive
information that leads to full mastery of the technology.
Debugging, Tuning, and Testing Microsoft .NET 2.0
Applications
John Robbins ● ISBN 0-7356-2202-7
CLR via C#, Second Edition
Jeffrey Richter ● ISBN 0-7356-2163-2
For more information about Microsoft Press® books and other learning products,
visit: www.microsoft.com/books and www.microsoft.com/learning
Microsoft Press products are available worldwide wherever quality computer books are sold. For more information, contact your book or
computer retailer, software reseller, or local Microsoft Sales Office, or visit our Web site at www.microsoft.com/mspress. To locate your
nearest source for Microsoft Press products, or to order directly, call 1-800-MSPRESS in the United States. (In Canada, call 1-800-268-2222.)
Additional Resources for Visual Basic Developers
Published and Forthcoming Titles from Microsoft Press
Microsoft® Visual Basic® 2005 Express Edition:
Build a Program Now!
Programming Microsoft Visual Basic 2005
Core Reference
Patrice Pelland
Francesco Balena
●
ISBN 0-7356-2213-2
Featuring a full working edition of the software, this fun and
highly visual guide walks you through a complete programming project—a desktop weather-reporting application—from
start to finish. You’ll get an introduction to the Microsoft Visual
Studio® development environment and learn how to put the
lightweight, easy-to-use tools in Visual Basic Express to work
right away—creating, compiling, testing, and delivering your
first ready-to-use program. You’ll get expert tips, coaching,
and visual examples each step of the way, along with pointers
to additional learning resources.
Microsoft Visual Basic 2005 Step by Step
Michael Halvorson
●
ISBN 0-7356-2131-4
With enhancements across its
visual designers, code editor,
language, and debugger that
help accelerate the development
and deployment of robust,
elegant applications across the
Web, a business group, or an
enterprise, Visual Basic 2005
focuses on enabling developers
to rapidly build applications.
Now you can teach yourself
the essentials of working with
Visual Studio 2005 and the
new features of the Visual
Basic language—one step at a time. Each chapter puts you
to work, showing you how, when, and why to use specific
features of Visual Basic and guiding as you create actual components and working applications for Microsoft Windows®.
You’ll also explore data management and Web-based
development topics.
●
ISBN 0-7356-2183-7
Get the expert insights,
indispensable reference, and
practical instruction needed
to exploit the core language
features and capabilities in Visual
Basic 2005. Well-known Visual
Basic programming author
Francesco Balena expertly guides
you through the fundamentals,
including modules, keywords,
and inheritance, and builds your
mastery of more advanced topics
such as delegates, assemblies,
and My Namespace. Combining
in-depth reference with extensive, hands-on code examples
and best-practices advice, this Core Reference delivers the
key resources that you need to develop professional-level
programming skills for smart clients and the Web.
Programming Microsoft Visual Basic 2005
Framework Reference
Francesco Balena
●
ISBN 0-7356-2175-6
Complementing Programming Microsoft Visual Basic 2005 Core
Reference, this book covers a wide range of additional topics
and information critical to Visual Basic developers, including
Windows Forms, working with Microsoft ADO.NET 2.0 and
ASP.NET 2.0, Web services, security, remoting, and much more.
Packed with sample code and real-world examples, this book
will help developers move from understanding to mastery.
Programming Microsoft Windows Forms
Charles Petzold ● ISBN 0-7356-2153-5
Microsoft ASP.NET 2.0 Step by Step
George Shepherd ● ISBN 0-7356-2201-9
Programming Microsoft Web Forms
Douglas J. Reilly ● ISBN 0-7356-2179-9
Microsoft ADO.NET 2.0 Step by Step
Rebecca Riordan ● ISBN 0-7356-2164-0
Debugging, Tuning, and Testing Microsoft .NET 2.0
Applications
John Robbins ● ISBN 0-7356-2202-7
Programming Microsoft ASP.NET 2.0 Core Reference
Dino Esposito ● ISBN 0-7356-2176-4
For more information about Microsoft Press® books and other learning products,
visit: www.microsoft.com/books and www.microsoft.com/learning
Microsoft Press products are available worldwide wherever quality computer books are sold. For more information, contact your book or
computer retailer, software reseller, or local Microsoft Sales Office, or visit our Web site at www.microsoft.com/mspress. To locate your
nearest source for Microsoft Press products, or to order directly, call 1-800-MSPRESS in the United States. (In Canada, call 1-800-268-2222.)
Additional Resources for C# Developers
Published and Forthcoming Titles from Microsoft Press
Microsoft® Visual C#® 2005 Express Edition:
Build a Program Now!
Programming Microsoft Visual C# 2005
Core Reference
Patrice Pelland
Donis Marshall
●
ISBN 0-7356-2229-9
In this lively, eye-opening, and hands-on book, all you need is
a computer and the desire to learn how to program with Visual
C# 2005 Express Edition. Featuring a full working edition of the
software, this fun and highly visual guide walks you through a
complete programming project—a desktop weather-reporting
application—from start to finish. You’ll get an unintimidating
introduction to the Microsoft Visual Studio® development environment and learn how to put the lightweight, easy-to-use tools
in Visual C# Express to work right away—creating, compiling,
testing, and delivering your first, ready-to-use program. You’ll
get expert tips, coaching, and visual examples at each step of
the way, along with pointers to additional learning resources.
Microsoft Visual C# 2005 Step by Step
John Sharp
●
ISBN 0-7356-2129-2
Visual C#, a feature of Visual Studio 2005, is a modern programming language designed to deliver a productive environment for creating business frameworks and reusable objectoriented components. Now you can teach yourself essential
techniques with Visual C#—and start building components and
Microsoft Windows®–based applications—one step at a time.
With Step by Step, you work at your own pace through handson, learn-by-doing exercises. Whether you’re a beginning programmer or new to this particular language, you’ll learn how,
when, and why to use specific features of Visual C# 2005. Each
chapter puts you to work, building your knowledge of core
capabilities and guiding you as you create your first C#-based
applications for Windows, data management, and the Web.
●
ISBN 0-7356-2181-0
Get the in-depth reference and
pragmatic, real-world insights
you need to exploit the enhanced
language features and core capabilities in Visual C# 2005. Programming expert Donis Marshall
deftly builds your proficiency
with classes, structs, and other
fundamentals, and advances your
expertise with more advanced
topics such as debugging,
threading, and memory management. Combining incisive
reference with hands-on coding examples and best practices,
this Core Reference focuses on mastering the C# skills you need
to build innovative solutions for smart clients and the Web.
CLR via C#, Second Edition
Jeffrey Richter
●
ISBN 0-7356-2163-2
Complementing Programming Microsoft Visual C# 2005 Core
Reference, this book covers a wide range of additional topics
and information critical to Visual C# developers, including
Windows Forms, working with Microsoft ADO.NET 2.0 and
Microsoft ASP.NET 2.0, Web services, security, remoting, and
much more. Packed with sample code and real-world examples,
this book will help developers move from understanding to
mastery.
In this new edition of Jeffrey
Richter’s popular book, you get
focused, pragmatic guidance on
how to exploit the common language runtime (CLR) functionality in Microsoft .NET Framework
2.0 for applications of all types—
from Web Forms, Windows
Forms, and Web services to
solutions for Microsoft SQL
Server™, Microsoft code names
“Avalon” and “Indigo,” consoles,
Microsoft Windows NT® Service,
and more. Targeted to advanced developers and software
designers, this book takes you under the covers of .NET for an
in-depth understanding of its structure, functions, and
operational components, demonstrating the most practical
ways to apply this knowledge to your own development efforts.
You’ll master fundamental design tenets for .NET and get
hands-on insights for creating high-performance applications
more easily and efficiently. The book features extensive code
examples in Visual C# 2005.
Programming Microsoft Windows Forms
Charles Petzold ● ISBN 0-7356-2153-5
Programming Microsoft Web Forms
Douglas J. Reilly ● ISBN 0-7356-2179-9
CLR via C++
Jeffrey Richter with Stanley B. Lippman
ISBN 0-7356-2248-5
Debugging, Tuning, and Testing Microsoft .NET 2.0
Applications
John Robbins ● ISBN 0-7356-2202-7
Programming Microsoft Visual C# 2005
Framework Reference
Francesco Balena
●
ISBN 0-7356-2182-9
For more information about Microsoft Press® books and other learning products,
visit: www.microsoft.com/books and www.microsoft.com/learning
Microsoft Press products are available worldwide wherever quality computer books are sold. For more information, contact your book or
computer retailer, software reseller, or local Microsoft Sales Office, or visit our Web site at www.microsoft.com/mspress. To locate your
nearest source for Microsoft Press products, or to order directly, call 1-800-MSPRESS in the United States. (In Canada, call 1-800-268-2222.)

Similar documents

User Guide - Consumer Cellular

User Guide - Consumer Cellular JNQBJSFECFDBVTFPGJUTUPVDITDSFFOLFZCPBSE t$PQZSJHIUª-(&MFDUSPOJDT *OD"MMSJHIUT SFTFSWFE-(BOEUIF-(MPHPBSFSFHJTUFSFE USBEFNBSLTPG-((SPVQBOEJUTSFMBUFEFOUJUJFT"MM ...

More information