Setting up background images for forms in Delphi

前端 未结 4 620
后悔当初
后悔当初 2020-12-22 01:48

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 fo

相关标签:
4条回答
  • 2020-12-22 02:04
    1. Put a TImageon 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.

    2. Load a graphic.

      var
        img: TBitmap;
      begin
        img := TBitmap.Create;
        try
          img.LoadFromFile('S:\background.bmp');
      
    3. Assign it to the image control.

          Image1.Picture := img;
      
    4. 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');
    

    See also: How to add background images to Delphi forms

    0 讨论(0)
  • 2020-12-22 02:08

    This is the way all my applications show a form image. I load the image at form creation or when the application calls a specific showing event

      var
        vDest, vRect:  TRect;
      begin
        vRect := Rect(0, 0, FBackgroundImage.Width, FBackgroundImage.Height);
        vDest := Rect(0,0,Self.Width, Self.Height);
        Canvas.StretchDraw(vDest, FBackgroundImage);
    
    
      if FileExists(this) then
        FBackgroundImage.LoadFromFile(this);
    
    0 讨论(0)
  • 2020-12-22 02:21

    @Brendan

    thanks
    //from Brendan code;
    
    var
    vDest, vRect:  TRect;
    FBackgroundImage: TGraphic;
    begin
    FBackgroundImage := image1.Picture.Graphic; //LOAD from invisible image
    vRect := Rect(0, 0, FBackgroundImage.Width, FBackgroundImage.Height);
    vDest := Rect(0,0,Self.Width, Self.Height);
    Canvas.StretchDraw(vDest, FBackgroundImage);
    end;
    
    0 讨论(0)
  • 2020-12-22 02:25

    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.

    0 讨论(0)
提交回复
热议问题