From: ebo.die...@mmp-obec.nl (Ebo Dieben)
Date: 1999/01/04
Hi,
I'm printing ascii text on the printercanvas and use DT_CALCRECT to
determine the size of the rectangle that will be needed to fit it all.
Everything works fine unless I don't have enough space and need another
page. In that case I will have to breakup the string. But how do I
determine how to break the string up in parts that will fit on the
seperate pages?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - -
Well, my problem is the same: I'm printing in a canvas
(metafile/printer, don't matter) long lines of text from a Tmemo. I
need to make some margins and wrap text, ok, it's simple using the rect
paramenter of function DrawText or DrawTextEx. Also wrapping words at
spaces these functions do the job well...
But, if the text is longer than the page, the field "uiLengthDrawn" of
DRAWTEXTPARAMS (DrawTextEx) informs that the text was draw up to some
length, but at canvas always are missing some characters/letters at
end...
And if I try to begin "a next page" at that position, of course the
printted text will be missing some part.
Here is some code to exemplify:
procedure Print;
const text: ansistring = '1st line - long paragraph long paragraph long
paragraph long paragraph long paragraph long paragraph long paragraph
long paragraph long paragraph '#13#10+
'2nd line - long paragraph long paragraph .....'#13#10+
.......
'nth line - long paragraph....';
PageWidth = 100; // total page width
var printArea: Trect; // area to print, let make some margins
yHeight: integer; // height of 1 single line of text
PageHeight: integer; // height of the printable area
hdc: Windows.HDC; (some HDC, let's say, the memo1.canvas.handle)
rectText: TRECT; // rect to get information about text dimensions
yPos: integer;
dtp: TDrawTextParams;
begin
yHeight:= .......Font.Height;
// will print 5 lines of text, may be only a few paragraphs...
PageHeight:= yHeight * 5;
// create the TRect: margins top-left-right = 5 pixels , height =
10 lines of text
printArea:=Rect( 5, 5, PageWidth-5, PageHeight+5 );
// initalize DRAWTEXTPARAMS
dtp.cbSize:=sizeof(dtp);
dtp.iTabLength:=10;
dtp.iLeftMargin:=0;
dtp.iRightMargin:=0;
dtp.uiLengthDrawn:=0;
// 1st page
yPos:=DrawTextEx(hdc,@text[1],-1,printArea,
DT_TOP or DT_LEFT or DT_WORDBREAK or DT_EXPANDTABS or
DT_NOPREFIX,@dtp);
// FIRST ERROR RESULT !!!
// yPos must be equal to the height of text draw, but not... it is
more...
// yPos > (printArea.bottom - printArea.top)
// SECOND ERROR RESULT !!!
// dtp.uiLengthDrawn doesn't point to the last printed char/text,
// then if next print begin at text[dtp.uiLengthDrawn]
// some characters will be missing at printed canvas
// 2nd page
// I will put bellow 1st only to show the error!
printArea.top := printArea.bottom+yPos;
printArea.bottom := printArea.top + PageHeight;
yPos:=DrawTextEx(hdc,@text[dtp.uiLengthDrawn],-1,printArea,
DT_TOP or DT_LEFT or DT_WORDBREAK or DT_EXPANDTABS or
DT_NOPREFIX,@dtp);
The 1st page
|--------------------------|
1 | 1st line long paragraph|
2 |paragraph long paragraph |
3 |long paragraph long |
4 |paragraph long paragraph |
5 |long paragraph long |
--> it's missing a whole word!
The 2nd page
0 |--------------------------|
1 |long paragraph long |
2 |paragraph long paragraph |
3 |long paragraph long |
4 |paragraph long paragraph |
5 |long paragraph long |
Well, if this is some bug or there is a better code to print long text
in more than one page, using Delphi and Windows API, please, help me!
thanks, yours,
Daniel,
from Brazil