• No results found

Making methods virtual

In document Component Writer s Guide (Page 66-69)

You make methods virtual when you want different types to be able to execute different code in response to the same method call.

If you create components intended to be used directly by application developers, you can probably make all your methods nonvirtual. On the other hand, if you create abstract components from which other components will be derived, consider making the added methods virtual. This way, derived components can override the inherited

virtual methods.

Declaring methods

Declaring a method in a component is the same as declaring any class method. To declare a new method in a component, do the following:

• Add the declaration to the component’s object-type declaration.

• Implement the method in the implementation part of the component’s unit. The following code shows a component that defines two new methods, one protected static method and one public virtual method.

type

TSampleComponent = class(TControl)

protected

procedure MakeBigger; { declare protected static method }

public

function CalculateArea: Integer; virtual; { declare public virtual method }

end; ƒ

implementation

ƒ

procedure TSampleComponent.MakeBigger; { implement first method }

begin

Height := Height + 5; Width := Width + 5;

end;

function TSampleComponent.CalculateArea: Integer; { implement second method }

begin

Result := Width * Height;

C h a p t e r

6

Chapter6

Using graphics in components

Windows provides a powerful graphics device interface (GDI) for drawing device- independent graphics. The GDI, however, imposes extra requirements on the programmer, such as managing graphic resources. Delphi takes care of all the GDI drudgery, allowing you to focus on productive work instead of searching for lost handles or unreleased resources.

As with any part of the Windows API, you can call GDI functions directly from your Delphi application. But you will probably find that using Delphi’s encapsulation of the graphic functions is faster and easier.

Note GDI functions are Windows-specific and do not apply to CLX applications. However, CLX components use the Qt library.

The topics in this section include: • Overview of graphics

• Using the canvas • Working with pictures • Off-screen bitmaps • Responding to changes

Overview of graphics

Delphi encapsulates the Windows GDI (Qt in CLX applications) at several levels. The most important to you as a component writer is the way components display their images on the screen. When calling GDI functions directly, you need to have a handle to a device context, into which you have selected various drawing tools such as pens, brushes, and fonts. After rendering your graphic images, you must restore the device

Instead of forcing you to deal with graphics at a detailed level, Delphi provides a simple yet complete interface: your component’s Canvas property. The canvas ensures that it has a valid device context, and releases the context when you are not using it. Similarly, the canvas has its own properties representing the current pen, brush, and font.

The canvas manages all these resources for you, so you need not concern yourself with creating, selecting, and releasing things like pen handles. You just tell the canvas what kind of pen it should use, and it takes care of the rest.

One of the benefits of letting Delphi manage graphic resources is that it can cache resources for later use, which can speed up repetitive operations. For example, if you have a program that repeatedly creates, uses, and disposes of a particular kind of pen tool, you need to repeat those steps each time you use it. Because Delphi caches graphic resources, chances are good that a tool you use repeatedly is still in the cache, so instead of having to recreate a tool, Delphi uses an existing one.

An example of this is an application that has dozens of forms open, with hundreds of controls. Each of these controls might have one or more TFont properties. Though this could result in hundreds or thousands of instances of TFont objects, most applications wind up using only two or three font handles, thanks to a font cache. Here are two examples of how simple Delphi’s graphics code can be. The first uses standard GDI functions to draw a yellow ellipse outlined in blue on a window, the way you would using other development tools. The second uses a canvas to draw the same ellipse in an application written with Delphi.

procedure TMyWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);

var

PenHandle, OldPenHandle: HPEN; BrushHandle, OldBrushHandle: HBRUSH;

begin

PenHandle := CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); { create blue pen } OldPenHandle := SelectObject(PaintDC, PenHandle); { tell DC to use blue pen } BrushHandle := CreateSolidBrush(RGB(255, 255, 0)); { create a yellow brush } OldBrushHandle := SelectObject(PaintDC, BrushHandle); { tell DC to use yellow brush } Ellipse(HDC, 10, 10, 50, 50); { draw the ellipse } SelectObject(OldBrushHandle); { restore original brush } DeleteObject(BrushHandle); { delete yellow brush } SelectObject(OldPenHandle); { restore original pen } DeleteObject(PenHandle); { destroy blue pen }

end;

procedure TForm1.FormPaint(Sender: TObject);

begin

with Canvas do

begin

Pen.Color := clBlue; { make the pen blue } Brush.Color := clYellow; { make the brush yellow } Ellipse(10, 10, 50, 50); { draw the ellipse }

end;

U s i n g t h e c a n v a s

In document Component Writer s Guide (Page 66-69)

Related documents