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

📄 ch02.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
	Needless to say, I think you ought to use the VCL to write contemporary Windows programs.
	However, if you also know the Windows API, you 
can become a great VCL programmer.
	If you don't know the Windows API, you will always be at a loss when using some features
	of the VCL, at least until such time as Windows becomes a true object-oriented operating
	system. Do you have to know OWL or 
MFC to become a great VCL programmer? No. <BR>
	<BR>
	Do you have to know the Windows API in order become a great VCL programmer? Absolutely!
	The Windows API material found in my Teach Yourself... books has a very long life
	from a technical, if not 
a commercial, point of view. Assuming that Java does not
	take over the world, a knowledge of the Windows API is always invaluable to a contemporary
	programmer. <BR>
	<BR>
	Remember, however, that even the glorious VCL has a shadow over it, in the 
form of
	Java. I never get too bogged down in the details of the VCL or any other framework,
	because I never know when this rapidly changing programming world is going to enter
	another period of mind-numbing change in which the slow ones get left 
behind! <BR>
	<BR>
	The Windows API is the only really complex programming paradigm that is worth learning
	in depth, because all the others are likely to change over time. The fact that the
	VCL is easy to use is not just a nice feature, it's a 
necessity in this contemporary
	programming world where the only constant is change. I should perhaps add that the
	reason you use the VCL instead of the raw Windows API is because you get your work
	done in about one-tenth the time. If you use OWL or 
MFC, your code will be much bigger
	than if you use raw Windows API code, but you will also have a better chance of getting
	your work done on time. If you want another four- or five-fold increase in productivity,
	use the VCL. 
<HR>


</BLOCKQUOTE>


<H3><A NAME="Heading31"></A><FONT COLOR="#000077">Using the VCL</FONT></H3>
<P>Now that you have heard an advertisement for the VCL, it might help to provide
a few more basic examples illustrating some of the virtues of this programming system.
These 
are very simple examples that are a bit atypical of the type of code you will
see in this book, or even in the latter portions of this chapter. I feel, however,
that these basic examples are useful when illustrating some of the key features of
RAD 
programming with the VCL. Their presence ensures that everyone understands the
benefits of visual programming with the VCL.</P>
<P>I will, however, use these basic examples to explore some fairly complex aspects
of the VCL, and especially of the code 
that executes just before and after the main
form of your application is made visible. In particular, I will look at the code
in the project source file for a typical BCB application that uses the VCL.</P>
<P>The first program I want to discuss uses a 
standard Delphi component called <TT>TShape</TT>.
If you open up BCB and drop the <TT>TShape</TT> component on a form, you will see
that it draws a simple white rectangle on the screen.</P>
<P>Of course, the <TT>TShape</TT> object can perform more 
than this one simple trick.
For instance, if you pull down the list associated with the <TT>Shape</TT> property
in the Object Inspector, you see that you can easily work with ellipses, circles,
squares, and other assorted shapes. Furthermore, if you 
expand the <TT>Brush</TT>
property, you can change the shape's color. The <TT>pen</TT> property enables you
to change the width and color of the outline of a <TT>TShape</TT> object.

<DL>
	<DT></DT>
</DL>



<BLOCKQUOTE>
	<P>
<HR>
<FONT 
COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Don't forget that you can expand
	properties that have a plus sign (+) next to them by double-clicking the property's
	name. A <TT>Color</TT> property always has a dialog associated with it. To bring
	up the 
dialog, double-click the area to the right of the <TT>Color</TT> property.
	This area is called the property editor. (Later in the book I will show how to create
	your own property editors and how to use existing property editors.) Select a color
	
from the dialog, click the OK button, and the color you chose automatically takes
	effect. 
<HR>


</BLOCKQUOTE>

<P>As just described, it's trivial to change the major characteristics of a <TT>TShape</TT>
object at design time. However, I spoke 
earlier about BCB supporting two-way tools.
Anything that you do with the visual tools you can also do in code. It is, of course,
a little more work to make the same changes at runtime that you made at design time,
but the basic principles are still 
simple. The SHAPEDEM and SHAPEDEM2 programs on
the CD that accompanies this book show you how to proceed.

<DL>
	<DT></DT>
</DL>



<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>I try to avoid it, but you might
	find a few 
places in the sample programs where I hard-code in the path to a file.
	You will probably have to edit these paths to get the program to run on your system.
	<BR>
	<BR>
	I go to a great deal of effort to ensure that the code that accompanies this book
	
works correctly. If you are having trouble running any particular program, check
	the readme files found on the CD that accompanies this book. If you still can't get
	the program running, go to my Web site and see if there is an update, hint, or bug
	
report available. My Web site is <A HREF="javascript:if(confirm('http://users.aol.com/charliecal.  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://users.aol.com/charliecal.'" tppabs="http://users.aol.com/charliecal."><TT>users.aol.com/charliecal</TT>.
	</A>
<HR>


</BLOCKQUOTE>

<P>At its core, the SHAPEDEM program consists of nothing more than a <TT>TShape</TT>
object placed on a form, 
along with two scroll bars and a menu. What's interesting
about the program is the ease with which you can change the size, color, and shape
of the <TT>TShape</TT> object at runtime.</P>
<P>You can find the code for the program in Listings 2.1 through 
2.3. Remember that
if you want to view the source for the project file in your application, you can
select the View | Project Source menu item.<BR>
<BR>
<A NAME="Heading34"></A><FONT COLOR="#000077"><B>Listing 2.1. The code for 
SHAPEDEM.CPP.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">#include &lt;vcl.h&gt;

#pragma hdrstop



USEFORM(&quot;Main.cpp&quot;, Form1);

USERES(&quot;ShapeDem.res&quot;);



WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)

{

  
Application-&gt;Initialize();

  Application-&gt;CreateForm(__classid(TForm1), &amp;Form1);

  Application-&gt;Run();



  return 0;

}

</FONT></PRE>
<P><A NAME="Heading36"></A><FONT COLOR="#000077"><B>Listing 2.2. The header for the
main unit in 
SHAPEDEM.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">#ifndef MainH

#define MainH

#include &lt;Classes.hpp&gt;

#include &lt;Controls.hpp&gt;

#include &lt;StdCtrls.hpp&gt;

#include &lt;Forms.hpp&gt;

#include &lt;ExtCtrls.hpp&gt;

#include 
&lt;Dialogs.hpp&gt;

#include &lt;Menus.hpp&gt;



class TForm1 : public TForm

{

__published:

  TShape *Shape1;

  TScrollBar *ScrollBar1;

  TScrollBar *ScrollBar2;

  TColorDialog *ColorDialog1;

  TMainMenu *MainMenu1;

  TMenuItem *Shapes1;

  
TMenuItem *ShapeColor1;

  TMenuItem *FormColor1;

  TMenuItem *Shapes2;

  TMenuItem *Rectangle1;

  TMenuItem *Square1;

  TMenuItem *RoundRect1;

  TMenuItem *RoundSquare1;

  TMenuItem *Ellipes1;

  TMenuItem *Circle1;

  void __fastcall 
ShapeColor1Click(TObject *Sender);

  void __fastcall FormColor1Click(TObject *Sender);

  void __fastcall Rectangle1Click(TObject *Sender);

  void __fastcall ScrollBar1Change(TObject *Sender);

  void __fastcall ScrollBar2Change(TObject *Sender);

  
void __fastcall FormResize(TObject *Sender);

private:

public:

  virtual __fastcall TForm1(TComponent* Owner);

};



extern TForm1 *Form1;



#endif

</FONT></PRE>
<P><A NAME="Heading37"></A><FONT COLOR="#000077"><B>Listing 2.3. The code for the

main unit in SHAPEDEM.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">#include &lt;vcl.h&gt;

#pragma hdrstop

#include &quot;Main.h&quot;



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

TForm1 *Form1;



__fastcall TForm1::TForm1(TComponent* Owner)

  : 
TForm(Owner)

{

  Shape1-&gt;Left = 0;

  Shape1-&gt;Top = 0;

}



void __fastcall TForm1::ShapeColor1Click(TObject *Sender)

{

  if (ColorDialog1-&gt;Execute())

    Shape1-&gt;Brush-&gt;Color = ColorDialog1-&gt;Color;

}



void __fastcall 
TForm1::FormColor1Click(TObject *

⌨️ 快捷键说明

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