Board index » delphi » Painting a form's background with a BMP

Painting a form's background with a BMP

Hi All,

I'm sure this question has been addressed a million times, so forgive me (I
can't find an answer).

I want to paint a bitmap on the background of a form. The bitmap can be any
size, but I want to paint it stretched.  I am using the following code:

type
  TForm1 = class(TForm)
  ....
private
  procedure Paint; override;

...

implementation

procedure TForm1.Paint;
begin
  inherited;
  Canvas.StretchDraw(Rect(0,0,Width,Height),Form2.Image1.Picture.Bitmap);
end;

Form2 is just a form I created for convenience to test this idea. The proc
works great, except for a few things.

For one, Rect(0,0,Width,Height) doesn't seem to get to the very left and
very top of the form (oh yeah, Form1 has BorderStyle = bsNone), I have to
use Rect(-GetSystemMetrics(SM_CXFRAME) * 2,-GetSystemMetrics(SM_CYFRAME) *
2,Width,Height).

Another thing that I can't fix is painful flickering everytime something
covering Form1 is moved. Is there any way to redraw only the portion that
needs redrawing?

A third thing is that SOME controls that are supposed to be transparent are
not anymore, but some are??? These controls are 3rd party (1st class) so the
fault may lie there - I don't know.

Any help is, of course, greatly appreciated.

TIA,
Rich

 

Re:Painting a form's background with a BMP


  If you are just wanting to have a bitmap on the forms background
Then why don't you just add a TImage to the Form. Add the Bitmap that you
want
and set the size and position that you want.
  But using this simple method, all controls that are added. Will cover that
part of the image. The only controls that will not cover will be
TBevel
And there may be others, But this is something to think about.
This will also take away the Flickering.

This link below is to the Newsgroup for a posting that I had made several
months ago.
But it was for loading and running an Animated wizard.gif, that had a bad
flickering issue.

http://216.101.185.148/scripts/isapi.dll/article?id=234E7759&article=...

Hope this helps.

Wayne
p.s. I would do the TImage one myself, as long as you are not doing
something totally
different.

Quote
"Rich" <r...@vistasoftware.com> wrote in message news:3cebb41c$1_2@dnews...
> Hi All,

> I'm sure this question has been addressed a million times, so forgive me
(I
> can't find an answer).

> I want to paint a bitmap on the background of a form. The bitmap can be
any
> size, but I want to paint it stretched.  I am using the following code:

> type
>   TForm1 = class(TForm)
>   ....
> private
>   procedure Paint; override;

> ...

> implementation

> procedure TForm1.Paint;
> begin
>   inherited;
>   Canvas.StretchDraw(Rect(0,0,Width,Height),Form2.Image1.Picture.Bitmap);
> end;

> Form2 is just a form I created for convenience to test this idea. The proc
> works great, except for a few things.

> For one, Rect(0,0,Width,Height) doesn't seem to get to the very left and
> very top of the form (oh yeah, Form1 has BorderStyle = bsNone), I have to
> use Rect(-GetSystemMetrics(SM_CXFRAME) * 2,-GetSystemMetrics(SM_CYFRAME) *
> 2,Width,Height).

> Another thing that I can't fix is painful flickering everytime something
> covering Form1 is moved. Is there any way to redraw only the portion that
> needs redrawing?

> A third thing is that SOME controls that are supposed to be transparent
are
> not anymore, but some are??? These controls are 3rd party (1st class) so
the
> fault may lie there - I don't know.

> Any help is, of course, greatly appreciated.

> TIA,
> Rich

Re:Painting a form's background with a BMP


Quote
> I want to paint a bitmap on the background of a form. The bitmap can be any
> size, but I want to paint it stretched.  I am using the following code:

By far the easiest way to accomplish this is to drop a TImage on the form,
align it to alClient, set its Stretch property to true, load a bitmap into it.
Done. Just make sure the image is the bottommost control on the form.

If you are still plagues with flicker set the forms doublebuffered property to
true.

Quote
> procedure TForm1.Paint;
> begin
>   inherited;
>   Canvas.StretchDraw(Rect(0,0,Width,Height),Form2.Image1.Picture.Bitmap);
> end;

The best place to do the painting if you want to stick with this is in a
handler for the WM_ERASEBKGND message. Or you can leave the code in Paint and
just set the forms brush.style to bsClear. This way the form will not first
clear the background before the bitmap is painted.

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Re:Painting a form's background with a BMP


Hey Peter and Wayne,

I know about the TImage trick (that's the way I was doing it originally),
but I am trying to find a way to only have 1 bitmap at run time since my app
has over 100 forms that need to have the same background. I also hate it
when there is a TImage aligned to the client because it makes selecting
multiple controls on the form a hassle, and selecting the form itself can
only be done through the object inspector.

Thanks for the tips! I am going to try them right now.

Rich

Quote
"Rich" <r...@vistasoftware.com> wrote in message news:3cebb41c$1_2@dnews...
> Hi All,

> I'm sure this question has been addressed a million times, so forgive me
(I
> can't find an answer).

> I want to paint a bitmap on the background of a form. The bitmap can be
any
> size, but I want to paint it stretched.  I am using the following code:

> type
>   TForm1 = class(TForm)
>   ....
> private
>   procedure Paint; override;

> ...

> implementation

> procedure TForm1.Paint;
> begin
>   inherited;
>   Canvas.StretchDraw(Rect(0,0,Width,Height),Form2.Image1.Picture.Bitmap);
> end;

> Form2 is just a form I created for convenience to test this idea. The proc
> works great, except for a few things.

> For one, Rect(0,0,Width,Height) doesn't seem to get to the very left and
> very top of the form (oh yeah, Form1 has BorderStyle = bsNone), I have to
> use Rect(-GetSystemMetrics(SM_CXFRAME) * 2,-GetSystemMetrics(SM_CYFRAME) *
> 2,Width,Height).

> Another thing that I can't fix is painful flickering everytime something
> covering Form1 is moved. Is there any way to redraw only the portion that
> needs redrawing?

> A third thing is that SOME controls that are supposed to be transparent
are
> not anymore, but some are??? These controls are 3rd party (1st class) so
the
> fault may lie there - I don't know.

> Any help is, of course, greatly appreciated.

> TIA,
> Rich

Re:Painting a form's background with a BMP


Hi Peter,

Doing it in the WM_ERASEBKGND made it worse - but I'm sure I'm doing
something wrong. Here's my code:

procedure TForm1.WMEraseBkgnd(var Msg: TWMEraseBkgnd);
begin
 inherited;

Canvas.StretchDraw(Rect(-GetSystemMetrics(SM_CXFRAME)*2,-GetSystemMetrics(SM
_CYFRAME)*2,ClientWidth,ClientHeight),frmBackDrop.imgBackdrop.Picture.Bitmap
);
end;

Any suggestions?

TIA,
Rich

"Peter Below (TeamB)" <100113.1...@compuXXserve.com> wrote in message
news:VA.00008b52.009384c5@antispam.compuserve.com...

Quote
> > I want to paint a bitmap on the background of a form. The bitmap can be
any
> > size, but I want to paint it stretched.  I am using the following code:

> By far the easiest way to accomplish this is to drop a TImage on the form,
> align it to alClient, set its Stretch property to true, load a bitmap into
it.
> Done. Just make sure the image is the bottommost control on the form.

> If you are still plagues with flicker set the forms doublebuffered
property to
> true.

> > procedure TForm1.Paint;
> > begin
> >   inherited;

Canvas.StretchDraw(Rect(0,0,Width,Height),Form2.Image1.Picture.Bitmap);

- Show quoted text -

Quote
> > end;

> The best place to do the painting if you want to stick with this is in a
> handler for the WM_ERASEBKGND message. Or you can leave the code in Paint
and
> just set the forms brush.style to bsClear. This way the form will not
first
> clear the background before the bitmap is painted.

> --
> Peter Below (TeamB)
> Use the newsgroup archives :
> http://www.mers.com/searchsite.html
> http://www.tamaracka.com/search.htm
> http://groups.google.com
> http://www.prolix.be

Re:Painting a form's background with a BMP


Quote
"Rich" <r...@vistasoftware.com> wrote in message news:3cebfcc0$1_2@dnews...
> I know about the TImage trick (that's the way I was doing it originally),
> but I am trying to find a way to only have 1 bitmap at run time since my
> app has over 100 forms that need to have the same background.

If you copy the contents of your mainform TImage to new forms as they are
created it should keep resource usage down to reasonable levels.  (You won't
have 100 forms loaded at once, surely.)

Quote
> I also hate it
> when there is a TImage aligned to the client because it makes selecting
> multiple controls on the form a hassle, and selecting the form itself can
> only be done through the object inspector.

If the issue is one of positioning other controls then there is no hassle.
Select and drag all the required controls to their new position in the usual
way, on releasing the mouse the TImage will snap back to where it belongs -
aligned alClient.

To select the form, click on any control on the form and hit Escape until
you see that nothing is selected - that indicates that the form itself is
selected.

Regards,
Chris Luck.

Re:Painting a form's background with a BMP


Rich,  the idea of overriding WM_ERASEBACKGROUND so to NOT erase the
background.  So don't call inherited or else you'll still get flicker,  and
also I think the message result should be 1 - not too sure as I don't have
any source handy right now.  Since you don't appear to be returning a
message result,  then the message is treated as unhandled - however,  since
you're calling inherited, this may not be true.

Regards

Stan.

Re:Painting a form's background with a BMP


Quote
> (You won't have 100 forms loaded at once, surely.)

LOL! Maybe I will ....

Thanks Chris, those are both reasonable suggestions ... I'll see what I can
do.

Rich

Quote
"Chris Luck" <ch...@bvhg.freeXXserve.co.uk> wrote in message

news:3cec2e7d_2@dnews...
Quote
> "Rich" <r...@vistasoftware.com> wrote in message

news:3cebfcc0$1_2@dnews...
Quote
> > I know about the TImage trick (that's the way I was doing it
originally),
> > but I am trying to find a way to only have 1 bitmap at run time since my
> > app has over 100 forms that need to have the same background.

> If you copy the contents of your mainform TImage to new forms as they are
> created it should keep resource usage down to reasonable levels.  (You
won't
> have 100 forms loaded at once, surely.)

> > I also hate it
> > when there is a TImage aligned to the client because it makes selecting
> > multiple controls on the form a hassle, and selecting the form itself
can
> > only be done through the object inspector.

> If the issue is one of positioning other controls then there is no hassle.
> Select and drag all the required controls to their new position in the
usual
> way, on releasing the mouse the TImage will snap back to where it
belongs -
> aligned alClient.

> To select the form, click on any control on the form and hit Escape until
> you see that nothing is selected - that indicates that the form itself is
> selected.

> Regards,
> Chris Luck.

Re:Painting a form's background with a BMP


Quote
In article <3cec0797$1_2@dnews>, Rich wrote:
> Doing it in the WM_ERASEBKGND made it worse - but I'm sure I'm doing
> something wrong. Here's my code:

As Stan indicates you do *not* call inherited. Change to

 procedure TForm1.WMEraseBkgnd(var Msg: TWMEraseBkgnd);
 begin
   msg.result := 1;

Canvas.StretchDraw(Rect(-GetSystemMetrics(SM_CXFRAME)*2,-GetSystemMetrics(SM
 _CYFRAME)*2,ClientWidth,ClientHeight),frmBackDrop.imgBackdrop.Picture.Bitmap
 );
 end;
--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Other Threads