1. 程式人生 > 實用技巧 >delphi7之 fastreport本地多臺印表機情況下如何選擇指定印表機列印

delphi7之 fastreport本地多臺印表機情況下如何選擇指定印表機列印

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