如何打印窗体上的一个stringgrid的内容 ( 积分: 100 )

  • 主题发起人 主题发起人 goltiger
  • 开始时间 开始时间
G

goltiger

Unregistered / Unconfirmed
GUEST, unregistred user!
我想打印窗体上的stringgrid的内容,请问如何控制,最好有代码
 
我想打印窗体上的stringgrid的内容,请问如何控制,最好有代码
 
dbgrideh可以直接打印
 
也可以用reportmachine吧 我记得它可以打印一个控件的内容!
 
李清峰 (2000-07-26 20:21:00)
我是在c++ Builder 环境下实现的:
定义一 TlistView PList;
将 StringGrid 下的内容按每个网个单元,逐行写入 PList;
调用下面的函数打印:
void TViewParaFrm::RawPrintQEvent()
{
char szTemp[200];
char szDriver[80];
char szDevice[80];
char szPort[80];
int hDevMode;
SYSTEMTIME st;
GetLocalTime(&st);
//设置打印标题
sprintf(szTemp,"
%s打印 ( %d年%02d月%02d日 %02d:%02d:%02d )",
ComboBox1->Items->Strings[ComboBox1->ItemIndex].c_str(),st.wYear,st.wMonth,st.wDay,
st.wHour,st.wMinute,st.wSecond);
RawPrinter1->Title = +"打印";
TPrinter *Printer1;
Printer1 = Printer();
hDevMode = 0;
Printer1->GetPrinter(szDevice,szDriver,szPort,hDevMode);
RawPrinter1->DriverName = AnsiString(szDriver);
RawPrinter1->PortName = AnsiString(szPort);
RawPrinter1->DeviceName = AnsiString(szDevice);
RawPrinter1->startraw();
RawPrinter1->write(szTemp);
RawPrinter1->writeln();
//逐行打印
for(int i=0;i<pList->Count;i++)
{
sprintf(szTemp,"
%s",pList->Strings.c_str());
RawPrinter1->write(szTemp);
RawPrinter1->writeln();
}
RawPrinter1->
endraw();
}
C++ Builder 与 Delphi 用法相似, 只是语法不同

 
试试 Grid++Report, 有专门的例子说明此问题。主页:www.GridppReport.com
 
c++ Builder环境:
void __fastcall TLL::PrintStringGrid(TStringGrid *SG, char *topic)
{
//TODO: Add your source code here
const leftblank=1;
const rightblank=1;
const topblank=1;
const bottomblank=1;
CPrintor example_p;
AnsiString s;
int x,y,i;
TPrinter *Prntr=Printer();
int width[100];
example_p.x=GetDeviceCaps(Prntr->Handle,LOGPIXELSX)/2.54;
example_p.y=GetDeviceCaps(Prntr->Handle,LOGPIXELSY)/2.54;
example_p.scale=GetDeviceCaps(Prntr->Handle,LOGPIXELSX)/Screen->PixelsPerInch;
s="打印";
example_p.step=Prntr->Canvas->TextHeight(s)+40;
Prntr->Orientation=poLandscape;
//横向
/***** 取各列的最大宽度 *****/
for(int i=0;i<SG->ColCount;i++)
{
width=SG->Canvas->TextWidth(SG->Cells[0]);
for(int j=1;j<SG->RowCount;j++)
{
if( SG->Canvas->TextWidth(SG->Cells[j])>=width )
width=SG->Canvas->TextWidth(SG->Cells[j]);
}
if( i==0 )
width+=10;
else
width+=20;
}
/***** Over ******/
Prntr->begin
Doc();
x=example_p.x*leftblank;
y=example_p.y*topblank;
if( topic!=NULL )
{
Prntr->Canvas->Font->Name=&quot;隶书&quot;;
Prntr->Canvas->Font->Size=16;
Prntr->Canvas->TextOut(x+8,y+8,topic);
y=y+example_p.step*2;
}
Prntr->Canvas->Font->Name=&quot;宋体&quot;;
Prntr->Canvas->Font->Size=10;
for(int i=1;i<SG->RowCount;i++)
{
for(int j=0;j<SG->ColCount;j++)
{
if( SG->ColWidths[j]<0 )
continue;
if( (x+width[j]*example_p.scale)<=
(Prntr->PageWidth-example_p.x*rightblank) )
{
Prntr->Canvas->Rectangle(x,y,x+width[j]*example_p.scale,y+example_p.step);
}
if( y==example_p.y*topblank || (i==1 &amp;&amp;
j==0)) //标题
{
for(int m=0;m<SG->ColCount;m++)
{
if( SG->ColWidths[m]<0 )
continue;
if( (x+width[m]*example_p.scale)<=
(Prntr->PageWidth-example_p.x*rightblank) )
{
Prntr->Canvas->Rectangle(x,y,x+width[m]*example_p.scale,y+example_p.step);
}
Prntr->Canvas->TextOut(x+8,y+8,SG->Cells[m][0]);
x=x+width[m]*example_p.scale;
}
x=example_p.x*leftblank;
y=y+example_p.step;

if( (x+width[j]*example_p.scale)<=
(Prntr->PageWidth-example_p.x*rightblank) )
{
Prntr->Canvas->Rectangle(x,y,x+width[j]*example_p.scale,y+example_p.step);
}
Prntr->Canvas->TextOut(x+8,y+8,SG->Cells[j]);
}
else
{
Prntr->Canvas->TextOut(x+8,y+8,SG->Cells[j]);
}
x=x+width[j]*example_p.scale;
}
x=example_p.x*leftblank;
y=y+example_p.step;
if( (y+example_p.step)>(Prntr->PageHeight-example_p.y*bottomblank) )
{
Prntr->NewPage();
y=example_p.y*topblank;
}
}
Prntr->EndDoc();
}
 
procedure TForm1.Button1Click(Sender: TObject);
begin
mi:=0;
form2.QuickRep1.Preview;
end;
var myqrl:array[1..100] of Tqrlabel;
{$R *.dfm}
procedure TForm2.FormCreate(Sender: TObject);
var s:integer;
begin
for s:=1 to 100do
begin
Myqrl:=TQrlabel.Create(qrband2);
with Myqrldo
begin
Parent:=qrband2;
Left:=(s-1)*50+8;
Top:=4;
alignment:=tacenter;
font.Size:=10;
font.Name:='宋体';
enabled:=false;
Caption:='';
end;
end;
end;

procedure TForm2.QuickRep1NeedData(Sender: TObject;
var MoreData: Boolean);
var s,j:byte;
begin
s:=form1.StringGrid1.ColCount;
for j:=1 to sdo
begin
myqrl[j].Enabled:=true;
myqrl[j].Caption:=form1.StringGrid1.Cells[j-1,mi];
end;
inc(mi);
MoreData:=(mi<form1.StringGrid1.RowCount+1);
end;
 
uses
printers;
//StringGrid Inhalt ausdrucken
procedure PrintStringGrid(Grid: TStringGrid;
Title: string;
Orientation: TPrinterOrientation);
var
P, I, J, YPos, XPos, HorzSize, VertSize: Integer;
AnzSeiten, Seite, Zeilen, HeaderSize, FooterSize, ZeilenSize, FontHeight: Integer;
mmx, mmy: Extended;
Footer: string;
begin
//Kopfzeile, Fu&amp;szlig;zeile, Zeilenabstand, Schriftgr&amp;ouml;&amp;szlig;e festlegen
HeaderSize := 100;
FooterSize := 200;
ZeilenSize := 36;
FontHeight := 36;
//Printer initializieren
Printer.Orientation := Orientation;
Printer.Title := Title;
Printer.begin
Doc;
//Druck auf mm einstellen
mmx := GetDeviceCaps(Printer.Canvas.Handle, PHYSICALWIDTH) /
GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSX) * 25.4;
mmy := GetDeviceCaps(Printer.Canvas.Handle, PHYSICALHEIGHT) /
GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY) * 25.4;
VertSize := Trunc(mmy) * 10;
HorzSize := Trunc(mmx) * 10;
SetMapMode(Printer.Canvas.Handle, MM_LOMETRIC);
//Zeilenanzahl festlegen
Zeilen := (VertSize - HeaderSize - FooterSize) div ZeilenSize;
//Seitenanzahl ermitteln
if Grid.RowCount mod Zeilen <> 0 then
AnzSeiten := Grid.RowCount div Zeilen + 1
else
AnzSeiten := Grid.RowCount div Zeilen;
Seite := 1;
//Grid Drucken
for P := 1 to AnzSeitendo
begin
//Kopfzeile
Printer.Canvas.Font.Height := 48;
Printer.Canvas.TextOut((HorzSize div 2 - (Printer.Canvas.TextWidth(Title) div 2)),
- 20,Title);
Printer.Canvas.Pen.Width := 5;
Printer.Canvas.MoveTo(0, - HeaderSize);
Printer.Canvas.LineTo(HorzSize, - HeaderSize);
//Fu&amp;szlig;zeile
Printer.Canvas.MoveTo(0, - VertSize + FooterSize);
Printer.Canvas.LineTo(HorzSize, - VertSize + FooterSize);
Printer.Canvas.Font.Height := 36;
Footer := 'Seite: ' + IntToStr(Seite) + ' von ' + IntToStr(AnzSeiten);
Printer.Canvas.TextOut((HorzSize div 2 - (Printer.Canvas.TextWidth(Footer) div 2)),
- VertSize + 150,Footer);
//Zeilen drucken
Printer.Canvas.Font.Height := FontHeight;
YPos := HeaderSize + 10;
for I := 1 to Zeilendo
begin
if Grid.RowCount >= I + (Seite - 1) * Zeilen then
begin
XPos := 0;
for J := 0 to Grid.ColCount - 1do
begin
Printer.Canvas.TextOut(XPos, - YPos,
Grid.Cells[J, I + (Seite - 1) * Zeilen - 1]);
XPos := XPos + Grid.ColWidths[J] * 3;
end;
YPos := YPos + ZeilenSize;
end;
end;
//Seite hinzufügen
Inc(Seite);
if Seite <= AnzSeiten then
Printer.NewPage;
end;
Printer.EndDoc;
end;

//Example
procedure TForm1.Button1Click(Sender: TObject);
begin
PrintStringGrid(StringGrid1, 'StringGrid Print Landscape', poLandscape);
PrintStringGrid(StringGrid1, 'StringGrid Print Portrait', poPortrait);
end;
 
打印其实和打印预览是一回事,不过是将打印预览的Canvas变成了打印机的Canvas,
你在打印机上的Canvas画出你要的表格不就得了.
 
谢谢前辈的指点我也学了不少!!!
我自己试试看!!!!
 
后退
顶部