Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner focuses on teaching first time programmers how to program using Visual C++ 2005 Express Edition as a found
Trang 3Microsoft Visual C++ 2005 Express Edition
Programming for the Absolute Beginner focuses on
teaching first time programmers how to program using Visual C++ 2005 Express Edition as a foundation
language Written for the entry-level user, the book
assumes no prior programming or scripting experience Whether you are a new or experienced C++ developer, this book delivers the tools and libraries you need to write a variety of programs Written in a straight-
diverse fields, including the computer game industry, healthcare, and embedded systems development He is passionate about demystifying technology and has long been a mentor to students new to programming.
Jerry Lee Ford, Jr is an author, educator, and an IT
professional with more than 16 years of experience in information technology, including roles as an
automation analyst, technical manager, technical
support analyst, automation engineer, and security
analyst Jerry has a master's degree in business
administration from Virginia Commonwealth University
Trang 4books, including Microsoft Visual Basic 2005 Express
Edition Programming for the Absolute Beginner,
VBScript Professional Projects, Microsoft Windows Shell Scripting and WSH Administrator's Guide, Learn
Trang 5The Thomson Course Technology PTR logo and related trade dress aretrademarks of Thomson Course Technology and may not be used withoutwritten permission.
Trang 6Information contained in this book has been obtained by Thomson
Course Technology PTR from sources believed to be reliable However,because of the possibility of human or mechanical error by our sources,Thomson Course Technology PTR, or others, the Publisher does notguarantee the accuracy, adequacy, or completeness of any informationand is not responsible for any errors or omissions or the results obtainedfrom use of such information Readers should be particularly aware of thefact that the Internet is an ever-changing entity Some facts may havechanged since this book went to press
Educational facilities, companies, and organizations interested in multiplecopies or licensing of this book should contact the publisher for quantitydiscount information Training manuals, CD-ROMs, and portions of thisbook are also available individually or can be tailored for specific needs.ISBN: 1-59200-816-x
Trang 7About the Author
Aaron Miller is a software developer with more than 15 years of
experience He has worked in a number of diverse fields, including thecomputer game industry, healthcare, and embedded systems
development He is passionate about demystifying technology and haslong been a mentor to students new to programming
Jerry Lee Ford, Jr is an author, educator, and an IT professional with
more than 16 years of experience in information technology, includingroles as an automation analyst, technical manager, technical supportanalyst, automation engineer, and security analyst Jerry has a master'sdegree in business administration from Virginia Commonwealth
University in Richmond, Virginia He also is the author of 15 other books,
including Microsoft Visual Basic 2005 Express Edition Programming for
Trang 8Windows Shell Scripting and WSH Administrator's Guide, Learn VBScript
in a Weekend, Microsoft Windows Shell Scripting for the Absolute
Beginner, Learn JavaScript in a Weekend, and Microsoft Windows XP Professional Administrator's Guide He has more than five years of
experience as an adjunct instructor teaching networking courses in
information technology Jerry lives in Richmond, Virginia, with his wife,Mary, and their children, William, Alexander, and Molly
Trang 9Whether you're a student, a game mod maker, or just someone who isinterested in learning to program, Microsoft Visual C++ 2005 ExpressEdition is a powerful way to introduce yourself to the C++ programminglanguage The Express Edition comes complete with the tools that makecreating, managing, and maintaining your projects simple and easy Ithelps you to become a more effective programmer by allowing you to putprojects together quickly, focus only on the important details, and get thehints and help you need when you need them
Microsoft Visual C++ 2005 Express Edition is part of the Microsoft VisualStudio 2005 line of products This powerful series includes Visual Basic,Visual C#, and Visual J# The Microsoft Visual Studio 2005 line has
become the top choice of today's professional programmer, garnering
awards from leading industry magazines such as PC Week and PC
Magazine You can find versions of each product being used throughout
the software engineering world By mastering the Express Edition toolset,you give yourself a competitive edge when it comes to learning one of thelatest technologies available to the programming industry
Trang 10With so many products to choose from, it might be hard to decide whichbest fits your needs Each language has its strengths But if you are
looking for the most powerful among them, Microsoft Visual C++ 2005Express Edition is it
The Express Edition allows you to employ the NET Framework, a newcoding technology that is growing in popularity The NET Frameworkfacilitates the construction of applications that can be run on Windows,smart devices like the PocketPC, and the Web With it, you can writecode modules in Visual C++ and use them in Visual Basic, Visual C#, orVisual J# The NET Framework features many new enhancements tomake your programs more robust and secure
Microsoft has designed this new version of Visual C++ to be the mostflexible and extensible of its languages It is aimed at writing robust
Windows and server-side applications Unlike the other NET languages,Visual C++ lets you directly access the hardware that your machine isrunning on, enabling you to maximize its full capabilities
Visual C++ 2005 Express Edition has many features and capabilities,some of which are tricky to master For this reason, Microsoft has put astrong effort into making the product easier to use The core code libraryhas been redesigned, allowing you to build your programs on a strongerfoundation that is easier to read and write Your own code will now bemore streamlined thanks to context-sensitive technology that does abetter job of understanding what you type Improvements in online help,project management, and the ever-popular IntelliSense reference
technology place a wealth of information at your fingertips
New features such as WinForms allow you to rapidly create a programgraphically You can drag and drop buttons, menus, and other interfaceelements onto a page to quickly get a program up and working Then youcan have the editor generate code for you with a few mouse clicks
Trang 11This book has been designed with several audiences in mind:
Those who want to program but have no experience doing soThose who know C++ but not NET (there's a world of difference)Those who want to develop applications quickly
Those who want to become more comfortable with NET, possiblybranching out to other languages such as C#
No Experience Necessary
This book assumes no prior knowledge of C++ It is an excellent startingplace if you're interested in learning the basics of the language and how
to build your own applications Each chapter starts with a broad overview
to ground you in the concepts being discussed Then it moves on to astep-by-step approach, explaining precisely what you need to do to getevery chapter program up and running By the end, you will be able tobuild your own programs from scratch and be well on your way to
becoming a competent C++ programmer
C++ Programmers New to NET
If you're experienced with programming in C++, be prepared for somechanges Writing code meant solely to run in the Windows environment isquite different from writing code to conform to the NET Framework'srequirements Even simple commands such as cout and cin have
become obsolete as Microsoft has focused on a system of creating codethat works reliably on a diverse range of platforms in addition to the Web.This book reacquaints you with C++ from the NET perspective It startssimply, concentrates on the basics of the NET Framework, and givesexamples in each chapter that reinforce the new paradigm By the end ofthis book, C++ programmers will have a good grasp of the ground-levelbasics and the confidence to begin deepening their exploration of the
Trang 12Developing Applications Quickly
Visual C++ 2005 Express Edition has been designed with productivity inmind The improved interface features several optimizations that makethe tools you need easier to find and use The all-new inclusion of
WinForms and several interface elements give you the power to rapidlydesign and lay out a potential application to see how it will look and feel.And when you're ready to build a fully functional program, the automatedcode generation features make it easy not only to drop in functionalityright where you need it, but also to test and improve your code each step
of the way With Microsoft Visual C++ 2005 Express Edition, you'll haveprograms up and working in minutes
Branching Out from NET
The core concept behind the entire Microsoft Visual Studio 2005 productline is cross compatibility between each of its languages The interface isuniform across the entire product line Because of this, should you decide
to expand your programming knowledge to cover Visual Basic, Visual C#,
or Visual J#, you will be branching out into using a product whose lookand feel you will already be familiar with Almost all the productivity-
enhancing and help features you will come to rely on in the Visual C++
2005 Express Edition will be readily available to you in these other VisualStudio products
Learning Visual C++ 2005 Express Edition offers further advantages Itoffers you an excellent springboard if you want to dive into the emergingrealm of C# development By the time you finish reading this book, youshould be comfortable with many of the same language structures andmethodologies that exist in C# This advantage allows you to concentrate
on the differences, thus accelerating the process of mastering both
languages
Trang 13You need to download Visual C++ 2005 Express Edition, available at thetime of this writing at http://lab.msdn.microsoft.com/express/visualc/.Alternatively, you can order a CD online from
http://lab.msdn.microsoft.com/vs2005/get/order/ In addition, you needthe following:
PC with a speed of at least 600MHz (1GHz recommended)
Windows XP, Windows 2000, or Windows 2003 Server
At least 128MB of Random Access Memory (RAM) (256MBrecommended)
At least 1.3GB of hard disk space
A display capable of 800×600 256 colors (1024×768 high color,16-bit recommended)
Microsoft mouse or compatible pointing device
A CD or DVD drive is required
Trang 14As you learn the language, my goal is to make the process fun, fast, andinteresting Every chapter includes games for the sample programs Atthe end of each chapter are challenges to help you hone your skills Thisbook also includes the following special elements:
Hint This is where I'll put hints or advice I've learned that can help
make your programs lean and mean and Visual C++ easier touse
Trang 15Chapter 1: An Introduction to Visual C++ 2005 Express
Trang 16With the release of Visual C++ 2005 Express Edition, Microsoft has
turned its attention to the needs of a new generation of programmers.The changes that Microsoft has made respond to the growing need ofprogrammers to quickly develop applications that are robust and able towork on multiple platforms With the seamless integration of the NETFramework, Visual C++ developers now enjoy the ability to create
applications that can run under the Windows operating system, as thecore of a server-side applet, or on many other platforms
This chapter begins your process of becoming a C++ programmer bycovering information that orients you to the C++ language, Visual C++,and the NET Framework When you're finished with this chapter, you'll
be ready to dive right in and create your first program By developing thisapplication, you'll gain a sense of the steps required to build a programand some of the strengths of developing using the Visual C++ toolset
In this chapter, you learn the following:
What Visual C++ 2005 Express is and what it can do
What NET is and how it works with Visual C++ 2005 ExpressWhat Visual Studio is and how Visual C++ fits into it
What the five basic steps are to building a Visual C++ application
Trang 17In this chapter, as with each chapter to follow, you learn how to create aVisual C++ application Each chapter provides a simple, fun way to learnthe language, introduce you to the various tools you need to create eachprogram, and give you a foundation on which to expand your knowledge
of C++ and the NET Framework
The application that you learn to create in this chapter is a simple
program called the Joke Machine Its purpose is to display to the usertwo jokes It is a fully functional Windows application, complete withbuttons for closing and minimizing the program, buttons associated witheach of the two jokes, and a text box used for displaying the jokes
Although the program is easy to develop, it illustrates the powerful
techniques for designing and programming Windows applications
When you first launch the Joke Machine, it appears in a window, asshown in Figure 1.1
Figure 1.1: The starting screen of the Joke
Machine
Trang 18
Figure 1.2: Clicking the first button triggers the first
joke
Figure 1.3 displays the result of clicking the second joke
Trang 19Figure 1.3: Clicking the second button triggers the second
joke
Trang 20The process of learning something new can be challenging, but it canalso be fun and deeply rewarding This chapter provides a broad
overview that will enable you to advance to later chapters
This chapter starts with a quick overview of Visual C++ and the core
concepts that go into making a program Next, it offers a quick dash ofhistory The chapter then covers some of the capabilities of Visual C++
2005 Express and where it fits into the Microsoft family of Visual Studioproducts After that, it moves into foundational information about NET.Finally, you experience creating your first Windows application
As you read this book, don't worry about trying to understand each detail.You might be using commands before you understand what they mean.This might be somewhat confusing at first, but if you focus on the bigpicture, you'll progress to understanding the details in no time
Each program you design and build gives you a better understanding ofthe tools necessary to create Windows applications You also gain a solidunderstanding of the code behind these applications and the principlesthat make up the C++ language
Trang 21EXPRESS
The visual in Visual C++ means that you can see and interact with all the
tools and code necessary for making an application It has also come tomean the process by which you create the visual portion of an applicationthat your users will see, also known as the Graphical User Interface
(GUI) Within the name, C++ refers to the language that you use to buildyour applications: the C++ programming language
applications without assistance, just like any other tool, game, or
application you have used
You create applications by compiling code You then translate this codeinto a format that your computer can read When you're ready to run aprogram, you only need to locate it on your computer system and click itsicon
Hint Compiling is the process of translating the English-like code
statements that make up an application into a format that a
computer can understand and execute
The C++ programming language has earned a reputation for being one ofthe most powerful and flexible languages available Microsoft Visual C++gained immediate popularity by offering these advantages to
programmers in a set of consolidated tools that were easy to use andunderstand As a result, Visual C++ quickly set a standard as one of themost widely used compilers among professionals the world over Bothlarge and small businesses use it to create Windows and server-sideWeb components, and game companies use it to create the latest
Trang 22Visual C++ has gained widespread popularity for a number of reasons.Three of the most important reasons include its support of the following:
design your GUI affects how easily your users find the program they
want, and this impacts how happy they are with the program overall
One of the best things about using Visual C++ 2005 Express is how
quickly and efficiently you can create a professional-looking GUI Whenyou start a new project, Visual C++ gives you a new blank window towork with Next to this window is a series of readily usable interface
components, such as check boxes, text boxes, menus, and buttons Youcan add them to your program by clicking on the element you want anddragging it on top of the window Visual C++ then lets you position andlayer them as needed The result is a working application that you cancreate in minutes
Rapid Application Development
Visual C++ is a Rapid Application Development (RAD) tool RAD refers tothe process of being able to quickly get the skeleton of a program up andrunning By dragging and dropping interface components onto a window,you can quickly allow users to preview the way your program will lookand feel Because your users can give you feedback early, RAD can saveyou work Rather than spending many long hours writing and testing yourprogram, Visual C++'s RAD capabilities allow you to respond to userexpectations early and deliver a product that is more suited to their liking
Trang 23programmers quickly create a mockup of an application's GUIfor initial review by the users for whom the application is
intended to demonstrate how the application will ultimately lookand operate
Object-Oriented Programming
One of great strengths of the C++ language is that it is an Object-Oriented Programming (OOP) language OOP is a way of designing andbuilding applications out of small, organized components Using it, youcan create an application whose parts are well tested, self-contained, andable to be used in other applications These components can even beextended in new and different ways, saving you time in writing new code.Whereas the GUI relates to what you see, OOP involves how you buildyour program and how all of its parts interact From the perspective ofOOP, you build a program out of classes Classes essentially act as
independent units of computer code that can store, process, and provideaccess to information When the program runs, the class becomes anobject, and the information given to it can then be used to control how theobject works In OOP, the interaction of multiple objects is what givesyour program functionality
In Visual C++, for example, when you drag a button onto a window, thebutton is a self-contained component It has everything it needs to drawitself, including its size and the font used on its label When you press thebutton, it knows how to communicate with the rest of your program sothat it can behave properly
The great benefit of OOP lies in the fact that you can use powerful
objects without having to concern yourself with the details of how thecode is used to write the functions Well-written classes simply drop in to
a program OOP allows you to draw on the expertise of other
programmers, such as those at Microsoft when you use the buttons andother interface tools they provide to build your own program Later, asyou become a more competent programmer, you can provide classes forother programmers to use
Trang 24methodology involving the creation of self-contained modulesthat store all the code and information necessary to carry out aspecific task, in addition to the functionality needed to interactwith that module and its information
A Little Visual C++ History
Visual C++ is built upon the C++ programming language The C++
language is an outgrowth of an earlier language called C, which itself isolder than some programmers writing it! (It was invented in the 1970s.)The C programming language was a powerful language because it moreclosely modeled how computers worked internally than many other
languages at the time This enabled programmers to gain an
unprecedented level of control over how the computer used and storedinformation, which in turn allowed them to write programs that were fastand flexible Throughout the 1970s and 1980s, C became extremelypopular It's still widely used today
One major problem that C had was that many programmers found it easy
to write code that caused them headaches Because the language was
so powerful, programmers could create mistakes that were difficult tofind Some mistakes could even be as damaging as the program waspowerful Even when programmers perfected a module, they found itdifficult to use that code in other programs
With the invention of the C++ language in the mid-1980s, the designerssought to provide a way of keeping C's power but integrating OOP
principles The result was a language that improved on many of C's
strongest features C++ has more capabilities, matches the power andspeed of C, and makes up for many of the original language's
shortcomings
The initial tools for developing applications in C and C++ were difficult touse Many required that a programmer understand cryptic files or
programs with obscure command-line interfaces But as the process ofprogramming became more complex and applications became larger, thetools for developing applications began to become more centralized and
Trang 25Visual C++ represents Microsoft's version of a toolset that brings all theprograms and resources that you need to build applications under oneroof It also includes extensions and enhancements that improve the C++programming language Initially, Visual C++ was targeted at creatingWindows applications The compiler included numerous optimizations tothe Visual C++ compiler targeted at creating both client- and server-sidebusiness applications But as Internet development gained monumentalpopularity, Microsoft increasingly saw the need to provide tools that
would allow developers to create Web-based server and Internet browserapplications
As time went by, strategists at Microsoft began to envision tools that
would serve development for what they termed the Next Generation
Internet This new Internet would be made up of many different types ofdevices, including handheld PDAs and smart devices, not just full-fledged
PC servers and client systems, as had been most popular in the past In
2000, Microsoft released the NET Framework to serve developers inmeeting this new opportunity
The first attempts at integrating NET into Visual C++ produced manydifficulties for established Visual C++ programmers It also was
challenging for new programmers to understand Many had trouble withthe changes they had to make to their coding style and philosophy toaccommodate NET But despite these problems, the power that NETprovided was compelling Businesses reported strong increases in
productivity, some managing to produce in months what had taken a year
or more
With improvements to the NET Framework and the Visual C++ compiler,the release of Visual C++ 2005 Express Edition marks a more successfuland simpler integration that brings Visual C++ up to speed with its
contemporaries, Visual Basic, Visual C#, and Visual J# Microsoft hasadopted a new emphasis on giving programmers the ability to focus onthe language that they are most efficient in This envisions the NET
Framework as serving as the connecting glue that allows programmers tomaximize the strengths of their favorite language to create programs that
Trang 26Trap To run an application created using Visual C++ NET, a
computer must have the NET Framework installed on it
Microsoft has upgraded the NET Framework several timessince its introduction Visual C++ NET 2002 was designed towork with NET Framework 1.0 Visual C++ NET 2003 wasdesigned to work with NET Framework 1.1, and Visual C++.NET 2005 works with NET Framework 2.0 Although an
application created using Visual C++ NET 2002 runs on anycomputer that has a version of NET installed on it, an
application written using Visual C++ NET 2003 requires that.NET 1.1 or 2.0 is installed In similar fashion, an applicationwritten using Visual C++ NET 2005 requires NET 2.0
IN THE REAL WORLD
The emphasis on the NET Framework has caused Microsoft to offer aprogramming library with NET that somewhat verges away from thewidely accepted C++ standard, known as the ANSI/ISO Standard,drafted by the American National Standards Institute This means thatC++ code written in the past might need to be updated if a
programmer wants it to function with the NET Framework Microsoftand several other companies have been working with the EuropeanComputer Manufacturer's Association (ECMA) to develop a new
standard for C++ code intended for use with NET Programmers canalso still write and compile ANSI/ISO Standard C++ code without the.NET Framework
Trang 27Visual C++ 2005 Express Edition has been targeted toward the first-timeprogrammer Microsoft realizes that a great many hobbyist and
programming enthusiasts have not had access to this tool due to pricing.That's why its main purpose is to provide a simplified and streamlinedintroduction to Windows application development It provides completesupport for the Microsoft NET Framework and can be used to createapplications for Windows and the Web Because of the redesigned
interface and emphasis on creating a tool that is accessible to a newgeneration of budding professionals, Visual C++ is relatively easy to getstarted with and can be used to create world-class desktop applications
If you have not done so yet, now would be a good time to install VisualC++ 2005 Express You can download a 30-day trial version by
connecting to the Microsoft Web page at
http://msdn.microsoft.com/vstudio/express/visualc and selecting
Download After you've installed Visual C++, the first time you start it, yousee the screen shown in Figure 1.4
Trang 28divided into three sections The Recent Projects section contains linksthat can quickly load previous projects you've created The Getting
Started section has links that provide you with the material you need ingetting ready to start writing programs The MSDN: Visual Studio 2005section keeps you informed of the latest happenings in the communityand provides a quick and easy way to access articles and informationregarding Visual Studio and Visual C++ And if you have a comment orfind an error in Visual C++, the Visual C++ 2005 Express Headlinessection allows you to give feedback
IN THE REAL WORLD
A large community already supports development of Visual C++
applications, including Microsoft Visual C++ 2005 Express Edition Ifyou're interested in making games and projects just for fun, Microsofthas created a Web site specifically for you where you can get thelatest tips and tricks: http://msdn.microsoft.com/coding4fun/
What can Visual C++ 2005 Express Do?
Visual C++ 2005 Express Edition has been created to support Windowsand Web application development Microsoft designed it to be the entrypoint for both new and experienced programmers into accessing the fullrange of capabilities of Visual Studio With it, programmers have a fullyfunctional, professional-grade compiler that can create such applications
as these:
Windows games, including those using the DirectX graphicslibrary
Windows desktop applications
Report and text file generators
Tools to support game development
Trang 292005 Express Edition
Programmers who are familiar with C++ and want to bring existing codeinto Visual C++ should also know that it does not support MFC and ATLtechnologies Microsoft does offer support for these technologies in theVisual C++ Standard and Professional Editions, however
Finally, if you want to develop native Win32 applications or employ
DirectX, you must download a separate Software Development Kit
(SDK) After you've installed that, you can link to these libraries and
develop native Windows and graphical applications or export existingcode
Trang 30PROGRAMMING LANGUAGES
Visual C++ 2005 Express is just one of several Visual Studio Expressdevelopment options available Following are some of the others:
Visual Basic 2005 Express This product offers Windows
developers a means of building applications quickly using theeasy-to-understand Visual Basic programming language It isespecially aimed at first-time programmers who want to get
programs up and running quickly without getting bogged down inthe technical details of their computer system
Visual C# 2005 Express This product offers Windows
developers a language that leverages some of the strengths andconcepts of C, C++, and Java but is easier to use It is also aimed
IN THE REAL WORLD
In addition to Visual C++ 2005 Express Edition, Microsoft offers both aStandard and Professional Edition The Standard Edition is targeted atindividual software professionals or small businesses and includes awider range of development tools than the Express Edition The
Professional Edition is intended for building larger scale, mission-critical business applications and expands on the Standard Edition'soptions by adding a more powerful compiler and solutions for SQLdevelopment These higher-end products also feature a wider range oftools for building Web applications
Companies that develop applications using Visual C++ often purchase
it as part of a package deal that Microsoft calls Visual Studio When
Trang 31development tools and Microsoft technical support solutions Finally, atthe upper end of the range lies Visual Studio NET Enterprise
Architect, which runs somewhere around $2,500
Visual Basic 2005 Express
Visual Basic is a programming language geared toward rapid applicationdevelopment It is based on the BASIC (Beginner's All-Purpose SymbolicInstruction Code) language, which is popular for its easy-to-read, English-like statements Visual Basic has widespread support among businessprofessionals and hobbyists alike, and versions of it are built into
Visual J# 2005 Express
Trang 32extensions to the language As a result, Visual J# 2005 Express is notcompatible with Java, so you won't be able to run programs developedwith Visual J# using Sun's Java Virtual Machine However, Visual J#
provides individuals with previous Java programming experience with agreat language for Windows application development using NET
IN THE REAL WORLD
Visual C++ is so powerful that Microsoft relies heavily on it to developmany of its tools and applications In fact, along with a little help fromanother language known as Assembly, which supports precise, low-level control over computer systems, Microsoft developed every one ofthe Visual Studio products using Visual C++!
Trang 33Microsoft envisions a Next Generation Internet made up of many differentkinds of devices It sees users not only communicating and sharing datausing PCs and PDAs, but also with mobile devices such as cell phonesand set-top boxes such as digital movie recorders But standing in theway of this interlinking of devices are many challenges, chief among
them a common way to develop applications that work well with eachother
Microsoft has developed the NET Framework as an answer to this
challenge Whether Express or full-featured versions, each of the variousVisual Studio programming languages is designed to work with the
Microsoft NET Framework Microsoft hopes that the NET Framework
provides a seamless standard for interoperability, the capability of
applications or libraries that are written in different languages to work wellwith each other regardless of the platform
Before NET
Before the introduction of NET, developing and releasing a product ofteninvolved dealing with a conceptual alphabet soup Microsoft created andencouraged the use of several versions of the Component Object Model(COM), including COM+ and Distributed Component Object Model
(DCOM) Using COM, Windows programmers were able to write
programs more quickly by relying on a consistent and unified group ofWindows services and system resources, such as data sharing and
networking support This, in turn, allowed programs to rely on DynamicLink Libraries (DLLs), or files that a program would link to access thefunctionality it needed
COM, along with Microsoft Foundation Classes (MFC), represented one
of Microsoft's first attempts at creating an OOP standard for building
Windows applications MFC provided a way to build modules that utilizedbasic Windows functionality COM, in turn, gave programmers the ability
to create component libraries made up of code that could be reused bydifferent languages, such as Visual C++ and Visual Basic
Trang 34speed graphics functions Because DLLs sometimes changed, whenprogrammers were ready to deploy their applications, they would have toconsider whether the DLL they were releasing with their product mightconflict with a DLL that was already on the user's system Things wouldget really complicated if DLLs depended on other DLLs to function
game, for instance, might need several DLLs to give it access to high-Unfortunately, with thousands of different software packages and
computer configurations to deal with, programmers didn't always get itright Sometimes the DLLs they installed replaced files that were already
on a user's computer with older or incompatible versions of the samefiles This often caused other applications on users' computers to
mysteriously break The situation was so common and difficult to dealwith that programmers, even those at Microsoft, ultimately began to refer
to it as DLL Hell.
Microsoft's solution to DLL Hell was the NET Framework The NETFramework is now responsible for giving Visual Studio programminglanguages the functionality that they used to get from DLL files BecauseDLL files are not needed to develop Windows applications, deploymentpackages are now a lot smaller Today, instead of worrying about whatversion of DLL files users have installed on their computers,
Figure 1.5 depicts the NET Framework's role in the application
development process As you can see, it enables applications to be
developed using any combination of NET-supported programming
Trang 35applications
Hint A class is the actual, self-contained code that comprises an
object, such as a menu, a button, or a text box When you
create a new form or add a control to a form when building anapplication interface, you are adding new objects based onpredefined classes stored in the NET class library
The NET class library contains the code required to create forms,
buttons, and other Visual Studio controls For example, to create a newform within a Visual C++ application, call upon a predefined class thatalready has everything defined within it to create the new form Thissaves you from having to write all the underlying low-level code yourself
If you then drag and drop a Button control onto the form, the NET classlibrary automatically supplies your application with all the underlying
Trang 36The CLR
The CLR provides the NET Framework with a collection of services thatfacilitate application execution These services include the following:
Language (MSIL) One of the jobs of the CLR is to convert MSIL codeinto binary code that the computer understands
Hint Compiling is the process of converting the programming
statements that make up a given application into a format thatthe operating system can execute
After the original source code for the application is converted to MSIL, itdoes not matter whether it was written in Visual C++, Visual C#, or anyother programming language supported by Visual Studio Because ofthis, you can mix and match multiple programming languages to create asingle application
Although all this might sound pretty complicated right now, the good news
is that you don't need to understand it all to use it The process of linkingyour program to the services or functionality it needs, communicating withother languages, or even distributing your program to other platformsoccurs automatically Microsoft Visual C++ 2005 Express Edition takes
Trang 37comfort and familiarity with the language and concepts, you'll get theexperience you need to start making sense of it all
Hint If you want to learn more about the features of the NET
Framework, visit http://www.microsoft.com/net
Trang 38Now that you have a bit of the foundation of Visual C++ out of the way,it's time to move on to programming this chapter's project, the Joke
Machine Through the development of this game, you will learn the basicsteps involved in developing a Windows application As you create yourfirst Visual C++ application, try to develop an intuitive feel for the overallprocess involved The details will become clearer as you practice and will
be explained thoroughly in the chapters to come
Designing the Game
The Joke Machine's design is straightforward, involving basic yet
fundamental Windows programming techniques The game begins bydisplaying its GUI It then waits for the user to click on a button beforedisplaying a knock-knock joke The game continues to run, allowing theuser to click either of its buttons as many times as desired Like otherWindows applications, you can minimize or maximize the game and
continue to run it until you click the Close button in the upper-right corner
of the window
As you will see as you build the Joke Machine, Visual C++ applicationsare typically created in five steps, as outlined here:
Trang 39Before you can do this, you need to launch Visual C++ 2005 ExpressEdition if you have not already done so Next, create the project by
Trang 40Hint A template is a collection of files, property, and Visual C++
compiler settings that Microsoft provides as a starting point forrapidly creating a particular type of Windows application
IN THE REAL WORLD
Visual C++ 2005 Express Edition provides many different project
templates They are all designed to assist developers in getting thesettings for Visual C++ and their application configured quickly Thefollowing types of projects are available from the New Project dialogbox:
Windows Forms Application Creates an application with a
Graphical User Interface that is designed to run on theWindows desktop The application is built using forms andcontrols that the Visual C++ development environmentprovides
Windows Forms Control Library Creates a module intended
to provide custom-designed controls to a Windows application.The behavior and appearance of the control are containedwithin the module
CLR Console Application Creates a text-based NET
application that is typically run from the Windows commandprompt This is useful for developing tools or other programsthat have little need for user interaction or mouse control