打印时如何只打印表单中的DbGrid和Panel,而其它控件不打印呢?难啊。

jiaxianglei 2003-08-26 07:48:31
在表单中有SpeedButton,treeview,DbGrid,Panel。但是我想点打印时只打印DbGrid,如何实现?只打印Panel又该如何实现呢?高手请进。
...全文
90 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰刀封情 2003-09-07
  • 打赏
  • 举报
回复
procedure TForm1.Button2Click(Sender: TObject);
var
pl,pr,pt,pb:integer;//页边距
px,py:integer;//打印机分辨率
ps:integer;//打印机与显示器分辨率之比
lh:integer;//行高
i:integer;
x,y:integer;//打印坐标
rowb,colb:integer; //行距、行距
begin
//初始化页面
pl:=spinedit1.Value;
pr:=spinedit2.Value;
pt:=spinedit3.Value;
pb:=spinedit4.Value;
rowb:=spinedit6.Value;//行距
colb:=spinedit7.Value;//列距
//将获得的打印机分辨率由厘米转换为英寸
px:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/2.54);
py:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSY)/2.54);
//将屏幕分辨率转换为打印机的比例 
ps:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)
/Screen.PixelsPerInch);
//设置打印方向
if combobox1.Text='横向打印' then
printer.Orientation:=poPortrait;
else
printer.Orientation:=poLandscape;
//获得打印的字体和大小
printer.Canvas.Font.Name:=combobox2.Text;
printer.canvas.Font.Size:=spinedit5.Value;
printer.canvas.Font.Color:=clred;
printer.Canvas.Pen.Color:=clblue;
//设置行高
lh:=printer.canvas.textheight(dbgrid1.Font.Name);
//打印的起始位置(打印机画布上的位置)
x:=PX*pl;
y:=PY*pt;
//打印DBGrid1中的内容
if (table1.Active=true) and (table1.RecordCount>0) then
begin
//设置一打印机任务标题
printer.Title:='DBGrid内容打印';
with printer do
begin
BeginDoc;
table1.First;
while not table1.Eof do
begin
for i:=0 to DBGrid1.FieldCount-1 do
begin
//设定打印的宽度
if (x+DBGrid1.Columns.Items[i].Width*ps)
<=(PageWidth-ps*pr) then
begin
//画出表格线
Printer.Canvas.Rectangle(x,y,x+dbgrid1.Columns.Items[i].Width*ps,y+lh);
//如果为第一行时,打印DBGrid1确的标题
if y=PY*pt then
Canvas.TextOut(x+rowb,y+colb,
DBGrid1.Columns[i].Title.Caption)
else
//其它行,打印每行的内容
Printer.Canvas.TextOut(x+rowb,y+colb,
DBGrid1.Fields[i].asString);
end;
//下一列的横坐标,下一列的纵坐标不变
x:=x+dbgrid1.Columns.Items[i].Width*ps;
end;
//打印下一记录
if not (y=py*pt) then
table1.next;
x:=px*pl; //打印下一行时,横坐标回到行首
y:=y+lh;
//打印满一页时,新建一页
if (y+lh)>(PageHeight-py*pb) then
begin
NewPage;
x:=px*pl;
y:=py*pt;
end;
end;
EndDoc;
end;
table1.First;//回到DBGrid1首行
Application.MessageBox('已打印完成','打印',32);
end;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
//获得打印可选字体,并设置初始字体为DBGrid1的字体,字号
combobox2.Items:=printer.Fonts;
combobox2.Text:=dbgrid1.Font.Name;
spinedit5.value:=dbgrid1.Font.Size;
table1.DatabaseName:=ExtractFilePath(Application.ExeName);
table1.open;
end;
end.
从这源码就可以学到如何只打印dbgrid中的数据了

xinyu7737 2003-09-06
  • 打赏
  • 举报
回复
好象不能单独打印DbGrid,除非把它单独放在一表单中。
jiaxianglei 2003-08-26
  • 打赏
  • 举报
回复
我想应该可以实现的呀,但就是不知道怎么实现。各位大虾帮帮忙呀。
function CapitalizeMoney(aMoney: Double; aIsUnit: Boolean; var oMoneyStr: string): string; //小写人民币转换成大写人民币 function IsExistForm(aFormObject: string): Boolean; //查询窗体是否存在 function CalcAmountOfField(aQry: TQuery; aFieldName: string; var oAmountStr: string): string; //计算合计 function ChangeChineseToPY(aChinese: string; aIsCapital: Boolean; var oPYStr: string): string; //汉字转换成拼音码 function TrimTextOfEdt(aFormObject: TForm): string; //清除Form上EDIT的前后空格 function ClearTextOfEdt(aFormObject: TForm): string; //清除Form上EDIT的内容 function ClearCaptionOfPnl(aFormObject: TForm): string; //清除Form上Panel的内容 function SetReadOnlyOfEdt(aFormObject: TForm; aIs: Boolean): string; // 使FORM上EDIT不可写 function FullItemOfCB(aQry: TQuery; aFieldName: string; aCBObject: TComBoBox): string; //填充ComBoBox的内容 function FullItemOfLB(aQry: TQuery; aFieldName: string; aLBObject: TListBox): string; //填充ListBox的内容 function FilterQry(aQry: TQuery; aFieldName: string; aFilterValue: string): string; //对单个字段的过滤 function FilterPiPeiMa(aQry: TQuery; aFilterValue: string): string; //对拼音码,五笔码,自定码的组合过滤 function FilterQryByDBG(aQry: TQuery; aDBGrid: TDBGrid; aFilterValue: string): string; //对DBGrid的指定列的过滤 function LocateQryByDBG(aQry: TQuery; aDBGrid: TDBGrid; aLocateValue: string): string; //对DBGrid的指定列的定位 function SetLocalTimeForServerTime(aQry: TQuery): string; //设置本机间为服务器间 function SwapQueryRecord(aIsUp: Boolean; var oQry: TQuery): string; //交换记录 // function GetPaperSize(aPaperSizeStr: string): TQRPaperSize; function GetQRBandType(aQRBandTypeStr: string): TQRBandType; function GetAlignment(aAlignmentStr: string): TAlignment; function GetBoolean(aBooleanStr: string): Boolean; function GetColor(aColorStr: string): TColor; function GetFontStyle(aFontStyleStr: string): TFontStyles; function GetDataType(aDataTypeStr: string): TQRSysDataType; function GetPageOptions(aOptionStr: string): TQuickReportOptions; function GetPageOrientation(aOrientationStr: string): TPrinterOrientation;

5,930

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧