Re:Printing error with RTF and Windows NT 4.0
"Leslie R. Thomas" <thom...@csra.net> wrote:
Quote
>When attempting to use the print method of an RTF control, I get an
>application error under both NT Workstation and NT Server 4.0. Anyone have
>any insight to this problem ?
>BTW it works fine with Windows 95.
I had this problem a couple of months ago.
For reference purposes, here is the code in question:
procedure TCustomRichEdit.Print(const Caption: string);
var
Range: TFormatRange;
LastChar, MaxLen, LogX, LogY: Integer;
begin
FillChar(Range, SizeOf(TFormatRange), 0);
with Printer, Range do
begin
LogX := GetDeviceCaps(Handle, LOGPIXELSX);
LogY := GetDeviceCaps(Handle, LOGPIXELSY);
hdc := Handle; // <<<<--- NT 4.0 returns Information Context
hdcTarget := hdc;
if IsRectEmpty(PageRect) then
begin
rc.right := PageWidth * 1440 div LogX;
rc.bottom := PageHeight * 1440 div LogY;
end
else begin
rc.left := PageRect.Left * 1440 div LogX;
rc.top := PageRect.Top * 1440 div LogY;
rc.right := PageRect.Right * 1440 div LogX;
rc.bottom := PageRect.Bottom * 1440 div LogY;
end;
rcPage := rc;
Title := Caption;
BeginDoc; // <<<<--- Now REAL Device Context created
LastChar := 0;
MaxLen := GetTextLen;
chrg.cpMax := -1;
repeat
chrg.cpMin := LastChar;
LastChar := SendMessage(Self.Handle, EM_FORMATRANGE, 1,
Longint(@Range));
if (LastChar < MaxLen) and (LastChar <> -1) then NewPage;
until (LastChar >= MaxLen) or (LastChar = -1);
EndDoc;
end;
SendMessage(Handle, EM_FORMATRANGE, 0, 0);
end;
The error is caused because accessing the Printer.Handle property
outside of a BeginDoc/EndDoc block returns an Information Context (IC)
handle under NT 4.0. The EM_FORMATRANGE attempts to use this IC has
the real printer DC, which causes the violation. If the Handle
property was accessed AFTER the BeginDoc, a true Device Context handle
would be returned.
I solved the problem by creating my own print procedure.
I hope this helps.
David R. Michael
davi...@busprod.com
www.busprod.com/davidrm