The TScrollBar Component


Although many controls have built-in scrolling capabilities, there are situations when we have to let the user scroll controls that don't support scrolling. The two most important properties of the TScrollBar component are the Kind and Position properties. The Kind property enables you to define whether the scroll bar is vertical or horizontal, and the Position property enables you to determine the position of the scroll bar.

Additionally, the Min and Max properties are used to limit the scrolling range of the scroll bar. The most important event is the OnChange event that fires every time the position of the scroll bar is modified.

The TScrollBar component, for instance, enables you to easily scroll an image on the form. To enable image scrolling, place a TImage component in a container component like TPanel and set the AutoSize property of the TImage component to True. Then add two TScrollBar components and set the Kind property of one TScrollBar component to sbVertical.

image from book
Figure 14-18: Components needed to implement image scrolling

In order to scroll an image, we have to load an image into the TImage component. To do this, use the TImage.Picture.LoadFromFile method. The Picture property enables the TImage component to load different file types like bitmaps, icons, and metafiles. The following example tries to load an image called image from book image.bmp from the application root directory.

procedure TMainForm.FormCreate(Sender: TObject); var   ImagePath: string; begin   ImagePath := ExtractFilePath(Application.ExeName) + 'image.bmp';   Image1.Picture.LoadFromFile(ImagePath);   { define scroll range }   VertBar.Max := Image1.Height - Panel1.Height;   HorzBar.Max := Image1.Height; end; 

To scroll the image, write event handlers for the OnChange event of the two TScrollBar components and use their Position property to determine how much the image should be scrolled horizontally and vertically.

Listing 14-22: Scrolling an image

image from book
procedure TMainForm.HorzBarChange(Sender: TObject); begin   Image1.Left := -HorzBar.Position; end; procedure TMainForm.VertBarChange(Sender: TObject); begin   Image1.Top := -VertBar.Position; end; 
image from book

image from book
Figure 14-19: Scrolling an image with the TScrollBar component



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net