Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to print a TScrollBox that contains controls generated at runtime Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Tomas Rutkauskas

How to print a TScrollBox that contains controls generated at runtime


If this is some kind of custom control you developed yourself teach it to print 
itself. In fact it may be able to do that already using the PaintTo method. The 
main problem here is scaling. If your control uses device units (pixels) as 
measures instead of some device-independent unit like mm or inches you will need to 
scale the printer.canvas before you pass it to a controls PaintTo method. Scaling 
the printer canvas to the screen resolution is pretty straightforward. Here is an 
older example that you can tailor to your needs.

Print all of a forms client area, even if parts are not visible. The form will clip 
the output to the visible area if you try to output it to a canvas using using the 
forms paintto method. But one can print the controls on it individually and that is 
not clipped:

1   procedure TForm1.Button1Click(Sender: TObject);
2   var
3     c: TControl;
4     i: Integer;
5     topX, topY: Integer;
6   begin
7     printer.begindoc;
8     try
9       { Scale printer to screen resolution. }
10      SetMapMode(printer.canvas.handle, MM_ANISOTROPIC);
11      SetWindowExtEx(printer.canvas.handle, GetDeviceCaps(canvas.handle, LOGPIXELSX),
12        GetDeviceCaps(canvas.handle, LOGPIXELSY), nil);
13      SetViewportExtEx(printer.canvas.handle, GetDeviceCaps(printer.canvas.handle,    
14              LOGPIXELSX),
15        GetDeviceCaps(printer.canvas.handle, LOGPIXELSY), nil);
16      topX := 10;
17      topY := 10;
18      for i := 0 to controlcount - 1 do
19      begin
20        c := controls[i];
21        if c is TWinControl then
22          TWinControl(c).paintto(printer.canvas.handle, c.left + topX, + topy);
23      end;
24    finally
25      printer.enddoc;
26    end;
27  end;

The problem here is that this only prints TWinControl descendents, if you have 
TLabels or TImages on the form they are not printed. The solution is to put 
everything on the form onto a single top level TPanel. This panel is *not* aligned 
to alClient, it has its left and top set to 0 and its width and height is such that 
all controls fit on it. The code above then prints this panel unclipped and the 
panel prints any non-TWinControls on it.

The usual caveats for PaintTo apply: not all controls will implement this method properly (a Windows limitation). Bitmaps on the form may not appear on the printer if the printer is not able to print device-dependent bitmaps for the screen. It may be advisable to first paint the form to a properly sized tBitmaps canvas (you can omit all the scaling stuff for that since the bitmap resolution is the same as the screens) and then print the bitmap as a device independent bitmap using StretchDIBits.

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC