Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 459
  • Last Modified:

Component Only Functions As "Background Image"

I want to add a background image into the TListview, I have performed a
search in many newsgroups/websites and found out it is quite difficult to
achieve a nice result, so I think it is better to look for a 3rd party
component.

As I have built many codes for the TListview in my program, I don't want to
replace it completely with another component, so I want a component that
ONLY functions as a "Semi-Transparent Background Image".

Any suggestion would be appreciated. Thank you.

raymng
0
Raymond
Asked:
Raymond
  • 4
  • 3
  • 2
1 Solution
 
BinskyCommented:
I'm not sure if this would work as a Listview background, but you might be helped if you use the GR32 component?

Link : http://g32.org

Otherwise you might need to write a transparant image component yourself...

Robin
0
 
RaymondAuthor Commented:
Thanks Binsky. But G32 is a graphic library rather than a component.

0
 
BinskyCommented:
True, but it does make it possible to add better looking pictures...

Otherwise Mike Lischke has created a Graphicex package, that makes it possible to use jpg images, you might want to use that, it's free if your programs are non-commercial...

Link : http://www.lischke-online.de/

Then again, I might not totally understand what it is you want, these two are the only additions I've made to the Builder for graphics, and until now I've never needed anything else.

Robin
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RaymondAuthor Commented:
Binsky:

What I want is to add a background image to an existing TListView control of a Form.

raymng
0
 
odissey1Commented:
Hi raymng,

This is an example howto add a backgroung image to TListView. Not perfect thowgh, but still you can try. You can also read some paper regarding Bkgr image here
http://www.pcmag.com/article2/0,4149,1191185,00.asp

As you mentioned, There not much info on TListView bkgr. Actually, I havn't seen any reliable ListView replacement with BKGR image at all. The reason is TListView is VERY buggy, not funny. I think this was done intentionally by M$ to cut out competitors (Delphi). So look only for components that pure VCL, not TListView wrappers.

Here is the code. email me if you need whole project <botadjik@nmsu.edu>

Regards,
odissey1

unit ListViewBkgr_1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ImgList, ComCtrls, StdCtrls,  CommCtrl,  AppEvnts, ExtCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    ImageList1: TImageList;
    ImageList2: TImageList;
    Panel1: TPanel;
    Button1: TButton;
    CheckBox1: TCheckBox;
    Label1: TLabel;
    ApplicationEvents1: TApplicationEvents;
    procedure ListView1CustomDraw(Sender: TCustomListView;
      const ARect: TRect; var DefaultDraw: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ListView1CustomDrawItem(Sender: TCustomListView;
      Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var B: TBitmap;
    i: integer;
begin
     ListView1.DoubleBuffered:=True;
     ListView1.Parent.DoubleBuffered:=True;

     //Delphi Form can hold only small images! - else AV
     B:=TBitmap.Create;
     try
       B.LoadFromFile(ExtractFilePath(Application.ExeName)+'Sunset2.bmp');
       ImageList2.Clear;
       ImageList2.Width:=B.Width;
       ImageList2.Height:=B.Height;
       ImageList2.Add(B,nil);
     finally
       B.Free;
     end;

//    ImageList1.BlendColor:=clHighlight; //see bug below
       for i:=0 to 100 do Button1.Click; //add extra 100 items
end;

procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
    ImageIndex: integer;
begin
     ImageIndex:=ListView1.Items.Count  mod ImageList1.Count;
     i:=ListView1.Items.Count;
     ListView1.Items.Add;
     ListView1.Items[i].Caption:='add item #'+IntToStr(i);
     ListView1.Items[i].ImageIndex:=ImageIndex;
end;

procedure TForm1.ListView1CustomDraw(Sender: TCustomListView;
  const ARect: TRect; var DefaultDraw: Boolean);
begin
     Sender.Canvas.Brush.Color:=clWindow;
     Sender.Canvas.FillRect(Sender.Canvas.ClipRect);
     ImageList2.Draw(Sender.Canvas, 0,0,0,True);

     SetBkMode(Sender.Handle,TRANSPARENT); //small focus rect?
     ListView_SetTextBkColor(Sender.Handle, CLR_NONE);//caption bkgr
     ListView_SetBKColor(Sender.Handle,CLR_NONE); //image bkgr - no Bkgr drag!
                                                                          //does not work in Report!!!
end;

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var top: integer;
    ARect: TRect;
    S: String;
begin
      DefaultDraw:=False;
    //to set the canvas's background mode to TRANSPARENT,
    // so the background will show through behind the text
//    SetBkMode(Sender.Canvas.Handle,TRANSPARENT); //small focus rect
//    ListView_SetTextBkColor(Sender.Handle, CLR_NONE);//caption bkgr
//    ListView_SetBKColor(Sender.Handle,CLR_NONE); //image bkgr

    if cdsSelected in State then begin
      Sender.Canvas.Brush.Style:=bsSolid;
      Sender.Canvas.Brush.Color:=clHighlight;    //clBackground
//      ARect:=Item.DisplayRect(drBounds); //cell Rect
      ARect:=Item.DisplayRect(drLabel);    //bigger Rect
      Sender.Canvas.FillRect(ARect);
//      S:=Item.Caption;
//      DrawText(Sender.Canvas.Handle,PChar(S),Length(S),ARect, DT_CALCRECT); //exact Rect
    end;

    ARect:=Item.DisplayRect(drLabel);
    ARect:=Rect(ARect.Left+3,ARect.Top+1,ARect.Right,ARect.Bottom+1); //adjust
    S:=Item.Caption;
    Sender.Canvas.Brush.Style:=bsClear;
//    Sender.Canvas.TextOut(ARect.Left+2, ARect.Top+1, S);//out of bound
//    DrawText(Sender.Canvas.Handle,PChar(S),Length(S),ARect, DT_CALCRECT);
    DrawText(Sender.Canvas.Handle,PChar(S),Length(S),ARect, DT_END_ELLIPSIS);

    // inverts the rectangle occupied by the text (stored in the variable R)
    // by setting the canvas's CopyMode property to cmDstInvert and copying
    // the contents of that rectangle to itself.

    //bug: if enabled then no edit mode!
//    if cdsSelected in State
//    then ImageList1.DrawingStyle:=dsSelected
//    else ImageList1.DrawingStyle:=dsNormal;

    ARect:=Item.DisplayRect(drIcon);
    ImageList1.Masked:=TRue;
    ImageList1.Draw( Sender.Canvas, ARect.Left+0, ARect.Top+0, Item.ImageIndex );
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
     IF (Sender as TCheckBox).Checked
     then  ListView1.ViewStyle:=vsIcon;
     else  ListView1.ViewStyle:=vsReport;
end;

procedure TForm1.ApplicationEvents1Idle(Sender: TObject;
  var Done: Boolean);
begin
     ListView1.Repaint;
end;

end.
0
 
RaymondAuthor Commented:
Thanks odissey1,

The result is not so nice still.  Do you know any 3rd party component can do this better?

raymng
0
 
odissey1Commented:
Hi raymng,

Did you tried to download example from article above? I was reluctant to pay $5 to see if the author could achieve better result. So I just followed his advices... :)


I use VirtualTreeView by Mike Lischke (www.delphi-gems.com). Please consider this before using it.

Pros:
It is free. Very professional (see the names of the top experts in the Delphi section here). Thousands users. Frequent updates. Good support thru network group. Very fast with HUGE data structures.

Cons:
It is not easy to get use to it. You cannot just replace your TListView with TVirtualTreeView. Bkgr image can only be TILED (you can make it big though). It only supports Report mode (no Icons). Text / Icons are not transparent - you will get some rows on top with white backgrounf with whatever left from image below rows.

Sincerely,
odissey1

0
 
RaymondAuthor Commented:
Thanks odissey1.
Maybe I will give up the background image.  :-(

0
 
odissey1Commented:
Hi raymng,

Thanks for points. Let me know if any success.

Regards,
odissey1
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now