Wednesday, January 5, 2011

Adding A Background Image Directly To A Form

Windows, web pages, multimedia programs, etc. have backgrounds. How come your 
Delphi form doesn't?--------------------------------------------------------------------------------Of course you could place an Image component on your form and set it's Alignmentto Client to place a background on your form. But, here's another way to do it:
(1) Add following to your form's Public declarations section:

    bmpBackground : TBitmap;

(2) Double click on your form and add bitmap initialization code to the
FormCreate procedure:

    bmpBackground := TBitmap.Create;
    bmpBackground.LoadFromFile( 'c:\windows\setup.bmp' );

(3) Go to the form's events list and double click on OnPaint. Add followingline 
    to the FormPaint procedure:

    Canvas.Draw( 0, 0, bmpBackground );

(4) Finally insert the following code to FormDestroy
procedure (OnDestroy event):

    bmpBackground.Free
 
 
 
 
Or just Try This Code below :
 

unit Unit1;

interface

uses
  Windows, SysUtils, Classes, Graphics, Forms;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormDestroy(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
    backgroundImage : TBitmap;
  end;

var
  Form1: TForm1;                            

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
backgroundImage := TBitmap.Create;
backgroundImage.LoadFromFile(ExtractFilePath(Application.ExeName)+'background.bmp');
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
Canvas.Draw( 0, 0, backgroundImage );
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
backgroundImage.Free;
end;

end.

 
Attaching a background image directly to a form is easy; and a much less cumbersome alternative to using an image component stuck behind all your other controls.  Essentially you just create the TBitmap when the form is created, load the image from a file, then paint it onto the canvas.  (Don't forget to free the TBitmap at the end when the form is destroyed, as shown!)
Especially useful with 'containerless' forms (no titlebar or borders), with a little graphical ingenuity, you can create great-looking applications.  Since the image file is external, and loaded when the application starts, it also means you can change the 'skin' of the application simply by changing the image graphic.

Source : http://www.codefoot.com/delphi/form_background_image.shtml

 
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
      { 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);
    begin
      m.Result := LRESULT(False);
    end;
  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);
    var
      x, y: Integer;
      iBMWid, iBMHeight : Integer;
    begin
      iBMWid := MyBitmap.Width;
      iBMHeight := MyBitmap.Height;
      y := 0;
      while y < Height do
      begin
        x := 0;
        while x < Width do
        begin
          Canvas.Draw(x, y, MyBitmap);
          x := x + iBMWid;
        end;
        y := y + iBMHeight;
      end;
    end;
  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);
    begin
      Application.OnHint := ShowHint;
      MyBitmap := TBitmap.Create;
      MyBitmap.LoadFromFile('Brick4.bmp');
    end;
  4. Whoops, almost forgot! You need to destroy the bitmap when you exit!
    procedure TForm1.FormClose(Sender: TObject; 
                      var Action: TCloseAction);
    begin
      Action := caFree;
      bmpBackground.Free;
    end;
Well, that's it. Don't you just love the quick and dirty ones?
 
 
 

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum