怎么打印出ListView中的内容 一个ListView,显示内容为ID 姓名 性别 年龄1 XX XX XX....n XX XX XX要通过打印机这样打印出来 职员表1 XX XX XX....n XX XX XX请问该怎么编程 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls;type TForm1 = class(TForm) ListView1: TListView; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}uses printers;procedure TForm1.Button1Click(Sender: TObject);var i:integer; item:TListItem; id, name, sex, age:string; lpstring:string;begin for i:=0 to ListView1.Items.Count-1 do begin item:=ListView1.Items[i]; id:=item.Caption; name:=item.SubItems[0]; sex:=item.SubItems[1]; age:=item.SubItems[2]; lpstring:=Format('%s---%s----%s-----%s',[id,name,sex,age]); Printer.Canvas.TextRect(Rect(200,200,PageWidth-200,PageHeight-200),200, 200, lpstring); end;end;end. unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls;type TForm1 = class(TForm) ListView1: TListView; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}uses printers;procedure TForm1.Button1Click(Sender: TObject);var i:integer; item:TListItem; id, name, sex, age:string; lpstring:string;begin Printer.BeginDoc; for i:=0 to ListView1.Items.Count-1 do begin item:=ListView1.Items[i]; id:=item.Caption; name:=item.SubItems[0]; sex:=item.SubItems[1]; age:=item.SubItems[2]; lpstring:=Format('%s---%s----%s-----%s',[id,name,sex,age]); Printer.Canvas.TextRect(Rect(200,200,PageWidth-200,PageHeight-200),200, 200, lpstring); end; Printer.EndDoc;end;end. //哎,楼主真小气,呵呵!不过还是要帮你!//前面一定要加Printers单元//全局常量const ChiefTitle:string='信息安全综合强审计系统'; InferiorTitle:string='中心管理日志报表';function TMainForm.PrintListview(OListView: TListView):boolean;//-----------------------------------------------------var pWidth, pHeight, i: Integer; v, h: Real; CurItem,iColumnCount: Integer; aCols: array of Integer; iTotColsWidth, iInnerWidth, TopMarg, LinesOnPage, CurLine, TekstHeight, CurCol: Integer; CurRect: TRect; CurStr: string; CurLeft, NumPages, TmpPos: Integer;begin result:=false; if PrintDialog1.Execute then begin iColumnCount:= oListview.Columns.Count; SetLength(aCols,oListview.Columns.Count+ 1); Printer.Title := '打印报表-汉邦软科集团'; //提交给打印机的名称 Printer.Copies := 1; Printer.Orientation := poPortrait; //报表是垂直显示的 Printer.BeginDoc; pHeight := Printer.PageHeight; //当前打印页的高度 pWidth := Printer.PageWidth; //当前打印页的宽度 v := (pHeight + (2 * GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY))) / (29.7 * 0.95); //0.95 用来给不同机器打印机做调整因素 h := (pWidth + (2 * GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX))) / 21; // 计算总的宽度 iTotColsWidth := 0; for i := 0 to iColumnCount - 1 do iTotColsWidth := iTotColsWidth + oListView.Columns[i].Width; //计算间距 aCols[0] := Round(1.5 * h); //left margin aCols[iColumnCount + 0] := pWidth - Round(1.5 * h); //rigth margin iInnerWidth := aCols[iColumnCount + 0] - aCols[0]; // space between margins //计算每行开始 for i := 0 to iColumnCount - 1 do aCols[i + 1] := aCols[i] + Round(oListView.Columns[i].Width / iTotColsWidth * iInnerWidth); TopMarg := Round(2.5 * v); with Printer.Canvas do begin Font.Size := 10; Font.Style := []; Font.Name := 'Times New Roman'; Font.Color := RGB(0, 0, 0); TekstHeight := Printer.Canvas.TextHeight('dummy'); LinesOnPage := Round((PHeight - (5 * v)) / TekstHeight); NumPages := 1; // 得到所有的打印机列表 while (NumPages * LinesOnPage) < oListView.Items.Count do inc(NumPages); // 开始打印 CurLine := 0; for CurItem := 0 to oListView.Items.Count - 1 do begin if (CurLine > LinesOnPage) or (CurLine = 0) then begin if (CurLine > LinesOnPage) then Printer.NewPage; CurLine := 1; if Printer.PageNumber = NumPages then begin MoveTo(aCols[1], topMarg); for i := 1 to iColumnCount - 1 do begin LineTo(aCols[i], TopMarg + (TekstHeight * (oListView.Items.Count - CurItem + 2))); MoveTo(aCols[i + 1], topMarg); end; end else begin //在数据中移动垂直线 <显示分割线> for i := 1 to iColumnCount - 1 do begin MoveTo(aCols[i], topMarg); LineTo(aCols[i], TopMarg + (TekstHeight * (LinesOnPage + 1))); end; end; Font.Style := [fsBold]; // 打印listview的columns项目 for i := 0 to iColumnCount - 1 do begin TextRect(Rect(aCols[i] + Round(0.1 * h), TopMarg - Round(0.1 * v), aCols[i + 1] - Round(0.1 * h) , TopMarg + TekstHeight - Round(0.1 * v)), ((aCols[i + 1] - aCols[i]) div 2) + aCols[i] - (TextWidth(oListview.Columns.Items[i].Caption) div 2), TopMarg - Round(0.1 * v), oListview.Columns.Items[i].Caption); end; // 在column headers之下拖动水平线 MoveTo(aCols[0] - Round(0.1 * h), TopMarg + TekstHeight - Round(0.05 * v)); LineTo(aCols[iColumnCount] + Round(0.1 * h), TopMarg + TekstHeight - Round(0.05 * v)); //打印日期和页码 Font.Size := 8; Font.Style := []; TmpPos := (TextWidth('日期: ' + DateToStr(Date) + ' 页码: ' + IntToStr(Printer.PageNumber) + ' / ' + IntToStr(NumPages))) div 2; TmpPos := PWidth - Round(1.5 * h) - (TmpPos * 2); Font.Size := 8; Font.Style := []; TextOut(TmpPos,printer.PageHeight-Round(0.5 * v), '日期: ' + DateToStr(Date) + ' 页码: ' + IntToStr(Printer.PageNumber) + ' / ' + IntToStr(NumPages)); // 打印报表主要标题 Font.Size := 18; TextOut((printer.pagewidth - TextWidth(ChiefTitle)) div 2, Round(0.9 * v),ChiefTitle); // 打印报表次要标题 Font.Size := 14; TextOut((printer.pagewidth - TextWidth(InferiorTitle)) div 2, Round(1.6 * v),InferiorTitle); Font.Size := 10; Font.Style := []; end; CurRect.Top := TopMarg + (CurLine * TekstHeight); CurRect.Bottom := TopMarg + ((CurLine + 1) * TekstHeight); // 打印listview的内容 for CurCol := -1 to iColumnCount - 2 do begin CurRect.Left := aCols[CurCol + 1] + Round(0.1 * h); CurRect.Right := aCols[CurCol + 2] - Round(0.1 * h); try if CurCol = -1 then CurStr := oListView.Items[CurItem].Caption else CurStr := oListView.Items[CurItem].SubItems[CurCol]; except CurStr := ''; end; CurLeft := CurRect.Left; // 排的左边 //把数据写到TextRect TextRect(CurRect, CurLeft, CurRect.Top, CurStr); end; Inc(CurLine); end; end; Printer.EndDoc; result:=true; end;end; 请教三楼v:=(PageHeight+(2*GetDeviceCaps(Printer.Handle,PHYSICALOFFSETY)))/(29.7 * 0.95);//0.95 用来给不同机器打印机做调整因素h := (pWidth + (2 * GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX))) / 21;的作用是什么 adoconnection向adoquery赋数据库名的代码怎么写? ehlib的问题,下拉列表 在adoquery的sql语句中使用参数,系统提示找不到参数 从应用层返回数据信息! 如何实现模糊查询 控件原理,高手请进! 数据库是SQL SERVER,DBGRID怎样可以将如果记录的数量大于100显示红色,小于则为白色? 哪里有Delphi 6的安装程序下?Delphi 5也行! 只向高手请教!!!!!!! 请教如何修改如何提交数据(一对多的表) 请大家帮忙!! adotable sqlserver 2000
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
implementation{$R *.dfm}uses printers;procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
item:TListItem;
id,
name,
sex,
age:string;
lpstring:string;
begin
for i:=0 to ListView1.Items.Count-1 do
begin
item:=ListView1.Items[i];
id:=item.Caption;
name:=item.SubItems[0];
sex:=item.SubItems[1];
age:=item.SubItems[2];
lpstring:=Format('%s---%s----%s-----%s',[id,name,sex,age]);
Printer.Canvas.TextRect(Rect(200,200,PageWidth-200,PageHeight-200),200, 200, lpstring);
end;
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
implementation{$R *.dfm}uses printers;procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
item:TListItem;
id,
name,
sex,
age:string;
lpstring:string;
begin
Printer.BeginDoc;
for i:=0 to ListView1.Items.Count-1 do
begin
item:=ListView1.Items[i];
id:=item.Caption;
name:=item.SubItems[0];
sex:=item.SubItems[1];
age:=item.SubItems[2];
lpstring:=Format('%s---%s----%s-----%s',[id,name,sex,age]);
Printer.Canvas.TextRect(Rect(200,200,PageWidth-200,PageHeight-200),200, 200, lpstring);
end;
Printer.EndDoc;
end;end.
//前面一定要加Printers单元
//全局常量
const ChiefTitle:string='信息安全综合强审计系统';
InferiorTitle:string='中心管理日志报表';
function TMainForm.PrintListview(OListView: TListView):boolean;
//-----------------------------------------------------
var
pWidth, pHeight, i: Integer;
v, h: Real;
CurItem,iColumnCount: Integer;
aCols: array of Integer;
iTotColsWidth, iInnerWidth, TopMarg, LinesOnPage, CurLine, TekstHeight, CurCol: Integer;
CurRect: TRect;
CurStr: string;
CurLeft, NumPages, TmpPos: Integer;
begin
result:=false;
if PrintDialog1.Execute then
begin
iColumnCount:= oListview.Columns.Count;
SetLength(aCols,oListview.Columns.Count+ 1);
Printer.Title := '打印报表-汉邦软科集团'; //提交给打印机的名称
Printer.Copies := 1;
Printer.Orientation := poPortrait; //报表是垂直显示的
Printer.BeginDoc;
pHeight := Printer.PageHeight; //当前打印页的高度
pWidth := Printer.PageWidth; //当前打印页的宽度
v := (pHeight + (2 * GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY))) / (29.7 * 0.95);
//0.95 用来给不同机器打印机做调整因素
h := (pWidth + (2 * GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX))) / 21;
// 计算总的宽度
iTotColsWidth := 0;
for i := 0 to iColumnCount - 1 do
iTotColsWidth := iTotColsWidth + oListView.Columns[i].Width;
//计算间距
aCols[0] := Round(1.5 * h); //left margin
aCols[iColumnCount + 0] := pWidth - Round(1.5 * h); //rigth margin
iInnerWidth := aCols[iColumnCount + 0] - aCols[0]; // space between margins
//计算每行开始
for i := 0 to iColumnCount - 1 do
aCols[i + 1] := aCols[i] + Round(oListView.Columns[i].Width / iTotColsWidth * iInnerWidth);
TopMarg := Round(2.5 * v);
with Printer.Canvas do
begin
Font.Size := 10;
Font.Style := [];
Font.Name := 'Times New Roman';
Font.Color := RGB(0, 0, 0);
TekstHeight := Printer.Canvas.TextHeight('dummy');
LinesOnPage := Round((PHeight - (5 * v)) / TekstHeight);
NumPages := 1;
// 得到所有的打印机列表
while (NumPages * LinesOnPage) < oListView.Items.Count do
inc(NumPages);
// 开始打印
CurLine := 0;
for CurItem := 0 to oListView.Items.Count - 1 do
begin
if (CurLine > LinesOnPage) or (CurLine = 0) then
begin
if (CurLine > LinesOnPage) then Printer.NewPage;
CurLine := 1;
if Printer.PageNumber = NumPages then
begin
MoveTo(aCols[1], topMarg);
for i := 1 to iColumnCount - 1 do
begin
LineTo(aCols[i], TopMarg + (TekstHeight * (oListView.Items.Count - CurItem + 2)));
MoveTo(aCols[i + 1], topMarg);
end;
end
else
begin
//在数据中移动垂直线 <显示分割线>
for i := 1 to iColumnCount - 1 do
begin
MoveTo(aCols[i], topMarg);
LineTo(aCols[i], TopMarg + (TekstHeight * (LinesOnPage + 1)));
end;
end; Font.Style := [fsBold];
// 打印listview的columns项目
for i := 0 to iColumnCount - 1 do
begin
TextRect(Rect(aCols[i] + Round(0.1 * h), TopMarg - Round(0.1 * v), aCols[i + 1] - Round(0.1 * h)
, TopMarg + TekstHeight - Round(0.1 * v)), ((aCols[i + 1] - aCols[i]) div 2) +
aCols[i] - (TextWidth(oListview.Columns.Items[i].Caption) div 2),
TopMarg - Round(0.1 * v), oListview.Columns.Items[i].Caption);
end; // 在column headers之下拖动水平线
MoveTo(aCols[0] - Round(0.1 * h), TopMarg + TekstHeight - Round(0.05 * v));
LineTo(aCols[iColumnCount] + Round(0.1 * h), TopMarg + TekstHeight - Round(0.05 * v)); //打印日期和页码
Font.Size := 8;
Font.Style := [];
TmpPos := (TextWidth('日期: ' + DateToStr(Date) + ' 页码: ' +
IntToStr(Printer.PageNumber) + ' / ' + IntToStr(NumPages))) div 2; TmpPos := PWidth - Round(1.5 * h) - (TmpPos * 2); Font.Size := 8;
Font.Style := [];
TextOut(TmpPos,printer.PageHeight-Round(0.5 * v), '日期: ' + DateToStr(Date) +
' 页码: ' + IntToStr(Printer.PageNumber) + ' / ' + IntToStr(NumPages)); // 打印报表主要标题
Font.Size := 18;
TextOut((printer.pagewidth - TextWidth(ChiefTitle)) div 2, Round(0.9 * v),ChiefTitle);
// 打印报表次要标题
Font.Size := 14;
TextOut((printer.pagewidth - TextWidth(InferiorTitle)) div 2, Round(1.6 * v),InferiorTitle);
Font.Size := 10;
Font.Style := [];
end; CurRect.Top := TopMarg + (CurLine * TekstHeight);
CurRect.Bottom := TopMarg + ((CurLine + 1) * TekstHeight); // 打印listview的内容
for CurCol := -1 to iColumnCount - 2 do
begin
CurRect.Left := aCols[CurCol + 1] + Round(0.1 * h);
CurRect.Right := aCols[CurCol + 2] - Round(0.1 * h);
try
if CurCol = -1 then
CurStr := oListView.Items[CurItem].Caption
else
CurStr := oListView.Items[CurItem].SubItems[CurCol];
except
CurStr := '';
end;
CurLeft := CurRect.Left; // 排的左边
//把数据写到TextRect
TextRect(CurRect, CurLeft, CurRect.Top, CurStr); end;
Inc(CurLine);
end;
end;
Printer.EndDoc;
result:=true;
end;end;
//0.95 用来给不同机器打印机做调整因素
h := (pWidth + (2 * GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX))) / 21;的作用是什么