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 properties of a...

Mastering 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 Animate Control in a Button

Now that you know how the Animate control works, we can use it to build another animated button. Simply place an Animate control and a large button (possibly with a large font as well) in a form. Then write the following code to make the button the parent window of the Animate control at run time and position it properly procedure TForm1.FormCreate(Sender TObject) var hDiff Button1.Height - Animate1.Height Animate1.SetBounds (hDiff div 2, hDiff div 2, Animate1.Width, Animate1.Height)...

Drawing in a Bitmap

I've already mentioned that by using an Image component, you can draw images directly in a bitmap. Instead of drawing on the surface of a window, you draw on a bitmap in memory and then copy the bitmap to the surface of the window. The advantage is that instead of having to repaint the image each time an OnPaint event occurs, the component copies the bitmap back to video. Technically, a TBitmap object has its own canvas. By drawing on this canvas, you can change the contents of the bitmap. As...

Printing Shapes

Besides painting the shapes on a form canvas, we can paint them on a printer canvas, effectively printing them Because it is possible to execute the same methods on a printer canvas as on any other canvas, you might be tempted to add to the program a new method for printing the shapes. This is certainly easy, but an even better option is writing a single output method to use for both the screen and the printer. As an example of this approach, I've built a new version of the program, called...

Figure

When you place a new grid component on a form, it contains one fixed row and one fixed column by default. In its appearance, the grid can have lines of different sizes, or it can have no lines. You can set the size of each column or row independently of the others because the RowSize, ColWidth, and RowHeight properties are arrays. For the grid's behavior, you can let the user resize the columns and the rows (goColSizing and goRowSizing), drag entire columns and rows to a new position...

Building a First Example

To build this example I placed a TeeChart component in a form and then simply added four 3D Bar series that is, four objects of the TBarSeries class. Then I set up some global properties, such as the title of the chart, and so on. Here is a summary of this information, taken from the textual description of the form object Chartl TChart AnimatedZoom True Title.Text.Strings 'Simple TeeChart Demo for Mastering Delphi' BevelOuter bvLowered object Seriesl SeriesColor Marks.Visible end object Series2...

An Image Viewer

The ShapeBmp program can be used as an image viewer, because you can load any bitmap in it. In general, in the Image control you can load any graphic file type that has been registered with the VCL TPicture class. The default file formats are bitmap files BMP , icon files ICO , or Windows metafiles WMF . Bitmap and icon files are well-known formats. Windows metafiles, however, are not so common. They are a collection of graphical commands, similar to a list of GDI function calls that need to be...

Mines in a Grid

The StringGrid component uses the Cells array to store the values of the elements and also has an Objects property to store custom data for each cell. The DrawGrid component, instead, doesn't have a predefined storage. For this reason, the next example defines a two-dimensional array to store the value of the grid's cells that is, of the playing field. The Mines example is a clone of the MineSweeper game included with Windows. If you have never played this game, I suggest you try it and read...

Bitmaps to the

When the Image control is connected to a bitmap, there are some additional operations you can do, but before we examine them, I have to introduce bitmap formats. There are different types of bitmaps in Windows. Bitmaps can be device-independent or not, a term used to indicate whether the bitmap has extra palette management information. BMP files are usually device-independent bitmaps. Another difference relates to the color depth that is, the number of different colors the bitmap can use or, in...

Scrolling an Image

An advantage of the way automatic scrolling works in Delphi is that if the size of a single big component contained in a form changes, scroll bars are added or removed automatically. A good example is the use of the Image component. If the AutoSize property of this component is set to True and you load a new picture into it, the component automatically sizes itself, and the form adds or removes the scroll bars as needed. If you load a large bitmap in the ImageV example, you will notice that...

Adding Data to the Chart

Now we simply initialize the data of the string grid and copy it to the series of the chart. This takes place in the handler of the OnCreate event of the form. This method fills the fixed items of the grid and the series names, then fills the data portion of the string grid, and finally calls the handler of the OnClick event of the Update button, to update the chart procedure TForm1.FormCreate Sender TObject var fills the fixed column and row, and the chart series names for I 1 to 5 do Cells I,...

The Rotating World

The second example of animation, World, has a button featuring the earth, which slowly rotates, showing the various continents. You can see some samples in Figure 22.11, but, of course, you should run the program to see its output. In the previous example, the image changed when the button was pressed. Now the image changes by itself, automatically. This occurs thanks to the presence of a Timer component, which receives a message at fixed time intervals. Here is a summary of the component...

Rotating Text

In this chapter, we've covered a lot different examples of the use of bitmaps, and we've created graphics of many types. However, the most important type of graphics we usually deal with in Delphi applications is text. In fact, even when showing a label or the text of an Edit box, Windows still paints it in the same was as any other graphical element. I've actually presented an example of font painting earlier in this chapter in the FontGrid example. Now I'm getting back to this topic with a...

An Animated Bitmap in a Button

Bitmap buttons are easy to use and can produce better-looking applications than the standard push buttons the Button component . To further improve the visual effect of a button, we can also think of animating the button. There are basically two kinds of animated buttons buttons that change their glyph slightly when they are pressed and buttons having a moving image, regardless of the current operation. I'll show you a simple example of each kind, Fire and World. For each of these examples,...

Many Images in a Bitmap

The Fire example used a manual approach. I loaded two bitmaps and changed the value of the Glyph property when I wanted to change the image. The BitBtn component, however, can also handle a number of bitmaps automatically. You can prepare a single bitmap that contains a number of images or glyphs and set this number as the value of the NumGlyphs property. All such sub-bitmaps must have the same size because the overall bitmap is divided into equal parts. If you provide more than one glyph in...

Using Tee Chart

TeeChart is a VCL-based charting component built by David Berneda and licensed to Borland for inclusion in the Developer and Client Server versions of Delphi. The TeeChart component is very complex Delphi includes a Help file and other reference material for this component, so I won't spend time listing all of its features. I'll just build a couple of examples. TeeChart comes in three versions the stand-alone component in the Additional page of the Component Palette , the data-aware version in...

Drawing Shapes

Now let's look at an Image component example that will paint on a bitmap. The idea is simple. I've basically written a simplified version of the Shape example, by placing an Image component on its form and redirecting all the output operations to the canvas of this Image component. In this example, ShapeBmp, I've also added some new menu items to save the image to a file and to load an existing bitmap. To accomplish this, I've added to the form a couple of default dialog components, OpenDialog...

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 nonstandard 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...

Graphical Grids

Grids represent another interesting group of Delphi graphical components. The system offers different grid components a grid of strings, one of images, database-related grids, and a sample grid of colors. The first two kinds of grids are particularly useful because they allow you to represent a lot of information and let the user navigate it. Of course, grids are extremely important in database programming, and they can be customized with graphics as we've seen in Chapter 10 The DrawGrid and...

Grid of Fonts

If you place a StringGrid component on a form and set its options properly, you have a full working editor of strings arranged in a grid, without doing any programming at all. To make the example more interesting, I've decided to draw each cell of the grid with a different font, varying both its size and its typeface. You can see the result of the FontGrid program in Figure 22.15. An example of the output of the FontGrid application An example of the output of the FontGrid application A amp...

Two State Button

The first example, the Fire program, has a very simple form, containing only a bitmap button. This button is connected to a Glyph representing a cannon. Imagine such a button as part of a game program. As the button is pressed, the glyph changes to show a firing cannon. As soon as the button is released, the default glyph is loaded again. In between, the program displays a message if the user has actually clicked the button. To write this program, we need to handle three of the button's events...

List of Bitmaps the Use of Resources and a Control Canvas

The World program works, but it is very slow, for a couple of reasons. First of all, at each timer interval, it needs to read a file from the disk, and although a disk cache can make this faster, it is certainly not the most efficient solution. Besides reading the file from disk, the program has to create and destroy Windows bitmap objects, and this takes some time. The second problem depends on how the image is updated When you change the button's bitmap, the component is completely erased and...

The Animate Control

There is a better way to obtain animation than displaying a series of bitmaps in sequence. Use the Win32 Animate common control. The Animate control is based on the use of AVI Audio Video Interleaved files, a series of bitmaps similar to a movie. Actually, the Animate control can display only those AVI files that have a single video stream, are uncompressed or compressed with RLE8 compression, and have no palette changes and if they have sound, it is ignored. In practice, the files...