Article

From:
To:
All
Subject:
Re: Printing TScrollBox contents
Newsgroup:
borland.public.delphi.language.objectpascal

Re: Printing TScrollBox contents

In article <✉newsgroups.borland.com>, Rafael Cotta wrote:
> What I need is mix text and images on the bottom of the text. So I used this
> approach: use a TRichEdit that is resized as the text grows (when lines are
> added/deleted I change TRichEdit.Height) inside a TScrollBox, and the
> TGraphiControl descendants (GC) on the bottom of the scrollbox.
> 
> ---------------- \
> |              | |
> |  TRichEdit   | |
> |              | |
> ---------------- > TScrollBox
> | GC | GC | GC | |
> ---------------- |
> | GC | GC | GC | |
> ---------------- /
> 
> This is used to view a document format I am working on, that only needs
> images on the bottom.
> 
> What approach would you use to print all this as a single document? Maybe
> the scrollbox + richedit + graphicctrl approach is not the best one. what
> would you suggest?

You use the printer.canvas, as i said. Perhaps the following example can give 
you some ideas. It uses a memo, but i posted examples to print a richedit 
using EM_FORMATRANGE as well, search the newsgroup archives. 

Printing example, how to print a bitmap, arrange some text to the right of it and underline the whole mess.
You do these tasks using the methods of the Printer.Canvas. Following is an example. It expects the bitmap to print in image1 (a TImage) on the form, the text to print next to it in memo1. Note that some of the comments wrap here due to width limits of the message editor. Of course they must be one line to compile. Connect the Button2Click method to the OnClick event of a button. Tested with Delphi 3.02/ NT 4.0/ Epson Stylus Color printer.
// This procedure has been adapted from the one found near the end of // the Delphi 1 MANUALS.TXT file. procedure PrintBitmap(Bitmap: TBitmap; printrect: TRect); var   Info: PBitmapInfo;   InfoSize: Integer;   Image: Pointer;   ImageSize: Longint; begin   with Bitmap do   begin     GetDIBSizes(Handle, InfoSize, ImageSize);     Info := AllocMem(InfoSize);     try       Image := AllocMem(ImageSize);       try         GetDIB(Handle, Palette, Info^, Image^);         with Info^.bmiHeader, printrect do           StretchDIBits(Printer.Canvas.Handle, Left, Top, Right-Left,             Bottom-Top, 0, 0, biWidth, biHeight, Image, Info^,             DIB_RGB_COLORS, SRCCOPY);       finally         FreeMem(Image, ImageSize);       end;     finally       FreeMem(Info, InfoSize);     end;   end; end;
procedure TForm1.Button2Click(Sender: TObject); var   pagerect, imagerect, textrect: TRect;   resX, resY: Integer;   S: String; begin   printer.begindoc;   try     // Get printer resolution in X and Y direction, in dots per inch. Both     // may not be identical on some printers.     resX := GetDeviceCaps( printer.handle, LOGPIXELSX );     resY := GetDeviceCaps( printer.handle, LOGPIXELSY );
    // set print area to page minus some borders     pagerect := Rect( resx, // 1 inch left border                       resy, // 1 inch top border                       printer.pagewidth - resx * 3 div 4, // 0.75 inch right border                       printer.pageheight - resy // 1 inch bottom border                     );
    // Image area will start at top left corner of printing area and     // cover half the width of the printing area, minus 0.25 inch     // for spacing between image and text. The height of the image     // is calculated to maintain the images aspect ration. The image     // we print is a bitmap contained in image1 on the form. For this     // example this is the factory.bmp from the images\splash\256colors     // directory under the Delphi root. The image1-Autosize property is     // set to true, so its width and height properties reflect the     // actual size of the bitmap.     imagerect := Rect( pagerect.left,                        pagerect.top,                        (pagerect.Left + pagerect.right) div 2 - resX div 4,                        0 );     imagerect.bottom :=       imagerect.top +       (imagerect.right - imagerect.left) * image1.height div image1.width       * resY div resX; // accounts for anisotropic printer resolution
    // Text area is to the right of the image and has the same height.     // There is a 0.5 inch spacing between image and text.     textrect := Rect ( imagerect.right + resX div 2,                        imagerect.top,                        pagerect.right,                        imagerect.bottom );
    // Setup complete so far. The text to print is taken from memo1 on the form.     // Note: Since Printer.Canvas.StretchDraw or CopyRect will not work properly     // on all printers with a device-dependent bitmap we use the PrintBitmap     // procedure above to convert the bitmap to a device-independent bitmap     // first and print it using StretchDIBits.     PrintBitmap( image1.picture.bitmap, imagerect );
    // Frame the image     With Printer.Canvas Do Begin       Pen.Width := 2;       Pen.Color := clBlack;       Pen.Style := psSolid;       Brush.Style := bsClear;       With imagerect Do         Rectangle( left, top, right, bottom );     End;
    // Get the text out of the memo, eliminating all soft linebreaks in the     // process. We use the memos font for printing the text.     S := memo1.Lines.Text;     Printer.Canvas.Font := memo1.Font;
    // The TCanvas class has no text output method that can perform wordwrap     // on the fly. The API has one, so we use this.     Windows.DrawText( printer.canvas.handle, Pchar(S), Length(S),                       textrect,                       DT_LEFT or DT_NOPREFIX or DT_WORDBREAK );
    // Draw a 0.05 inch thick horizontal line 0.25 inch below the image     // across the printing area     With Printer.Canvas Do Begin       Pen.Width := resy div 20;       MoveTo( pagerect.left, imagerect.bottom + resy div 4 );       LineTo( pagerect.right, imagerect.bottom + resy div 4 );     End;   finally     // Done, eject the page and end job     printer.enddoc   end; end;

-- Peter Below (TeamB) Use the newsgroup archives : http://www.mers.com/searchsite.html http://www.tamaracka.com/search.htm http://groups.google.com http://www.prolix.be
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Mon, 13 May 2024 15:14:08 UTC
Copyright © 2009-2024
HREF Tools Corp.