W
wallysw
Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾,小弟写了一个打印MEMO的函数,但打印多行时就会提示出错,我检查了一天,也不知道错在哪里,代码贴出来,麻烦大家帮我看一下,如果问题解决了,小弟献上100分,分不多,只是麻烦各位帮小弟一个忙了。
procedure TForm1.PrintMemo(AMemo: TMemo);
var
i, CurHeigth, sLine: Integer;
TotalStr, TempStr: WideString;
StrList: TStringList;
Prn: TPrinter;
FRect: TRect;
begin
StrList := TStringList.Create;
Prn := TPrinter.Create;
Prn.Canvas.Font := AMemo.Font;
FRect := Rect(200, 200, Prn.PageWidth - 200, Prn.PageHeight - 200);
{计算每行应打印多少个字}
TotalStr := AMemo.Lines.Text;
for i := 1 to Length(TotalStr)do
begin
if (TotalStr <> #$D) and ((Prn.Canvas.TextWidth(TempStr) * 3) < (FRect.Right - 300)) then
begin
TempStr := TempStr + TotalStr;
end else
begin
StrList.Add(TempStr + TotalStr);
TempStr := '';
end;
end;
StrList.Add(TempStr);
{打印部份}
sLine := 1;
CurHeigth := FRect.Top;
Prn.begin
Doc;
for i := 0 to StrList.Count - 1do
begin
if CurHeigth < (FRect.Bottom - 200) then
begin
CurHeigth := FRect.Top + (Prn.Canvas.TextHeight(StrList.Strings) * sLine);
Prn.Canvas.TextOut(200, CurHeigth, StrList.Strings);
{打印多行时,提示这里出错,无法访问内存地址错误}
sLine := sLine + 1;
end else
begin
Prn.Canvas.TextOut(200, CurHeigth, StrList.Strings);
Prn.NewPage;
CurHeigth := FRect.Top;
sLine := 1;
end;
end;
Prn.EndDoc;
Prn.Free;
StrList.Clear;
StrList.Free;
end;
procedure TForm1.PrintMemo(AMemo: TMemo);
var
i, CurHeigth, sLine: Integer;
TotalStr, TempStr: WideString;
StrList: TStringList;
Prn: TPrinter;
FRect: TRect;
begin
StrList := TStringList.Create;
Prn := TPrinter.Create;
Prn.Canvas.Font := AMemo.Font;
FRect := Rect(200, 200, Prn.PageWidth - 200, Prn.PageHeight - 200);
{计算每行应打印多少个字}
TotalStr := AMemo.Lines.Text;
for i := 1 to Length(TotalStr)do
begin
if (TotalStr <> #$D) and ((Prn.Canvas.TextWidth(TempStr) * 3) < (FRect.Right - 300)) then
begin
TempStr := TempStr + TotalStr;
end else
begin
StrList.Add(TempStr + TotalStr);
TempStr := '';
end;
end;
StrList.Add(TempStr);
{打印部份}
sLine := 1;
CurHeigth := FRect.Top;
Prn.begin
Doc;
for i := 0 to StrList.Count - 1do
begin
if CurHeigth < (FRect.Bottom - 200) then
begin
CurHeigth := FRect.Top + (Prn.Canvas.TextHeight(StrList.Strings) * sLine);
Prn.Canvas.TextOut(200, CurHeigth, StrList.Strings);
{打印多行时,提示这里出错,无法访问内存地址错误}
sLine := sLine + 1;
end else
begin
Prn.Canvas.TextOut(200, CurHeigth, StrList.Strings);
Prn.NewPage;
CurHeigth := FRect.Top;
sLine := 1;
end;
end;
Prn.EndDoc;
Prn.Free;
StrList.Clear;
StrList.Free;
end;