Windows with Delphi Windows API (94) Windows Filesystem (41) Windows Forms (69) Windows Graphics (38)
Exchange Links About this site Links to us 
|
Putting a background image on a ListView (Delphi 7)
5 comments. Current rating: (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:
- 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.
- 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:
|
|
|
|
Great idea!
|
|
|
|
|
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...
|
|
|
|
|
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) |
|
|
|
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
|
|
|
|
|
This doesn't work with Delphi2009 + Vista
|
|