自定义打印窗口(50分)

  • 主题发起人 主题发起人 小苹果
  • 开始时间 开始时间

小苹果

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好、我想自定义打印窗口。在网上有例子,可是都是一样的,并且我编译的时候
根本就是编译不了。
大家有编译好的例子吗??我想认真看一下。。。。就50分吧。。
ok ,,,不够,我可以再加点。。
applexiaohong@163.net
applexiaohong@163.com
 
unit u_prev;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
qrprntr, ComCtrls, ToolWin, ExtCtrls, Menus, ImgList, StdCtrls, comobj, Grids,
Db, DBTables, QuickRpt;
type
Tf_prev = class(TForm)
QRPreview1: TQRPreview;
CoolBar1: TCoolBar;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
ToolButton5: TToolButton;
ToolButton6: TToolButton;
ToolButton7: TToolButton;
ToolButton8: TToolButton;
ToolButton10: TToolButton;
ImageList1: TImageList;
ToolButton4: TToolButton;
ToolButton9: TToolButton;
ToolButton11: TToolButton;
ToolButton12: TToolButton;
ToolButton13: TToolButton;
StatusBar1: TStatusBar;
PopupMenu1: TPopupMenu;
N751: TMenuItem;
N501: TMenuItem;
N251: TMenuItem;
PopupMenu2: TPopupMenu;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
MenuItem3: TMenuItem;
PrintersetupDialog1: TPrinterSetupDialog;
PrintDialog1: TPrintDialog;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N9: TMenuItem;
N10: TMenuItem;
N11: TMenuItem;
N12: TMenuItem;
N13: TMenuItem;
N14: TMenuItem;
N1001: TMenuItem;
N1101: TMenuItem;
N1251: TMenuItem;
N7551: TMenuItem;
N502: TMenuItem;
N252: TMenuItem;
ToolButton14: TToolButton;
SaveDialog1: TSaveDialog;
N15: TMenuItem;
StringGrid1: TStringGrid;
Query1: TQuery;
StringGrid2: TStringGrid;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure ToolButton2Click(Sender: TObject);
procedure ToolButton5Click(Sender: TObject);
procedure ToolButton6Click(Sender: TObject);
procedure QRPreview1PageAvailable(Sender: TObject;
PageNum: Integer);
procedure ToolButton7Click(Sender: TObject);
procedure ToolButton8Click(Sender: TObject);
procedure ToolButton4Click(Sender: TObject);
procedure ToolButton10Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure ToolButton13Click(Sender: TObject);
procedure N751Click(Sender: TObject);
procedure N501Click(Sender: TObject);
procedure N251Click(Sender: TObject);
procedure MenuItem1Click(Sender: TObject);
procedure MenuItem2Click(Sender: TObject);
procedure MenuItem3Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure N14Click(Sender: TObject);
procedure N7Click(Sender: TObject);
procedure N8Click(Sender: TObject);
procedure N9Click(Sender: TObject);
procedure N10Click(Sender: TObject);
procedure N1001Click(Sender: TObject);
procedure N1101Click(Sender: TObject);
procedure N1251Click(Sender: TObject);
procedure N7551Click(Sender: TObject);
procedure N502Click(Sender: TObject);
procedure N252Click(Sender: TObject);
procedure ToolButton14Click(Sender: TObject);
procedure N15Click(Sender: TObject);
procedure SaveExcel(psSaveFile :string);
private
{ Private declarations }
public
{ Public declarations }
end;

var
f_prev: Tf_prev;
pagecount:integer;
implementation
uses pass;
{$R *.DFM}
procedure Tf_prev.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
try
qrpreview1.qrprinter:=nil;
qrpreview1.qrprinter.free;
stringgrid1.Destroy;
except
end;
end;

procedure Tf_prev.ToolButton2Click(Sender: TObject);
begin
qrpreview1.ZoomToWidth;
end;

procedure Tf_prev.ToolButton5Click(Sender: TObject);
begin
if pagecount=0 then
exit;
qrpreview1.pagenumber:=1;
toolbutton5.enabled:=false;
toolbutton6.enabled:=false;
if pagecount>1 then
begin
toolbutton7.enabled:=true;
toolbutton8.enabled:=true;
end
else
begin
toolbutton7.enabled:=false;
toolbutton8.enabled:=false;
end;
statusbar1.simpletext:='共'+inttostr(pagecount)+'页,第'+inttostr(qrpreview1.pagenumber)+'页';
end;

procedure Tf_prev.ToolButton6Click(Sender: TObject);
begin
if pagecount=0 then
exit;
if qrpreview1.pagenumber<>1 then
qrpreview1.pagenumber:=qrpreview1.pagenumber-1;
if qrpreview1.pagenumber=1 then
begin
toolbutton5.enabled:=false;
toolbutton6.enabled:=false;
end
else
begin
toolbutton5.enabled:=true;
toolbutton6.enabled:=true;
end;
if qrpreview1.pagenumber=pagecount then
begin
toolbutton7.enabled:=false;
toolbutton8.enabled:=false;
end
else
begin
toolbutton7.enabled:=true;
toolbutton8.enabled:=true;
end;
statusbar1.simpletext:='共'+inttostr(pagecount)+'页,第'+inttostr(qrpreview1.pagenumber)+'页';
end;

procedure Tf_prev.QRPreview1PageAvailable(Sender: TObject;
PageNum: Integer);
begin
pagecount:=pagenum;
statusbar1.simpletext:='共'+inttostr(pagecount)+'页,第'+inttostr(qrpreview1.pagenumber)+'页';
if pagecount<2 then
begin
toolbutton5.enabled:=false;
toolbutton6.enabled:=false;
toolbutton7.enabled:=false;
toolbutton8.enabled:=false;
end
else
begin
toolbutton5.enabled:=true;
toolbutton6.enabled:=true;
toolbutton7.enabled:=true;
toolbutton8.enabled:=true;
end;
end;

procedure Tf_prev.ToolButton7Click(Sender: TObject);
begin
if pagecount=0 then
exit;
if qrpreview1.pagenumber<pagecount then
qrpreview1.pagenumber:=qrpreview1.pagenumber+1;
if qrpreview1.pagenumber=1 then
begin
toolbutton5.enabled:=false;
toolbutton6.enabled:=false;
end
else
begin
toolbutton5.enabled:=true;
toolbutton6.enabled:=true;
end;

if qrpreview1.pagenumber=pagecount then
begin
toolbutton7.enabled:=false;
toolbutton8.enabled:=false;
end
else
begin
toolbutton7.enabled:=true;
toolbutton8.enabled:=true;
end;
statusbar1.simpletext:='共'+inttostr(pagecount)+'页,第'+inttostr(qrpreview1.pagenumber)+'页';
end;

procedure Tf_prev.ToolButton8Click(Sender: TObject);
begin
if pagecount=0 then
exit;
if qrpreview1.pagenumber<>pagecount then
qrpreview1.pagenumber:=pagecount;
toolbutton7.enabled:=false;
toolbutton8.enabled:=false;
if qrpreview1.pagenumber>1 then
begin
toolbutton5.enabled:=true;
toolbutton6.enabled:=true;
end
else
begin
toolbutton5.enabled:=false;
toolbutton6.enabled:=false;
end;
statusbar1.simpletext:='共'+inttostr(pagecount)+'页,第'+inttostr(qrpreview1.pagenumber)+'页';
end;

procedure Tf_prev.ToolButton4Click(Sender: TObject);
begin
try
qrpreview1.QRPrinter.PrintSetup;
except
end;
qrpreview1.qrprinter.print;
end;

procedure Tf_prev.ToolButton10Click(Sender: TObject);
begin
qrpreview1.pagenumber:=1;
statusbar1.simpletext:='';
close;
end;

procedure Tf_prev.FormShow(Sender: TObject);
begin
pagecount:=0;
//权限检查:数据转出
query1.Close;
query1.parambyname('zgbh').asstring:=gsZgbh;
query1.parambyname('gnbh').asstring:='BFSJZC';
query1.prepare;
query1.Open;
query1.fetchall;
if query1.RecordCount >0 then
begin
ToolButton14.Enabled :=true;
end
else
begin
ToolButton14.Enabled :=false;
end;
end;

procedure Tf_prev.ToolButton13Click(Sender: TObject);
begin
try
qrpreview1.QRPrinter.PrintSetup;
except
end;
end;

procedure Tf_prev.N751Click(Sender: TObject);
begin
qrpreview1.zoom:=75;
end;

procedure Tf_prev.N501Click(Sender: TObject);
begin
qrpreview1.zoom:=50;
end;

procedure Tf_prev.N251Click(Sender: TObject);
begin
qrpreview1.zoom:=25;
end;

procedure Tf_prev.MenuItem1Click(Sender: TObject);
begin
qrpreview1.zoom:=100;
end;

procedure Tf_prev.MenuItem2Click(Sender: TObject);
begin
qrpreview1.zoom:=110;
end;

procedure Tf_prev.MenuItem3Click(Sender: TObject);
begin
qrpreview1.zoom:=125;
end;

procedure Tf_prev.N2Click(Sender: TObject);
begin
toolbutton13.CLICK;
end;

procedure Tf_prev.N3Click(Sender: TObject);
begin
TOOLBUTTON4.CLICK;
end;

procedure Tf_prev.N5Click(Sender: TObject);
begin
TOOLBUTTON10.CLICK;
end;

procedure Tf_prev.N14Click(Sender: TObject);
begin
TOOLBUTTON2.CLICK;
end;

procedure Tf_prev.N7Click(Sender: TObject);
begin
TOOLBUTTON5.CLICK;
end;

procedure Tf_prev.N8Click(Sender: TObject);
begin
TOOLBUTTON6.CLICK;
end;

procedure Tf_prev.N9Click(Sender: TObject);
begin
TOOLBUTTON7.CLICK;
end;

procedure Tf_prev.N10Click(Sender: TObject);
begin
TOOLBUTTON8.CLICK;
end;

procedure Tf_prev.N1001Click(Sender: TObject);
begin
qrpreview1.zoom:=100;
end;

procedure Tf_prev.N1101Click(Sender: TObject);
begin
qrpreview1.zoom:=110;
end;

procedure Tf_prev.N1251Click(Sender: TObject);
begin
qrpreview1.zoom:=125;
end;

procedure Tf_prev.N7551Click(Sender: TObject);
begin
qrpreview1.zoom:=75;
end;

procedure Tf_prev.N502Click(Sender: TObject);
begin
qrpreview1.zoom:=50;
end;

procedure Tf_prev.N252Click(Sender: TObject);
begin
qrpreview1.zoom:=25;
end;

procedure Tf_prev.N15Click(Sender: TObject);
begin
TOOLBUTTON14.CLICK;
end;

procedure Tf_prev.ToolButton14Click(Sender: TObject);
//转出形成EXCEL文件
var
vsSavefile :string;
//文件名(含路径)
begin
//取出转出的文件路径及文件名
if not SaveDialog1.Execute then
exit;
//文件加上扩展名
if Pos('.XLS', UpperCase(SaveDialog1.FileName)) = 0 then
vsSavefile := SaveDialog1.FileName + '.XLS'
else
vsSavefile := SaveDialog1.FileName;
//判断文件是否存在
if FileExists(vsSavefile) then
begin
if Application.MessageBox('文件已经存在,是否覆盖保存?','提示信息',MB_ICONINFORMATION + MB_OKCANCEL) = IDOK then
DeleteFile(vsSavefile)
else
exit;
end;
SaveExcel(vsSavefile);
//转出
end;

procedure tf_prev.SaveExcel(psSaveFile :string);
//数据转出形成EXCEL文件
var
viCol, viRow, viCount, viRowcount, viColCount, i, j: integer;
//循环变量,控制Excel表中当前行、列
Excel: Variant;
WorkBook, WorkSheet : variant;
//返回当前的Excel对象
begin
{if psSaveFile ='' then
exit;
//创建Excel对象
try
Excel := Createoleobject('Excel.Application');
except
application.messagebox('没有安装Excel或者Excel安装不正确,不能进行数据转出','提示信息',64);
exit;
end;
}
try
Excel := Createoleobject('Excel.Application');
except
application.messagebox('没有安装Excel或者Excel安装不正确,不能进行数据转出','提示信息',64);
exit;
end;

//总总行数
viRowCount :=stringgrid1.RowCount;
viColCount :=stringgrid1.ColCount;
try
//生成Excel对象的实例
WorkBook := Excel.WorkBooks.Add;
WorkSheet := WorkBook.WorkSheets.Add;
//获得当前数据集的记录数,为进度条的生成产生条件
//将数字的输入当作文本存储,否则数字输入的前导零不能存储,即0012只能存为12.
// WorkSheet.Range['A1:Z'+IntToStr(viRowCount+1)].NumberFormatLocal := '@';
//往Excel表中添加字段名(第一行),并且设置标题内容居中
for i :=1 to viColCountdo
begin
WorkSheet.Cells[1,i].Value :=trim(stringgrid1.cells[0, i-1]);
WorkSheet.Cells[1,i].HorizontalAlignment := 3;
end;

//开始数据的转入
//从第二行开始(第一行存储字段名)
viRow:=2;
for i :=2 to viRowCountdo
begin
for j :=1 to viColcountdo
begin
if length(stringgrid1.cells[I-1,J-1])>=18 then
WorkSheet.CELLS[viRow,j].NumberFormatLocal := '@';
WorkSheet.CELLS[viRow,j].Value :=stringgrid1.cells[I-1,J-1];//trim(stringgrid1.cells[I-1,J-1]);//[i-1, j-1]);
WorkSheet.Cells[viRow,j].HorizontalAlignment := 2;
end;
//控制进度条的显示
inc(viRow);
end;

//将Excel读入的数据保存到文件中
WorkSheet.SaveAs(psSaveFile);
Application.MessageBox('数据转出成功!','提示信息',MB_ICONINFORMATION + mb_ok+mb_iconInformation);
finally
//关闭Excel
try
WorkBook.Close(SaveChanges:=False);
except
end;
if not VarIsEmpty(Excel) then
begin
Excel.DisplayAlerts := False;
Excel.Quit;
end;
end;
end;

end.
 
多人接受答案了。
 
后退
顶部