A
ArJianzeng
Unregistered / Unconfirmed
GUEST, unregistred user!
大家好!!!我是个dephi的编程新手,最近接触到一个有关于打印预览的问题,不知那为大虾和高手肯帮帮忙...
具体代码如下:
procedure TForm1.buttonClick(Sender :TObject);
var
BlackImage , WhiteImage : TImage ;
intCapsX,intCapsY : Integer ;
intPageWidth,intPageHeight : Integer ;
intRowCounts ,intPianYi: Integer ;
intTextWidth,intTextHeight : Integer ;
intPageCount : Integer;
I , J , K , int : Integer ;
intTopText : String ;
//页头。
ListPrintTest : TStringList;
iCount :Integer;
intTextCount :Integer;
// X,Y,mapW,mapH : Integer;//用于{//*....*//}部分.
begin
ListPrintTest :=TStringList.Create ;
ListPrintTest.AddStrings(MainForm.RichEditor.Lines);
//坐标象素转换成英寸。
intCapsX :=GetDeviceCaps(Printer.Handle,LOGPIXELSX) ;
intCapsY :=GetDeviceCaps(Printer.Handle,LOGPIXELSY);
intPageWidth :=Printer.PageWidth *PixelsPerInch div (intCapsX);
//纸张每页宽度;
intPageHeight :=Printer.PageHeight*PixelsPerInch div (intCapsY);
//纸张每页高度;
intPianYi :=intPageHeight div intPageWidth;
ViewSize :=ViewSize div 100;
Canvas.Font.Size :=MainForm.RichEditor.Font.Size *ViewSize ;
intTextWidth:=Canvas.TextWidth ('啊');
intTextHeight:=Canvas.TextHeight ('啊');
intTextCount:=intPageHeight-(ScrollBox1.Top+80);
intRowCounts:=(intTextCount-60+intTextHeight) div (intTextHeight+intPianYi);// 每页行数;
intPageCount:=(ListPrintTest.Count-1) div intRowCounts;
//页数。
if ((ListPrintTest.Count-1) Mod intRowCounts)<>0 then
//遇到余数行。
intPageCount:=intPageCount+1;
if ((intRowCounts*intTextHeight+intPianYi) >= (intTextCount-60+intTextHeight))then
begin
intRowCounts :=(intPageHeight-intTextCount-60)div (intTextHeight+intPianYi);
;
intPageCount:=intPageCount+1;
end;
StartPrintDlg.FromPage:=1;
StartPrintDlg.MinPage :=1;
StartPrintDlg.MaxPage :=intPageCount;
StartPrintDlg.ToPage := intPageCount;
J:=0;
for I:= StartPrintDlg.FromPage to StartPrintDlg.ToPagedo
begin
Inc(J);
K:=(J-1)*(intPageHeight+40)+intTextHeight+10;
mapImage :=TImage.Create(self);
with mapImagedo
//底纹.
begin
Top :=0;
Left :=0;
Width :=(intPageWidth+50);
Height :=(intPageHeight+50)*intPageCount;
Canvas.Brush.Color :=ScrollBox1.Color ;
Canvas.Rectangle(0,0,width,Height);
IncrementalDisplay := True;
stretch := True ;
Transparent :=True;
Parent :=ScrollBox1 ;
Visible :=True;
Picture.LoadFromFile(ExtractFilePath(Application.ExeName)+'prCode06.bmp');
end;
//以下是画出底纹.
{//* mapw :=0;
mapH :=0;
for X :=0 to mapImage.Widthdo
begin
for Y :=0 to mapImage.Heightdo
begin
while (mapW <=mapImage.Width*(2+K) )and (mapH<=mapImage.Height*(2+K))do
with mapImage.Canvasdo
begin
pen.Width :=1;
pen.Style := psDot;
pen.Color :=clwhite ;
MoveTo(mapw,0);
LineTo(0,mapH);
inc(mapW,8);
inc(mapH,8);
end;
end;
end;
*//}
BlackImage:=TImage.Create (Self);
//生成预览面板;
with BlackImagedo
begin
Left:=intTextWidth+10;
Top:=K+10;
width:=intPageWidth*ViewSize;
Height:=intPageHeight*ViewSize;
Canvas.Brush.Color :=clBlack;
Canvas.Rectangle(0,0,Width,Height);
Parent:=ScrollBox1;
Enabled:=True;
end;
WhiteImage:=TImage.Create(Self);
with WhiteImagedo
begin
Left:=(intTextWidth+5);
Top:=(K+5);
Width:=(intPageWidth)*ViewSize;
Height:=intPageHeight*ViewSize;
Canvas.Brush.Color :=clWhite;
Canvas.Rectangle(0,0,width,Height);
Parent:=ScrollBox1;
Enabled:=True;
end;
intMovePos :=WhiteImage.Height;
intTopText :='源代码打印系统例程.';
//页眉。
WhiteImage.Canvas.Font.Name :='宋体';
WhiteImage.Canvas.Font.Size :=10;
intTextHeight:=WhiteImage.Canvas.TextHeight ('啊');
WhiteImage.Canvas.TextOut(60+intPianYi,60,intTopText);
//打印出页眉。
WhiteImage.Canvas.MoveTo(60,70+intTextHeight);
WhiteImage.Canvas.LineTo(intPageWidth-60,70+intTextHeight);
//画线条。
if StartPrinting=False then
begin
with WhiteImage.Canvasdo
begin
Font.Assign(MainForm.RichEditor.Font);
Font.Size :=(MainForm.RichEditor.Font.Size )*ViewSize;
intTextWidth:=Canvas.TextWidth ('啊');
intTextHeight:=Canvas.TextHeight ('啊');
if ListPrintTest.Count <> 0 then
begin
for int:=0 to (intRowCounts)do
//输出。
begin
TextOut(intTextWidth+intPianYi+60,int*intTextHeight+100,
ListPrintTest.Strings[0]);
ListPrintTest.Delete(0);
if ListPrintTest.Count <=0 then
ListPrintTest.Add('');
end;
end;
end;
以上运行后出现的情况是:
* 当预览显示一页时,底纹可以正常显示,但当显示到三页是会出现接近死机的现象,而且,显示出来后,预览页面只显示最后一页,其他的好象都被覆盖了.
* 为什么" Picture.LoadFromFile(ExtractFilePath (Application.ExeName)+'prCode06.bmp');"与加了{//*...*//}部分的运行效果是一样的???
具体代码如下:
procedure TForm1.buttonClick(Sender :TObject);
var
BlackImage , WhiteImage : TImage ;
intCapsX,intCapsY : Integer ;
intPageWidth,intPageHeight : Integer ;
intRowCounts ,intPianYi: Integer ;
intTextWidth,intTextHeight : Integer ;
intPageCount : Integer;
I , J , K , int : Integer ;
intTopText : String ;
//页头。
ListPrintTest : TStringList;
iCount :Integer;
intTextCount :Integer;
// X,Y,mapW,mapH : Integer;//用于{//*....*//}部分.
begin
ListPrintTest :=TStringList.Create ;
ListPrintTest.AddStrings(MainForm.RichEditor.Lines);
//坐标象素转换成英寸。
intCapsX :=GetDeviceCaps(Printer.Handle,LOGPIXELSX) ;
intCapsY :=GetDeviceCaps(Printer.Handle,LOGPIXELSY);
intPageWidth :=Printer.PageWidth *PixelsPerInch div (intCapsX);
//纸张每页宽度;
intPageHeight :=Printer.PageHeight*PixelsPerInch div (intCapsY);
//纸张每页高度;
intPianYi :=intPageHeight div intPageWidth;
ViewSize :=ViewSize div 100;
Canvas.Font.Size :=MainForm.RichEditor.Font.Size *ViewSize ;
intTextWidth:=Canvas.TextWidth ('啊');
intTextHeight:=Canvas.TextHeight ('啊');
intTextCount:=intPageHeight-(ScrollBox1.Top+80);
intRowCounts:=(intTextCount-60+intTextHeight) div (intTextHeight+intPianYi);// 每页行数;
intPageCount:=(ListPrintTest.Count-1) div intRowCounts;
//页数。
if ((ListPrintTest.Count-1) Mod intRowCounts)<>0 then
//遇到余数行。
intPageCount:=intPageCount+1;
if ((intRowCounts*intTextHeight+intPianYi) >= (intTextCount-60+intTextHeight))then
begin
intRowCounts :=(intPageHeight-intTextCount-60)div (intTextHeight+intPianYi);
;
intPageCount:=intPageCount+1;
end;
StartPrintDlg.FromPage:=1;
StartPrintDlg.MinPage :=1;
StartPrintDlg.MaxPage :=intPageCount;
StartPrintDlg.ToPage := intPageCount;
J:=0;
for I:= StartPrintDlg.FromPage to StartPrintDlg.ToPagedo
begin
Inc(J);
K:=(J-1)*(intPageHeight+40)+intTextHeight+10;
mapImage :=TImage.Create(self);
with mapImagedo
//底纹.
begin
Top :=0;
Left :=0;
Width :=(intPageWidth+50);
Height :=(intPageHeight+50)*intPageCount;
Canvas.Brush.Color :=ScrollBox1.Color ;
Canvas.Rectangle(0,0,width,Height);
IncrementalDisplay := True;
stretch := True ;
Transparent :=True;
Parent :=ScrollBox1 ;
Visible :=True;
Picture.LoadFromFile(ExtractFilePath(Application.ExeName)+'prCode06.bmp');
end;
//以下是画出底纹.
{//* mapw :=0;
mapH :=0;
for X :=0 to mapImage.Widthdo
begin
for Y :=0 to mapImage.Heightdo
begin
while (mapW <=mapImage.Width*(2+K) )and (mapH<=mapImage.Height*(2+K))do
with mapImage.Canvasdo
begin
pen.Width :=1;
pen.Style := psDot;
pen.Color :=clwhite ;
MoveTo(mapw,0);
LineTo(0,mapH);
inc(mapW,8);
inc(mapH,8);
end;
end;
end;
*//}
BlackImage:=TImage.Create (Self);
//生成预览面板;
with BlackImagedo
begin
Left:=intTextWidth+10;
Top:=K+10;
width:=intPageWidth*ViewSize;
Height:=intPageHeight*ViewSize;
Canvas.Brush.Color :=clBlack;
Canvas.Rectangle(0,0,Width,Height);
Parent:=ScrollBox1;
Enabled:=True;
end;
WhiteImage:=TImage.Create(Self);
with WhiteImagedo
begin
Left:=(intTextWidth+5);
Top:=(K+5);
Width:=(intPageWidth)*ViewSize;
Height:=intPageHeight*ViewSize;
Canvas.Brush.Color :=clWhite;
Canvas.Rectangle(0,0,width,Height);
Parent:=ScrollBox1;
Enabled:=True;
end;
intMovePos :=WhiteImage.Height;
intTopText :='源代码打印系统例程.';
//页眉。
WhiteImage.Canvas.Font.Name :='宋体';
WhiteImage.Canvas.Font.Size :=10;
intTextHeight:=WhiteImage.Canvas.TextHeight ('啊');
WhiteImage.Canvas.TextOut(60+intPianYi,60,intTopText);
//打印出页眉。
WhiteImage.Canvas.MoveTo(60,70+intTextHeight);
WhiteImage.Canvas.LineTo(intPageWidth-60,70+intTextHeight);
//画线条。
if StartPrinting=False then
begin
with WhiteImage.Canvasdo
begin
Font.Assign(MainForm.RichEditor.Font);
Font.Size :=(MainForm.RichEditor.Font.Size )*ViewSize;
intTextWidth:=Canvas.TextWidth ('啊');
intTextHeight:=Canvas.TextHeight ('啊');
if ListPrintTest.Count <> 0 then
begin
for int:=0 to (intRowCounts)do
//输出。
begin
TextOut(intTextWidth+intPianYi+60,int*intTextHeight+100,
ListPrintTest.Strings[0]);
ListPrintTest.Delete(0);
if ListPrintTest.Count <=0 then
ListPrintTest.Add('');
end;
end;
end;
以上运行后出现的情况是:
* 当预览显示一页时,底纹可以正常显示,但当显示到三页是会出现接近死机的现象,而且,显示出来后,预览页面只显示最后一页,其他的好象都被覆盖了.
* 为什么" Picture.LoadFromFile(ExtractFilePath (Application.ExeName)+'prCode06.bmp');"与加了{//*...*//}部分的运行效果是一样的???