Delphi 5 - Mastering Delphi 5 - Chapter 22 - Graphics.pdf
(
1910 KB
)
Pobierz
2784126 UNPDF
SYBEX Supplement
Mastering™ Delphi™ 5
by Marco Cantù
Chapter 22: Graphics in Delphi
Screen reproductions produced with Collage Complete.
Collage Complete is a trademark of Inner Media Inc.
SYBEX, Network Press, and the Network Press logo are registered trademarks of SYBEX Inc.
Mastering, Expert Guide, Developer’s Handbook, and No experience required. are trademarks of SYBEX Inc.
TRADEMARKS: SYBEX has attempted throughout this book to distinguish proprietary trademarks from descriptive terms by
following the capitalization style used by the manufacturer.
Netscape Communications, the Netscape Communications logo, Netscape, and Netscape Navigator are trademarks of
Netscape Communications Corporation.
Microsoft® Internet Explorer ©1996 Microsoft Corporation. All rights reserved. Microsoft, the Microsoft Internet Explorer logo,
Windows, Windows NT, and the Windows logo are either registered trademarks or trademarks of Microsoft Corporation in
the United States and/or other countries.
The author and publisher have made their best efforts to prepare this book, and the content is based upon final release soft-
ware whenever possible. Portions of the manuscript may be based upon pre-release versions supplied by software manufac-
turer(s). The author and the publisher make no representation or warranties of any kind with regard to the completeness or
accuracy of the contents herein and accept no liability of any kind including but not limited to performance, merchantability,
fitness for any particular purpose, or any losses or damages of any kind caused or alleged to be caused directly or indirectly
from this book.
Photographs and illustrations used in this book have been downloaded from publicly accessible file archives and are used in
this book for news reportage purposes only to demonstrate the variety of graphics resources available via electronic access.
Text and images available over the Internet may be subject to copyright and other rights owned by third parties. Online avail-
ability of text and images does not imply that they may be reused without the permission of rights holders, although the
Copyright Act does permit certain unauthorized reuse as fair use under 17 U.S.C. Section 107.
Copyright ©1999 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved. No part of this pub-
lication may be stored in a retrieval system, transmitted, or reproduced in any way, including but not limited to photocopy,
photograph, magnetic or other record, without the prior agreement and written permission of the publisher.
CHAPTER
TWENTY-TWO
22
Graphics in Delphi
Painting on a form
Animated buttons
An image viewer
Drawing over a bitmap
Graphical grids and games
Using TeeChart
Windows metafiles
4
Chapter 22 • Graphics in Delphi
I
n Chapter 6 of
Mastering Delphi 5,
I introduced the Canvas object, Windows
painting process, and the OnPaint event. In this bonus chapter, I’m going to start
from this point and continue covering graphics, following a number of different
directions. (For all the code discussed here and in
Mastering Delphi 5,
check the
Sybex Web site.)
I’ll start with the development of a complex program to demonstrate how the
Windows painting model works. Then I’ll focus on some graphical components,
such as graphical buttons and grids. During this part of the chapter we’ll also add
some animation to the controls.
Finally, this chapter will discuss the use of bitmaps, covering some advanced
features for fast graphics rendering, metafiles, the TeeChart component (including
its use on the Web), and few more topics related to the overall issue of graphics.
Drawing on a Form
In Chapter 6, we saw that it is possible to paint directly on the surface of a form in
response to a mouse event. To see this behavior, simply create a new form with
the following OnMouseDown event handler:
procedure
TForm1.FormMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
Canvas.Ellipse (X-10, Y-10, X+10, Y+10);
end
;
The program
seems
to work fairly well, but it doesn’t. Every click produces a new
circle, but if you minimize the form, they’ll all go away. Even if you cover a por-
tion of your form with another window, the shapes behind that other form will
disappear, and you might end up with partially painted circles.
As I detailed in Chapter 6, this direct drawing is not automatically supported by
Windows. The standard approach is to store the painting request in the OnMouse-
Down event and then reproduce the output in the OnPaint event. This event, in fact,
is called by the system every time the form requires
repainting
. However, you’ll
need to force its activation by calling the Invalidate or Repaint methods in the
mouse-event handler. In other words, Windows knows when the form has to be
repainted because of a system operation (such as placing another window in front
of your form), but your program must notify the system when painting is required
because of user input or other program operations.
Drawing on a Form
5
The Drawing Tools
All the output operations in Windows take place using objects of the TCanvas
class. The output operations usually don’t specify colors and similar elements but
use the current drawing tools of the canvas. Here is a list of these drawing tools
(or
GDI objects,
from the Graphics Device Interface, which is one of the Windows
system libraries):
•
The Brush property determines the color of the enclosed surfaces. The
brush is used to fill closed shapes, such as circles or rectangles. The proper-
ties of a brush are Color, Style, and optionally, Bitmap.
•
The Pen property determines the color and size of the lines and of the borders
of the shapes. The properties of a pen are Color, Width, and Style, which
includes several dotted and dashed lines (available only if the Width is 1
pixel). Another relevant subproperty of the Pen is the Mode property, which
indicates how the color of the pen modifies the color of the drawing surface.
The default is simply to use the pen color (with the pmCopy style), but it is also
possible to merge the two colors in many different ways and to reverse the
current color of the drawing surface.
•
The Font property determines the font used to write text in the form, using
the TextOut method of the canvas. A font has a Name, Size, Style, Color,
and so on.
TIP
Experienced Windows programmers should note that a Delphi canvas technically
represents a Windows device context. The methods of the
TCanvas
class are simi-
lar to the GDI functions of the Windows API. You can call extra GDI methods by
using the
Handle
property of the canvas, which is a handle of an
HDC
type.
Colors
Brushes, pens, and fonts (as well as forms and most other components) have a
Color property. However, to change the color of an element properly, using non-
standard colors (such as the color constants in Delphi), you should know how
Windows treats the color. In theory, Windows uses 24-bit RGB colors. This means
you can use 256 different values for each of the three basic colors (red, green, and
blue), obtaining 16 million different shades.
However, you or your users might have a video adapter that cannot display
such a variety of colors, although this is increasingly less frequent. In this case,
Windows either uses a technique called
dithering,
which basically consists of
Plik z chomika:
musli_com
Inne pliki z tego folderu:
Delphi 5 - Delphi 5 Referenz und Praxis.pdf
(6446 KB)
Delphi 5 - Entwicklerhandbuch.pdf
(5677 KB)
Delphi 5 - Referenz und Praxis.pdf
(5707 KB)
Delphi 5 - Object Pascal Language Guide.pdf
(3043 KB)
Delphi 5 - Sprachreferenz.pdf
(2961 KB)
Inne foldery tego chomika:
Delphi
Delphi 2005
Delphi 6
Delphi 7
Delphi 8
Zgłoś jeśli
naruszono regulamin