Jump to content
Willicious

Displaying an independent background in a Delphi app

Recommended Posts

I'm working on an existing application here, so I have no option but to work with the codebase that's already there. Very frustrating, I know, but that's why I'm taking to the Forums for help with this.

 

Basically, I'm trying to display a background image.

 

But that's not all.

 

Currently, the image on the form is comprised of a number of different elements that get combined into a resizable bitmap (principally, the idea here is that the image will then look the same in terms of positioning/spacing/etc no matter how big the window is).

 

One of these elements is the background.

 

The problem is that the bitmap is set to an INTERNAL_SCREEN_WIDTH and INTERNAL_SCREEN_HEIGHT. So, if the window is improportionately sized (for any reason), we end up with empty horizontal (or vertical, depending on which way the window is sized) borders at the edges. If these INTERNAL_SCREEN values are meddled with, it causes all sorts of other problems with the display elsewhere in the app, so these can't really be changed.

 

So, I want to separate the background from the combined bitmap image and display it independently, ideally tiling it across the actual background of the form, and then the remaining elements of the combined bitmap can display over it, scaling and resizing as normal. Then, the background image should - in theory - fill these empty spaces at the edges.

 

Here's what the code looks like:

 

procedure TGameBaseMenuScreen.InitializeImage;
begin
  with ScreenImg do
  begin
    Bitmap.SetSize(INTERNAL_SCREEN_WIDTH, INTERNAL_SCREEN_HEIGHT);
    DrawBackground;

    BoundsRect := Rect(0, 0, ClientWidth, ClientHeight);

    ScreenImg.Align := alClient;
    ScreenImg.ScaleMode := smResize;
    ScreenImg.BitmapAlign := baCenter;
  end;
end;

 

Here's what I've tried:

 

1) Moving "DrawBackground;" to before Bitmap.SetSize in an attempt to handle the drawing of the background before the setting of the internal screen width & height. The app runs, but the background image isn't displayed at all (instead it's just a black background). All of the other elements are displayed as normal.

 

2) This:

 

constructor TGameBaseMenuScreen.Create(aOwner: TComponent);
begin
  inherited;

  DrawBackground;
  InitializeImage;

...etc

So that DrawBackground is removed entirely from the "InitializeImage" procedure and run as a separate, independent procedure on creation of the menu screen. I get the same result (black background, other elements of the combined bitmap appearing as normal).

 

I have no idea what else to try, or why I'm getting the above result from what I've tried so far. Any suggestions would be very welcome!

Share this post


Link to post

It looks like you're using a Graphics32 TImage32 control to display the bitmap. You should have stated that as otherwise your code makes no sense.

 

I don't really understand what it is you are trying to do. Would you mind posting a picture/mockup of your desired output and another of the experienced output?

Share this post


Link to post
On 4/9/2023 at 8:23 PM, Anders Melander said:

Would you mind posting a picture/mockup of your desired output and another of the experienced output?

Yes, it is indeed a Graphics32 TImage32, apologies for not mentioning that previously.

 

So, here's the current behaviour. The background image, menu items and logo all form part of a single bitmap which is generated and then displayed within the fixed internal screen boundaries:

 

mockup.thumb.png.a1ca5a33d81aa12ddebe6ca58e0142da.png

 

At present, my only options for increasing the size of the background image are to increase these fixed values, which causes all sorts of other problems elsewhere in the app (so, this is an absolute last-resort option which I probably won't do; I'd rather put up with the borders than mess with these values tbh.)

 

The following is the desired behaviour. I want to display the background image completely independently, tiling it across the form and then displaying the menu bitmap on top of it:

 

mockup2.thumb.png.b7df6b15a0e7a682906745c1b1a07fa0.png

 

So now, the window can be resized and will always display the background. Meanwhile, the other menu items will always be centred according to the fixed internal screen values.

 

Question is, how do I achieve this?

Edited by Willicious

Share this post


Link to post
On 4/9/2023 at 11:17 PM, programmerdelphi2k said:

would be: 1 bitmap on background... and +1 other image over it?

 

and if you draw 1st bitmap on "canvas of form using OnPaint()"?

This sounds promising, I'll see if I can figure out how to make this work with what's there.

Share this post


Link to post

If I understand you correctly then you could just set ScreenImg.Bitmap.BitmapAlign=baTile and then display the other stuff on top of that with a TBitmapLayer.

Share this post


Link to post

@Willicious

 

maybe some like this:

var
  LBackgroundBitmap: TBitmap;

procedure MyLoadBackgroundBitmap;
begin
  LBackgroundBitmap := TBitmap.Create;
  LBackgroundBitmap.LoadFromFile('Background.bmp'); // use a picture with same size than "your screen"!
  //
  // -- try save resource/memory -- needs tests if works or not?
  // LBackgroundBitmap.Dormant;   // Free up GDI resources
  // LBackgroundBitmap.FreeImage; // Free up Memory... dont lose the bitmap!
  //
  // ....... LBackgroundBitmap.ReleaseHandle;  // This will actually lose the bitmap!
end;

procedure MyFreeBackgroundBitmap;
begin
  LBackgroundBitmap.Free;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  if (LBackgroundBitmap <> nil) and not(LBackgroundBitmap.Empty) then
    begin
      Canvas.Brush.Bitmap := LBackgroundBitmap;
      Canvas.FillRect(Rect(0, 0, Width, Height)); // Canvas.Draw(0, 0, LBackgroundBitmap);
    end;
end;

initialization

MyLoadBackgroundBitmap;

finalization

MyFreeBackgroundBitmap;

end.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×