DelphiFAQ Home Search:

Putting a background image on a ListView (Delphi 7)

 

comments5 comments. Current rating: 4 stars (4 votes). Leave comments and/ or rate it.

Question:

I need to display a bitmap as a background picture in a TListView control and it seems not to work.
I send a message LVM_SETBKIMAGE with the handle to a TLVBkImage, but no success.

Answer:

The code is ok but you need to change the handling of the WM_ERASEBKGND message by either traditional subclassing or - more Delphi style - defining your own TMyListView derived from TListView as shown in the example. Then change the declaration of your listview control from TListView to TMyListView (both in your DFM and in your PAS file) and it will work.

Notes:

  1. I noticed that I had to provide a full path to the image file. Just the file name (even though it was located in the application directory) did not work.
  2. BenoƮt Thonnart, a reader of this page wrote me that after he added uses ComObj; to his (version of this) code, it worked.

You can download a complete demo project that will compile under Delphi 7 (and since it has the constant definitions integrated, also under Delphi 5 and Delphi 6).
TListviewBackgroundImage.zip(240 kB)

type
  TMyListView = class(TListView)
  protected
    procedure WndProc(var Message: TMessage);
      override;
  end;

procedure TMyListView.WndProc(var Message: TMessage);
begin
  if Message.Msg = WM_ERASEBKGND then
    DefaultHandler(Message)
  else
    inherited WndProc(Message);
end;

procedure TForm1.LVBGImage;
var
  BKimg : TLVBKIMAGE;
begin
  FillChar(BKimg, SizeOf(BKimg), 0);
  BKimg.ulFlags := LVBKIF_SOURCE_URL  or LVBKIF_STYLE_TILE;
  BKimg.pszImage := PChar('c:\windows\angler.bmp');
  BKimg.xOffsetPercent := 0;
  BKimg.yOffsetPercent := 0;
  CoInitialize(nil);
  SendMessage(ListView1.Handle, LVM_SETTEXTBKCOLOR, 0, integer(CLR_NONE));
  SendMessage(ListView1.Handle, LVM_SETBKIMAGE, 0, integer(@BKimg));
  CoUninitialize;
end;

Comments:

2005-12-12, 16:48:02
anonymous from Brazil  
rating
Great idea!
2006-03-09, 09:00:46
Craig from United Kingdom  
rating
I'd been looking for this FOREVER :D.

But how can I get it to load from a TBitmap instead of a file? I tried LVBKIF_STYLE_HBITMAP and LVBKIF_STYLE_MASK and it won't work...
2007-07-07, 16:30:07
anonymous from Sweden  
rating
Thanks, but cannot get it to only show one image (instead of showing same image side by side of itself), anyone know how to fix? :)
2008-03-13, 21:35:14   (updated: 2008-03-13, 21:35:44)
from Indonesia  
how to insert the substiem in listview in delphi i have tried so many time but i can found until noW , would you like to help meIII
2008-12-23, 04:30:39
[hidden]  
rating
This doesn't work with Delphi2009 + Vista

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Los Angeles, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.