Board index » delphi » Printing A TreeView

Printing A TreeView


2004-01-14 08:24:49 PM
delphi203
Does anyone know how to do this please? The IDE does it but there is no
call in TTreeView.
Regards
 
 

Re:Printing A TreeView

In article <XXXX@XXXXX.COM>, Roger Nevell writes:
Quote

Does anyone know how to do this please? The IDE does it but there is no
call in TTreeView.

Print a treeview
var
bmp: TBitmap;
begin
bmp:= TBitmap.Create;
try
bmp.width := treeview.width;
bmp.height := treeview.height;
bmp.canvas.lock;
try
treeview.perform( WM_PRINT,
bmp.canvas.handle,
PRF_CHILDREN or PRF_CLIENT or
PRF_NONCLIENT );
finally
bmp.canvas.unlock;
end;
...print the bitmap as device-independent bitmap
finally
bmp.free
end;
Drawing directly to the printer canvas is probably possible but you have
to mess with mapping modes, scale the printer canvas, move its origin
and so on, the above may be easier to code, especially if you already
have a PrintBitmap routine flying around somewhere.
Peter Below (TeamB)
Use the newsgroup archives :
www.mers.com/searchsite.html
www.tamaracka.com/search.htm
groups.google.com
www.prolix.be
 

Re:Printing A TreeView

Roger Nevell writes:
Quote
Does anyone know how to do this please? The IDE does it but there is no
call in TTreeView.

Regards


www.5thelephant.com/delphi/files/Ext_TreeView.zip
Take a look at the TV_Print() procedure
 

Re:Printing A TreeView

I'm curious; are these necessary...
Quote
bmp.canvas.lock;
bmp.canvas.unlock;
- Brian
 

Re:Printing A TreeView

Chris Willig <XXXX@XXXXX.COM>writes news:<40059ef9$XXXX@XXXXX.COM>...
Quote
Roger Nevell writes:
>Does anyone know how to do this please? The IDE does it but there is no
>call in TTreeView.
>
>Regards
>
>

www.5thelephant.com/delphi/files/Ext_TreeView.zip

Take a look at the TV_Print() procedure
I had the same problem so I made this procedure to print the full
treeview content into a Canvas. The TreeView control doesn't need to
be visible as it makes the drawing to a memory bitmap. I use this
function to draw into a Bitmap's Canvas and then print it.
procedure TreeViewDrawTo(TV: TTreeView; ACanvas: TCanvas);
var
ViewWidth,
ViewHeight,
NodeHeight,
X, Y,
OldX, OldY,
LastX,
LastY : Integer;
ScrollInfo : TScrollInfo;
Rect : TRect;
DC,
MemDC: HDC;
MemBitmap,
OldBitmap: HBITMAP;
begin
if TV.Items.Count>0 then
begin
Rect := TV.Items.GetFirstNode.DisplayRect(False);
NodeHeight := Rect.Bottom - Rect.Top;
ViewWidth := TV.ClientWidth;
ViewHeight := (TV.ClientHeight div NodeHeight) * NodeHeight;
// determine last scroll position
with ScrollInfo do
begin
// init the scroll info structure
ZeroMemory(@ScrollInfo, SizeOf(ScrollInfo));
cbSize := SizeOf(TScrollInfo);
fMask := SIF_ALL;
end;
GetScrollInfo(TV.Handle, SB_HORZ, ScrollInfo);
LastX := ScrollInfo.nPos;
GetScrollInfo(TV.Handle, SB_VERT, ScrollInfo);
LastY := ScrollInfo.nPos;
// create memory DC
DC := GetDC(0);
MemBitmap := CreateCompatibleBitmap(DC, ViewWidth, ViewHeight);
ReleaseDC(0, DC);
MemDC := CreateCompatibleDC(0);
OldBitmap := SelectObject(MemDC, MemBitmap);
try
// scroll through the entire tree and capture the output;
// start at top of the window
Y := 0;
TV.Perform(WM_VSCROLL, SB_TOP, 0);
repeat
// scroll to left border
X := 0;
TV.Perform(WM_HSCROLL, SB_TOP, 0);
repeat
// paint content
TV.Perform(WM_ERASEBKGND, MemDC, MemDC);
SendMessage(TV.Handle, WM_PAINT, MemDC, 0);
// capture current content...
BitBlt(ACanvas.Handle, X, Y, ViewWidth, ViewHeight,
MemDC, 0, 0, SRCCOPY);
// .. and scroll one page to the right
TV.Perform(WM_HSCROLL, SB_PAGERIGHT, 0);
GetScrollInfo(TV.Handle, SB_HORZ, ScrollInfo);
OldX := X;
X := ScrollInfo.nPos;
until (OldX = X);
// scroll one page down
TV.Perform(WM_VSCROLL, SB_PAGEDOWN, 0);
GetScrollInfo(TV.Handle, SB_VERT, ScrollInfo);
OldY := Y;
if ScrollInfo.nPage>0 then
Y := ScrollInfo.nPos * NodeHeight
else
Y := 0;
until (OldY = Y);
// finally scroll to the last position
TV.Perform(WM_HSCROLL, MakeWParam(SB_THUMBPOSITION, LastX), 0);
TV.Perform(WM_VSCROLL, MakeWParam(SB_THUMBPOSITION, LastY), 0);
finally
SelectObject(MemDC, OldBitmap);
DeleteDC(MemDC);
DeleteObject(MemBitmap);
end;
end;
end;