Solved

Need working code for getting TProgressBar on TListView

Posted on 2011-09-10
4
1,227 Views
Last Modified: 2012-05-12
I want to be able to place Progress Bars (TProgressBar) onto List Views (TListView) in Delphi, and I can find a lot of incomplete guides on how to do this.  Most source code I can find does not tackle the fact that sometimes people might need to scroll the ListView window.  The best code I can find at present is attached, however, the progress bars don't repaint correctly, and you will end up with the wrong progress bars on the wrong lines, especially if you're a fast scroller, and it mostly happens when you're scrolling back up.

Does anybody have any working code which will help me be able to achieve the ability create TProgressBars on a TListView, and be able to scroll and resize while updating correctly and efficiently?

N.B. I do not want to rely on third party components such as TSM's AdvListView or Virtual Treeview (neither of them compile to 64 bit anyway), and I most certainly do not want to draw the progress bars manually.  TProgressBar is what I need.

I am running Delphi XE2.

Thanks in advance.
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListView1: TListView;
    procedure FormCreate(Sender: TObject);
    procedure ListView1CustomDrawItem(Sender: TCustomListView;
      Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
  private
    { Private declarations }
    procedure WMNotify(var Message: TWMNotify); message WM_NOTIFY;
    procedure AdjustProgressBar(item: TListItem; r: TRect);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Byte;
  r: TRect;
  pb: TProgressBar;
begin
  Listview1.Columns.Add.Width := 100;
  Listview1.Columns.Add.Width := 200;
  Listview1.ViewStyle := vsReport;

  Randomize;
  for i:=0 to 40 do
  begin
    Listview1.Items.Add.Caption := 'Texte ' + IntToStr(i);
    r := Listview1.Items[i].DisplayRect(drBounds);
    pb := TProgressBar.Create(Self);
    pb.Parent := Listview1;
    pb.Position := Random(pb.Max);
    Listview1.Items[i].Data := pb;
    AdjustProgressBar(Listview1.Items[i], r);
  end;end;

  procedure TForm1.WMNotify(var Message: TWMNotify);
var
  i: Integer;
  r: TRect;
begin

  case Message.NMHdr.code of
    HDN_ITEMCHANGED, HDN_ITEMCHANGING:
      begin
        for i:=0 to Listview1.Items.Count-1 do
        begin
          r := Listview1.Items[i].DisplayRect(drBounds);
          AdjustProgressBar(Listview1.Items[i], r);
        end;

        ListView1.Repaint;
      end;end;
  inherited;
end;

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
  r: TRect;
  pb: TProgressBar;
begin
  r := Item.DisplayRect(drBounds);
  if r.Top>=Listview1.BoundsRect.Top then
    AdjustProgressBar(Item, r);
end;

procedure TForm1.AdjustProgressBar(item: TListItem; r: TRect);
var
  pb: TProgressBar;
begin
  r.Left := r.Left + Listview1.columns[0].Width;
  r.Right := r.Left + Listview1.columns[1].Width;
  pb := item.Data;
  pb.BoundsRect := r;
end;

end.

Open in new window

0
Comment
Question by:Lester_Clayton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 36517473
>> and I most certainly do not want to draw the progress bars manually.
I'm about sure that is by far your best shot.
trying to move in sync a component above another such as grids or list view can be a real nightmare, as what you have seen already suggests.

A progressbar is really not very hard to paint (except if you love those new fancy Vista/Seven greenish animations, and can't cope without it), I believe you can do it in 2 lines of code (2 rectangles part) in a OnCustomDrawItem event.
0
 
LVL 3

Accepted Solution

by:
VahaC earned 500 total points
ID: 36528680
and I most certainly do not want to draw the progress bars manually.  TProgressBar is what I need.
As for me best solution is to draw progress bars manually

Look attached example TListview-vs-ProgressBar.zip
0
 
LVL 9

Author Closing Comment

by:Lester_Clayton
ID: 36529059
Thank you very much for the code.  It may not be the TProgressBar component, but it looks exactly the same!  I've implemented it and I like what I see.  Kudos.
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36529233
This is indeed some piece of code that went into the trouble of drawing nice smooth progressbar.

Now you see ?  You most certainly WANT to draw the progress bars manually instead of trying to play with components "inclusion". That is very often the case as far as custom ListView, TreeView etc... are concerned, and knowing how to do custom drawing on these components can be the key to nice interfaces, with less efforts than it appears at first glance

And besides, there are so many code sample around to help you on details.Like this excellent article
http://www.delphidabbler.com/articles?article=16
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

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…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 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