- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


3204:TRichEdit Printing in Delphi 2 & Windows NT 4.0

KEYWORDS: RichEdit, Print NT, Delphi 2 AREA: Utilities

TRichEdit Printing Under Windows NT 4.0

This document provides a Delphi Unit that will solve the
"divide by zero" error that occurs when printing from a
TRichEdit control under Windows NT 4.0.  To use this Unit
simply include it in the USES clause of the Unit that you
want to print from.  Instead of calling the RichEdit.Print()
method to print, call the PrintRichEdit() procedure and pass
it the TRichEdit control that you want to print as a parameter.

If you own the VCL source code you can make the changes
shown below to the TCustomRichEdit.Print method in the
COMCTRLS.PAS file.

unit PrtRichU;

interface

uses
  ComCtrls;

procedure PrintRichEdit(const Caption: string;
                        const RichEdt: TRichEdit);

implementation
uses
  Windows, RichEdit, Printers;

procedure PrintRichEdit(const Caption: string;
                        const RichEdt: TRichEdit);
var
  Range: TFormatRange;
  LastChar, MaxLen, LogX, LogY, OldMap: Integer;
begin
  FillChar(Range, SizeOf(TFormatRange), 0);
  with Printer, Range do
  begin
    BeginDoc;
    hdc := Handle;
    hdcTarget := hdc;
    LogX := GetDeviceCaps(Handle, LOGPIXELSX);
    LogY := GetDeviceCaps(Handle, LOGPIXELSY);
    if IsRectEmpty(RichEdt.PageRect) then
    begin
      rc.right := PageWidth * 1440 div LogX;
      rc.bottom := PageHeight * 1440 div LogY;
    end
    else begin
      rc.left := RichEdt.PageRect.Left * 1440 div LogX;
      rc.top := RichEdt.PageRect.Top * 1440 div LogY;
      rc.right := RichEdt.PageRect.Right * 1440 div LogX;
      rc.bottom := RichEdt.PageRect.Bottom * 1440 div LogY;
    end;
    rcPage := rc;
    Title := Caption;
    LastChar := 0;
    MaxLen := RichEdt.GetTextLen;
    chrg.cpMax := -1;
    OldMap := SetMapMode(hdc, MM_TEXT);
    SendMessage(RichEdt.Handle, EM_FORMATRANGE, 0, 0);
    try
      repeat
        chrg.cpMin := LastChar;
        LastChar := SendMessage(RichEdt.Handle, EM_FORMATRANGE, 1,
                                Longint(@Range));
        if (LastChar < MaxLen) and (LastChar <> -1) then NewPage;
      until (LastChar >= MaxLen) or (LastChar = -1);
      EndDoc;
    finally
      SendMessage(RichEdt.Handle, EM_FORMATRANGE, 0, 0);
      SetMapMode(hdc, OldMap);
    end;
  end;
end;

end.

        TI



* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile