How can I make my program load an image and make it the background for a form? I need the exact code for it. I've looked all over the internet and the only things I've found are various tweaks and fixes to make backgrounds work as intended in special circumstances. I've also tried some Delphi books I have and I can't find it anywhere.
Put a
TImage
on your form. Make sure it's behind all other controls on the form. You can right-click it and choose the "send to back" menu option.Load a graphic.
var img: TBitmap; begin img := TBitmap.Create; try img.LoadFromFile('S:\background.bmp');
Assign it to the image control.
Image1.Picture := img;
Clean up.
finally img.Free; end; end;
You can also combine the last three steps to load the graphic and put it in the image control all at once. Thanks to Jon for the suggestion.
Image1.Picture.LoadFromFile('B:\background.bmp');
I believe it's done by overriding the default behaviour for the WM_ERASEBACKGROUND message. Have a look here http://www.google.be/search?rlz=1C1GGLS%5FnlBE291BE303&sourceid=chrome&ie=UTF-8&q=delphi+wmerasebackground+background+image (perhaps there's an example here http://www.google.com/codesearch?hl=nl&lr=&q=wmerasebackground+bitmap+lang%3Apascal&sbtn=Zoeken )
What I would do is use the forms OnPaint event, get the canvas (Form1.Canvas), and then use the Draw method (which takes an image) to draw the image you want. Something like the following:
procedure TForm1.FormPaint(Sender: TObject);
var
mypic: TBitMap;
begin
mypic := TBitMap.Create;
try
mypic.LoadFromFile('cant.bmp');
Form1.Canvas.Draw(0, 0, mypic);
finally
FreeAndNil(mypic);
end;
end;
Note that this could be extremely slow.