代码如下:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,Forms, Dialogs, DB, ADODB, StdCtrls, ExtCtrls,Grids,StrUtils,zyPrint, DBGrids; type TForm1 = class(TForm) sas: TPanel; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; ADOConnection1: TADOConnection; Button5: TButton; ADODataSet1: TADODataSet; Grid: TStringGrid; Memo1: TMemo; Grid1: TDBGrid; DataSource1: TDataSource; Button6: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); private { Private declarations } procedure myPrint(pState:DllPrintState); //只需调用这个函数就可以打印 public { Public declarations } end;var Form1: TForm1;implementation {$R *.dfm} procedure TForm1.myPrint(pState:DllPrintState); begin CallDllPrint('zyPrintcrt.dll',Application,nil,Grid,nil,nil,nil,nil,nil,nil,Self.Caption,pState); //CallDllPrint函数请看zyprint.pas里的说明 end;procedure TForm1.FormCreate(Sender: TObject); var i,j:Integer; begin //表一 ADODataSet1.CommandText:='select * from xueshenglist'; ADODataSet1.Connection:=ADOConnection1; ADODataSet1.Open; for i:=0 to ADODataSet1.FieldCount - 1 do Grid.Cells[i,0]:=ADODataSet1.FieldDefs[i].Name; Grid.RowCount:=ADODataSet1.RecordCount+1; Grid.ColCount:=ADODataSet1.FieldCount; Grid.Hint:='xueshenglist表'; //这个必须写下,为了区分数据表来源 for i:=1 to ADODataSet1.RecordCount do begin for j:=0 to ADODataSet1.FieldCount - 1 do Grid.Cells[j,i]:=ADODataSet1.Fields[j].AsString; ADODataSet1.Next; end; end; procedure TForm1.Button1Click(Sender: TObject); begin myPrint(hpsDesign); end;procedure TForm1.Button2Click(Sender: TObject); begin myPrint(hpsPreview); end;procedure TForm1.Button3Click(Sender: TObject); begin myPrint(hpsPrint); end;procedure TForm1.Button4Click(Sender: TObject); begin myPrint(hpsPrintNoDialog); end;procedure TForm1.Button5Click(Sender: TObject); begin close; end;end.
zyprint.pas代码: unit zyPrint;interfaceuses Forms, Windows, Adodb, Grids, DB, SysUtils, StrUtils;type T_SetInit=procedure(App:TApplication;AModuleName,AWay:pchar;ADbType:Integer;ACn:TADOConnection); stdcall; T_AddGrid=procedure(AName:PChar;ARowCount:Integer;AColCount:Integer); stdcall; T_SetGridCells=procedure(AGridName:PChar;ACol:Integer;ARow:Integer;AValue:PChar); stdcall; T_Design=procedure(); stdcall; T_Print=procedure(ShowDialog:Boolean); stdcall; DllPrintState=(hpsDesign,hpsPreview,hpsPrint,hpsPrintNoDialog); //打印方式(设计,预览,打印,直接打印) procedure CallDllPrint(DllPath:string;App:TApplication;AdoConn:TAdoConnection; Grid1,Grid2,Grid3,Grid4,Grid5,Grid6,Grid7:TStringGrid;Module:string;PrintState:DllPrintState;defaultWay:string=''); //DllPath:DLL(zyPrintcrt.dll)所在的目径 //AdoConn:TAdoConnection为nil时打印方案保存在本地数据库以(Access)为格式 //Module:模块名 //PrintState:分预览,打印,设计,直接打印 //Module:为模块名称. //Way:调用的方案名implementationprocedure CallDllPrint(DllPath:string;App:TApplication;AdoConn:TAdoConnection;Grid1,Grid2,Grid3,Grid4,Grid5,Grid6,Grid7:TStringGrid;Module:string;PrintState:DllPrintState;defaultWay:string=''); var DllHandle: THandle; ProcAddr: FarProc; SetInit: T_SetInit; AddGrid: T_AddGrid; SetGridCells: T_SetGridCells; Design: T_Design; Print: T_Print; i,j:Integer; begin DllHandle := GetModuleHandle(pchar(DllPath)); try if DllHandle=0 then begin DllHandle :=LoadLibrary(pchar(DllPath)); end; if DllHandle<>0 then begin ProcAddr := GetProcAddress(DllHandle, 'SetInit'); if ProcAddr <> nil then begin SetInit := ProcAddr; SetInit(App,PChar(Module),PChar(defaultWay),1,AdoConn); end else MessageBox(Application.Handle,pchar('动态链接库zyPrintcrt.dll内部错误!'),'提示',MB_OK+MB_ICONINFORMATION); ProcAddr := GetProcAddress(DllHandle, 'AddGrid'); if ProcAddr <> nil then begin AddGrid := ProcAddr; if Grid1<>nil then AddGrid(PChar(Grid1.hint),Grid1.RowCount,Grid1.ColCount); if Grid2<>nil then AddGrid(PChar(Grid2.hint),Grid2.RowCount,Grid2.ColCount); //if Grid3<>nil then AddGrid(PChar(Grid3.hint),Grid3.RowCount,Grid3.ColCount); //if Grid4<>nil then AddGrid(PChar(Grid4.hint),Grid4.RowCount,Grid4.ColCount); //if Grid5<>nil then AddGrid(PChar(Grid5.hint),Grid5.RowCount,Grid5.ColCount); //if Grid6<>nil then AddGrid(PChar(Grid6.hint),Grid6.RowCount,Grid6.ColCount); //if Grid7<>nil then AddGrid(PChar(Grid7.hint),Grid7.RowCount,Grid7.ColCount); //这里只写了7个表,可以再增加,增加参数Grid8即可 //if Grid8<>nil then AddGrid(PChar(Grid8.hint),Grid8.RowCount,Grid8.ColCount); end else MessageBox(Application.Handle,pchar('动态链接库zyPrintcrt.dll内部错误!'),'提示',MB_OK+MB_ICONINFORMATION); ProcAddr := GetProcAddress(DllHandle, 'SetGridCells'); if ProcAddr <> nil then begin SetGridCells := ProcAddr; if Grid1<>nil then begin for i:=0 to Grid1.RowCount-1 do for j:=0 to Grid1.ColCount-1 do SetGridCells(PChar(Grid1.hint),j,i,PChar(Grid1.Cells[j,i])); end; if Grid2<>nil then begin for i:=0 to Grid2.RowCount-1 do for j:=0 to Grid2.ColCount-1 do SetGridCells(PChar(Grid2.hint),j,i,PChar(Grid2.Cells[j,i])); end; if Grid3<>nil then begin for i:=0 to Grid3.RowCount-1 do for j:=0 to Grid3.ColCount-1 do SetGridCells(PChar(Grid3.hint),j,i,PChar(Grid3.Cells[j,i])); end; if Grid4<>nil then begin for i:=0 to Grid4.RowCount-1 do for j:=0 to Grid4.ColCount-1 do SetGridCells(PChar(Grid4.hint),j,i,PChar(Grid4.Cells[j,i])); end; if Grid5<>nil then begin for i:=0 to Grid5.RowCount-1 do for j:=0 to Grid5.ColCount-1 do SetGridCells(PChar(Grid5.hint),j,i,PChar(Grid5.Cells[j,i])); end; if Grid6<>nil then begin for i:=0 to Grid6.RowCount-1 do for j:=0 to Grid6.ColCount-1 do SetGridCells(PChar(Grid6.hint),j,i,PChar(Grid6.Cells[j,i])); end; if Grid7<>nil then begin for i:=0 to Grid7.RowCount-1 do for j:=0 to Grid7.ColCount-1 do SetGridCells(PChar(Grid7.hint),j,i,PChar(Grid7.Cells[j,i])); end; end else MessageBox(Application.Handle,pchar('动态链接库zyPrintcrt.dll内部错误!'),'提示',MB_OK+MB_ICONINFORMATION); case PrintState of hpsDesign: ProcAddr := GetProcAddress(DllHandle, 'ShowDesign'); hpsPreview: ProcAddr := GetProcAddress(DllHandle, 'ShowPreview'); hpsPrint: ProcAddr := GetProcAddress(DllHandle, 'ShowPrint'); hpsPrintNoDialog:ProcAddr := GetProcAddress(DllHandle, 'ShowPrint'); end; if ProcAddr <> nil then begin case PrintState of hpsDesign: begin Design := ProcAddr; Design(); end; hpsPreview: begin Design := ProcAddr; Design(); end; hpsPrint: begin Print := ProcAddr; Print(True); end; hpsPrintNoDialog:begin Print := ProcAddr; Print(False); end; end; end else MessageBox(Application.Handle,pchar('动态链接库zyPrintcrt.dll内部错误!'),'提示',MB_OK+MB_ICONINFORMATION); end else MessageBox(Application.Handle,pchar('装载动态链接库zyPrintcrt.dll时错误!'),'提示',MB_OK+MB_ICONINFORMATION); finally FreeLibrary(DllHandle); end; end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,Forms,
Dialogs, DB, ADODB, StdCtrls, ExtCtrls,Grids,StrUtils,zyPrint, DBGrids;
type
TForm1 = class(TForm)
sas: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
ADOConnection1: TADOConnection;
Button5: TButton;
ADODataSet1: TADODataSet;
Grid: TStringGrid;
Memo1: TMemo;
Grid1: TDBGrid;
DataSource1: TDataSource;
Button6: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
procedure myPrint(pState:DllPrintState);
//只需调用这个函数就可以打印
public
{ Public declarations }
end;var
Form1: TForm1;implementation
{$R *.dfm}
procedure TForm1.myPrint(pState:DllPrintState);
begin
CallDllPrint('zyPrintcrt.dll',Application,nil,Grid,nil,nil,nil,nil,nil,nil,Self.Caption,pState);
//CallDllPrint函数请看zyprint.pas里的说明
end;procedure TForm1.FormCreate(Sender: TObject);
var
i,j:Integer;
begin
//表一
ADODataSet1.CommandText:='select * from xueshenglist';
ADODataSet1.Connection:=ADOConnection1;
ADODataSet1.Open;
for i:=0 to ADODataSet1.FieldCount - 1 do
Grid.Cells[i,0]:=ADODataSet1.FieldDefs[i].Name;
Grid.RowCount:=ADODataSet1.RecordCount+1;
Grid.ColCount:=ADODataSet1.FieldCount;
Grid.Hint:='xueshenglist表'; //这个必须写下,为了区分数据表来源
for i:=1 to ADODataSet1.RecordCount do
begin
for j:=0 to ADODataSet1.FieldCount - 1 do
Grid.Cells[j,i]:=ADODataSet1.Fields[j].AsString;
ADODataSet1.Next;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
myPrint(hpsDesign);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
myPrint(hpsPreview);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
myPrint(hpsPrint);
end;procedure TForm1.Button4Click(Sender: TObject);
begin
myPrint(hpsPrintNoDialog);
end;procedure TForm1.Button5Click(Sender: TObject);
begin
close;
end;end.
unit zyPrint;interfaceuses
Forms, Windows, Adodb, Grids, DB, SysUtils, StrUtils;type
T_SetInit=procedure(App:TApplication;AModuleName,AWay:pchar;ADbType:Integer;ACn:TADOConnection); stdcall;
T_AddGrid=procedure(AName:PChar;ARowCount:Integer;AColCount:Integer); stdcall;
T_SetGridCells=procedure(AGridName:PChar;ACol:Integer;ARow:Integer;AValue:PChar); stdcall;
T_Design=procedure(); stdcall;
T_Print=procedure(ShowDialog:Boolean); stdcall;
DllPrintState=(hpsDesign,hpsPreview,hpsPrint,hpsPrintNoDialog); //打印方式(设计,预览,打印,直接打印)
procedure CallDllPrint(DllPath:string;App:TApplication;AdoConn:TAdoConnection;
Grid1,Grid2,Grid3,Grid4,Grid5,Grid6,Grid7:TStringGrid;Module:string;PrintState:DllPrintState;defaultWay:string='');
//DllPath:DLL(zyPrintcrt.dll)所在的目径
//AdoConn:TAdoConnection为nil时打印方案保存在本地数据库以(Access)为格式
//Module:模块名
//PrintState:分预览,打印,设计,直接打印
//Module:为模块名称.
//Way:调用的方案名implementationprocedure CallDllPrint(DllPath:string;App:TApplication;AdoConn:TAdoConnection;Grid1,Grid2,Grid3,Grid4,Grid5,Grid6,Grid7:TStringGrid;Module:string;PrintState:DllPrintState;defaultWay:string='');
var
DllHandle: THandle;
ProcAddr: FarProc;
SetInit: T_SetInit;
AddGrid: T_AddGrid;
SetGridCells: T_SetGridCells;
Design: T_Design;
Print: T_Print;
i,j:Integer;
begin
DllHandle := GetModuleHandle(pchar(DllPath));
try
if DllHandle=0 then
begin
DllHandle :=LoadLibrary(pchar(DllPath));
end;
if DllHandle<>0 then
begin
ProcAddr := GetProcAddress(DllHandle, 'SetInit');
if ProcAddr <> nil then
begin
SetInit := ProcAddr;
SetInit(App,PChar(Module),PChar(defaultWay),1,AdoConn);
end
else
MessageBox(Application.Handle,pchar('动态链接库zyPrintcrt.dll内部错误!'),'提示',MB_OK+MB_ICONINFORMATION);
ProcAddr := GetProcAddress(DllHandle, 'AddGrid');
if ProcAddr <> nil then
begin
AddGrid := ProcAddr;
if Grid1<>nil then AddGrid(PChar(Grid1.hint),Grid1.RowCount,Grid1.ColCount);
if Grid2<>nil then AddGrid(PChar(Grid2.hint),Grid2.RowCount,Grid2.ColCount);
//if Grid3<>nil then AddGrid(PChar(Grid3.hint),Grid3.RowCount,Grid3.ColCount);
//if Grid4<>nil then AddGrid(PChar(Grid4.hint),Grid4.RowCount,Grid4.ColCount);
//if Grid5<>nil then AddGrid(PChar(Grid5.hint),Grid5.RowCount,Grid5.ColCount);
//if Grid6<>nil then AddGrid(PChar(Grid6.hint),Grid6.RowCount,Grid6.ColCount);
//if Grid7<>nil then AddGrid(PChar(Grid7.hint),Grid7.RowCount,Grid7.ColCount);
//这里只写了7个表,可以再增加,增加参数Grid8即可
//if Grid8<>nil then AddGrid(PChar(Grid8.hint),Grid8.RowCount,Grid8.ColCount);
end
else
MessageBox(Application.Handle,pchar('动态链接库zyPrintcrt.dll内部错误!'),'提示',MB_OK+MB_ICONINFORMATION);
ProcAddr := GetProcAddress(DllHandle, 'SetGridCells');
if ProcAddr <> nil then
begin
SetGridCells := ProcAddr;
if Grid1<>nil then
begin
for i:=0 to Grid1.RowCount-1 do
for j:=0 to Grid1.ColCount-1 do
SetGridCells(PChar(Grid1.hint),j,i,PChar(Grid1.Cells[j,i]));
end;
if Grid2<>nil then
begin
for i:=0 to Grid2.RowCount-1 do
for j:=0 to Grid2.ColCount-1 do
SetGridCells(PChar(Grid2.hint),j,i,PChar(Grid2.Cells[j,i]));
end;
if Grid3<>nil then
begin
for i:=0 to Grid3.RowCount-1 do
for j:=0 to Grid3.ColCount-1 do
SetGridCells(PChar(Grid3.hint),j,i,PChar(Grid3.Cells[j,i]));
end;
if Grid4<>nil then
begin
for i:=0 to Grid4.RowCount-1 do
for j:=0 to Grid4.ColCount-1 do
SetGridCells(PChar(Grid4.hint),j,i,PChar(Grid4.Cells[j,i]));
end;
if Grid5<>nil then
begin
for i:=0 to Grid5.RowCount-1 do
for j:=0 to Grid5.ColCount-1 do
SetGridCells(PChar(Grid5.hint),j,i,PChar(Grid5.Cells[j,i]));
end;
if Grid6<>nil then
begin
for i:=0 to Grid6.RowCount-1 do
for j:=0 to Grid6.ColCount-1 do
SetGridCells(PChar(Grid6.hint),j,i,PChar(Grid6.Cells[j,i]));
end;
if Grid7<>nil then
begin
for i:=0 to Grid7.RowCount-1 do
for j:=0 to Grid7.ColCount-1 do
SetGridCells(PChar(Grid7.hint),j,i,PChar(Grid7.Cells[j,i]));
end;
end
else
MessageBox(Application.Handle,pchar('动态链接库zyPrintcrt.dll内部错误!'),'提示',MB_OK+MB_ICONINFORMATION);
case PrintState of
hpsDesign: ProcAddr := GetProcAddress(DllHandle, 'ShowDesign');
hpsPreview: ProcAddr := GetProcAddress(DllHandle, 'ShowPreview');
hpsPrint: ProcAddr := GetProcAddress(DllHandle, 'ShowPrint');
hpsPrintNoDialog:ProcAddr := GetProcAddress(DllHandle, 'ShowPrint');
end;
if ProcAddr <> nil then
begin
case PrintState of
hpsDesign: begin
Design := ProcAddr;
Design();
end;
hpsPreview: begin
Design := ProcAddr;
Design();
end;
hpsPrint: begin
Print := ProcAddr;
Print(True);
end;
hpsPrintNoDialog:begin
Print := ProcAddr;
Print(False);
end;
end;
end
else
MessageBox(Application.Handle,pchar('动态链接库zyPrintcrt.dll内部错误!'),'提示',MB_OK+MB_ICONINFORMATION);
end
else
MessageBox(Application.Handle,pchar('装载动态链接库zyPrintcrt.dll时错误!'),'提示',MB_OK+MB_ICONINFORMATION);
finally
FreeLibrary(DllHandle);
end;
end;end.