Solved

Need working code for getting TProgressBar on TListView

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month6 days, 22 hours left to enroll

622 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