Solved

Component Only Functions As "Background Image"

Posted on 2003-11-19
9
441 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:raymng
  • 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:raymng
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
 

Author Comment

by:raymng
ID: 9786648
Binsky:

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

raymng
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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:raymng
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:raymng
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now