Tile a non-MDIframe window with a backgrund bitmap

Spread the love

Tile a non-MDIframe window with a backgrund bitmap

type
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FWallpaper: TBitmap;
    procedure WMEraseBkGnd(var Msg: TWMEraseBkGnd); message WM_ERASEBKGND;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
procedure TForm1.FormDestroy(Sender: TObject);
begin
  FWallpaper.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Load the bitmap
  if OpenDialog1.Execute then
  begin
    if not Assigned(FWallpaper) then
      FWallpaper := TBitmap.Create;
    FWallpaper.LoadFromFile(OpenDialog1.FileName);
    Invalidate;
  end;
end;

procedure TForm1.WMEraseBkGnd(var Msg: TWMEraseBkGnd);
var
  row, col: Integer;
begin
  if not Assigned(FWallpaper) then
    inherited
  else
  begin
    // Draw the bitmap
    for Row := 0 to ClientHeight div FWallpaper.Height do
      for Col := 0 to ClientWidth div FWallpaper.Width do
        BitBlt(Msg.Dc,
          Col * FWallpaper.Width,
          Row * FWallpaper.Height,
          FWallpaper.Width,
          FWallpaper.Height,
          FWallpaper.Canvas.Handle,
          0,
          0,
          SRCCOPY);
    Msg.Result := 1;
  end; { else }
end;

end.