2010. november 17., szerda

How to draw lines and a bitmap on a TStatusPanel


Problem/Question/Abstract:

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

unit ScreenStatusBarBMP;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    StatusBar: TStatusBar;
    procedure FormCreate(Sender: TObject);
    procedure StatusBarDrawPanel(StatusBar: TStatusBar; Panel: TStatusPanel; const Rect: TRect);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  StatusBar.Panels[0].Text := 'Zero';
  StatusBar.Panels[1].Text := 'One'; {ignored since psOwnerDraw style}
  StatusBar.Panels[2].Text := 'Two'
end;

procedure TForm1.StatusBarDrawPanel(StatusBar: TStatusBar; Panel: TStatusPanel;
  const Rect: TRect);
var
  Bitmap: TBitmap;
begin
  if Panel.Index = 1 then {not necessary if only one panel is owner drawn}
  begin
    {Draw red "X" in StatusPanel}
    StatusBar.Canvas.Pen.Color := clRed;
    StatusBar.Canvas.MoveTo(0, 0);
    StatusBar.Canvas.LineTo(Rect.Right - 1, Rect.Bottom - 1);
    StatusBar.Canvas.MoveTo(Rect.Left, Rect.Bottom - 1);
    StatusBar.Canvas.LineTo(Rect.Right - 1, Rect.Top);
    {Read Bitmap from file and display in middle of panel; In real app could get bitmap
    from resource file.}
    Bitmap := TBitmap.Create;
    try
      Bitmap.LoadFromFile('C:\Program Files\Common Files\Images\Buttons\Alarm.BMP');
      {Draw bitmap centered in panel}
      StatusBar.Canvas.Draw((Rect.Left + Rect.Right - Bitmap.Width) div 2,
        (Rect.Top + Rect.Bottom - Bitmap.Height) div 2, Bitmap);
    finally
      Bitmap.Free
    end;
  end;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése