It’s the complete programming guide for intermediate and advanced developers who want to create .NET applications with Visual C++ and the Microsoft .NET Framework.. Visual C++ .NETalso i
Trang 1.NET, Sixth Edition
by George Shepherd and David Kruglinski
ISBN:0735615497
Microsoft Press © 2003 (1038 pages)
The in-depth reference that covers both classic, core Windows competencies and modern NET programming.
Trang 4Back Cover
Learn how to write solid Visual C++ NET code today with this comprehensive reference—the latest edition
of the industry’s most trusted text It’s the complete programming guide for intermediate and advanced
developers who want to create NET applications with Visual C++ and the Microsoft NET Framework.
Focusing on core programming techniques,
instructions, and solutions, this book shows you what’s new in Visual C++ NET and walks you through the
development life cycle with this powerful language Get in-depth coverage of the language’s syntax, tools, and APIs—along with expert advice and timesaving
techniques Take advantage of complete, task-based instruction plus your Microsoft Windows and C++
controls; Microsoft Win32 core memory
management; Windows message processing and multithreaded programming.
MFC document-view architecture: Menus, keyboard accelerators, the Rich Edit control, and property sheets; toolbars and status bars; a reusable frame
Trang 5Windows base class; separating the document from its view; reading and writing document—SDI and MDI applications; printing and print preview;
splitter windows and multiple views; context-sensitive Help; dynamic link libraries, MFC
programs without document or view classes.
COM, automation, ActiveX and OLE: Automation and IDispatch, data transfer with Clipboard and
OLE drag-and-drop, the Active Template Library, ActiveX Controls, OLEDB templates.
Programming for the Internet: TCP/IP, Winsock, and WinINet; dynamic HTML; ATL Server.
.NET and beyond: The NET platform; NET
interoperability with managed C++; Windows
Forms, GDI+, and NET Web services with C++; Microsoft ADO.NET.
About the Authors
Besides writing NET development tools for Suncfusion, George Shepherd teaches courses for DevelopMentor For Microsoft Press, he has coauthored several editions
Trang 6Programming with Microsoft Visual C++ NET, Sixth Edition
International directly at fax (425) 936-7329 Visit our Web site at
www.microsoft.com/mspress Send comments to:
mspinput@microsoft.com
Trang 7ActiveX, FoxPro, IntelliSense, Microsoft, Microsoft Press, MSDN, MS-Windows are either registered trademarks or trademarks of MicrosoftCorporation in the United States and/or other countries Other productand company names mentioned herein may be the trademarks of theirrespective owners
The example companies, organizations, products, domain names, e-mailaddresses, logos, people, places, and events depicted herein are
fictitious No association with any real company, organization, product,domain name, e-mail address, logo, person, place, or event is intended
Thank you Sandy Daston and Ted Shepherd—my family, for your supportwhile I wrote this book
Thank you, Denise Bankaitis As the project editor, you kept me going byreminding me of the importance of this project (a key C++ reference for.NET) and by coordinating the efforts of the rest of the team, which
includes Julie Xiao, Ina Chang, Danielle Bird, Juliana Aldous, Joel
Panchot, Carl Diltz, and Gina Cassill
Thank you, Julie Xiao, for keeping the manuscript accurate
Trang 8Thank you, Danielle Bird and Juliana Aldous As acquisition editors, yougot this project rolling and kept it on track
Thank you, Joel Panchot, for making sure the art in this book looks good.Thank you, Carl Diltz and Gina Cassill, for composing the manuscript andmaking it look great
I would also like to thank the folks at DevelopMentor, for providing a
wonderful environment and community for thinking and learning aboutmodern computing You guys are wonderful
Drill Bit
The year was 1914, and the electric motor was still newfangled Alongcame two bright young fellows, S Duncan Black and Alonzo G Decker,
who saw its possibilities They hooked one of the new motors up to a drill bit—a circular piece of metal with a chisel edge and cutting lips— and
the world's first electric drill was born It had a pistol grip and a triggerswitch, and it made boring holes a snap, but it didn't really catch on until
20 years later when, in 1946, Black and Decker designed a model forconsumers, and the tool took off As usual, necessity was the mother ofinvention and the father of a highly successful company—a bit of wisdomyou can drill home time and time again.[*]
At Microsoft Press, we use tools to illustrate our books for software
developers and IT professionals Tools very simply and powerfully
Trang 9extending their capabilities, precision, and reach From simple calipersand pliers to digital micrometers and lasers, these stylized illustrationsgive each book a visual identity, and a personality to the series Withtools and knowledge, there's no limit to creativity and innovation Our tag
line says it all: The tools you need to put technology to work.
The manuscript for this book was prepared and galleyed using MicrosoftWord Pages were composed by Microsoft Press using Adobe
FrameMaker+SGML for Windows, with text in Garamond and displaytype in Helvetica Condensed Composed pages were delivered to theprinter as electronic prepress files
Trang 10The release of the Microsoft Visual Studio NET (and Visual C++ NET inparticular) has underscored Microsoft's increasing focus on Internet
technologies, which are at the heart of the Microsoft NET architecture Inaddition to supporting the NET initiative, Visual C++ NET keeps all theproductivity-boosting features you're familiar with, such as Edit And
Continue, IntelliSense, AutoComplete, and code tips Visual C++ NETalso includes many new features such as managed code extensions for.NET programming, support for attributed code, and a more consistentdevelopment environment These features take Visual C++ NET to anew level This book will get you up to speed on the latest technologiesintroduced into Visual C++
Trang 11The technology churn we face these days is pretty impressive We wentfrom no computers on our office desktops to nearly everyone having acomputer running MS-DOS in the 1980s to nearly everyone running
Microsoft Windows by the mid-1990s The technology wheel is about toturn again In the late 1990s, everyone was developing Web sites byhand using tools such as raw Hypertext Markup Language (HTML),
Common Gateway Interface (CGI), Internet Server Application
Programming Interface (ISAPI) DLLs, Java, and Active Server Pages(ASP) In July 2000, Microsoft announced to the world that it would
change all that by betting the company on a new technology directionnamed NET
The current thrust of Microsoft is indeed NET For a number of years, it'sbeen possible to build a Web site by setting up a server somewhere,getting an IP address, and putting up some content Anyone with the URL
of your site can surf there and check it out Commercial enterprises havebeen taking advantage of the Web by posting information that's useful tocustomers The Web has also become an invaluable research tool andefficient news broadcast medium
The computing world of the near future will involve the Web heavily
However, rather than just having human eyeballs look at Web sites,
computers themselves will look at Web sites That is, Web sites will beprogrammable through Web services The NET vision also pushes theresponsibility of providing a rich user interface out to the server
With so much emphasis on Web services and server-based user
interfaces, it might seem that standalone applications and client-side userinterface scenarios—normally the realm of tools such as the MicrosoftFoundation Class Library (MFC)—will be left in the dust But the need forrich client-side user interfaces is unlikely to go away Many thought thatthe advent of the PC and distribution technologies would spell the end ofcentralized processing on mainframes and minicomputers It turns outthat PCs and distribution technologies only added to the available
Trang 12interfaces provided by the server only adds to the options available tosoftware developers Rich client-side user interfaces will continue to beviable for many types of applications, running alongside other
applications that use other kinds of user interfaces (such as server-generated user interfaces)
MFC is a mature and well-understood technology that's accompanied by
a host of third-party extensions For at least a little while longer, MFCrepresents the most effective way to write full-featured standalone
applications A good portion of this book will focus on MFC-style
development, but we'll also cover Windows Forms—the NET way towrite client-side user interfaces
Of course, the next question is: Where does this leave COM? COM hassolved many problems related to distributed processing, but it has someserious shortcomings—mostly centered around component versioningand type information Microsoft's NET vision is based on the commonlanguage runtime The runtime takes the place of COM as the
interoperability standard within NET We'll cover NET and the commonlanguage runtime in depth in Part VI of this book
COM and the common language runtime represent different approaches
to component architecture, but Microsoft has taken great care to ensure aseamless coexistence The interoperability path between COM and theruntime is smooth in most cases Within the NET world, you probablywon't find yourself using COM as a component architecture However,you might find yourself using Active Template Library (ATL) Server, which
is a high-performance means of writing Web sites
I've updated the coverage of ATL and MFC in this edition of the bookbecause you'll still find it very useful More important, I'll show you how toleverage your heritage code (sounds better than "legacy code," doesn'tit?) as you move into the NET world
Trang 13The NET platform has introduced a new C++-like language named C#.C# is a curly-brace-oriented language without all the headaches of C++.Much of C#'s appeal is due to the fact that it's missing some of the moreproblematic elements of C++ (such as raw pointer management) whilemaintaining the useful features (such as virtual functions) The C#
compiler eventually emits managed code—the kind that runs under thecommon language runtime
However, the entire world isn't going to switch over to C# overnight
There's just too much C++ code out there to convert Also, it will take abit of time for developers to become fully comfortable with C# In themeantime, NET has introduced extensions to C++ for producing
managed code (code that runs under the common language runtime).Managed Extensions for C++ will help ease the burden of developingsoftware for the NET platform because they allow you to quickly updateexisting C++ code to work with NET Getting the managed code features
in C++ means sprinkling your code with various keywords In the end, C#and managed C++ boil down to the same executable code once the
compilers are done with it In the NET world, you'll probably find yourselfwriting new components using C# while using managed C++ to add NETfeatures to your existing code base
Trang 14In recent years, we've seen a great deal of interest in the Java
programming language and platform Java became a great boon for
Internet developers by providing a useful means of distributing client userinterfaces (through Java applets) and by providing enterprise solutionsthrough Java Enterprise Edition Now, NET has become the best Internetdevelopment platform available today Unlike the Java platform, whichrequires that you write all your code using the Java syntax, NET oftenlets you use multiple syntaxes to arrive at the same machine instructionset You can use C++ (the main focus of this book) and its managed
extensions, Visual Basic NET, C#, and even a host of third-party NETlanguages to write your programs Once you develop your source code, it
is compiled to intermediate language and then eventually machine codebefore it runs Because NET code is managed by a runtime, you getbenefits such as garbage collection and better code security
Trang 15Visual C++ NET, with its sophisticated application framework and
support for NET, is for professional programmers, and so is this book I'llassume that you're proficient in the C language—you can write an if
statement without consulting the manual And I'll assume that you'vebeen exposed to the C++ language—you've at least taken a course orread a book even if you haven't written much code You might comparelearning C++ to learning French You can study French in school, but youwon't be able to speak fluently unless you go to a French-speaking
country and start talking to people
The Visual C++ wizards save you time and improve accuracy, but
programmers must understand the code that the wizards generate and,ultimately, they must understand the structure of the MFC and ATL
libraries, the inner workings of the Windows operating system, and how.NET works I won't assume, however, that you already know Windowsand NET programming I'm sure that proficient C programmers can learnWindows the MFC way and the NET way It's more important to knowC++ than it is to know the Win32 application programming interface
based applications
(API) You should, however, know how to run Windows and Windows-If you're already experienced with the Win32 API or with the MFC library,there's something in this book for you, too You'll learn about new
features such as the Multiple Top-Level Interface (MTI) and the VisualC++ NET wizards If you haven't already figured out the ComponentObject Model (COM), this book presents some important theory that willget you started on understanding ActiveX controls You'll also learn aboutATL Server and OLE DB templates And you'll learn about C++
programming for the Internet (including Dynamic HTML) Finally, thisbook includes hard-to-find coverage of the new managed C++
extensions
Trang 16It's impossible to cover every aspect of Windows and NET programming
in a single book I've excluded topics that depend on special-purposehardware and software, such as MAPI, TAPI, and communications portaccess I'll cover using ActiveX controls in an application and writing
Richter's Applied NET Programming (Microsoft Press, 2002)
Trang 17When you're starting with Visual C++ NET, you can use this book as atutorial by going through it sequentially Later, you can use it as a
reference by looking up topics in the table of contents or in the index.Because of the tight interrelationships among many application
framework elements, it wasn't possible to cleanly isolate each concept inits own chapter, so the book isn't organized as an encyclopedia Whenyou use this book, you'll definitely want to keep the online help availablefor looking up classes and member functions
If you're experienced with earlier versions of Visual C++, scan Part I for
an overview of new features Then skip the basic MFC coverage in Part IIbut read the more advanced coverage Also, be sure to read the NETcoverage Much of the software development community's efforts areheading in this direction, and Visual C++ NET fully supports the NETprogramming model
Trang 18As the table of contents shows, this book has six parts and an appendixsection
Part I: Windows, Visual C++ NET, and Application
Framework Fundamentals
This part tries to strike a balance between abstract theory and practicalapplication After a quick review of Win32 and the Visual C++ NETcomponents, you'll be introduced to the MFC application framework andthe document-view architecture You'll look at a simple "Hello, world!"program built with the MFC library classes that requires only 30 lines ofcode
Part II: MFC Essentials
The MFC library documentation presents all the application frameworkelements in quick succession, with the assumption that you're at leastfamiliar with the original Windows API In Part II of this book, you'reconfined to one major application framework component—the view,which is really a window You'll learn what experienced Windows
programmers know already, but in the context of C++ and the MFClibrary classes You'll use the Visual C++ NET tools that eliminate much
of the coding drudgery that early Windows programmers had to endure.This part covers a lot of territory, including graphics programming withbitmaps, dialog data exchange, ActiveX control usage, 32-bit memorymanagement, and multi-threaded programming The exercises will helpyou to write reasonably sophisticated Windows-based programs, butthose programs won't take advantage of the advanced application
framework features
Part III: MFC's Document-View Architecture
This part introduces the real core of application framework programming
—the document-view architecture You'll learn what a document is
Trang 19Along the way, you'll learn about command message processing, toolbarsand status bars, splitter frames, and context-sensitive help You'll also beintroduced to the Single Document Interface (SDI), the Multiple
Document Interface (MDI), and the Multiple Top-Level Interface (MTI),which is the current standard for Windows-based applications such asMicrosoft Word
Part III also discusses dynamic link libraries (DLLs) written with the MFClibrary You'll learn the distinction between an extension DLL and a
regular DLL
Part IV: COM, Automation, ActiveX, and OLE
COM itself deserves more than one book Part IV will get you started inlearning fundamental COM theory from the MFC point of view You'll
progress to Automation, which is the link between C++ and Visual Basicfor Applications (VBA) You'll also become familiar with uniform data
transfer, and you'll learn the basics of compound documents and
embedded objects You'll learn about the ATL class library support forOLE DB
Part V: Programming for the Internet
This part starts with a technical Internet tutorial that covers the TCP/IPprotocol and the fundamentals of Internet programming You'll learn how
to develop servers using ATL Server, and you'll learn how to program forDynamic HTML
Part VI: NET and Beyond
The Internet is evolving as the next frontier for software development.The Internet is no longer just about building Web sites for people to
simply look at—it's about Web sites that people can program The wire's
Trang 20supports both these notions, along with a new way to write client userinterfaces: Windows Forms Part VI covers what NET is all about andwhat you can do with it as a platform Included here are chapters on thecommon language runtime and managed code, programming managedcomponents using C++, ASP.NET, and ADO.NET
Appendixes
Appendix A contains a list of message map macros and their
corresponding handler function prototypes The code wizards availablefrom Class View usually generate this code for you, but sometimes youmust make manual entries
Appendix B offers a description of the MFC application framework'sruntime class information and dynamic creation system This is
independent of the runtime type information (RTTI) feature that is now apart of ANSI C++
Trang 21A few old computers out there are still running Windows 3.1 However,there's not much point in spending money writing new programs for
obsolete technology This edition of Programming with Microsoft VisualC++ NET is about 32-bit programming for Windows 98/Me and WindowsNT/2000/XP using the Win32 API If you really need to do 16-bit
programming, I suggest that you find an old copy of the second edition ofthis book
Trang 22To use this book, you'll need to have Visual C++ NET or Visual Studio.NET installed on your computer Any computer that satisfies the
minimum requirements for Visual C++ NET will work effectively withmost of the examples in this book Be aware that Windows XP HomeEdition and Windows NT 4.0 don't support the hosting of ASP.NET Webapplications with the NET Framework You can build these projects onthese operating systems, but you'll need to upload the projects to aproperly configured host to execute them
Trang 23You can find the sample files on the book's companion CD, along withother supplemental content To access the files on the CD, insert the disc
in your computer's CD drive and make a selection from the menu thatappears If the AutoRun feature is not enabled on your system (if a menudoesn't appear when you insert the CD into the drive), run StartCD.exe inthe root folder of the companion CD Installing the sample files on yourhard disk requires approximately 60 MB of disk space If you have troublerunning any of these files, refer to the text in the book that describes
these programs
With a conventional C-language program using the Windows API, thesource code files tell the whole story With the MFC library applicationframework, things are not so simple The MFC Application Wizard
generates much of the C++ code, and the resources originate in the
resource editors The examples in the early chapters of this book includestep-by-step instructions for using the tools to generate and customizethe source code files You'd be well advised to walk through those
instructions for the first few examples—there's very little code to type Forthe middle chapters, use the code from the sample files but read throughthe steps to appreciate the role of the resource editors and the wizards.For the final chapters, not all the source code is listed You'll need to
examine the sample files for those examples
Aside from the sample files, the book's supplemental content includestwo eBook installations: a standalone eBook installation and a VisualStudio Help eBook installation The standalone eBook installation allowsyou to access an electronic version of the print book directly from yourdesktop The Visual Studio Help eBook installation allows you to accessthe second electronic version of the print book directly from the VisualStudio NET help system
Trang 24In addition to the companion CD, this book also includes a DVD with anevaluation copy of Visual Studio NET Professional This evaluation copycan help you follow the examples in this book and get you started
learning Visual C++ NET, but the software will expire and stop working
60 days after you install it You can learn more about this evaluation copyand its system requirements at
http://msdn.microsoft.com/vstudio/productinfo/trial.asp Note that no
product support is available for the trial version
Trang 25One of the biggest selling points behind MFC during the 1990s was thegreat class libraries available for extending the framework With WindowsForms on the horizon, it's time to keep an eye out for class libraries thatextend Windows Forms
MFC and its extensions were confined to the C++ language, but the NETcommon language runtime offers a variety of syntaxes for writing
Windows Forms, including C#, Visual Basic NET, and Managed C++.Syncfusion, a company based in Cary, North Carolina, provides a widevariety of NET tools to make programming for NET easier Syncfusion'sEssential Suite includes components to make your NET Windows Formsapplications more solid and polished You can download a fully functional15-day trial version from http://www.syncfusion.com, as well as the
Essential Suite Interactive Showcase, an application that shows severalSyncfusion components in action The components run under the
common language runtime, so they work with Managed C++ as well aswith C# and Visual Basic NET
Trang 26Every effort has been made to ensure the accuracy of this book and thecontents of the companion CD Microsoft Press provides corrections forbooks at http://www.microsoft.com/mspress/support/
To connect directly to the Microsoft Press Knowledge Base and submit aquery, go to: http://www.microsoft.com/mspress/support/search.asp
If you have comments, questions, or ideas regarding this book or thecompanion content or questions that are not answered by querying theKnowledge Base, please send them to Microsoft Press using postal mail
Trang 27Part I: Windows, Visual C++ NET, and Application Framework Fundamentals
Trang 28Chapter 1: Windows and Visual C++ NET
Chapter 2: The Microsoft Foundation Class Library ApplicationFramework
Trang 29Chapter 1: Windows and Visual C++ NET
In the early nineties, the battle was for the desktop operating system.Now that battle is over, and Microsoft Windows runs on the vast majority
of personal computer systems This chapter summarizes the low-levelWindows programming model (Win32, in particular) and shows you howthe Microsoft Visual C++ NET components work together to help youwrite applications for Windows Along the way, you might learn some newthings about Windows as well
Trang 30When you write an MS-DOS–based application in C, the only absolute
requirement is a function named main The operating system calls main
when the user runs the program, and from that point on, you can use anyprogramming structure you want If your program needs to get user
creating the application's main window, which must have its own code toprocess messages that Windows sends it An essential difference
between a program written for MS-DOS and a program written for
Windows is that an MS-DOS–based program calls the operating system
to get user input but a Windows-based program processes user input viamessages from the operating system
Trang 31programs For example, a WM_CREATE message is sent when a
window is being created, a WM_LBUTTONDOWN message is sent when the user presses the left mouse button, a WM_CHAR message is sent when the user types a character, and a WM_CLOSE message is sent
when the user closes a window All messages have two 32-bit
parameters that convey information such as cursor coordinates, key
code, and so forth Windows sends WM_COMMAND messages to the
appropriate window in response to user menu choices, dialog box buttonclicks, and so on Command message parameters vary depending on thewindow's menu layout You can define your own messages, which yourprogram can send to any window on the desktop These user-definedmessages actually make C++ look a little like Smalltalk
Don't worry yet about how these messages are connected to your code.That's the job of the application framework Be aware, though, that theWindows message processing requirement imposes a lot of structure onyour program Don't try to force your Windows-based programs to looklike your old MS-DOS programs Study the examples in this book, andthen be prepared to start fresh
The Windows Graphics Device Interface
Many MS-DOS programs write directly to the video memory and the
printer port The disadvantage of this technique is the need to supplydriver software for every video board and every printer model Windowsintroduced a layer of abstraction called the Graphics Device Interface(GDI) Windows provides the video and printer drivers, so your programdoesn't need to know the type of video board and printer attached to thesystem Instead of addressing the hardware, your program calls GDI
functions that reference a data structure called a device context.
Windows maps the device context structure to a physical device andissues the appropriate input/output instructions The GDI is almost as fast
as direct video access, and it allows different applications written for
Windows to share the display
Later in the book, we'll look at GDI+ As you might guess, GDI+ is thesuccessor to GDI The services of GDI+ are exposed through a set of
Trang 32independent path objects, scalable regions, alpha blending, and multipleimage formats
Resource-Based Programming
To do data-driven programming in MS-DOS, you must either code thedata as initialization constants or provide separate data files for yourprogram to read When you program for Windows, you store data in aresource file using a number of established formats The linker combinesthis binary resource file with the C++ compiler's output to generate anexecutable program Resource files can include bitmaps, icons, menudefinitions, dialog box layouts, and strings They can even include
custom resource formats that you define
You use a text editor to edit a program, but you generally use WYSIWYG(what you see is what you get) tools to edit resources If you're laying out
a dialog box, for example, you select elements (buttons, list boxes, and
so forth) from an array of icons called a control palette, and you position
and size the elements with the mouse Visual C++ NET has graphicsresource editors for all standard resource formats
Memory Management
With each new version of Windows, memory management gets easier Ifyou've heard horror stories about locking memory handles, thunks, andburgermasters, don't worry That's all in the past Today you simply
allocate the memory you need, and Windows takes care of the details.Chapter 10 describes current memory management techniques for
Win32, including virtual memory and memory-mapped files
Dynamic-Link Libraries
In the MS-DOS environment, all of a program's object modules are
statically linked during the build process Windows allows dynamic
Trang 33libraries (DLLs), which saves memory and disk space Dynamic linkingincreases program modularity because you can compile and test DLLsseparately
Designers originally created DLLs for use with the C language, and C++has added some complications The MFC library developers succeeded
in combining all the application framework classes into a few ready-builtDLLs This means that you can statically or dynamically link the
application framework classes into your application In addition, you cancreate your own extension DLLs that build on the MFC DLLs Chapter 22includes information about creating MFC extension DLLs and regularDLLs
The Win32 Application Programming Interface
Early Windows programmers wrote applications in C for the Win16
application programming interface (API) Of course, today few folks write16-bit applications Most developers write applications using the Win32API The main difference between the Win16 functions and the Win32functions is that in the latter, many of the parameters have been widened
So while the Windows API has changed over the years (and continues tochange), developers using the MFC library have remained insulated fromthese changes because the MFC standard was designed to work witheither Win16 or Win32 underneath
Trang 34Visual C++ NET consists of several complete Windows application
language Windows-based programs using only the Win32 API C-
application development
Finally, Visual C++ NET includes the Active Template Library (ATL),
which you can use to develop ActiveX controls ATL programming is
neither Win32 C-language programming nor MFC programming, and it'scomplex enough to deserve its own book However, we'll touch on ATLdevelopment in this book ATL will probably find itself most at home withinthe high-performance Web server environment
The first section of this book is about C++ programming within the MFClibrary application framework that's part of Visual C++ NET You'll beusing the C++ classes documented in the Microsoft Visual C++ MFCLibrary Reference included in the Visual Studio NET documentation, andyou'll also be using application framework–specific Visual C++ NET toolssuch as Class View
Note Use of the MFC library programming interface doesn't cut you
off from the Win32 functions In fact, you'll almost always needsome direct Win32 calls in your MFC library programs
A quick run-through of the Visual C++ NET components will help you getyour bearings before you zero in on the application framework Figure 1-1shows an overview of the Visual C++ MFC application build process
Trang 35MFC application build process
Visual C++ NET and the Build Process
Visual Studio NET is a suite of developer tools that includes Visual C++.NET The Visual Studio NET integrated development environment (IDE)
is shared by several tools, including Visual C++ NET, Microsoft VisualC#, and Microsoft Visual Basic NET The IDE has come a long way fromthe original Visual Workbench, which was based on QuickC for Windows.Docking windows, configurable toolbars, and a customizable editor thatruns macros are now part of Visual Studio NET The online help system(now integrated with the MSDN Library viewer) works like a Web
browser Figure 1-2 shows Visual C++ NET in action
Trang 36Figure 1-2: Visual C++ NET windows.
If you've used earlier versions of Visual C++, you already understandhow Visual C++ NET operates (although some of the menus might have
changed) But if you're new to IDEs, you'll need to know what a project is.
A project is a collection of interrelated source files that are compiled andlinked to make up an executable Windows-based program or a DLL
Source files for each project are generally stored in a separate
subdirectory A project also depends on many files outside the projectsubdirectory, such as include files and library files
Trang 37project per solution To work on an existing project, you tell Visual C++.NET to open the SLN file, and then you can edit and build the project.Visual C++ NET creates some intermediate files too Table 1-1 lists thefiles that Visual C++ NET generates in the solution
Trang 38Windows common controls
Each project usually has one text-format resource script (RC) file thatdescribes the project's menu, dialog box, string, and accelerator
resources The RC file also has #include statements to bring in resources
from other subdirectories These resources include project-specific items,such as bitmap (BMP) and icon (ICO) files, and resources common to allVisual C++ NET programs, such as error message strings Editing the
RC file outside the resource editors is not recommended The resourceeditors can also process EXE and DLL files, so you can use the
Clipboard to "steal" resources, such as bitmaps and icons, from otherWindows-based applications
The C/C++ Compiler
The Visual C++ NET compiler can process both C source code and C++source code It determines the language by looking at the source code'sfilename extension The C extension indicates C source code, and theCPP or CXX extension indicates C++ source code The compiler is
compliant with all ANSI (American National Standards Institute)
standards, including the latest recommendations of a working group onC++ libraries, and has additional Microsoft extensions Templates,
exceptions, and runtime type information (RTTI) are fully supported inVisual C++ NET The C++ Standard Template Library (STL) is also
included, although it is not integrated into the MFC library
The Source Code Editor
Visual C++ NET includes a sophisticated source code editor that
supports many features such as dynamic syntax coloring, auto-tabbing,keyboard bindings for a variety of popular editors (such as VI and
EMACS), and pretty printing Starting with Visual C++ 6, the environmentincludes a feature named AutoComplete If you have used any of theMicrosoft Office products or Visual Basic, you might already be familiar
Trang 39runtime library code, and Windows code It then writes the project's EXEfile An incremental link option minimizes the execution time when onlyminor changes have been made to the source files The MFC header
Visual C++ and Visual Basic debuggers and adding many new features.These new features include the following:
Cross-language debugging Visual Studio NET lets you debug
projects that are part of the same solution even if they're written
in different languages
Attachment to a running program Visual Studio NET lets you
Trang 40Remote debugging Visual Studio NET supports remote
debugging That is, you can attach to a program that's running onanother server
Debugging of ASP.NET Web applications ASP.NET files are
compiled, so they get the same treatment during debugging thatother languages get This makes it much easier than before todebug Web applications
.NET Framework classes for debugging and code tracing The
.NET Framework classes make it easy to instrument and put
trace statements in your code Because these classes are
managed code, you can run them within managed C++ code.Figure 1-3 shows the integrated debugger in action