我以前试过将一整个控件,如ListView存成位图,然后打印位图,效果不错的.
下面是简单源码,另外要自己控制换页function getControlbitmap(lv: TWinControl): TBitmap;
var
Ofs: Integer;
begin
Result := TBitmap.Create;
try
Result.Width := ClientWidth;
Result.Height := ClientHeight;
Result.Canvas.Brush := Brush;
Result.Canvas.FillRect(ClientRect);
Result.Canvas.Lock;
try
if GetWindowLong(Handle, GWL_STYLE) and WS_BORDER <> 0 then
Ofs := -1 // Don't draw form border
else
Ofs := 0; // There is no border
lv.PaintTo(Result.Canvas.Handle, Ofs, Ofs);
finally
Result.Canvas.Unlock;
end;
except
Result.Free;
raise;
end;
end;procedure printControl(lv: TWinControl);
var
Ofs: Integer;
//ptBitmap: TBitmap;
FormImage: TBitmap;
Info: PBitmapInfo;
InfoSize: DWORD;
Image: Pointer;
ImageSize: DWORD;
Bits: HBITMAP;
DIBWidth, DIBHeight: Longint;
PrintWidth, PrintHeight: Longint;
pdlg: TPrintDialog;
begin
Printer.Orientation := poLandscape;
pdlg := TPrintDialog.Create(self);
if not pdlg.Execute then
exit; FormImage := TBitmap.Create;
try
FormImage.Width := lv.ClientWidth;
FormImage.Height := lv.ClientHeight;
FormImage.Canvas.Brush := Brush;
FormImage.Canvas.FillRect(lv.ClientRect);
FormImage.Canvas.Lock;
try
// if GetWindowLong(Handle, GWL_STYLE) and WS_BORDER <> 0 then
Ofs := -1; // Don't draw form border
// else
// Ofs := 0; // There is no border
lv.PaintTo(FormImage.Canvas.Handle, Ofs, Ofs);
finally
FormImage.Canvas.Unlock;
end;
except
FormImage.Free;
Application.MessageBox('Print list view error', nil, MB_OK);
exit;
end;
Printer.BeginDoc;
try
FormImage := getControlbitmap(lv);
Canvas.Lock;
try
{ Paint bitmap to the printer }
with Printer, Canvas do
begin
Bits := FormImage.Handle;
GetDIBSizes(Bits, InfoSize, ImageSize);
Info := AllocMem(InfoSize);
try
Image := AllocMem(ImageSize);
try
GetDIB(Bits, 0, Info^, Image^);
with Info^.bmiHeader do
begin
DIBWidth := biWidth + 50;
DIBHeight := biHeight + 50;
end;
case PrintScale of
poProportional:
begin
PrintWidth := MulDiv(DIBWidth, GetDeviceCaps(Handle,
LOGPIXELSX), PixelsPerInch);
PrintHeight := MulDiv(DIBHeight, GetDeviceCaps(Handle,
LOGPIXELSY), PixelsPerInch);
end;
poPrintToFit:
begin
PrintWidth := MulDiv(DIBWidth, PageHeight, DIBHeight);
if PrintWidth < PageWidth then
PrintHeight := PageHeight
else
begin
PrintWidth := PageWidth;
PrintHeight := MulDiv(DIBHeight, PageWidth, DIBWidth);
end;
end;
else
PrintWidth := DIBWidth;
PrintHeight := DIBHeight;
end;
Canvas.TextOut(10, 10, FormatDateTime('yyyy-mm-dd hh:nn', Now()));
StretchDIBits(Canvas.Handle, 50, 200, PrintWidth, PrintHeight, 0, 0,
DIBWidth, DIBHeight, Image, Info^, DIB_RGB_COLORS, SRCCOPY);
finally
FreeMem(Image, ImageSize);
end;
finally
FreeMem(Info, InfoSize);
end;
end;
finally
Canvas.Unlock;
FormImage.Free;
end;
finally
Printer.EndDoc;
end;
end;
下面是简单源码,另外要自己控制换页function getControlbitmap(lv: TWinControl): TBitmap;
var
Ofs: Integer;
begin
Result := TBitmap.Create;
try
Result.Width := ClientWidth;
Result.Height := ClientHeight;
Result.Canvas.Brush := Brush;
Result.Canvas.FillRect(ClientRect);
Result.Canvas.Lock;
try
if GetWindowLong(Handle, GWL_STYLE) and WS_BORDER <> 0 then
Ofs := -1 // Don't draw form border
else
Ofs := 0; // There is no border
lv.PaintTo(Result.Canvas.Handle, Ofs, Ofs);
finally
Result.Canvas.Unlock;
end;
except
Result.Free;
raise;
end;
end;procedure printControl(lv: TWinControl);
var
Ofs: Integer;
//ptBitmap: TBitmap;
FormImage: TBitmap;
Info: PBitmapInfo;
InfoSize: DWORD;
Image: Pointer;
ImageSize: DWORD;
Bits: HBITMAP;
DIBWidth, DIBHeight: Longint;
PrintWidth, PrintHeight: Longint;
pdlg: TPrintDialog;
begin
Printer.Orientation := poLandscape;
pdlg := TPrintDialog.Create(self);
if not pdlg.Execute then
exit; FormImage := TBitmap.Create;
try
FormImage.Width := lv.ClientWidth;
FormImage.Height := lv.ClientHeight;
FormImage.Canvas.Brush := Brush;
FormImage.Canvas.FillRect(lv.ClientRect);
FormImage.Canvas.Lock;
try
// if GetWindowLong(Handle, GWL_STYLE) and WS_BORDER <> 0 then
Ofs := -1; // Don't draw form border
// else
// Ofs := 0; // There is no border
lv.PaintTo(FormImage.Canvas.Handle, Ofs, Ofs);
finally
FormImage.Canvas.Unlock;
end;
except
FormImage.Free;
Application.MessageBox('Print list view error', nil, MB_OK);
exit;
end;
Printer.BeginDoc;
try
FormImage := getControlbitmap(lv);
Canvas.Lock;
try
{ Paint bitmap to the printer }
with Printer, Canvas do
begin
Bits := FormImage.Handle;
GetDIBSizes(Bits, InfoSize, ImageSize);
Info := AllocMem(InfoSize);
try
Image := AllocMem(ImageSize);
try
GetDIB(Bits, 0, Info^, Image^);
with Info^.bmiHeader do
begin
DIBWidth := biWidth + 50;
DIBHeight := biHeight + 50;
end;
case PrintScale of
poProportional:
begin
PrintWidth := MulDiv(DIBWidth, GetDeviceCaps(Handle,
LOGPIXELSX), PixelsPerInch);
PrintHeight := MulDiv(DIBHeight, GetDeviceCaps(Handle,
LOGPIXELSY), PixelsPerInch);
end;
poPrintToFit:
begin
PrintWidth := MulDiv(DIBWidth, PageHeight, DIBHeight);
if PrintWidth < PageWidth then
PrintHeight := PageHeight
else
begin
PrintWidth := PageWidth;
PrintHeight := MulDiv(DIBHeight, PageWidth, DIBWidth);
end;
end;
else
PrintWidth := DIBWidth;
PrintHeight := DIBHeight;
end;
Canvas.TextOut(10, 10, FormatDateTime('yyyy-mm-dd hh:nn', Now()));
StretchDIBits(Canvas.Handle, 50, 200, PrintWidth, PrintHeight, 0, 0,
DIBWidth, DIBHeight, Image, Info^, DIB_RGB_COLORS, SRCCOPY);
finally
FreeMem(Image, ImageSize);
end;
finally
FreeMem(Info, InfoSize);
end;
end;
finally
Canvas.Unlock;
FormImage.Free;
end;
finally
Printer.EndDoc;
end;
end;
TO:Crob(我干嘛这么帅)
谢谢谢谢!我也曾想过是否可以这样,但自已很快又否定了,以为这是不可能的。多谢高手。TO:liang_z(千山一刀之忍者神龟)
请勿介意。我真是这样,我知道我痛,很多地方痛,所以只要是药我都通吃。名方偏方只要是药方,高手您就请随便给我开一剂都行,反正死不了人。
关于报表,当然最起码的控件的使用方法就不必让高手们费时了,大家有什么好的方法,还望赐教,随便说说都行。
TO:ingener(祝福)
对不起。
请保重!
祝福您(如果有办法,我真想扣您几分。