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

📄 ch07.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!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 7 -- Graphics</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="ch06.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch06.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="ch08.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch08.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><A
HREF="ch08.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch08.htm"></A></H2>
<P>
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">- 7 
-</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Graphics</FONT></H2>
<P>This chapter covers the basics of graphics programming in the VCL. The VCL encapsulates
the Windows Graphics Device Interface, or GDI. GDI 
programming can be a subtle and
dangerous process. The VCL tames this technology and makes it extremely easy to use.</P>
<P>In the following pages you will learn about:

<UL>
	<LI>The <TT>TCanvas</TT> object
	<P>
	<LI>Painting shapes on the screen
	
<P>
	<LI>Working with colors
	<P>
	<LI>Working with bitmaps
	<P>
	<LI>Working with metafiles
	<P>
	<LI>Drawing fractals
	<P>
	<LI>Working with fonts
</UL>

<P>It's important to understand that the basic graphics functionality presented here
is a far 
cry from the sophisticated tools you find in the DirectX programs seen in
Chapter 28, &quot;Game Programming.&quot; This is just the basic functionality needed
to present standard Windows programs. However, the material presented in this chapter
is 
useful in most standard Windows programs, and it is part of the core knowledge
that all VCL programmers should possess. In particular, it shows how the VCL encapsulates
and simplifies Windows GDI programming.
<H3><A NAME="Heading3"></A><FONT 
COLOR="#000077">Graphics.Hpp</FONT></H3>
<P>The core of the VCL graphics support is found in <TT>Graphics.Hpp</TT>. The following
objects can be found in this file: 
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD 
ALIGN="LEFT"><B>Object</B></TD>
		<TD ALIGN="LEFT"><B>Description</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT"><TT>TCanvas</TT></TD>
		<TD ALIGN="LEFT">This is the basic graphics object used to paint shapes on a form or other 
surface.
			It is the primary wrapper around the GDI.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT"><TT>TBrush</TT></TD>
		<TD ALIGN="LEFT">This is the object used to designate the color or pattern that fills in the center
			of shapes 
such as rectangles or ellipses.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT"><TT>TPen</TT></TD>
		<TD ALIGN="LEFT">This is the object used for drawing lines, and as the outline for rectangles or ellipses.</TD>
	</TR>
	<TR ALIGN="LEFT" 
rowspan="1">
		<TD ALIGN="LEFT"><TT>TPicture</TT></TD>
		<TD ALIGN="LEFT">This is the generalized, high-level VCL wrapper around &quot;pictures&quot; such
			as bitmaps, metafiles, or icons.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD 
ALIGN="LEFT"><TT>TMetaFileCanvas</TT></TD>
		<TD ALIGN="LEFT">This is the drawing surface for a metafile.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT"><TT>TMetaFile</TT></TD>
		<TD ALIGN="LEFT">This is the VCL wrapper around a windows 
metafile.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT"><TT>TBitmap</TT></TD>
		<TD ALIGN="LEFT">This is the VCL wrapper around bitmaps.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT"><TT>TIcon</TT></TD>
		<TD 
ALIGN="LEFT">This is the VCL wrapper around icons.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT"><TT>TGraphicsObject</TT></TD>
		<TD ALIGN="LEFT">This is the base class for <TT>TBrush</TT>, <TT>TFont</TT>, and <TT>TPen</TT>.</TD>
	
</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT"><TT>TGraphic</TT></TD>
		<TD ALIGN="LEFT">This is the base class for <TT>TMetaFile</TT>, <TT>TBitmap</TT>, and <TT>Ticon</TT>.</TD>
	</TR>
</TABLE>
</P>
<P>Many of these objects are explored in 
the next few pages. In particular, I demonstrate
how to use bitmaps and pens and show how to draw basic geometric shapes to the screen.
You also see how to work with bitmaps and metafiles.</P>
<P>After the <TT>TFont</TT> object, the next most 
important graphics object in the
VCL is <TT>TCanvas</TT>. This is a wrapper around the Windows GDI, or Graphics Device
Interface. The GDI is the subsystem Windows programmers use to paint pictures and
other graphics objects. Most of the content of 
<TT>Graphics.Hpp</TT> is aimed at
finding ways to simplify the GDI so that it is relatively easy to use.</P>
<P>Two other important graphics-based objects not found in <TT>Graphics.Hpp</TT>
are <TT>TImage</TT> and <TT>TPaintBox</TT>. Both of these 
components are covered
in this chapter.
<H3><A NAME="Heading4"></A><FONT COLOR="#000077">The TColor Type</FONT></H3>
<P>Almost all the graphics objects use the simple <TT>TColor</TT> type. This is one
of the building blocks on which the whole graphics 
system is laid.</P>
<P>For most practical purposes, a variable of type <TT>TColor</TT> is synonymous
with the built in Windows type <TT>COLORREF</TT>. However, the actual declaration
for <TT>TColor</TT> looks like this:</P>
<PRE><FONT 
COLOR="#0066FF">enum TColor {clMin=-0x7fffffff-1, clMax=0x7fffffff};

</FONT></PRE>
<P>If you know the <TT>COLORREF</TT> type, you can see that it is similar to the
<TT>TColor</TT> type. In a sense, the <TT>TColor</TT> type is nothing but a set of

predefined <TT>COLORREF</TT>s.</P>
<P>The Windows palette system enables you to define three different colors, Red,
Green, and Blue, where each color has 255 different shades. These colors are specified
in the last three bytes of the 4-byte long value 
used to represent a variable of
type <TT>TColor</TT>.</P>
<P>Here is what the three primary colors look like:</P>
<PRE><FONT COLOR="#0066FF">Canvas-&gt;Brush-&gt;Color = 0x000000FF;  // Red

Canvas-&gt;Brush-&gt;Color = 0x0000FF00;  // Green


Canvas-&gt;Brush-&gt;Color = 0x00FF0000;  // Blue

</FONT></PRE>
<P>You worked with these colors, and combinations of these colors, in the RGBShape
program from Chapter 2, &quot;Basic Facts about C++Builder.&quot;</P>
<P>Of course, it's not convenient 
to have to write out these numbers directly in
hex. Instead, you can use the RGB macro:</P>
<PRE><FONT COLOR="#0066FF">Canvas-&gt;Brush-&gt;Color = RGB(255, 0, 0);  // Red

Canvas-&gt;Brush-&gt;Color = RGB(0, 255, 0);  // Green


Canvas-&gt;Brush-&gt;Color = RGB(0, 0, 255);  // Blue

</FONT></PRE>
<P>You can, of course, combine red, green, and blue to produce various shades:</P>
<PRE><FONT COLOR="#0066FF">RGB(255, 0, 255); // Purple

RGB(255, 255, 0); // Yellow

RGB(127, 127, 
127);  // Gray

</FONT></PRE>
<P>The VCL also provides a series of constants you can use to specify common colors.
<TT>clBlack</TT>, for instance, has the same internal number you would obtain from
the following code:</P>
<PRE><FONT 
COLOR="#0066FF">COLORREF clBlack = RGB(0, 0, 0);

</FONT></PRE>
<P>Here are some declarations from <TT>GRAPHICS.HPP</TT>:</P>
<PRE><FONT COLOR="#0066FF">#define clBlack TColor(0)

#define clMaroon TColor(128)

#define clGreen TColor(32768)

#define 
clOlive TColor(32896)

</FONT></PRE>
<P>If you want to experiment with this system, you can create a new project in BCB,
drop a button on the main form and run some changes on a method that looks like this:</P>
<PRE><FONT COLOR="#0066FF">void 
__fastcall TForm1::Button1Click(TObject *Sender)

{

  Canvas-&gt;Brush-&gt;Color = 0x0000FFFF;

  Canvas-&gt;Rectangle(0, 0, 200, 200);

}

</FONT></PRE>
<P>This code will draw a bright yellow rectangle in the upper right corner of the
main form. Try 
changing the values of the <TT>Brush</TT> color to <TT>0x00FF0000</TT>,
to <TT>RGB(255, 0, 255)</TT>, to <TT>clBlue</TT>, and so on. If you get tired of
drawing rectangles, you can switch to ellipses instead:</P>
<PRE><FONT 
COLOR="#0066FF">Canvas-&gt;Ellipse(0, 0, 200, 200);

</FONT></PRE>
<P>I talk more about drawing shapes with a canvas later in the chapter.</P>
<P>Most of the time you will pick colors from the <TT>Color</TT> property editor
provided by most 
components. For instance, you can double click the <TT>Color</TT>
property for a <TT>TForm</TT> object to bring up a <TT>TColorDialog</TT>. However,
there are times when you need to work with the <TT>TColor</TT> type directly, which
is why I have 
explained the topic in some detail.
<H4><A NAME="Heading5"></A><FONT COLOR="#000077">The Canvas Object</FONT></H4>
<P>All forms, and many components, have a canvas. You can think of a canvas as being
the surface on which graphics objects can paint. In 
short, the metaphor used by the
developers here is of a painter's canvas.</P>
<P>This <TT>TCanvas</TT> object is brought into the core component hierarchy through
aggregation, which means in many cases that you access its features via a field of
an 
object such as a <TT>TForm</TT> or <TT>TImage</TT> object. For instance, you can
write the following:</P>
<PRE><FONT COLOR="#0066FF">Form1-&gt;Canvas-&gt;TextOut(1, 1, &quot;Hello from the canvas&quot;);

</FONT></PRE>
<P>This statement writes the 
words <TT>&quot;Hello from the canvas&quot;</TT> in
the upper-left corner of a form. Notice that you do not have to access or reference
a device context directly in order to use this method.</P>
<P>Of course, most of the time you are accessing 
<TT>Form1</TT> from inside of <TT>Form1</TT>,
so you can write</P>
<PRE><FONT COLOR="#0066FF">Canvas-&gt;TextOut(1, 1, &quot;Hello from the canvas&quot;);

</FONT></PRE>
<P>However, you cannot write</P>
<PRE><FONT COLOR="#0066FF">Form1-&gt;TextOut(1, 
1, &quot;Hello from the canvas&quot;);

</FONT></PRE>
<P>This is because the <TT>Canvas</TT> object is aggregated into <TT>Form1</TT>.
<TT>TCanvas</TT> is not brought in through multiple inheritance. Furthermore, the
aggregation does not attempt to 
wrap each of the methods of <TT>TCanvas</TT> inside
methods of <TT>TForm</TT>. Aggregation is discussed in more detail in Chapters 19,
&quot;Inheritance,&quot; and 20, &quot;Encapsulation.&quot;</P>
<P>The <TT>Canvas</TT> object has several key 
methods that all VCL programmers should
know: <BR>
<BR>

<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>Arc</TT></TD>
		<TD ALIGN="LEFT">Draw an arc</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" 
ALIGN="LEFT"><TT>Chord</TT></TD>
		<TD ALIGN="LEFT">A closed figure showing the intersection of a line and an ellipse</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>CopyRect</TT></TD>
		<TD ALIGN="LEFT">Copy an area of 
one canvas to another</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>Draw</TT></TD>
		<TD ALIGN="LEFT">Draw a bitmap or other graphic on a canvas</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" 
ALIGN="LEFT"><TT>Ellipse</TT></TD>
		<TD ALIGN="LEFT">Draw an ellipse</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>FillRect</TT></TD>
		<TD ALIGN="LEFT">Fill a rectangle</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD 
WIDTH="106" ALIGN="LEFT"><TT>FloodFill</TT></TD>
		<TD ALIGN="LEFT">Fill an enclosed area</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>FrameRect</TT></TD>
		<TD ALIGN="LEFT">Draw a border around a rectangle</TD>
	</TR>
	
<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>LineTo</TT></TD>
		<TD ALIGN="LEFT">Draw a line</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>MoveTo</TT></TD>
		<TD ALIGN="LEFT">Draw a line</TD>
	</TR>
	
<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>Pie</TT></TD>
		<TD ALIGN="LEFT">Draw a pie-shaped object</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>Polygon</TT></TD>
		<TD ALIGN="LEFT">Draw a 
multisided object</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>PolyLine</TT></TD>
		<TD ALIGN="LEFT">Connect a set of points on the canvas</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" 
ALIGN="LEFT"><TT>Rectangle</TT></TD>
		<TD ALIGN="LEFT">Draw a rectangle</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>RoundRect</TT></TD>
		<TD ALIGN="LEFT">Draw a rectangle with rounded corners</TD>
	</TR>
	<TR 
ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>StretchDraw</TT></TD>
		<TD ALIGN="LEFT">Same as <TT>Draw</TT>, but stretches the object to fill an area</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" 
ALIGN="LEFT"><TT>TextHeight</TT></TD>
		<TD ALIGN="LEFT">The height of a string in the current font</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>TextOut</TT></TD>
		<TD ALIGN="LEFT">Output text</TD>
	</TR>
	<TR 
ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>TextRect</TT></TD>
		<TD ALIGN="LEFT">Output text in a defined area</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="106" ALIGN="LEFT"><TT>TextWidth</TT></TD>
		<TD ALIGN="LEFT">The 
width of a string in the current font</TD>
	</TR>
</TABLE>
<BR>
The following properties of the <TT>Canvas</TT> object are important:</P>
<PRE><FONT COLOR="#0066FF">Font

Brush

Pen

Pixels

</FONT></PRE>
<P>The following events can be important to 
some very technical programmers:</P>
<PRE><FONT COLOR="#0066FF">OnChange

OnChanging

</FONT></PRE>
<P>If you know the Windows API, many of these methods will be familiar to you. The
big gain from using the <TT>Canvas</TT> object rather than the raw 
Windows GDI calls
is that the resources you use will be managed for you automatically. In particular,
you never need to obtain a device context, nor do you have to select an object into
a device context.</P>
<P>In some cases, you will get better 
performance if you write directly to the Windows
GDI. However, it's a mistake to assume that you will always get better by doing so.
For instance, the VCL graphics subsystem will cache and share resources in a sophisticated
manner that would be very 
difficult to duplicate in your own code. My personal opinion
is that you should use the VCL graphics routines whenever possible, and only turn
to the raw Windows API when you run up against an area of graphics not covered by
the VCL.</P>
<P>The 
DrawShapes program demonstrates how easy it is to use the <TT>TCanvas</TT>
object in a program. This application delineates the outlines of a simple paint program
that has the capability to draw lines, rectangles, and ellipses. A screen shot of
the 
program is shown in Figure 7.1, and the source for the program appears in Listings
7.1 and 7.2.<BR>
<BR>
<A NAME="Heading6"></A><A HREF="07EBU01.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/07/07EBU01.jpg">FIGURE 7.1.</A><FONT COLOR="#000077">
</FONT><I>The DrawShapes program shows how to create the 
lineaments of a simple paint
program.</I></P>
<P><A NAME="Heading7"></A><FONT COLOR="#000077"><B>Listing 7.1. The header file for
the DrawShapes program declares an enumerated type and several simple fields.</B></FONT></P>
<PRE><FONT 
COLOR="#0066FF">///////////////////////////////////////

// Main.cpp

// DrawShapes Example

// Copyright (c) 1997 by Charlie Calvert

//

#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\Menus.hpp&gt;

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

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

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

enum TCurrentShape {csLine, csRectangle, csEllipse};


class TForm1 : public TForm

{

__published:

  TMainMenu *MainMenu1;

  TMenuItem *File1;

  TMenuItem *Open1;

  TMenuItem *Save1;

  TMenuItem *N1;

  TMenuItem *Rectangle1;

  TMenuItem *Shapes1;

  TMenuItem *Rectangle2;

  TMenuItem *Ellipse1;

⌨️ 快捷键说明

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