如何选中richedit中的内容并打印出来?
高手快来,100分! 问题点数:100、回复次数:7Top
1 楼netlib(河外孤星)回复于 2002-07-06 12:40:18 得分 2
richedit.printTop
2 楼Carfield(一只特别懒的猫)回复于 2002-07-06 12:42:23 得分 2
可以用QuickReport的QRRichEdit控件打印RichEdit中的内容
QRRichEdit.ParaentRichEdit:=RichEdit1;Top
3 楼floattofool(foolish)回复于 2002-07-06 12:54:16 得分 90
哈哈,中招,快快给分!
Printing rich edit selection using EM_FORMATRANGE:
procedure TForm1.Button2Click(Sender: TObject);
Var
printarea: Trect;
richedit_outputarea: TRect;
printresX, printresY: Integer;
fmtRange: TFormatRange;
nextChar : Integer;
S: String;
Begin
Printer.beginDoc;
try
With Printer.Canvas Do Begin
printresX := GetDeviceCaps( handle, LOGPIXELSX );
printresY := GetDeviceCaps( handle, LOGPIXELSY );
printarea :=
Rect( printresX, // 1 inch left margin
printresY * 3 div 2, // 1.5 inch top margin
Printer.PageWidth - printresX, // 1 inch right margin
Printer.PageHeight - printresY * 3 div 2 // 1.5 inch bottom
margin
);
// Define a rectangle for the rich edit text. The height is set to the
// maximum. But we need to convert from device units to twips,
// 1 twip = 1/1440 inch or 1/20 point.
richedit_outputarea :=
Rect( printarea.left * 1440 div printresX,
printarea.top * 1440 div printresY,
printarea.right * 1440 div printresX,
printarea.bottom* 1440 div printresY );
// Tell rich edit to format its text to the printer. First set
// up data record for message:
fmtRange.hDC := Handle; // printer handle
fmtRange.hdcTarget := Handle; // ditto
fmtRange.rc := richedit_outputarea;
fmtRange.rcPage :=
Rect( 0, 0,
Printer.PageWidth * 1440 div printresX,
Printer.PageHeight * 1440 div printresY );
// set range of characters to print to selection
fmtRange.chrg.cpMin := richedit1.selstart;
fmtRange.chrg.cpMax := richedit1.selStart + richedit1.sellength -1;
// remove characters that need not be printed from end of selection.
// failing to do so screws up the repeat loop below.
S:= richedit1.SelText;
While (fmtRange.chrg.cpMax > 0 ) and
(S[fmtRange.chrg.cpMax] <= ' ')
Do Dec(fmtRange.chrg.cpMax);
Repeat
// Render the text
nextChar := richedit1.Perform( EM_FORMATRANGE, 1,
Longint(@fmtRange));
If nextchar < fmtRange.chrg.cpMax Then Begin
// more text to print
printer.newPage;
fmtRange.chrg.cpMin := nextChar;
End; { If }
Until nextchar >= fmtRange.chrg.cpMax;
// Free cached information
richedit1.Perform( EM_FORMATRANGE, 0, 0);
End;
finally
Printer.EndDoc;
end;
end;
The richedit1.perform( EM_FORMATRANGE call returns the index of the last
character that could be fitted into the passed fmtrange.rc, + 1. So if
multiple pages are required one repeats with fmtrange.chrg.cpMin set to
this value, until all characters have been printed.
Top
4 楼floattofool(foolish)回复于 2002-07-06 12:55:09 得分 2
哈哈,中招,快快给分!
Printing rich edit selection using EM_FORMATRANGE:
procedure TForm1.Button2Click(Sender: TObject);
Var
printarea: Trect;
richedit_outputarea: TRect;
printresX, printresY: Integer;
fmtRange: TFormatRange;
nextChar : Integer;
S: String;
Begin
Printer.beginDoc;
try
With Printer.Canvas Do Begin
printresX := GetDeviceCaps( handle, LOGPIXELSX );
printresY := GetDeviceCaps( handle, LOGPIXELSY );
printarea :=
Rect( printresX, // 1 inch left margin
printresY * 3 div 2, // 1.5 inch top margin
Printer.PageWidth - printresX, // 1 inch right margin
Printer.PageHeight - printresY * 3 div 2 // 1.5 inch bottom
margin
);
// Define a rectangle for the rich edit text. The height is set to the
// maximum. But we need to convert from device units to twips,
// 1 twip = 1/1440 inch or 1/20 point.
richedit_outputarea :=
Rect( printarea.left * 1440 div printresX,
printarea.top * 1440 div printresY,
printarea.right * 1440 div printresX,
printarea.bottom* 1440 div printresY );
// Tell rich edit to format its text to the printer. First set
// up data record for message:
fmtRange.hDC := Handle; // printer handle
fmtRange.hdcTarget := Handle; // ditto
fmtRange.rc := richedit_outputarea;
fmtRange.rcPage :=
Rect( 0, 0,
Printer.PageWidth * 1440 div printresX,
Printer.PageHeight * 1440 div printresY );
// set range of characters to print to selection
fmtRange.chrg.cpMin := richedit1.selstart;
fmtRange.chrg.cpMax := richedit1.selStart + richedit1.sellength -1;
// remove characters that need not be printed from end of selection.
// failing to do so screws up the repeat loop below.
S:= richedit1.SelText;
While (fmtRange.chrg.cpMax > 0 ) and
(S[fmtRange.chrg.cpMax] <= ' ')
Do Dec(fmtRange.chrg.cpMax);
Repeat
// Render the text
nextChar := richedit1.Perform( EM_FORMATRANGE, 1,
Longint(@fmtRange));
If nextchar < fmtRange.chrg.cpMax Then Begin
// more text to print
printer.newPage;
fmtRange.chrg.cpMin := nextChar;
End; { If }
Until nextchar >= fmtRange.chrg.cpMax;
// Free cached information
richedit1.Perform( EM_FORMATRANGE, 0, 0);
End;
finally
Printer.EndDoc;
end;
end;
The richedit1.perform( EM_FORMATRANGE call returns the index of the last
character that could be fitted into the passed fmtrange.rc, + 1. So if
multiple pages are required one repeats with fmtrange.chrg.cpMin set to
this value, until all characters have been printed.
Top
5 楼codingfans(Jaklly)回复于 2002-07-06 13:08:24 得分 2
看不明白,UP!Top
6 楼johnsonrao(johnson)回复于 2002-07-06 14:05:47 得分 2
同意floattofool(foolish)Top




