方法1、把你要打印的内容做到报表中 方法2、直接打印窗体(把你不想打印的内容在这之前隐藏掉或者把要打印的移到另外一个空白窗体上)方法2的示例代码如下: 有时我们要打印任意排列的表或往已经印好的登记表上对号入座写上数据时,可以新建一个窗体(假设为Form1),再把Form1的BorderStyle设为bsNone、AutoScroll设为True,接下来再创建一个新窗体(假设为Form2),再建个按钮Button1,编写代码: procedure TForm2.Button1Click(Sender: TObject); begin Form1.Width :=900; Form1.Height :=800; Form1.Print; end; 接下来你在Form1上对应的位置写上数据,运行后按Button1就会一五一十的打印下来了。 //////////////////////////////////// procedure TForm1.Button3Click(Sender: TObject); var dc: HDC; isDcPalDevice : BOOL; MemDc :hdc; MemBitmap : hBitmap; OldMemBitmap : hBitmap; hDibHeader : Thandle; pDibHeader : pointer; hBits : Thandle; pBits : pointer; ScaleX : Double; ScaleY : Double; ppal : PLOGPALETTE; pal : hPalette; Oldpal : hPalette; i : integer;begin {Get the screen dc} dc:=GetDc(0); {Create a compatible dc} MemDc:=CreateCompatibleDc(dc); {create a bitmap} MemBitmap:=CreateCompatibleBitmap(Dc,form1.width,form1.height); {select the bitmap into the dc} OldMemBitmap:=SelectObject(MemDc, MemBitmap); {Lets prepare to try a fixup for broken video drivers} isDcPalDevice:=false; if GetDeviceCaps(dc,RASTERCAPS) and RC_PALETTE=RC_PALETTE then begin GetMem(pPal,sizeof(TLOGPALETTE)+(255*sizeof(TPALETTEENTRY))); FillChar(pPal^,sizeof(TLOGPALETTE)+(255*sizeof(TPALETTEENTRY)),#0); pPal^.palVersion:= 300; pPal^.palNumEntries =GetSystemPaletteEntries(dc,0,256,pPal^.palPalEntry); if pPal^.PalNumEntries<>0 then begin pal:=CreatePalette(pPal^); oldPal:=SelectPalette(MemDc, Pal, false); isDcPalDevice:=true end else FreeMem(pPal,sizeof(TLOGPALETTE)+(255*Sizeof(TPALETTEENTRY))); end; {copy from the screen to the memdc/bitmap} BitBlt(MemDc,0,0,form1.width,form1.height,Dc,form1.left,form1.top,SrcCopy); if isDcPalDevice=true then begin SelectPalette(MemDc,OldPal,false); DeleteObject(Pal); end; {unselect the bitmap} SelectObject(MemDc,OldMemBitmap); {delete the memory dc} DeleteDc(MemDc); {Allocate memory for a DIB structure} hDibHeader:= lobalAlloc(GHND,sizeof(TBITMAPINFO)+(sizeof(TRGBQUAD)*256)); {get a pointer to the alloced memory} pDibHeader:=GlobalLock(hDibHeader); {fill in the dib structure with info on the way we want the DIB} FillChar(pDibHeader^,sizeof(TBITMAPINFO)+ sizeof(TRGBQUAD)*256),#0); PBITMAPINFOHEADER(pDibHeader)^.biSize:=sizeof(TBITMAPINFOHEADER); PBITMAPINFOHEADER(pDibHeader)^.biPlanes:=1; PBITMAPINFOHEADER(pDibHeader)^.biBitCount:=8; PBITMAPINFOHEADER(pDibHeader)^.biWidth:=form1.width; PBITMAPINFOHEADER(pDibHeader)^.biHeight:=form1.height; PBITMAPINFOHEADER(pDibHeader)^.biCompression:=BI_RGB; {find out how much memory for the bits} GetDIBits(dc,MemBitmap,0,form1.height,nil,TBitmapInfo(pDibHeader^),DIB_RGB_COLORS); {Alloc memory for the bits} hBits:=GlobalAlloc(GHND,PBitmapInfoHeader(pDibHeader)^.BiSizeImage); {Get a pointer to the bits} pBits:=GlobalLock(hBits); {Call fn again, but this time give us the bits!} GetDIBits(dc,MemBitmap,0,form1.height,pBits,PBitmapInfo(pDibHeader)^,DIB_RGB_COLORS); {Lets try a fixup for broken video drivers} if isDcPalDevice=true then begin for i:=0 to (pPal^.PalNumEntries-1) do begin PBitmapInfo(pDibHeader)^.bmiColors[i].rgbRed:=pPal^.palPalEntry[i].peRed; PBitmapInfo(pDibHeader)^.bmiColors[i].rgbGreen:=pPal^.palPalEntry[i].peGreen; PBitmapInfo(pDibHeader)^.bmiColors[i].rgbBlue:=pPal^.palPalEntry[i].peBlue; end; FreeMem(pPal,sizeof(TLOGPALETTE)+(255* izeof(TPALETTEENTRY))); end; {Release the screen dc} ReleaseDc(0,dc); {Delete the bitmap} DeleteObject(MemBitmap); {Start print job} Printer.BeginDoc; {Scale print size} if Printer.PageWidth<Printer.PageHeight then begin ScaleX:=Printer.PageWidth; ScaleY:=Form1.Height* Printer.PageWidth/Form1.Width); end else begin ScaleX:=Form1.Width*(Printer.PageHeight/Form1.Height); ScaleY:=Printer.PageHeight; end; {Just incase the printer drver is a palette device} isDcPalDevice:=false; if GetDeviceCaps(Printer.Canvas.Handle,RASTERCAPS) and RC_PALETTE=RC_PALETTE then begin {Create palette from dib} GetMem(pPal,sizeof(TLOGPALETTE)+(255*Sizeof(TPALETTEENTRY))); FillChar(pPal^,sizeof(TLOGPALETTE)+(255*sizeof(TPALETTEENTRY)),#0); pPal^.palVersion:=$300; pPal^.palNumEntries:=256; for i:=0 to (pPal^.PalNumEntries-1) do begin pPal^.palPalEntry[i].peRed:=PBitmapInfo(pDibHeader)^.bmiColors[i].rgbRed; pPal^.palPalEntry[i].peGreen:=PBitmapInfo(pDibHeader)^.bmiColors[i].rgbGreen; pPal^.palPalEntry[i].peBlue:=PBitmapInfo(pDibHeader)^.bmiColors[i].rgbBlue; end; pal:=CreatePalette(pPal^); FreeMem(pPal,sizeof(TLOGPALETTE)+(255*sizeof(TPALETTEENTRY))); oldPal:=SelectPalette(Printer.Canvas.Handle,Pal,false); isDcPalDevice:=true end; {send the bits to the printer} StretchDiBits(Printer.Canvas.Handle, 0, 0, Round(scaleX), Round(scaleY), 0, 0, Form1.Width, Form1.Height, pBits, PBitmapInfo(pDibHeader)^, DIB_RGB_COLORS, SRCCOPY); {Just incase you printer drver is a palette device} if isDcPalDevice = true then begin SelectPalette(Printer.Canvas.Handle, oldPal, false); DeleteObject(Pal); end; {Clean up allocated memory} GlobalUnlock(hBits); GlobalFree(hBits); GlobalUnlock(hDibHeader); GlobalFree(hDibHeader); {End the print job} Printer.EndDoc; end;
方法2、直接打印窗体(把你不想打印的内容在这之前隐藏掉或者把要打印的移到另外一个空白窗体上)方法2的示例代码如下:
有时我们要打印任意排列的表或往已经印好的登记表上对号入座写上数据时,可以新建一个窗体(假设为Form1),再把Form1的BorderStyle设为bsNone、AutoScroll设为True,接下来再创建一个新窗体(假设为Form2),再建个按钮Button1,编写代码:
procedure TForm2.Button1Click(Sender: TObject);
begin
Form1.Width :=900;
Form1.Height :=800;
Form1.Print;
end;
接下来你在Form1上对应的位置写上数据,运行后按Button1就会一五一十的打印下来了。
////////////////////////////////////
procedure TForm1.Button3Click(Sender: TObject);
var
dc: HDC;
isDcPalDevice : BOOL;
MemDc :hdc;
MemBitmap : hBitmap;
OldMemBitmap : hBitmap;
hDibHeader : Thandle;
pDibHeader : pointer;
hBits : Thandle;
pBits : pointer;
ScaleX : Double;
ScaleY : Double;
ppal : PLOGPALETTE;
pal : hPalette;
Oldpal : hPalette;
i : integer;begin
{Get the screen dc}
dc:=GetDc(0); {Create a compatible dc}
MemDc:=CreateCompatibleDc(dc); {create a bitmap}
MemBitmap:=CreateCompatibleBitmap(Dc,form1.width,form1.height); {select the bitmap into the dc}
OldMemBitmap:=SelectObject(MemDc, MemBitmap); {Lets prepare to try a fixup for broken video drivers}
isDcPalDevice:=false;
if GetDeviceCaps(dc,RASTERCAPS) and RC_PALETTE=RC_PALETTE then
begin
GetMem(pPal,sizeof(TLOGPALETTE)+(255*sizeof(TPALETTEENTRY)));
FillChar(pPal^,sizeof(TLOGPALETTE)+(255*sizeof(TPALETTEENTRY)),#0);
pPal^.palVersion:= 300;
pPal^.palNumEntries =GetSystemPaletteEntries(dc,0,256,pPal^.palPalEntry);
if pPal^.PalNumEntries<>0 then
begin
pal:=CreatePalette(pPal^);
oldPal:=SelectPalette(MemDc, Pal, false);
isDcPalDevice:=true
end
else
FreeMem(pPal,sizeof(TLOGPALETTE)+(255*Sizeof(TPALETTEENTRY)));
end; {copy from the screen to the memdc/bitmap}
BitBlt(MemDc,0,0,form1.width,form1.height,Dc,form1.left,form1.top,SrcCopy);
if isDcPalDevice=true then
begin
SelectPalette(MemDc,OldPal,false);
DeleteObject(Pal);
end; {unselect the bitmap}
SelectObject(MemDc,OldMemBitmap); {delete the memory dc}
DeleteDc(MemDc); {Allocate memory for a DIB structure}
hDibHeader:= lobalAlloc(GHND,sizeof(TBITMAPINFO)+(sizeof(TRGBQUAD)*256)); {get a pointer to the alloced memory}
pDibHeader:=GlobalLock(hDibHeader); {fill in the dib structure with info on the way we want the DIB}
FillChar(pDibHeader^,sizeof(TBITMAPINFO)+ sizeof(TRGBQUAD)*256),#0);
PBITMAPINFOHEADER(pDibHeader)^.biSize:=sizeof(TBITMAPINFOHEADER);
PBITMAPINFOHEADER(pDibHeader)^.biPlanes:=1;
PBITMAPINFOHEADER(pDibHeader)^.biBitCount:=8;
PBITMAPINFOHEADER(pDibHeader)^.biWidth:=form1.width;
PBITMAPINFOHEADER(pDibHeader)^.biHeight:=form1.height;
PBITMAPINFOHEADER(pDibHeader)^.biCompression:=BI_RGB; {find out how much memory for the bits}
GetDIBits(dc,MemBitmap,0,form1.height,nil,TBitmapInfo(pDibHeader^),DIB_RGB_COLORS); {Alloc memory for the bits}
hBits:=GlobalAlloc(GHND,PBitmapInfoHeader(pDibHeader)^.BiSizeImage); {Get a pointer to the bits}
pBits:=GlobalLock(hBits); {Call fn again, but this time give us the bits!}
GetDIBits(dc,MemBitmap,0,form1.height,pBits,PBitmapInfo(pDibHeader)^,DIB_RGB_COLORS); {Lets try a fixup for broken video drivers}
if isDcPalDevice=true then
begin
for i:=0 to (pPal^.PalNumEntries-1) do
begin
PBitmapInfo(pDibHeader)^.bmiColors[i].rgbRed:=pPal^.palPalEntry[i].peRed;
PBitmapInfo(pDibHeader)^.bmiColors[i].rgbGreen:=pPal^.palPalEntry[i].peGreen;
PBitmapInfo(pDibHeader)^.bmiColors[i].rgbBlue:=pPal^.palPalEntry[i].peBlue;
end;
FreeMem(pPal,sizeof(TLOGPALETTE)+(255* izeof(TPALETTEENTRY)));
end; {Release the screen dc}
ReleaseDc(0,dc); {Delete the bitmap}
DeleteObject(MemBitmap); {Start print job}
Printer.BeginDoc; {Scale print size}
if Printer.PageWidth<Printer.PageHeight then
begin
ScaleX:=Printer.PageWidth;
ScaleY:=Form1.Height* Printer.PageWidth/Form1.Width);
end
else
begin
ScaleX:=Form1.Width*(Printer.PageHeight/Form1.Height);
ScaleY:=Printer.PageHeight;
end; {Just incase the printer drver is a palette device}
isDcPalDevice:=false;
if GetDeviceCaps(Printer.Canvas.Handle,RASTERCAPS) and RC_PALETTE=RC_PALETTE then
begin
{Create palette from dib}
GetMem(pPal,sizeof(TLOGPALETTE)+(255*Sizeof(TPALETTEENTRY)));
FillChar(pPal^,sizeof(TLOGPALETTE)+(255*sizeof(TPALETTEENTRY)),#0);
pPal^.palVersion:=$300;
pPal^.palNumEntries:=256;
for i:=0 to (pPal^.PalNumEntries-1) do
begin
pPal^.palPalEntry[i].peRed:=PBitmapInfo(pDibHeader)^.bmiColors[i].rgbRed;
pPal^.palPalEntry[i].peGreen:=PBitmapInfo(pDibHeader)^.bmiColors[i].rgbGreen;
pPal^.palPalEntry[i].peBlue:=PBitmapInfo(pDibHeader)^.bmiColors[i].rgbBlue;
end;
pal:=CreatePalette(pPal^);
FreeMem(pPal,sizeof(TLOGPALETTE)+(255*sizeof(TPALETTEENTRY)));
oldPal:=SelectPalette(Printer.Canvas.Handle,Pal,false);
isDcPalDevice:=true
end; {send the bits to the printer}
StretchDiBits(Printer.Canvas.Handle,
0, 0,
Round(scaleX), Round(scaleY),
0, 0,
Form1.Width, Form1.Height,
pBits,
PBitmapInfo(pDibHeader)^,
DIB_RGB_COLORS,
SRCCOPY); {Just incase you printer drver is a palette device}
if isDcPalDevice = true then
begin
SelectPalette(Printer.Canvas.Handle, oldPal, false);
DeleteObject(Pal);
end; {Clean up allocated memory}
GlobalUnlock(hBits);
GlobalFree(hBits);
GlobalUnlock(hDibHeader);
GlobalFree(hDibHeader); {End the print job}
Printer.EndDoc;
end;
我没有打印机 没有办法及时测试,只能写好了,到别处测试下, 所以很费劲
edit可以用label代替
implementation
uses printers;
{$R *.dfm}procedure TForm1.PrintPanel(Panel: TPanel; SLeft, STop, SWidth, SHeight, Left, Top: LongInt;
HScale, ZScale: Real);
var
WJDC: HDC;
WJBitMap: TBitMap;
Info: PBitMapInfo;
InfoSize, ImageSize: DWORD;
Image: Pointer;
begin
Printer.BeginDoc; WJBitMap := TBitMap.Create;
WJBitMap.Width := SWidth;
WJBitMap.Height := SHeight; WJDC := GetDC(Panel.Handle);
BitBlt(WJBitMap.Canvas.Handle, 0, 0, SWidth, SHeight, WJDC, SLeft, STop, SRCCOPY);
ReleaseDC(Panel.Handle, WJDC); GetDIBSizes(WJBitMap.Handle, InfoSize, ImageSize);
GetMem(Info, InfoSize);
GetMem(Image, ImageSize);
GetDIB(WJBitMap.Handle, 0, Info^, Image^); StretchDIBits(Printer.Canvas.Handle, Left, Top, Round((Panel.Width - 2) * HScale),
Round((Panel.Height - 2) * ZScale), 0, 0, Info^.bmiHeader.biWidth,
Info^.bmiHeader.biHeight, Image, Info^, DIB_RGB_COLORS, SRCCOPY);
FreeMem(Image, ImageSize);
FreeMem(Info, InfoSize);
WJBitMap.Free;
Printer.EndDoc;
end;
var
Bitmap: TBitmap;
begin
Bitmap := TBitmap.Create;
Bitmap.Width := Panel1.Width;
Bitmap.Height := Panel1.Height;
try
Panel1.PaintTo(Bitmap.Canvas,0,0);
Bitmap.SaveToFile('g:\jjj.bmp');
//Use Bitmap Do SomeThing..
finally
Bitmap.Free;
end;
end; ///////打印image
//////
Image1.Picture.LoadFromFile('f:\jjj.bmp');
with Image1.Picture.Bitmap do
begin
Printer.BeginDoc;
Printer.Canvas.StretchDraw(Canvas.ClipRect,Image1.Picture.Bitmap);
Printer.EndDoc;
end; 这个是我的解决方法。感觉还可以,方便 和我一样的人
var
Form1: TForm1;implementation
uses printers;
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
var
Bitmap: TBitmap;
begin
Bitmap := TBitmap.Create;
Bitmap.Width := Panel1.Width;
Bitmap.Height := Panel1.Height;
try
Panel1.PaintTo(Bitmap.Canvas,0,0);
Bitmap.SaveToFile('f:\jjj.bmp');
//Use Bitmap Do SomeThing..
finally
Bitmap.Free;
end;
Image1.Picture.LoadFromFile('f:\jjj.bmp');
with Image1.Picture.Bitmap do
begin
Printer.BeginDoc;
Printer.Canvas.StretchDraw(Canvas.ClipRect,Image1.Picture.Bitmap);
Printer.EndDoc;
end;
end;