Author: Tomas Rutkauskas
How to draw lines and a bitmap on a TStatusPanel
Answer:
Example of drawing lines and BMP on StatusBar.Panels[1]. Assumes StatusBar is
placed on form. Right click on StatusBar to invoke panels editor. Add three panels
to StatusBar. Set Style for StatusBar.Panels[1] to psOwnerDraw. Add OnDrawPanel
event shown below to StatusBar to draw bitmap on Panels[1].
1 unit ScreenStatusBarBMP;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 ComCtrls, ExtCtrls;
8
9 type
10 TForm1 = class(TForm)
11 StatusBar: TStatusBar;
12 procedure FormCreate(Sender: TObject);
13 procedure StatusBarDrawPanel(StatusBar: TStatusBar; Panel: TStatusPanel; const
14 Rect: TRect);
15 private
16 { Private declarations }
17 public
18 { Public declarations }
19 end;
20
21 var
22 Form1: TForm1;
23
24 implementation
25
26 {$R *.DFM}
27
28 procedure TForm1.FormCreate(Sender: TObject);
29 begin
30 StatusBar.Panels[0].Text := 'Zero';
31 StatusBar.Panels[1].Text := 'One'; {ignored since psOwnerDraw style}
32 StatusBar.Panels[2].Text := 'Two'
33 end;
34
35 procedure TForm1.StatusBarDrawPanel(StatusBar: TStatusBar; Panel: TStatusPanel;
36 const Rect: TRect);
37 var
38 Bitmap: TBitmap;
39 begin
40 if Panel.Index = 1 then {not necessary if only one panel is owner drawn}
41 begin
42 {Draw red "X" in StatusPanel}
43 StatusBar.Canvas.Pen.Color := clRed;
44 StatusBar.Canvas.MoveTo(0, 0);
45 StatusBar.Canvas.LineTo(Rect.Right - 1, Rect.Bottom - 1);
46 StatusBar.Canvas.MoveTo(Rect.Left, Rect.Bottom - 1);
47 StatusBar.Canvas.LineTo(Rect.Right - 1, Rect.Top);
48 {Read Bitmap from file and display in middle of panel; In real app could get
49 bitmap
50 from resource file.}
51 Bitmap := TBitmap.Create;
52 try
53 Bitmap.LoadFromFile('C:\Program Files\Common Files\Images\Buttons\Alarm.BMP');
54 {Draw bitmap centered in panel}
55 StatusBar.Canvas.Draw((Rect.Left + Rect.Right - Bitmap.Width) div 2,
56 (Rect.Top + Rect.Bottom - Bitmap.Height) div 2, Bitmap);
57 finally
58 Bitmap.Free
59 end;
60 end;
61 end;
62
63 end.
|