D
dragon_man2002
Unregistered / Unconfirmed
GUEST, unregistred user!
这是我在网上搜到的代码!但它只能打印DBGrid中所有的内容,而且格式不能自定义!
DBGridDBGrid中的内容如下)
部门 员工 工资金额 领取日期 说明
部门1 张三 3000 2005-12-15 adasdasd
部门1 李四 4000 2005-12-15 adasdasd
部门2 王五 3200 2005-12-15 adasdasd
部门2 小刘 3500 2005-12-15 adasdasd
要求只打印选中的那一行,并以自定义的格式打印,格式如下:
部门1
____________________________________________
张三:
领取工资:3000元
签字:
____________________________________________
望大家指点
//==============================================================================
//打印DBGrid********************************************************************
//==============================================================================
procedure DBGridPrint(DBGrid: THxDBGrid;
Title: string);
var
PrintDialog: TPrintDialog;
RowHeight, Temp_X, Temp_Y, PageEdgeX, PageEdgeY, PixelsPerInchX, PixelsPerInchY: integer;
TempStr: string;
Scale:do
uble;
Rect: TRect;
procedure Print_Title;
begin
Rect := Bounds(0,0,Printer.PageWidth,PageEdgeY);
Printer.Canvas.Font.Name := '楷体_GB2312';
Printer.Canvas.Font.Style := Printer.Canvas.Font.Style + [fsBold];
Printer.Canvas.Font.Size := 20;
DrawText(Printer.Canvas.Handle,PChar(Title),Length(Title),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
Printer.Canvas.Rectangle(PageEdgeX, PageEdgeY, Printer.PageWidth - PageEdgeX, PageEdgeY + 1);
end;
//==============================================================================
//2.输出列头********************************************************************
//==============================================================================
procedure Print_Column;
var j: integer;
begin
Printer.Canvas.Font.Name := '黑体';
Printer.Canvas.Font.Size := 9;
Temp_X := PageEdgeX;
Temp_Y := PageEdgeY;
for j:=1 to DBGrid.Columns.Countdo
begin
if not DBGrid.Columns[j-1].Visible then
Continue;
TempStr := DBGrid.Columns[j-1].Title.Caption;
Rect := Bounds(Temp_X, Temp_Y, Trunc(DBGrid.Columns[j-1].Width*Scale), RowHeight);
case DBGrid.Columns[j-1].Field.Alignment of
taCenter: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
taLeftJustify: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
taRightJustify: if Rect.Right-Rect.Left>=Printer.Canvas.TextWidth(TempStr)
then
DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_RIGHT or DT_VCENTER or DT_SINGLELINE)
else
DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
end;
Temp_X := Temp_X + Trunc(DBGrid.Columns[j-1].Width*Scale);
end;
Temp_Y := Temp_Y + RowHeight;
end;
//==============================================================================
//3.输出DBGrid内容**************************************************************
//==============================================================================
procedure Print_Cells;
var j: integer;
begin
Printer.Canvas.Font.Name := '宋体';
Printer.Canvas.Font.Style := Printer.Canvas.Font.Style - [fsBold];
while Temp_Y<Printer.PageHeight-PageEdgeYdo
begin
Temp_X := PageEdgeX;
for j:=1 to DBGrid.Columns.Countdo
begin
if not DBGrid.Columns[j-1].Visible then
Continue;
//====================================================================
Rect := Bounds(Temp_X, Temp_Y, Trunc(DBGrid.Columns[j-1].Width*Scale), RowHeight);
if (DBGrid.Columns[j-1].Field is TCurrencyField)
or (DBGrid.Columns[j-1].Field is TLargeIntField)
or (DBGrid.Columns[j-1].Field is TSmallIntField)
or (DBGrid.Columns[j-1].Field is TIntegerField)
or (DBGrid.Columns[j-1].Field is TFloatField)
or (DBGrid.Columns[j-1].Field is TWordField)
then
TempStr := FormatFloat(',##0.00',DBGrid.Columns[j-1].Field.AsFloat)
else
TempStr := DBGrid.Columns[j-1].Field.AsString;
//====================================================================
case DBGrid.Columns[j-1].Field.Alignment of
taCenter: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
taLeftJustify: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
taRightJustify: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_RIGHT or DT_VCENTER or DT_SINGLELINE);
end;
Temp_X := Temp_X + Trunc(DBGrid.Columns[j-1].Width*Scale);
end;
Temp_Y := Temp_Y + RowHeight;
DBGrid.DataSource.DataSet.Next;
if DBGrid.DataSource.DataSet.Eof then
Exit;
end;
end;
//==============================================================================
//4.输出页脚********************************************************************
//==============================================================================
procedure Print_Footer;
begin
Temp_Y := Printer.PageHeight - PageEdgeY + RowHeight;
//========================================================================
//4.0.输出横线************************************************************
//========================================================================
Printer.Canvas.Rectangle(PageEdgeX, Temp_Y, Printer.PageWidth - PageEdgeX, Temp_Y + 1);
//========================================================================
//4.1.输出日期************************************************************
//========================================================================
Rect := Bounds(PageEdgeX, Temp_Y, Printer.PageWidth-PageEdgeX, RowHeight);
DrawText(Printer.Canvas.Handle,PChar(DateTimeToStr(Now)),Length(DateTimeToStr(Now)),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
//========================================================================
//4.2.输出页号************************************************************
//========================================================================
Rect := Bounds(PageEdgeX, Temp_Y, Printer.PageWidth-PageEdgeX*2, RowHeight);
DrawText(Printer.Canvas.Handle,PChar('#'+IntToStr(Printer.PageNumber)),Length('#'+IntToStr(Printer.PageNumber)),Rect,DT_RIGHT or DT_VCENTER or DT_SINGLELINE);
end;
//==============================================================================
begin
PrintDialog := TPrintDialog.Create(DBGrid);
if PrintDialog.Execute then
begin
//==========================================================================
//0.取当前打印机X,Y方向每英寸像素*******************************************
//==========================================================================
PixelsPerInchX := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
PixelsPerInchY := GetDeviceCaps(Printer.Handle, LOGPIXELSY);
//==========================================================================
//1.变量初始化**************************************************************
//==========================================================================
PageEdgeX := PixelsPerInchX div 6;
PageEdgeY := PixelsPerInchY div 5 * 4;
RowHeight := Trunc(1.5 * 9 * PixelsPerInchY / 72);
Scale := PixelsPerInchX / Screen.PixelsPerInch;
//==========================================================================
try
Printer.begin
Doc;
DBGrid.DataSource.DataSet.First;
while not DBGrid.DataSource.DataSet.Eofdo
begin
Print_Title;
Print_Column;
Print_Cells;
Print_Footer;
Printer.NewPage;
end;
//========================================================================
if not Printer.Aborted then
Printer.EndDoc;
except
on E:EPrinterdo
begin
MessageBox(Application.Handle,PChar('打印机没有准备好!'),'提示!',MB_OK+MB_ICONINFORMATION);
Printer.Abort;
Exit;
end;
on E:Exceptiondo
begin
raise;
Exit;
end;
end;
end;
//============================================================================
PrintDialog.Free;
end;
DBGridDBGrid中的内容如下)
部门 员工 工资金额 领取日期 说明
部门1 张三 3000 2005-12-15 adasdasd
部门1 李四 4000 2005-12-15 adasdasd
部门2 王五 3200 2005-12-15 adasdasd
部门2 小刘 3500 2005-12-15 adasdasd
要求只打印选中的那一行,并以自定义的格式打印,格式如下:
部门1
____________________________________________
张三:
领取工资:3000元
签字:
____________________________________________
望大家指点
//==============================================================================
//打印DBGrid********************************************************************
//==============================================================================
procedure DBGridPrint(DBGrid: THxDBGrid;
Title: string);
var
PrintDialog: TPrintDialog;
RowHeight, Temp_X, Temp_Y, PageEdgeX, PageEdgeY, PixelsPerInchX, PixelsPerInchY: integer;
TempStr: string;
Scale:do
uble;
Rect: TRect;
procedure Print_Title;
begin
Rect := Bounds(0,0,Printer.PageWidth,PageEdgeY);
Printer.Canvas.Font.Name := '楷体_GB2312';
Printer.Canvas.Font.Style := Printer.Canvas.Font.Style + [fsBold];
Printer.Canvas.Font.Size := 20;
DrawText(Printer.Canvas.Handle,PChar(Title),Length(Title),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
Printer.Canvas.Rectangle(PageEdgeX, PageEdgeY, Printer.PageWidth - PageEdgeX, PageEdgeY + 1);
end;
//==============================================================================
//2.输出列头********************************************************************
//==============================================================================
procedure Print_Column;
var j: integer;
begin
Printer.Canvas.Font.Name := '黑体';
Printer.Canvas.Font.Size := 9;
Temp_X := PageEdgeX;
Temp_Y := PageEdgeY;
for j:=1 to DBGrid.Columns.Countdo
begin
if not DBGrid.Columns[j-1].Visible then
Continue;
TempStr := DBGrid.Columns[j-1].Title.Caption;
Rect := Bounds(Temp_X, Temp_Y, Trunc(DBGrid.Columns[j-1].Width*Scale), RowHeight);
case DBGrid.Columns[j-1].Field.Alignment of
taCenter: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
taLeftJustify: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
taRightJustify: if Rect.Right-Rect.Left>=Printer.Canvas.TextWidth(TempStr)
then
DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_RIGHT or DT_VCENTER or DT_SINGLELINE)
else
DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
end;
Temp_X := Temp_X + Trunc(DBGrid.Columns[j-1].Width*Scale);
end;
Temp_Y := Temp_Y + RowHeight;
end;
//==============================================================================
//3.输出DBGrid内容**************************************************************
//==============================================================================
procedure Print_Cells;
var j: integer;
begin
Printer.Canvas.Font.Name := '宋体';
Printer.Canvas.Font.Style := Printer.Canvas.Font.Style - [fsBold];
while Temp_Y<Printer.PageHeight-PageEdgeYdo
begin
Temp_X := PageEdgeX;
for j:=1 to DBGrid.Columns.Countdo
begin
if not DBGrid.Columns[j-1].Visible then
Continue;
//====================================================================
Rect := Bounds(Temp_X, Temp_Y, Trunc(DBGrid.Columns[j-1].Width*Scale), RowHeight);
if (DBGrid.Columns[j-1].Field is TCurrencyField)
or (DBGrid.Columns[j-1].Field is TLargeIntField)
or (DBGrid.Columns[j-1].Field is TSmallIntField)
or (DBGrid.Columns[j-1].Field is TIntegerField)
or (DBGrid.Columns[j-1].Field is TFloatField)
or (DBGrid.Columns[j-1].Field is TWordField)
then
TempStr := FormatFloat(',##0.00',DBGrid.Columns[j-1].Field.AsFloat)
else
TempStr := DBGrid.Columns[j-1].Field.AsString;
//====================================================================
case DBGrid.Columns[j-1].Field.Alignment of
taCenter: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
taLeftJustify: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
taRightJustify: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_RIGHT or DT_VCENTER or DT_SINGLELINE);
end;
Temp_X := Temp_X + Trunc(DBGrid.Columns[j-1].Width*Scale);
end;
Temp_Y := Temp_Y + RowHeight;
DBGrid.DataSource.DataSet.Next;
if DBGrid.DataSource.DataSet.Eof then
Exit;
end;
end;
//==============================================================================
//4.输出页脚********************************************************************
//==============================================================================
procedure Print_Footer;
begin
Temp_Y := Printer.PageHeight - PageEdgeY + RowHeight;
//========================================================================
//4.0.输出横线************************************************************
//========================================================================
Printer.Canvas.Rectangle(PageEdgeX, Temp_Y, Printer.PageWidth - PageEdgeX, Temp_Y + 1);
//========================================================================
//4.1.输出日期************************************************************
//========================================================================
Rect := Bounds(PageEdgeX, Temp_Y, Printer.PageWidth-PageEdgeX, RowHeight);
DrawText(Printer.Canvas.Handle,PChar(DateTimeToStr(Now)),Length(DateTimeToStr(Now)),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
//========================================================================
//4.2.输出页号************************************************************
//========================================================================
Rect := Bounds(PageEdgeX, Temp_Y, Printer.PageWidth-PageEdgeX*2, RowHeight);
DrawText(Printer.Canvas.Handle,PChar('#'+IntToStr(Printer.PageNumber)),Length('#'+IntToStr(Printer.PageNumber)),Rect,DT_RIGHT or DT_VCENTER or DT_SINGLELINE);
end;
//==============================================================================
begin
PrintDialog := TPrintDialog.Create(DBGrid);
if PrintDialog.Execute then
begin
//==========================================================================
//0.取当前打印机X,Y方向每英寸像素*******************************************
//==========================================================================
PixelsPerInchX := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
PixelsPerInchY := GetDeviceCaps(Printer.Handle, LOGPIXELSY);
//==========================================================================
//1.变量初始化**************************************************************
//==========================================================================
PageEdgeX := PixelsPerInchX div 6;
PageEdgeY := PixelsPerInchY div 5 * 4;
RowHeight := Trunc(1.5 * 9 * PixelsPerInchY / 72);
Scale := PixelsPerInchX / Screen.PixelsPerInch;
//==========================================================================
try
Printer.begin
Doc;
DBGrid.DataSource.DataSet.First;
while not DBGrid.DataSource.DataSet.Eofdo
begin
Print_Title;
Print_Column;
Print_Cells;
Print_Footer;
Printer.NewPage;
end;
//========================================================================
if not Printer.Aborted then
Printer.EndDoc;
except
on E:EPrinterdo
begin
MessageBox(Application.Handle,PChar('打印机没有准备好!'),'提示!',MB_OK+MB_ICONINFORMATION);
Printer.Abort;
Exit;
end;
on E:Exceptiondo
begin
raise;
Exit;
end;
end;
end;
//============================================================================
PrintDialog.Free;
end;