Solved

Need working code for getting TProgressBar on TListView

Posted on 2011-09-10
4
1,214 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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