We help IT Professionals succeed at work.

How to print a listview

Peter Kiers
Peter Kiers asked
on
638 Views
Last Modified: 2012-05-09
Hi,

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

PK
Comment
Watch Question

Emmanuel PASQUIERFreelance Project Manager
CERTIFIED EXPERT
Top Expert 2010

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

Commented:
Continuetion of this link;
https://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_26312768.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

Emmanuel PASQUIERFreelance Project Manager
CERTIFIED EXPERT
Top Expert 2010

Commented:
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
Peter KiersOperator

Author

Commented:
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.
Peter KiersOperator

Author

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

P.
Freelance Project Manager
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Peter KiersOperator

Author

Commented:
Thanks it works.

Peter Kiers

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.