How to create a TScrollBar with a background bitmap #120

The example listed below shows a scrollbar with a BackBitMap property. You can set any picture you like there and it will be painted as the background of the control.

{ ... }
  TMyPictureScrollBar = class(TScrollBar)
  protected
    FBackBitMap: TBitMap;
    procedure CNCtlColorScrollBar(var Msg: TMessage);
      message CN_CTLCOLORSCROLLBAR;
    procedure SetBackBitMap(AValue: TBitMap);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property BackBitMap: TBitMap read FBackBitMap write SetBackBitMap;
  end;

constructor TMyPictureScrollBar.Create(AOwner: TComponent);
begin
  FBackBitMap := TBitMap.Create;
  FBackBitMap.Canvas.Brush.Color := clYellow;
  inherited Create(AOwner);
end;

destructor TMyPictureScrollBar.Destroy;
begin
  FBackBitMap.Free;
  FBackBitMap := nil;
  inherited Destroy;
end;

procedure TMyPictureScrollBar.CNCtlColorScrollBar(var Msg: TMessage);
begin
  if not FBackBitMap.Empty then
    FBackBitMap.Canvas.Brush.Bitmap := FBackBitMap;
  Msg.Result := FBackBitMap.Canvas.Brush.Handle;
end;

procedure TMyPictureScrollBar.SetBackBitMap(AValue: TBitMap);
begin
  FBackBitMap.Assign(AValue);
  Invalidate;
end;
Original resource: The Delphi Pool
Author: Serge Gubenko
Added: 2009/10/28
Last updated: 2009/10/28