Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

How to print a listview

Hi,

Can someone tell me how to print a listview.
Or know an example.

PK
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Here is the basic work you have to do to print something :
basically, you use printers unit, where is declared the Printer function giving you a TPrinter object
With it, you access the currently selected printer, you can change it from all the printers available, you can start the printing of a document, add pages etc... and you draw on each pages whatever you want like in all bitmap canvas

In the case of printing a TListView, you will have quite a bit work to do to print nicely all the columns. That depends on what you need really
Uses Printers;

procedure TForm1.btn1Click(Sender: TObject);
Var
 LineHeight,i,Y:Integer;
begin
 With Printer do
  begin
   BeginDoc;
   LineHeight:=Round(Canvas.TextHeight('H')*1.2);
   Y:=0;
   for i:=0 to lv1.Items.Count-1 do
    begin
     If Y+LineHeight>PageHeight Then
      begin
       NewPage;
       Y:=0;
      end;
     Canvas.TextOut(0,Y,lv1.Items[i].Caption);
     Y:=Y+LineHeight;
    end;
   EndDoc;
  end;
end;

Open in new window

Continuetion of this link;
https://www.experts-exchange.com/questions/26312768/Put-the-contents-of-a-listview-in-the-body-of-an-email.html

Just add 1button again and a Timage,   that makes 3buttons now from previous post.

procedure TForm1.Button3Click(Sender: TObject);
var
  bmp: TBitmap;
begin
{-R}
  bmp:= Tbitmap.Create;
  try
    bmp.width := listview1.width;
    bmp.height := listview1.height;
    with bmp.canvas do
    begin
      Lock;
      try
        listview1.perform( WM_PRINT, handle, PRF_CHILDREN or PRF_CLIENT or PRF_NONCLIENT or PRF_ERASEBKGND );
      finally
        Unlock
      end;

      image1.picture.bitmap := bmp;

    end;
  finally
    bmp.free
  end;

{+R}
end;

IF
this does not help, try this link;
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_21064698.html

Open in new window

systan, that is a nice code, only problem with that kind of approach is that it will print like it is displayed on screen. Much like a bitmap screen capture and printing of the image. So if there are lots of lines, you will get printed only the ones visible

Realizing ones own printing layout using Canvas methods can be a bitch, fastidious work, but that is the only way to get perfect results, customizable at will (because you can decide every aspect of the rendering), and only real alternative is to use Reporting tools - Rave reports for example
Avatar of Peter Kiers

ASKER

I go for the solution from Epasquier.
But I can not test it yet, because I have no printer installed.

I have asked a long time ago how to install a virtual printer.
So i can test my code. But I can not find the question anymore.
Does someone know the answer to that?

P.
I have test the code from espasquier, but it prints only the first column.

P.
ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks it works.

Peter Kiers