⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch01.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
instead. Rather than perfect, shoot for: "It works." Or, at 
best: "This
program is remarkably bug-free!&quot;</P>
<P>Even better, aim for programs that are creative, fun to use, and useful. The strongest
suit a programmer can take to a task is creativity. Even the best programmers look
like bunglers when 
compared to the reliability of a computer.</P>
<P>The best programmers also make mistakes with a frightening regularity. I try to
accept that fact, accept my limitations, and then find ways to program that are safe!
If I have the humility to admit I 
am not perfect, I can start making programs that
work and that get turned in on time!</P>
<P>Once again, I don't really care about my manager's schedule; I care about my schedule.
I want to start a project, bring it to fruition, and then move on to 
the next thing
that interests me! I don't want to be stuck working on the same task for years on
end!
<H3><A NAME="Heading11"></A><FONT COLOR="#000077">API Code Versus RAD Code</FONT></H3>
<P>At this stage, it might be helpful to give a few specific 
examples of the difference
between RAD programming with BCB and traditional Windows programming. My primary
point here is to show that BCB can do it all. If you want to write API code, BCB
will let you write it. If you want to write OWL code, BCB will 
let you do that, too.
If--heaven forbid--you should even be foolish enough to want to write MFC code (perish
the thought!), you can also do that with BCB.</P>
<P>In this section, you will see two sample programs. The first is a traditional
RAD 
application written in BCB, and the second is a standard Windows API program.
I will spend a few moments talking about each program and then will use these examples
to illustrate just what it is about RAD that I find appealing, as well as explain

something about the parts of RAD development that I will focus on in this book.
<H4><A NAME="Heading12"></A><FONT COLOR="#000077">A Very Short Introduction to the
VCL</FONT></H4>
<P>Reading this text over, I find that I am throwing a number of 
acronyms around.
One that really begs for a short explanation is VCL.</P>
<P>The VCL is the Visual Component Library. It is to BCB what OWL is to BC5, and
what MFC is to MSVC. (This is called acronym immersion therapy.) In other words,
it is the 
object- oriented library, or framework, that underlies BCB. The difference
between VCL and OWL is that the VCL is based on components, properties, and events,
while OWL and MFC have none of these features. In particular, events support something

called the delegation model, which is an alternative to simple inheritance.</P>
<P>The VCL fully supports all standard OOP concepts such as inheritance, polymorphism,
and encapsulation. What it brings to the party are components, properties, and 
events.
(Events are also known as closures.) One of the goals of this book is to explain
components, properties, and events in the clearest possible terms, and to state why
I feel this is the proper programming model for this stage of computer 
programming
development.</P>
<P>Perhaps most importantly, the VCL is written in Object Pascal. In fact, it is
literally the same code base used by Delphi. Later in this chapter, I will explain
a bit more about BCB's relationship to Delphi, and I will 
explore the subject in
detail in the next chapter. For now, you need to know only that the VCL would not
be any faster or smaller were it written in C++. Object Pascal has some stark differences
from C++, but speed and size are not among them. As 
explained previously, Object
Pascal is a real, compiled, object-oriented language that fully supports true inheritance,
polymorphism, and encapsulation. All Object Pascal code that works in Delphi works
automatically in C++Builder.</P>
<P>That's all I 
will say on this subject at this point, though I will spend considerable
time defining the VCL and its related programming models more carefully in later
chapters. In particular, Chapter 2 and Chapter 3 go into considerable depth on the
subject of 
VCL, how it works, and why it exists.
<H4><A NAME="Heading13"></A><FONT COLOR="#000077">On Using the Visual Tools</FONT></H4>
<P>Before I get started with a specific programming example, I want to take a moment
to discuss the technique I use when 
writing about BCB programs. Except for a few
places in the first chapters, I will generally skip over detailed descriptions of
the visual programming tools.</P>
<P>In this text, I will usually not explain the process of setting up a standard
component 
at all. For instance, if text on a <TT>TButton</TT> component says OK,
Exit, or Close, I will not say Set the <TT>Caption</TT> field of the <TT>TButton</TT>
component to Exit, or Close. Instead, I will assume that you can figure that much
out just by 
looking at the figure that accompanies my description of the program.</P>
<P>As you gain more experience with C++Builder, you will quickly learn how to work
with most of the properties associated with components. As a result, I usually do
not bother 
to write detailed explanations about setting up a component, such as that
you need to set its <TT>Align</TT> property to <TT>alClient</TT>, or its <TT>Stretch</TT>
property to <TT>True</TT>. I assume that you can see that much just from glancing
at 
the figure. Of course, I assume there will be times when you will want to run
the programs on disk to see exactly how I have achieved a particular affect.</P>
<P>My point here is that you should be able to glean information of this kind from
the 
manuals that ship with the product or from the online help. You could also turn
to a beginning level C++Builder book, such as Teach Yourself C++Builder in 21 Days
(published by Sams). In the current text, however, I will try to skip over that kind
of 
beginning material, in order to best give you what you expected when you bought
an intermediate- to advanced-level book.</P>
<P>I am aware, however, that BCB is a new product and that some introductory material
is needed. I will try to keep it to a 
minimum. In particular, almost all the introductory
material occurs in this chapter and the next. After that, I'll assume you know how
to use the environment.
<H4><A NAME="Heading14"></A><FONT COLOR="#000077">A Simple RAD Multimedia 
Program</FONT></H4>
<P>The code for the Multimedia RAD program is shown in Listing 1.1. The entire program
is found on the CD that accompanies this book. An explanation of the program follows
these listings.<BR>
<BR>
<A NAME="Heading15"></A><FONT 
COLOR="#000077"><B>Listing 1.1. The header file for
the Multimedia RAD program.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">//--------------------------------------------------------------------------

#ifndef MainH

#define MainH


//--------------------------------------------------------------------------

#include &lt;vcl\Classes.hpp&gt;

#include &lt;vcl\Controls.hpp&gt;

#include &lt;vcl\StdCtrls.hpp&gt;

#include &lt;vcl\Forms.hpp&gt;

#include &lt;vcl\ExtCtrls.hpp&gt;


#include &lt;vcl\MPlayer.hpp&gt;

#include &lt;vcl\Menus.hpp&gt;

#include &lt;vcl\Dialogs.hpp&gt;

//--------------------------------------------------------------------------

class TForm1 : public TForm

{

__published:  // IDE-managed Components

  
TPanel *Panel1;

  TImage *Image1;

  TMediaPlayer *MediaPlayer1;

  TMainMenu *MainMenu1;

  TMenuItem *File1;

  TMenuItem *Load1;

  TMenuItem *Play1;

  TMenuItem *N1;

  TMenuItem *Exit1;

  TOpenDialog *OpenDialog1;

  TMenuItem *Options1;

  
TMenuItem *ChangeBackground1;

  void __fastcall Load1Click(TObject *Sender);

  void __fastcall Play1Click(TObject *Sender);

  void __fastcall Exit1Click(TObject *Sender);

  void __fastcall ChangeBackground1Click(TObject *Sender);

private: // User 
declarations

public:  // User declarations

  virtual __fastcall TForm1(TComponent* Owner);

};

//--------------------------------------------------------------------------

extern TForm1 *Form1;


//--------------------------------------------------------------------------

#endif

</FONT></PRE>
<P><A NAME="Heading16"></A><FONT COLOR="#000077"><B>Listing 1.2. The main module
for the Multimedia RAD program.</B></FONT></P>
<PRE><FONT 
COLOR="#0066FF">///////////////////////////////////////

// File: Main.cpp

// Project: Muli-media RAD

// Copyright (c) 1997 by Charlie Calvert

#include &lt;vcl\vcl.h&gt;

#pragma hdrstop

#include &quot;Main.h&quot;

#pragma resource 
&quot;*.dfm&quot;

TForm1 *Form1;

__fastcall TForm1::TForm1(TComponent* Owner)

  : TForm(Owner)

{

}

void __fastcall TForm1::Load1Click(TObject *Sender)

{

  if (OpenDialog1-&gt;Execute())

  {

    MediaPlayer1-&gt;FileName = 
OpenDialog1-&gt;FileName;

    MediaPlayer1-&gt;Open();

  }

}

void __fastcall TForm1::Play1Click(TObject *Sender)

{

  try

  {

    MediaPlayer1-&gt;Play();

  }

  catch(EMCIDeviceError &amp;E)

  {

    AnsiString S(&quot;\rUse the File | Open 
menu item to select an AVI file.&quot;);

    ShowMessage(&quot;Bummer: &quot; + E.Message + &quot;. &quot; + S);

  }

}

void __fastcall TForm1::Exit1Click(TObject *Sender)

{

  Close();

}

void __fastcall TForm1::ChangeBackground1Click(TObject 
*Sender)

{

  AnsiString RootDir(ParamStr(0));

  AnsiString SaveDir = OpenDialog1-&gt;InitialDir;

  AnsiString SaveFilter = OpenDialog1-&gt;Filter;

  OpenDialog1-&gt;InitialDir = ExtractFilePath(RootDir);

  OpenDialog1-&gt;Filter = &quot;Picture 
| *.bmp&quot;;

  if (OpenDialog1-&gt;Execute())

  {

    Image1-&gt;Picture-&gt;LoadFromFile(OpenDialog1-&gt;FileName);

    Image1-&gt;Stretch = True;

  }

  OpenDialog1-&gt;InitialDir = SaveDir;

  OpenDialog1-&gt;Filter = SaveFilter;

}


</FONT></PRE>
<P>This program pops up in a window that has a picture of a Mayan temple as a background,
as shown in Figure 1.1. From the menu, you can pop up an open file common dialog
that will let you select and play either a movie file, WAV file, 
or MIDI file. You
can also browse to select new backgrounds for the main form.<BR>
<BR>
<A NAME="Heading17"></A><A HREF="01ebu01.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/01/01ebu01.jpg">FIGURE 1.1.</A><FONT COLOR="#000077">
</FONT><I>The main screen for the Multimedia Adventure program.</I>
<H4 
ALIGN="CENTER"></H4>
<H4><A NAME="Heading18"></A><FONT COLOR="#000077">The RAD Tasks for Creating the
Multimedia Program</FONT></H4>
<P>To create the program, bring up BCB and select New Application from the File menu.
Drop down the following 
components on the main form:</P>
<PRE><FONT COLOR="#0066FF">TPanel *Panel1;

TImage *Image1;

TMediaPlayer *MediaPlayer1;

TOpenDialog *OpenDialog1;

</FONT></PRE>

<DL>
	<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>



<BLOCKQUOTE>
	<P>
<HR>
<FONT 
COLOR="#000077"><B>NOTE: </B>:</FONT><B> </B>In some RAD programming books,
	code is presented that shows the exact location of the objects placed on a form.
	For instance, here are some selections from the text representation of the form for
	the 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -