1.首先获取当前本地所有的打印机
单元需要引用\'Printers\' 通过Printer.Printers获取打印机列表,存到配置文件或者是键值对中
2.frxReport.PrintOptions.Printer 打印的时候用取到的打印机名称对这个属性进行赋值就可以了。
示例代码:Unit1.pas
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, frxClass; type TForm1 = class(TForm) cbb_printers: TComboBox; Label1: TLabel; frxReport1: TfrxReport; btnPrint: TButton; procedure FormCreate(Sender: TObject); procedure btnPrintClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses Printers; procedure TForm1.FormCreate(Sender: TObject); begin cbb_printers.Items.AddStrings(Printer.Printers); if cbb_printers.Items.Count > 0 then cbb_printers.ItemIndex := 0; end; procedure TForm1.btnPrintClick(Sender: TObject); var print_template: string; begin if cbb_printers.Items.Count = 0 then begin ShowMessage(\'没有打印机\'); Exit; end; print_template := ExtractFilePath(ParamStr(0)) + \'print.fr3\'; if FileExists(print_template) then begin frxReport1.LoadFromFile(print_template); //frxReport1.DesignReport(); end else begin //ShowMessage(\'打印模板文件:【\' + print_template + \'】不存在\'); end; frxReport1.PrepareReport(); frxReport1.PrintOptions.ShowDialog:=false; frxReport1.PrintOptions.Printer := cbb_printers.Text; //frxReport1.Report.PrintOptions.Printer := cbb_printers.Text; frxReport1.print; end; end.
Unit1.dfm
object Form1: TForm1 Left = 189 Top = 220 Width = 766 Height = 457 Caption = \'Form1\' Color = clBtnFace Font.Charset = GB2312_CHARSET Font.Color = clBlue Font.Height = -19 Font.Name = \'微软雅黑\' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 25 object Label1: TLabel Left = 200 Top = 96 Width = 114 Height = 25 Caption = \'打印机列表:\' end object cbb_printers: TComboBox Left = 200 Top = 144 Width = 457 Height = 33 ItemHeight = 25 TabOrder = 0 Text = \'cbb_printers\' end object btnPrint: TButton Left = 272 Top = 216 Width = 161 Height = 57 Caption = \'打印\' TabOrder = 1 OnClick = btnPrintClick end object frxReport1: TfrxReport Version = \'4.9.32\' DotMatrixReport = False IniFile = \'\Software\Fast Reports\' PreviewOptions.Buttons = [pbPrint, pbLoad, pbSave, pbExport, pbZoom, pbFind, pbOutline, pbPageSetup, pbTools, pbEdit, pbNavigator, pbExportQuick] PreviewOptions.Zoom = 1.000000000000000000 PrintOptions.Printer = \'预设\' PrintOptions.PrintOnSheet = 0 ReportOptions.CreateDate = 44085.749619872690000000 ReportOptions.LastChange = 44085.749619872690000000 ScriptLanguage = \'PascalScript\' ScriptText.Strings = ( \'begin\' \'\' \'end.\') Left = 192 Top = 224 Datasets = <> Variables = <> Style = <> end end
请发表评论