Solved

Component Only Functions As "Background Image"

Posted on 2003-11-19
9
447 Views
Last Modified: 2010-04-03
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
Comment
Question by:Raymond
  • 4
  • 3
  • 2
9 Comments
 
LVL 1

Expert Comment

by:Binsky
ID: 9785697
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
 

Author Comment

by:Raymond
ID: 9785839
Thanks Binsky. But G32 is a graphic library rather than a component.

0
 
LVL 1

Expert Comment

by:Binsky
ID: 9786593
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:Raymond
ID: 9786648
Binsky:

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

raymng
0
 
LVL 2

Accepted Solution

by:
odissey1 earned 200 total points
ID: 9795962
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
 

Author Comment

by:Raymond
ID: 9801908
Thanks odissey1,

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

raymng
0
 
LVL 2

Expert Comment

by:odissey1
ID: 9804066
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
 

Author Comment

by:Raymond
ID: 9818309
Thanks odissey1.
Maybe I will give up the background image.  :-(

0
 
LVL 2

Expert Comment

by:odissey1
ID: 9819026
Hi raymng,

Thanks for points. Let me know if any success.

Regards,
odissey1
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question