📄 ch01.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<META NAME="Author" Content="Steph Mineart">
<TITLE>Ch 1 -- Introduction to C++Builder</TITLE>
</HEAD>
<BODY
BACKGROUND="bg1.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/bg1.gif" BGCOLOR="#FFFFFF">
<P ALIGN="CENTER"><IMG SRC="sams.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/sams.gif" WIDTH="75" HEIGHT="24" ALIGN="BOTTOM"
BORDER="0"><BR>
<BR>
<A HREF="index-3.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/index.htm"><IMG SRC="toc.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/toc.gif" WIDTH="40" HEIGHT="40" ALIGN="BOTTOM"
ALT="TOC" BORDER="0" NAME="toc4"></A><A HREF="fm.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/fm.htm"><IMG SRC="back-1.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/back.gif"
WIDTH="40" HEIGHT="40" ALIGN="BOTTOM" ALT="BACK" BORDER="0" NAME="toc1"></A><A HREF="ch02.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch02.htm"><IMG
SRC="forward.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/forward.gif" WIDTH="40" HEIGHT="40" ALIGN="BOTTOM"
ALT="FORWARD" BORDER="0"
NAME="toc2"></A></P>
<H2 ALIGN="CENTER"><FONT COLOR="#000077">Charlie Calvert's C++ Builder Unleashed</FONT></H2>
<P>
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">- 1 -</FONT></H2>
<H2 ALIGN="CENTER"><A
NAME="Heading2"></A><FONT COLOR="#000077">Introduction to C++Builder</FONT></H2>
<H2 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H2>
<H3><A NAME="Heading4"></A><FONT COLOR="#000077">Overview</FONT></H3>
<P>In this chapter I introduce Borland
C++Builder (BCB) and explain what it is about.
I also devote considerable time to explaining the purpose of this book and the philosophy
behind my approach to technical writing.</P>
<P>Technical subjects covered in this chapter include
<UL>
<LI>Creating a simple Multimedia RAD program that plays movies, WAV files, and MIDI
files.
<P>
<LI>Shutting down the BCB RAD programming tools and writing raw Windows API code
instead.
<P>
<LI>Creating components dynamically on the heap at
runtime.
<P>
<LI>Setting up event handlers (closures) dynamically at runtime.
<P>
<LI>A brief introduction to using exceptions. This topic is covered in more depth
in Chapter 5, "Exceptions."
<P>
<LI>A brief introduction to ANSI
strings. This subject is covered in more depth in
Chapter 3, "C++Builder and the VCL."
<P>
<LI>Using the online help.
<P>
<LI>Greping through the include and source files that come with the product and with
this book.
</UL>
<P>This
chapter includes sample programs or code snippets illustrating all of these
concepts. The sample programs for this chapter are found on the CD that accompanies
this book in the directory called <TT>Chap01</TT>. The same pattern is followed for
all
other chapters. For instance, the code for Chapter 2, "Basic Facts About
C++Builder," is in a subdirectory on the CD called <TT>Chap02</TT>.
<H3><A NAME="Heading5"></A><FONT COLOR="#000077">Getting in the Mood</FONT></H3>
<P>Programming is
part of an esoteric world where logic is sacred. Even if you understand
exactly why a program works, there is still a magical element involved. Things appear
and disappear. Objects materialize, and then dematerialize. They do so according
to strictly
defined logical rules; but still, there is the fact that things appear
and disappear right before our eyes.</P>
<P>To be a good programmer, you have to be a wizard. You have to study arcane material,
sit up over it until your eyes are bleary, and
ponder its meaning, seeking to understand
its mysteries. Many people never understand the subtleties of programming. They don't
ever penetrate to the inner mysteries of this challenging field.</P>
<P>But think of the joy you feel when you finally
figure it out! The profound satisfaction
of actually cracking the code, mastering the spells, and seeing through to the inner
mystery! The arcane minutiae of programming is part of a subtle, intricate world
that can be mastered only by a few dedicated
souls who are willing to work hard to
get at the inner truth of an algorithm, of an object hierarchy, of a coding technique.</P>
<P>Some products seem to be effective at capturing the essence of the beautiful,
mysterious logic that underlies the world
of programming. C++ has always had free
entry into this realm. C++Builder, however, raises the ante in the C++ world by allowing
you to create programs with a powerful set of tools that gracefully augment your
programming skills.</P>
<P>BCB is one of
the first serious compilers that allows you to pick up objects called
components with the mouse and move them around so that you can change the logic of
your program visually, rather than solely with code. The core of this technology
is component
programming--not large, bloated, difficult to create components but
small, sleek, easy-to-build components that run at lightning speed, components that
appear and disappear before your eyes at the click of a mouse.</P>
<P>Programming is intellectually
exciting. At times, it's even--dreaded word--fun!
C++Builder puts the excitement back in C++ programming. If you like to write fast
programs that are easy and fun to use, this is the right tool for you. Best of all,
C++Builder gives you full access to
all the advanced features of C++, including templates,
name spaces, operator overloading, and the entire Windows API, including cutting-edge
APIs such as DirectX, OLE Automation, and ActiveX.</P>
<P>Most of the time, BCB programming is surprisingly
easy. On occasion, it's very
challenging. It is, however, always interesting and exciting. Let other programmers
plod along with boring compilers made by some huge soulless conglomerate full of
middle managers who middle-manage their products into one
giant, boring access violation.
There is something different about BCB. Like its cousin Delphi, it has something
of the true spark of the real programmer's art in its sleek lines, in its fast compilation,
and in its subtle and artful use of the C++
language.
<H3><A NAME="Heading6"></A><FONT COLOR="#000077">The Audience for This Book</FONT></H3>
<P>Throughout this book, I walk a subtle line between extremes. Sometimes the text
has pulled me in the direction of system programming, and at other
times I have relied
on the RAD tools that make BCB so easy to use. At times, I have wanted to find the
fastest way to perform a particular task and at others I have wanted to find the
clearest, simplest way to perform a task. Almost a third of the
book concentrates
on database tasks, but I also dig deeply into OOP, component creation, and esoteric
Windows APIs such as DirectX.</P>
<P>C++ is the language of choice for programmers obsessed with speed, who long to
optimize their programs down to
the last clock cycle, and who love to plumb the most
intricate depths of the computer. Some C++ programmers feel physical pain when they
have to give up clock cycles that could be optimized out given sufficient time. In
short, C++ is a language
designed for creating operating systems and compilers.</P>
<P>RAD tools, on the other hand, are designed for programmers who have a job to do
and want to get it done quickly. These people want a safety net so they don't crash
and burn! They are
willing to give up clock cycles in return for usable code.</P>
<P>In short, RAD programmers are intent on getting a job done quickly and safely,
whereas C++ programmers are traditionally intent on creating the smallest, fastest
programs possible.</P>
<P>This book, and BCB as a whole, is about the meeting of these two diverse camps.
I am very much aware that many C++ programmers won't like the "smell" of
RAD, and that many RAD programmers will be appalled by the ornate subtleties of C++.
However, I believe that there is a place where these two groups can meet, and furthermore,
I think C++ can provide the high productivity tools that RAD programmers expect,
along with the high performance, system-loving, optimized intricacies that true
aficionados
of C++ demand.</P>
<P>In short, this book is for contemporary programmers who practice their art on
the cutting edge of modern programming techniques. That does not mean that this book
is about the most technical aspects of C++ and
Windows, nor does it mean that this
book is about a dangerous, new form of programming that wastes clock cycles indiscriminately.
Instead, this book is about techniques that allow systems programmers to get their
work done quickly, while allowing RAD
programmers to speed up and enhance their programs.</P>
<P>I should perhaps add that a large portion of this book is dedicated to client/server
database programmers. Nearly 80 percent of the applications made today involve databases,
and this tool
will undoubtedly be used very heavily by client/server developers.
I go into considerable lengths to talk about the advanced database features found
in BCB; I cover SQL, stored procedures, triggers, filters, lookups, and numerous
other database
techniques.
<H4><A NAME="Heading7"></A><FONT COLOR="#000077">BCB Versus VB</FONT></H4>
<P>There is one thing that ought to be made clear right at the start. The programs
you write with BCB are comparable in terms of size and performance with the
programs
you create with OWL or MFC. It would be a mistake to assume that BCB has any of the
limitations you find in VB or PowerBuilder, or even in Optima. Anything you can do
in MSVC or in BC5 you can also do in BCB, and you can do it with the same,
or an
increased, degree of subtlety and artfulness.</P>
<P>Both BCB and VB are RAD tools. But that is where the comparison between the two
products must end. VB is a nice product, but it is not a serious programming tool.
BCB is a very serious
programming tool. It is a real C++ compiler that comes with
all the bells and whistles.</P>
<P>The presence of RAD tools can lead you to believe that BCB is somehow crippled
in terms of performance or capability. However, that is an erroneous
conclusion.
If you take the time to explore the product in depth, you will find that it lacks
nothing in terms of power or capability.</P>
<P>The RAD tools in this package add no real overhead to your programs that you would
not find in either OWL or
MFC. The VCL is comparable to OWL and MFC in every way,
except for the fact that it is much easier to use and much more elegantly designed.</P>
<P>The word component can also conjure up images of slow, buggy, hard-to-understand
ActiveX controls. BCB
components are much faster, much smaller, and much easier to
make than ActiveX controls. OLE is a powerful technology--and one that I use quite
frequently--but it lacks the subtlety, speed, and elegance of the VCL code that underlies
BCB.
<H4><A
NAME="Heading8"></A><FONT COLOR="#000077">A Cautious Approach to Programming</FONT></H4>
<P>Having gone to some lengths to emphasize the technical depth of BCB, I want to
turn around and discuss the relatively conservative approach I take to the art
of
writing programs.</P>
<P>I have been writing code long enough to have grown suspicious of techniques that
are too fancy, too subtle, and too hard to parse, execute, and maintain. As a result,
I have adopted the style of programming championed by
people who want to write safe,
easy-to-maintain programs.</P>
<P>I tend to promote a conservative programming style--and indeed, almost all the
good programmers I know use these same techniques, even when writing code that is
designed for high
performance applications.</P>
<P>A certain degree of caution is necessary if you want to write robust code. When
in doubt, I always err on the side of caution.</P>
<P>Does this mean I want you to write slow, bloated code? No, of course not! My goal
is
to walk that fine line between writing code that is such a high wire act that
it can't be maintained, and writing code that is so high-level, so abstracted, that
its performance becomes an abomination.</P>
<P>BCB is about the place you can get the
maximum in terms of safety, without giving
up significant power in terms of speed and flexibility. It's about walking the line
between two extremes.
<H4><A NAME="Heading9"></A><FONT COLOR="#000077">On Using C++</FONT></H4>
<P>When creating the sample
applications for this book, I tried to choose code that
walks the middle line between being too cautious and too daring. I tried to take
the best ideas from the C++ language and combine them with the benefits of RAD.</P>
<P>I want to get far enough
into C++ to leverage its power, without going so far
that I spend whole chapters parsing the subtleties of some obscure syntactical corner
of the language. I also want to use many high-level, RAD-based tools, but I don't
want to rely on them so
completely that they overshadow the power of the C++ language.</P>
<P>The goal is to find the middle ground, the artful line that yields the best programs.
If I am in doubt, I will err on the side of the RAD programmers who have a job to
do. The
primary reason for this decision is simply that there are already many great
books out there on the intricacies of C++ and on the subtleties of the Windows API.
There is no need for another book on those subjects. Instead, I want to show what
C++Builder brings to the table.</P>
<P>When exploring BCB, however, I will always keep at least one eye on the system
programmer. I know what you want, I believe in your cause, and I want to show you
how BCB can help you complete even the subtlest
jobs more quickly than traditional
environments such as BC5 or MSVC. My promise is that the executables you produce
with BCB will be at least as small, and at least as fast as the executables you produce
with MFC or OWL. And, if you want, you can cut
out BCB's object-oriented tools and
produce tiny executables that match anything that you can do with BC5 or MSVC.</P>
<P>I am not trying to create a companion volume to a classic hard-core tome such
as the Zen of Assembly Language, More Effective
C++, Undocumented Windows, the ARM,
or Inside Windows. Books like that have their place, of course, but that is not the
kind of programming I want to write about.</P>
<P>Clearly, I am trying to set practical, reasonable goals for this book. However,
I
don't mean to imply that this is a plodding, methodical book that will never take
flight into any interesting subjects. On the contrary, I want to show how you can
do fancy, flashy, exciting things with a computer, without having to parse the
lowest-level
bits in the operating system. If you want to plumb to the lowest depths of the operating
system, I will take you right up to the edge, show you how to get started, and then
wish you Godspeed. You can use BCB to do some great system
programming, but I will
leave the specifics of how to proceed to other authors, or to a second book of my
own on the subject.</P>
<P>This book contains lots of exciting code on subjects such as multimedia, games,
and Internet programming. I
concentrate on very high-performance tools such as DirectX
and on cutting-edge technologies such as OLE. Unlike other books on these subjects,
however, my goal is to show how you can integrate these things into your projects
even if you are on a tight
schedule and even if you would not normally be inclined
to do the kind of spelunking that those names imply.</P>
<P>In my opinion, the kind of programming described in this book is the essence of
cutting-edge computer technology (at the time of this
writing). The best programmers
today use whatever tools they can find to allow them to quickly produce high-performance
programs. Plumbing the depths is fun, but it loses some of its appeal when the Internet
calls, or when you need to produce an
inventory program quickly, or when you want
to spice up an application so that your users actually enjoy sitting down to work
with one of your creations.</P>
<P>My point is quite simply that today many of the best programmers are specializing,
not in
plumbing the depths of the operating system, but in producing real-world applications
quickly. This is an advanced programming book that assumes a good deal of experience
on the part of the reader. However, I want your experience to be not deep and
narrow,
but broad and expansive.
<H4><A NAME="Heading10"></A><FONT COLOR="#000077">Humility, Crooked Timber, and the
Practical Programmer</FONT></H4>
<P>In the book Code Complete (published by Microsoft Press), Steve McConnell quotes
an award-winning
paper by Edsger Dijkstra called the "The Humble Programmer."
I regard this work as one of the guiding lights of this book.</P>
<P>I would much rather write a humble program that works well than be involved in
a flashy, ego-ridden project
that is never finished, or that ships two years late.
The key to getting things done is to show a little humility.</P>
<P>In particular, if you work under the assumption that any one programmer is perfect,
you are doomed to failure. Computers are
reliable; programmers make mistakes.</P>
<P>Computers, on the other hand, look remarkably dense when compared to the creativity
a good programmer can wield. Machines get a zero on the creativity scale, whereas
programmers can be very creative. The key
is not to try to make people like computers,
but to find the best way to leverage the virtues of both programmers and computers.</P>
<P>If you write code that assumes the programmer is perfect, sooner or later that
code will fail. Don't mix up the
programmer and the computer. The computer is the
one that doesn't make mistakes; the programmer is the one that comes up with ideas.</P>
<P>I write code that assumes I not only can make mistakes, but that I will make mistakes.
I write code that shies
away from the extremely low-level code that crimps my creative
side, and which invites bugs.</P>
<P>The code I like to write assumes that I tend to make errors, and that I should
be free to exercise a degree of creativity. Code that is too technical,
too cutting-edge,
or too clever is code that is prone to bugs and late, sleepless nights.</P>
<P>The right kind of code gets the job done quickly enough to leave programmers still
fresh and alert, so that they can exercise creativity in coming up with
the best
solutions.</P>
<P>Quite often in this book, I will recommend techniques that fly in the face of
the advice you undoubtedly get from that hotshot programmer who works down the hall.
My problem is not that I fail to appreciate the importance of
performance or producing
small, fast programs. Rather, I worship at a different shrine, the one that finds
the middle ground between code that is too subtle and code that is too abstract,
too high-level.</P>
<P>This book is dressed in jeans or cords,
good practical shoes, and a tough, but
attractive, plaid work shirt. Programmers who like to dress in patent leather shoes
and $2,000 suits might make fun of some of my techniques. What I like about the clothes
that this book wears is that they are
tough, well-suited to a wide variety of conditions,
and they look great on a wide range of people.</P>
<P>I don't write for someone who wants to be the best programmer in a group. Instead,
I am interested in people who want to make things. I want to
get from conception
to a finished product, and I don't care if all of the techniques I use aren't the
fanciest available. I don't, quite frankly, care all that much about the schedule
my manager wants to live by; rather, my goal is to get the job done
before I become
utterly sick of it. I like to make things. I want to finish the project.</P>
<P>Immanuel Kant is one writer who aptly captured the spirit by which most programmers
should live: "Out of timber so crooked as that from which man is
made nothing
entirely straight can be carved." In other words, don't expect your programs
to be perfect, and don't waste time trying to achieve perfection. Aim a little lower,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -