Give Your Forms a Background

Web pages use tiled bitmaps to create backgrounds. Is it possible to do this in Delphi?

Before I learned how to do this, to create a background on a form, I'd drop a TImage on my form, then set its Align property to alClient. For low-resolution bitmaps, the pixelation that would occur at times was absolutely terrible! But with the method that I'll show you here (Note: this is merely ONE way of doing it), you can easily tile bitmaps on the surface of your form. The trick is in trapping the WM_ERASEBKGND message in a handler, creating a bitmap at runtime, then writing a quick bit of code in the OnPaint event handler.  Let's go through the steps.

  1. In the private section of your code place the following:
      { Private declarations }
      MyBitmap: TBitmap;
      procedure WMEraseBkgnd(var m: TWMEraseBkgnd); 
        message WM_ERASEBKGND;

    Notice the declaration of MyBitmap. We'll be creating an instance for it below. The message handler for WM_ERASEBKGND looks like this:

    procedure TBmpform.WMEraseBkgnd(var m : TWMEraseBkgnd);
      m.Result := LRESULT(False);
  2. Then, create the following code for the OnPaint event handler Note: In the original article, the "x := x + MyBitmap.Width" is a bit inefficient in that continuously accessing the Bitmap.Width or .Height properties can slow things down - especially when you've got code in the OnPaint method. So what I did here was to simply set a couple of variables to store the Width and Height property values of the bitmap.
    procedure TBmpForm.FormPaint(Sender: TObject);
      x, y: Integer;
      iBMWid, iBMHeight : Integer;
      iBMWid := MyBitmap.Width;
      iBMHeight := MyBitmap.Height;
      y := 0;
      while y < Height do
        x := 0;
        while x < Width do
          Canvas.Draw(x, y, MyBitmap);
          x := x + iBMWid;
        y := y + iBMHeight;
  3. Finally, create an instance of the bitmap you want to tile in the background in the OnCreate event of your form:
    procedure TForm1.FormCreate(Sender: TObject);
      Application.OnHint := ShowHint;
      MyBitmap := TBitmap.Create;
  4. Whoops, almost forgot! You need to destroy the bitmap when you exit!
    procedure TForm1.FormClose(Sender: TObject; 
                      var Action: TCloseAction);
      Action := caFree;

Well, that's it. Don't you just love the quick and dirty ones?