老问题-打印 查询出来的dbgrind(30分)

  • 主题发起人 主题发起人 hellen
  • 开始时间 开始时间
H

hellen

Unregistered / Unconfirmed
GUEST, unregistred user!
1:如何打印查询出来的dbgrind中的内容?(不用quickrep )
多谢,详细一些的代码!
2:我在打印table中的内容代码如下:
begin

for i:=0 to DBGrid1.FieldCount-1do

begin

//假如所要打印的列超出了打印范围,则忽略该列
if (x+DBGrid1.Columns.Items.Width*PointScale)<=(Printer.PageWidth-PointX*RightBlank) then

begin
//画表格线
//每页的第一行打印表头
Printer.Canvas.Rectangle(x,y,x+DBGrid1.Columns.Items.Width*PointScale,y+PrintStep);
if (y=PointY*TopBlank+PrintStep) then

Printer.Canvas.TextOut(x+8,y+8,DBGrid1.Columns.Title.Caption)
else

Printer.Canvas.TextOut(x+8,y+8,DBGrid1.Fields.asString);
end;

//计算下一列的横坐标

x:=x+DBGrid1.Columns.Items.Width*PointScale;
end;

end;


他说“printer is not currently printing?
why?
 
希望有用
{**********************超级dbgrid打印控件*****************88}
//1999年6月16日
//Ver 1.0


unit PrintDbGrid;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, StdCtrls, Grids, DBGrids, Buttons, ExtCtrls,printers;


type ha=(paLeft,paRight,paCenter);
type va=(vaTop,vaBottom,vaCenter);

type
TPrintDbGrid = class(TComponent)
private
FLeftborder,FRightborder,FTopborder,FBottomBorder:integer;
//上下左右的空白
Ftabletop,Ftablebottom:integer;
//表格的上边界,下边界
FSourceGrid:TDBGrid;
//数据源
FRowsPerPage:integer;
//每页纸打多少页
FPageHeadText:string;
//页眉标题
FPageHeadFont:TFont;
//页眉字体
FPageHeadLine:boolean;
//页眉线
FPageHeadAlignment:ha;
//页眉位置
FPageFootText:string;
//页脚标题
FPageFootFont:TFont;
//页脚字体
FPageFootLine:boolean;
//页脚线
FPageFootAlignment:ha;
//页脚位置
FGridFont:TFont;
//表格的字体
FGridAlignmenth:ha;
//页脚横向位置
FGridAlignmentv:va;
//页脚竖向位置
FPreviewCaption:string;
//预览窗体的标题
FPageNumbers:integer;
//需要多少页,只读
FPreMaxZoom:integer;
//预览缩放最大比例
FPreMinZoom:integer;
//预览缩放最小比例
FGridVertLine,FGridhoriLine:boolean;
//是否画表格线(横线,竖线)
FBorderBold:boolean;
//是否加粗表格边线
FRowsPerPageAutoDetect:boolean;
FSumPerPage:boolean;
FSumPerPageText:string;
FSumTotal:boolean;
FSumTotalText:string;
FPrinterName:string;
WindowList: Pointer;


//以上为属性
//以下为内部变量
headha,footha,gridha,gridva:integer;
//页眉、页脚、表格等的位置
pagewidth,pageheight:integer;
//纸的大小
GridFontHeight:integer;
//写在Canvas上的每字的高度
HeadFontHeight:integer;
//页眉字体的高度
FootFontHeight:integer;
//页脚字体的高度
GridLies:integer;
//表格的字段数量
Liewidth:array[0..50] of integer;
//每列宽度
sc:single;
isDetect:boolean;
//是否在检测每页有多少行
pagehj:array[0..1000,0..50] of single;
//页数 //字段
RowsOfThisPage:integer;
Haveprinter:boolean;


//以下为动态生成各种控件
Meta:TMetaFile;
//wmf文件
MetaCanvas:TMetaFileCanvas;
//wmf文件的画布
frm:TForm;
//预览窗体
img:Timage;
//放Wmf文件的image
pn1,pn2:TPanel;
//两个Panel
scrbox:TScrollbox;
//放Pn2的Scrollbox
btn1,btn2:TSpeedbutton;
//两个按钮
scr1,scr2:TScrollbar;
//两个Scrollbar
label1,label2:TLabel;
//两个标签

procedure writecelldata(hb:TCanvas;page:integer);
procedure Initdata(aCanvas:TCanvas);
procedure setPageheadFont(value:TFont);
procedure setPageFootFont(value:TFont);
procedure setGridFont(value:TFont);
procedure Scr1change(sender:TObject);
procedure Scr2Change(sender:TObject);
procedure setpreviewcaption(Value:string);
procedure setPreMaxZoom(value:integer);
procedure setpreMinZoom(value:integer);
procedure setRowsPerPageAutoDetect(value:boolean);
procedure btn1click(sender:TObject);
procedure Btn2Click(sender:TObject);
procedure createsize;
function getprintername:string;
procedure Closefrm(Sender: TObject;
var Action: TCloseAction);
protected
procedure loaded;override;
public
Constructor Create(AOwner:TComponent);override;
Destructor Destroy;override;
procedure Preview(page:integer);
procedure Print(page:integer);
property PageNumbers:integer read FPageNumbers;
procedure DetectRowsPerPage;
procedure SavetoFile(FileName:string;Page:integer);
procedure DetectPageNumbers;
procedure PrinterSetup;
property PrinterName:string read getprintername;
procedure Closepreviewwindow;
published
property PageHeadText:string read FPageHeadText write FPageHeadText;
property PageHeadFont:TFont read FPageHeadFont write setPageHeadFont;
property PageHeadLine:Boolean read FPageHeadLine write FPageHeadLine;
property PageHeadAlignment:ha read FPageHeadAlignment write FPageHeadAlignment;
//页眉属性
property PageFootText:string read FPageFootText write FPageFootText;
property PageFootFont:TFont read FPageFootFont write setPageFootFont;
property PageFootLine:Boolean read FPageFootLine write FPageFootLine;
property PagefootAlignment:ha read FPageFootAlignment write FPageFootAlignment;
//页脚属性

property GridFont:TFont read FGridFont write setGridFont;
property GridAlignmenth:ha read FGridAlignmenth write FGridAlignmenth;
property GridAlignmentv:va read FGridAlignmentv write FGridAlignmentv;
property GridVertLine:boolean read FGridVertLine write FGridVertLine;
property GridHoriLine:boolean read FGridHoriLine write FGridHoriLine;
property BorderBold:boolean read FBorderBold write FBorderBold;
//表格属性

property LeftBorder:integer read FLeftBorder write FLeftBorder;
property RightBorder:integer read FRightBorder write FRightBorder;
property TopBorder:integer read FTopBorder write FTopBorder;
property BottomBorder:integer read FBottomborder write FBottomBorder;
property SourceGrid:TDbGrid read FSourceGrid write FSourceGrid;
property RowsPerPage:integer read FRowsPerPage write FRowsPerPage;

property PreviewCaption:string read FPreviewCaption write SetPreviewCaption;
property PreMaxZoom:integer read FPreMaxZoom write setPreMaxZoom;
property PreMinZoom:integer read FPreMinZoom write setPreMinZoom;

property RowsPerPageAutoDetect:boolean read FRowsPerPageAutoDetect write setRowsPerPageAutoDetect;

property SumPerPage:boolean read FSumPerPage write FSumPerPage;
property SumPerPageText:string read FSumPerPageText write FSumPerPageText;
property SumTotal:boolean read FSumTotal write FSumTotal;
property SumTotalText:string read FSumTotalText write FSumTotalText;
end;


procedure Register;

implementation


constructor TPrintDbGrid.Create(AOwner:TComponent);
var temp,temp1:integer;
begin

inherited Create(AOwner);
{ if printer.Printers.Count>0 then
Haveprinter:=true else
Haveprinter:=false;
if Haveprinter then
begin

sc:=windows.GetDeviceCaps(printer.handle,VERTSIZE)/screen.PixelsPerInch;
end;

meta:=TMetaFile.create;
if Haveprinter then
begin

meta.Width:=printer.PageWidth;
meta.Height:=printer.PageHeight;
end;

}

frm:=TForm.Create(Application);
frm.AutoScroll:=false;
frm.Hide;
frm.BorderStyle:=bsSingle;
frm.Font.Name:='宋体';
frm.font.Size:=10;
//生成预览窗体
// frm.BorderIcons:=[biSystemMenu,biMinimize];
frm.Left:=0;
frm.Top:=0;
frm.Width:=screen.Width;
frm.Height:=screen.Height;
frm.BorderIcons:=[biSystemMenu];
frm.OnClose:=closefrm;


pn1:=TPanel.Create(nil);
pn1.Parent:=frm;
pn1.Height:=50;
pn1.Align:=alTOP;
//生成Panel1

Scrbox:=TScrollbox.Create(nil);
Scrbox.parent:=frm;
scrbox.Left:=10;
scrbox.Top:=pn1.Height+10;
//生成Scrollbox


pn2:=TPanel.Create(nil);
pn2.Parent:=Scrbox;
pn2.AutoSize:=true;
pn2.Left:=1;
pn2.Top:=1;
pn2.Top:=pn1.Height+10;
pn2.Width:=frm.Width-10;
pn2.Color:=clWhite;
//生成Pn2

img:=Timage.Create(nil);
img.parent:=pn2;
img.Visible:=true;
img.Stretch:=true;
//生成Image

btn1:=TSpeedbutton.Create(nil);
btn2:=TSpeedbutton.Create(nil);
btn1.parent:=pn1;
btn2.parent:=pn1;
btn1.Width:=pn1.Height *4 div 3;
btn2.Width:=pn1.Height *4 div 3;
btn1.Height:=btn1.Width div 2;
btn2.Height:=btn2.width div 2;
btn1.Left:=1;
btn2.Left:=btn1.Left+btn1.Width;
btn1.Top:=pn1.Height div 6;
btn2.top:=pn1.Height div 6;
btn1.Caption:='打印';
btn2.Caption:='返回';
//生成btn1,btn2
btn1.OnClick:=btn1Click;
btn2.OnClick:=btn2Click;


label1:=TLabel.Create(nil);
label1.parent:=pn1;
label1.AutoSize:=true;
label1.Caption:='缩放比例';
label1.Top:=10;
label1.Left:=btn2.Left+btn2.Width+30;
label2:=TLabel.Create(nil);
label2.parent:=pn1;
label2.AutoSize:=true;
label2.Caption:='总页数/页数';
label2.Top:=30;
label2.Left:=btn2.Left+btn2.Width+30;
//生成两个标签




scr1:=TScrollbar.Create(nil);
scr1.parent:=pn1;
scr1.height:=12;
scr1.Width:=200;
scr1.Max:=200;
//生成scr1


scr2:=TScrollbar.Create(nil);
scr2.parent:=pn1;
scr2.height:=12;
scr2.Width:=200;
scr1.Top:=10;
scr2.Top:=30;
scr2.Min:=1;
scr1.Position:=30;
scr2.Position:=1;
//生成scr2

scr1.left:=btn2.Left+btn2.Width+150;
scr2.Left:=btn2.Left+btn2.Width+150;



FPageHeadFont:=TFont.Create;
FPageFootFont:=Tfont.Create;
FGridFont:=TFont.Create;
//生成三种字体

FPageHeadFont.Name:='宋体';
FPageHeadFont.Size:=18;
FPageHeadFont.Style:=[fsBold];
FPageFootFont.Name:='宋体';
FPageFootFont.Size:=12;
FGridFont.Name:='宋体';
FGridFont.Size:=12;

scr2.OnChange:=scr2change;
scr1.OnChange:=scr1change;

FPageHeadAlignment:=paCenter;
FGridAlignmenth:=paLeft;
FGridAlignmentv:=vaCenter;
FPageFootAlignment:=paRight;
FPreViewCaption:='SRW超级报表';

FPreMaxZoom:=2;
FPreMinZoom:=0;
FBottomBorder:=200;
FLeftborder:=100;
FRightBorder:=100;
FTopBorder:=200;
FSumPerPageText:='本页合计';
FSumTotalText:='总合计';
//以上是各种属性的默认值

for temp:=1 to 1000do
begin
//页数
for temp1:=0 to 50do
begin
//字段
Pagehj[temp1,temp]:=0;
end;

end;



end;




Destructor TPrintDbGrid.Destroy;
begin

// meta.free;
try
meta.Free;
except
end;


FPageHeadFont.free;
FPageFootFont.free;
FGridFont.Free;
inherited Destroy;
end;


procedure TPrintDbGrid.Preview(page:integer);
begin

if FSourceGrid=nil then
exit;
if FSourceGrid.DataSource=nil then
exit;
if FSourceGrid.DataSource.DataSet=nil then
exit;
if FSourceGrid.DataSource.State<>dsBrowse then
exit;
if FSourceGrid.DataSource.DataSet.State<>dsBrowse then
exit;

CreateSize;
metacanvas:=Tmetafilecanvas.Create(meta,0);
//生成WMF文件及Canvas
initdata(metacanvas);
writeCelldata(metacanvas,page);
metacanvas.Free;
img.Picture.Metafile:=meta;
img.AutoSize:=false;
img.Stretch:=true;
// frm.WindowState:=wsMaximized;
frm.AutoScroll:=false;
img.Top:=pn1.height+5;
img.Left:=1;
WindowList:= DisableTaskWindows(frm.Handle);
frm.Show;
frm.Caption:=FPreviewCaption;
scrbox.Width:=frm.Width-20;
scrbox.Height:=frm.Height-90;

scr1change(nil);
scr2change(nil);
scr2.Position:=page;

end;


procedure TPrintDbGrid.Print(page:integer);
begin

if FSourceGrid=nil then
exit;
if FSourceGrid.DataSource=nil then
exit;
if FSourceGrid.DataSource.DataSet=nil then
exit;
if FSourceGrid.DataSource.State<>dsBrowse then
exit;
if FSourceGrid.DataSource.DataSet.State<>dsBrowse then
exit;
if not Haveprinter then
exit;

CreateSize;
printer.Title:=FPreviewCaption;
printer.begin
Doc;
//开始打印
writeCelldata(printer.canvas,page);
if printer.Printing then
printer.EndDoc;
//结束打印
end;




procedure TPrintDbGrid.writecelldata(hb:TCanvas;page:integer);
var temp1,nowleft,nowtop:integer;
aRect:TRect;
nowh:integer;
tmp:integer;
tmp1,tmp2:integer;
zhj:single;
//总合计
begin

if not Haveprinter then
exit;//如果没有打印机,则退出
if page>FPageNumbers then
exit;
if SourceGrid.DataSource.DataSet.State<>dsBrowse then
exit;
//如果表格没有处在浏览状态则退出
for tmp:=0 to 50do
begin

pagehj[page,tmp]:=0;
end;


SourceGrid.DataSource.DataSet.DisableControls;//禁止控件更新
SourceGrid.DataSource.DataSet.First;
SourceGrid.DataSource.DataSet.MoveBy((page-1)*RowsOfThisPage);
//移到当前记录号处

with hbdo
begin

font.Assign(FPageHeadFont);
if hb<>printer.canvas then
font.Size:=trunc(font.size*sc)+1;
arect.Left:=Fleftborder;
arect.Top:=Ftopborder;
arect.Right:=pagewidth-Frightborder-1;
arect.Bottom:=aRect.top+FTableTop;
drawtext(handle,pchar(Fpageheadtext),length(FPageheadtext),aRect,headha);
//写页眉
if FPageHeadLine then
begin

pen.Width:=3;
//页眉线加粗
moveto(aRect.left,aRect.top+HeadFontHeight*3 div 2);
lineto(aRect.right,aRect.top+HeadFontHeight*3 div 2);
//画页眉线
end;

//画页眉线
pen.Width:=1;

//下面是填入数据
Font.assign(FGridFont);
if hb<>printer.canvas then
font.Size:=trunc(font.size*sc)+1;
nowtop:=Ftabletop;
nowleft:=Fleftborder;
if FGridHoriLine then
begin
//是否画横线
if FBorderBold then
pen.Width:=3 else
pen.Width:=1;
//是否加粗边线
moveto(Fleftborder,nowtop);
Lineto(pagewidth-FRightborder,nowtop);
//画顶上的线
end;

pen.Width:=1;
for temp1:=0 to GridLies-1do
begin
//写标题
aRect.Left:=nowleft+1;
aRect.Top:=nowtop+1;
aRect.Right:=aRect.Left+LieWidth[temp1]-1;
aRect.Bottom:=aRect.Top+GridFontHeight*5 div 3;
drawtext(handle,pchar(FSourceGrid.Columns.Items[temp1].Title.caption),length(FSourceGrid.Columns.Items[temp1].Title.caption),aRect,gridha or gridva);
nowleft:=nowleft+Liewidth[temp1];
end;

if FGridHoriLine then
begin

moveto(FLeftborder,nowtop+GridFontHeight*5 div 3);
Lineto(pagewidth-FRightborder,nowtop+GridFontHeight*5 div 3);//画下面的线
end;


nowtop:=nowtop+GridFontHeight*5 div 3;
nowh:=0;
repeat
nowleft:=Fleftborder;
if (page-1)*RowsOfThisPage<FSourceGrid.DataSource.DataSet.RecordCount then
begin

if FGridHoriLine then
begin

moveto(Fleftborder,nowtop);
Lineto(pagewidth-FRightborder,nowtop);
//画顶上的线
end;

for temp1:=0 to GridLies-1do
begin
//这里是填入数据
aRect.Left:=nowleft+1;
aRect.Top:=nowtop+1;
aRect.Right:=aRect.Left+LieWidth[temp1]-1;
aRect.Bottom:=aRect.Top+GridFontHeight*5 div 3;
drawtext(handle,pchar(FSourceGrid.Fields[temp1].asstring),length(FSourceGrid.Fields[temp1].asstring),aRect,gridha or gridva);
nowleft:=nowleft+Liewidth[temp1];
if (FSourceGrid.Fields[temp1].DataType=ftInteger)or (FSourceGrid.Fields[temp1].DataType=ftSmallint)
or (FSourceGrid.Fields[temp1].DataType=ftWord) or (FSourceGrid.Fields[temp1].DataType=ftFloat)
or (FSourceGrid.Fields[temp1].DataType=ftCurrency) then

Pagehj[page,temp1]:=Pagehj[page,temp1]+FSourceGrid.Fields[temp1].AsFloat;
end;


if (nowh>=RowsOfThisPage-1)or (FSourceGrid.DataSource.DataSet.Eof) then
pen.Width:=3 else
pen.width:=1;
if FGridHoriLine then
begin

moveto(Fleftborder,nowtop+GridFontHeight*5 div 3);
Lineto(pagewidth-FRightborder,nowtop+GridFontHeight*5 div 3);//画下面的线
end;

end;


pen.width:=1;
nowtop:=nowtop+GridFontHeight*5 div 3;
nowh:=nowh+1;
if FRowsPerPageAutoDetect then
begin

if (not FSumPerpage) and (nowTop+FootFontHeight*5 div 3>=FTableBottom) then
begin

FRowsPerPage:=nowh;
if isDetect then
exit;
end;

if (FSumPerpage) and (nowTop+FootFontHeight*5 div 3>=FTableBottom) then
begin

FRowsPerPage:=nowh-1;
if isDetect then
exit;
end;

end;

FSourceGrid.DataSource.DataSet.Next;
until (nowh>=RowsOfThisPage) or (FSourceGrid.DataSource.DataSet.Eof);

//以下处理页合计,总合计
tmp2:=0;
if FSumPerPage then
tmp2:=2;
if (FSumTotal) and (page=FPageNumbers) then
tmp2:=tmp2+2;
for tmp:=1 to tmp2do
begin

pen.Width:=1;
if FGridHoriLine then
begin

moveto(Fleftborder,nowtop);
Lineto(pagewidth-FRightborder,nowtop);
//画顶上的线
end;

aRect.Left:=FLeftBorder+1;
aRect.Top:=nowTop;
aRect.Right:=aRect.Left+LieWidth[0]-1;
aRect.Bottom:=aRect.Top+GridFontHeight*5 div 3;
if tmp=1 then
begin
//写页合计
if (FSumTotal) and (page=FPageNumbers) and (not FSumPerPage) then
DrawText(hb.handle,pchar(FSumTotalText),length(FSumTotalText),aRect,gridha or gridva)
else
DrawText(hb.handle,pchar(FSumPerPageText),length(FSumPerPageText),aRect,gridha or gridva);
end;

if tmp=3 then
DrawText(hb.handle,pchar(FSumTotalText),length(FSumTotalText),aRect,gridha or gridva);

nowleft:=Fleftborder;
for temp1:=0 to GridLies-1do
begin

aRect.Left:=nowleft+1;
aRect.Top:=nowtop+1+GridFontHeight*5 div 3;
aRect.Right:=aRect.Left+LieWidth[temp1]-1;
aRect.Bottom:=aRect.Top+GridFontHeight*5 div 3;
if (FSourceGrid.Fields[temp1].DataType=ftInteger)or (FSourceGrid.Fields[temp1].DataType=ftSmallint)
or (FSourceGrid.Fields[temp1].DataType=ftWord) or (FSourceGrid.Fields[temp1].DataType=ftFloat)
or (FSourceGrid.Fields[temp1].DataType=ftCurrency) then
begin

if tmp=1 then
begin

if (FSumTotal) and (page=FPageNumbers) and (not FSumPerPage) then
begin

zhj:=0;
for tmp1:=1 to FPageNumbersdo
begin

zhj:=zhj+Pagehj[tmp1,temp1];
end;

drawtext(handle,pchar(floattostr(zhj)),length(floattostr(zhj)),aRect,gridha or gridva);
end
else
drawtext(handle,pchar(floattostr(Pagehj[page,temp1])),length(floattostr(Pagehj[page,temp1])),aRect,gridha or gridva);
end;

if tmp=3 then
begin

zhj:=0;
for tmp1:=1 to FPageNumbersdo
begin

zhj:=zhj+Pagehj[tmp1,temp1];
end;

drawtext(handle,pchar(floattostr(zhj)),length(floattostr(zhj)),aRect,gridha or gridva);
end;


end;

nowleft:=nowleft+Liewidth[temp1];
end;


if FGridHoriLine then
begin

moveto(FLeftborder,nowtop+GridFontHeight*5 div 3);
Lineto(pagewidth-FRightborder,nowtop+GridFontHeight*5 div 3);//画下面的线
end;

nowtop:=nowtop+GridFontHeight*5 div 3;
end;

//以上是画线及填充页合计,总合计


//以下画表格的竖线
nowleft:=Fleftborder;
if FGridVertLine then
begin

for temp1:=0 to GridLiesdo
begin
//画竖线
if ((temp1=0) or (temp1=GridLies))and (FBorderBold) then
pen.width:=3 else
pen.Width:=1;
moveto(nowleft,Ftabletop);
lineto(nowleft,nowtop);
nowleft:=nowleft+LieWidth[temp1];
end;

end;


if FBorderBold then
begin

pen.width:=3;
moveto(FLeftborder,nowtop);
lineto(PageWidth-FRightBorder,nowtop);
end;



//以下处理页脚
pen.Width:=1;
Font.Assign(FPageFootFont);
if hb<>printer.canvas then
font.Size:=trunc(font.size*sc)+1;
arect.Left:=Fleftborder;
arect.Top:=FTableBottom+FootFontHeight;
arect.Right:=pagewidth-rightborder-1;
arect.Bottom:=Pageheight-FBottomBorder;
if FPageFootLine then
begin

pen.width:=3;
Moveto(aRect.left,aRect.top);
Lineto(Arect.Right,aRect.top);
end;

pen.width:=1;


arect.Left:=FLeftborder;
arect.Top:=aRect.top+FootFontHeight*2 div 3;
arect.Right:=pagewidth-rightborder-1;
arect.Bottom:=PageHeight-BottomBorder;
DrawText(handle,pchar('第'+inttostr(page)+'页'),length('第'+inttostr(page)+'页'),aRect,DT_CENTER or DT_SINGLELINE);//第几页

// arect.Left:=FLeftborder;
// arect.Top:=aRect.top+FootFontHeight*2 div 3;
// arect.Right:=pagewidth-rightborder-1;
// arect.Bottom:=PageHeight-BottomBorder;
DrawText(handle,pchar(Fpagefoottext),length(Fpagefoottext),aRect,Footha);//写页脚
//表格完成
end;

SourceGrid.DataSource.DataSet.EnableControls;//允许控件更新
end;


procedure TPrintDbGrid.Initdata(aCanvas:Tcanvas);
var temp1,temp2,temp3:integer;
begin


case FPageHeadAlignment of
paLeft:headha:=DT_LEFT or DT_SINGLELINE;
paRight:headha:=DT_RIGHT or DT_SINGLELINE;
paCenter:headha:=DT_CENTER or DT_SINGLELINE;
end;

case FPageFootAlignment of
paLeft:footha:=DT_LEFT or DT_SINGLELINE;
paRight:footha:=DT_RIGHT or DT_SINGLELINE;
paCenter:footha:=DT_CENTER or DT_SINGLELINE;
end;

case FGridAlignmenth of
paLeft:Gridha:=DT_LEFT or DT_SINGLELINE;
paRight:Gridha:=DT_RIGHT or DT_SINGLELINE;
paCenter:Gridha:=DT_CENTER or DT_SINGLELINE;
end;

case FGridAlignmentv of
vaTop:Gridva:=DT_TOP or DT_SINGLELINE;
vaBottom:Gridva:=DT_BOTTOM or DT_SINGLELINE;
vaCenter:Gridva:=DT_VCENTER or DT_SINGLELINE;
end;


if Haveprinter then
begin

PageWidth:=printer.PageWidth;
//纸的宽度
PageHeight:=printer.PageHeight;
//纸的高度
end;


aCanvas.Font.Assign(FGridFont);
GridFontHeight:=trunc(aCanvas.TextHeight('高')*sc);
//表格字的高度
aCanvas.Font.Assign(FPageHeadFont);
HeadFontHeight:=trunc(aCanvas.TextHeight('高')*sc);
//页眉字的高度
aCanvas.Font.Assign(FPageFootFont);
FootFontHeight:=trunc(aCanvas.TextHeight('高')*sc);
//页脚字的高度

GridLies:=FSourceGrid.Columns.Count;
//表的列数
Ftabletop:=Ftopborder+3*HeadFontHeight;
//表的上边界
Ftablebottom:=pageheight-FbottomBorder-3*FootFontHeight;
//表的下边界

temp2:=0;
{ for temp1:=0 to GridLies-1do
begin

LieWidth[temp1]:=SourceGrid.Fields[temp1].DisplayWidth;
temp2:=temp2+LieWidth[temp1];
end;

}//这样做在改变sourcegrid1的列宽度时打印时宽度不变
for temp1:=0 to GridLies-1do
begin

LieWidth[temp1]:=SourceGrid.Columns.Items[temp1].Width;
temp2:=temp2+LieWidth[temp1];
end;


temp3:=0;
for temp1:=0 to GridLies-2do
begin

LieWidth[temp1]:=(PageWidth-leftborder-rightborder)*LieWidth[temp1] div temp2;
temp3:=temp3+LieWidth[temp1];
end;

LieWidth[GridLies-1]:=PageWidth-rightborder-leftborder-temp3;
//以上计算出每一列的宽度。


if FRowsPerpage<4 then
FRowsPerPage:=4;
if RowsPerPageAutoDetect then
FRowsPerPage:=1000 else
RowsOfThisPage:=FRowsPerPage;
RowsOfThisPage:=FRowsPerPage;


if (FSumPerPage=true) then
RowsOfThisPage:=FRowsPerPage-2;
FPageNumbers:=SourceGrid.DataSource.DataSet.RecordCount div RowsOfThisPage;
//计算需要多少页?
if FRowsPerPage*RowsOfThisPage<>SourceGrid.DataSource.DataSet.RecordCount then

FPageNumbers:=SourceGrid.DataSource.DataSet.RecordCount div RowsOfThisPage+1;
//计算出页数


aCanvas.FillRect(rect(0,0,meta.width,meta.height));
scr2.Max:=FPageNumbers;
label2.Caption:='总页数/页数:'+inttostr(FPageNumbers)+'/1';
end;


procedure TPrintDbGrid.setPageheadFont(value:TFont);
begin

if FPageHeadFont<>Value then
FPageHeadFont.Assign(Value);
end;


procedure TPrintDbGrid.setPageFootFont(value:TFont);
begin

if FPageFootFont<>Value then
FPageFootFont.Assign(Value);
end;


procedure TPrintDbGrid.setGridFont(Value:Tfont);
begin

if FGridFont<>Value then
FGridFont.Assign(Value);
end;



procedure TPrintDbGrid.setpreviewcaption(Value:string);
begin

if FPreviewCaption<>Value then
begin

FPreViewCaption:=Value;
frm.caption:=value;
end;

end;




procedure TPrintDbGrid.Scr1change(Sender:TObject);
begin

img.width:=scr1.Position*meta.width div 100;
img.height:=scr1.position*meta.Height div 100;
pn2.Left:=(scrbox.Width-pn2.width) div 2;
if pn2.left<=0 then
pn2.Left:=1;
pn2.top:=(scrbox.height-pn2.height) div 2;
if pn2.Top<=0 then
pn2.Top:=1;
label1.caption:='缩放比例:'+inttostr(scr1.position)+'%';
label2.Caption:='总页数/页数:'+inttostr(FPageNumbers)+'/'+inttostr(scr2.position);

end;


procedure TPrintDbGrid.Scr2change(Sender:TObject);
begin

if sender<>nil then
preview(scr2.position);
pn2.Left:=(scrbox.Width-pn2.width) div 2;
if pn2.left<=0 then
pn2.Left:=1;
pn2.top:=(scrbox.height-pn2.height) div 2;
if pn2.Top<=0 then
pn2.Top:=1;
label1.caption:='缩放比例:'+inttostr(scr1.position)+'%';
label2.Caption:='总页数/页数:'+inttostr(FPageNumbers)+'/'+inttostr(scr2.position);

end;



procedure TPrintDbGrid.setPreMaxZoom(value:integer);
begin

if FPreMaxZoom<>Value then
begin

if (FPreMaxZoom<FPreMinZoom) or (FPreMaxZoom>10) then
begin

Application.MessageBox(pchar('该值必须在'+inttostr(FPreMinZoom)+'到10之间'),'错误',mb_ok+mb_iconError);
exit;
end;

FPreMaxZoom:=Value;
Scr1.Max:=Value*100;
end;

end;


procedure TPrintDbGrid.setpreMinZoom(value:integer);
begin

if FPreMinZoom<>Value then
begin

if (FPreMinZoom=FPreMaxZoom) or (FPreMinZoom<0) then
begin

Application.MessageBox(Pchar('该值必须在0到'+inttostr(FPreMaxZoom)+'之间'),'错误',mb_ok+mb_iconError);
exit;
end;

FPreMinZoom:=Value;
Scr1.Min:=Value*100;
end;


end;



procedure TPrintDbGrid.setRowsPerPageAutoDetect(value:boolean);
begin

if value=FRowsPerPageAutoDetect then
exit;
FRowsPerPageAutoDetect:=Value;
end;



procedure TPrintDbGrid.DetectRowsPerPage;
begin

if FRowsPerPageAutoDetect=false then
exit;
FRowsPerPage:=1000;
if FSourceGrid.DataSource.State<>dsBrowse then
exit;
if FSourceGrid.DataSource.DataSet.State<>dsBrowse then
exit;
isDetect:=true;
metacanvas:=Tmetafilecanvas.Create(meta,0);
//生成WMF文件及Canvas
initdata(MetaCanvas);
writeCelldata(metacanvas,1);
metacanvas.Free;

metacanvas:=Tmetafilecanvas.Create(meta,0);
//生成WMF文件及Canvas
initdata(MetaCanvas);
isDetect:=false;
//在调用WriteCelldata时就得到了FRowPerPage的值
//再次调用initdata是为了取得FPageNumbers的值

end;



procedure TPrintDbGrid.btn1click(sender:TObject);
var temp:integer;
begin

for temp:=1 to FPageNumbersdo
begin

Print(temp);
end;

end;


procedure TPrintDbGrid.loaded;
begin

inherited loaded;
DetectRowsPerPage;
end;



procedure TPrintDbGrid.SavetoFile(FileName:string;Page:integer);
begin

if FSourceGrid.DataSource.State<>dsBrowse then
exit;
if FSourceGrid.DataSource.DataSet.State<>dsBrowse then
exit;

metacanvas:=Tmetafilecanvas.Create(meta,0);
//生成WMF文件及Canvas
initdata(metacanvas);
writeCelldata(metacanvas,page);
metacanvas.Free;
try
meta.SaveToFile(Filename);
except
application.MessageBox('不能保存文件!','错误',mb_ok+mb_iconError);
end;


end;



procedure TPrintDbGrid.btn2click(sender:TObject);
begin

frm.Close;
end;



procedure TPrintDbGrid.DetectPageNumbers;
begin

if FSourceGrid=nil then
exit;
if FSourceGrid.DataSource=nil then
exit;
if FSourceGrid.DataSource.DataSet=nil then
exit;
if FSourceGrid.DataSource.State<>dsBrowse then
exit;
if FSourceGrid.DataSource.DataSet.State<>dsBrowse then
exit;

metacanvas:=Tmetafilecanvas.Create(meta,0);
//生成WMF文件及Canvas
initdata(metacanvas);
writeCelldata(metacanvas,1);
metacanvas.Free;
end;


procedure TPrintDBGrid.createsize;
begin

if printer.Printers.Count>0 then
Haveprinter:=true else
Haveprinter:=false;
printer.PrinterIndex:=-1;
printer.PrinterIndex:=0;
if Haveprinter then
begin

sc:=windows.GetDeviceCaps(printer.handle,VERTSIZE)/screen.PixelsPerInch;
end;


try
meta.Free;
except
end;

meta:=TMetaFile.create;
if Haveprinter then
begin

meta.Width:=printer.PageWidth;
meta.Height:=printer.PageHeight;
end;


end;


procedure TPrintDBGrid.PrinterSetup;
var ps:Tprintersetupdialog;
begin

if printer.Printers.Count>0 then
Haveprinter:=true else
Haveprinter:=false;
if not Haveprinter then
exit;
printer.PrinterIndex:=-1;
printer.PrinterIndex:=0;
ps:=Tprintersetupdialog.Create(nil);
try
ps.Execute;
except
application.MessageBox('设置打印机时出错!','错误',mb_ok+mb_iconerror);
end;

try
ps.free;
except
end;


end;


function TPrintDBGrid.getprintername:string;
begin

if printer.Printers.Count>0 then
Haveprinter:=true else
Haveprinter:=false;
if not Haveprinter then
exit;
printer.PrinterIndex:=-1;
printer.PrinterIndex:=0;
try
result:=printer.Printers.Strings[0];
except
result:='Error:Can not get PrinterName';
end;

end;


procedure TPrintDBGrid.Closefrm(Sender: TObject;
var Action: TCloseAction);
begin

EnableTaskWindows(WindowList);
end;


procedure TPrintDBGrid.Closepreviewwindow;
begin

frm.Close;
end;



procedure Register;
begin

RegisterComponents('standard', [TPrintDbGrid]);
end;





end.

 
>thanks to zhengzhijia
有没有简单一点的!!
 
目前没有,不过我可以考虑考虑在说。
 
找来Ehlib过来看看,它的打印功能不错,可以参考!
 
ur email
i will give u!
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
639
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
687
import
I
后退
顶部