Printer設定の保存・読み込み

 

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, PrintersDlgs, Forms, Controls,
  Graphics, Dialogs, Printers, ExtCtrls, StdCtrls, LCLType;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    PrinterSetupDialog1: TPrinterSetupDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}



{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  s : string ;
  sFileName : string ;
  sDirName : string ;
  F : TextFile;
begin
  if not PrinterSetupDialog1.Execute then Exit;

  // プリンタ名
  Memo1.Clear;
  s := 'プリンタ名:' + Printer.PrinterName ;
  Memo1.Lines.Add(s);
  // PaperSize
  s := 'PaperSize:' + (Printer.PaperSize.PaperName);
  Memo1.Lines.Add(s);
  //横方向の総ドット数
  s := '横:' + IntToStr(Printer.PageWidth);
  Memo1.Lines.Add(s);
  //横方向の解像度
  s := 'XDPI:' + IntToStr(Printer.XDPI);
  Memo1.Lines.Add(s);
  //縦方向の総ドット数
  s := '縦:' + IntToStr(Printer.PageHeight);
  Memo1.Lines.Add(s);
  //縦方向の解像度
  s := 'XDPI:' + IntToStr(Printer.YDPI);
  Memo1.Lines.Add(s);
  //印刷の向き
  if Printer.Orientation = poPortrait then s := 'Portrait'
  else s := 'Landscape';
  s := '向き  :' + s;
  Memo1.Lines.Add(s);
   //給紙ビンの数
 if Printer.SupportedBins.Count > 0 then
   begin
    s := 'BinCount:' + IntToStr(Printer.SupportedBins.Count);
    Memo1.Lines.Add(s);
   end;
 //給紙ビンの名前
  s := '給紙  :' + Printer.BinName;
  Memo1.Lines.Add(s);


  //設定保存

 

  //GetAppConfigDir(False)
  //win以外も考えるならば(False)が良いかも
  //C:\Users\ユーザー名\AppData\Local\Printer1

  sDirName := GetAppConfigDir(False);
  sDirName := Copy(sDirName,1,Length(sDirName) - 1);
  Memo1.Lines.Add(sDirName);
  application.ProcessMessages;;
  if not DirectoryExists(sDirName) then  ForceDirectories(sDirName);

  sFileName := GetAppConfigDir(False) + 'Printer.ini';
  Memo1.Lines.Add(sFileName);

  application.ProcessMessages;;
  AssignFile(F,sFileName);
  ReWrite(F);
    try
     // プリンタ名
      WriteLn(F,'PrinterName:' + Printer.PrinterName);
      // PaperSize
      WriteLn(F,'PaperSize:' + Printer.PaperSize.PaperName);
      //縦横
      if Printer.Orientation = poPortrait then s := 'Portrait'
      else s := 'Landscape';
      WriteLn(F,'Orientation:' + s);
      //給紙ビンの名前
      WriteLn(F,'BinName:' + Printer.BinName);
    Finally;
      CloseFile(F);
    end;
end;



procedure TForm1.Button2Click(Sender: TObject);
var
  s, sFileName : string ;
  sProperty, sValue:string;
  F : TextFile;

begin
  Memo1.Clear;

//設定読み込み
  // Button1で保存したファイルを読み込み
  sFileName := GetAppConfigDir(False) + 'Printer.ini';

  if not FileExists(sFileName) then
   begin
    Memo1.Lines.Add('保存したファイルが無い!');
    Exit;
   end;

  AssignFile(F,sFileName);
  ReSet(F);
   try
    While not Eof(F) do
     begin
      Readln(F, s);
      sProperty := Copy(S,1,Pos(':',S) - 1);
      sValue := Copy(S,Pos(':',S) + 1,200);
         // プリンタ名をセット
           if sProperty = 'PrinterName' then
                         Printer.SetPrinter(sValue)
         // PaperSize
      else if sProperty = 'PaperSize' then
                         Printer.PaperSize.PaperName := sValue
         //縦横
     else if sProperty = 'Orientation' then
          begin
           if  sValue = 'Landscape' then Printer.Orientation := poLandscape
           else Printer.Orientation := poPortrait;
          end
         //給紙ビンの名前
      else if sProperty = 'BinName' then
                         Printer.BinName := sValue;

     end;
    Finally;
      CloseFile(F);
    end;


    // プリンタ名
    s := 'プリンタ名:' + Printer.PrinterName ;
    Memo1.Lines.Add(s);
    // PaperSize
    s := 'PaperSize:' + (Printer.PaperSize.PaperName);
    Memo1.Lines.Add(s);
    //横方向の総ドット数
    s := '横:' + IntToStr(Printer.PageWidth);
    Memo1.Lines.Add(s);
    //横方向の解像度
    s := 'XDPI:' + IntToStr(Printer.XDPI);
    Memo1.Lines.Add(s);
    //縦方向の総ドット数
    s := '縦:' + IntToStr(Printer.PageHeight);
    Memo1.Lines.Add(s);
    //縦方向の解像度
    s := 'XDPI:' + IntToStr(Printer.YDPI);
    Memo1.Lines.Add(s);
    //印刷の向き
    if Printer.Orientation = poPortrait then s := 'Portrait'
    else s := 'Landscape';
    s := '向き  :' + s;
    Memo1.Lines.Add(s);
   //給紙ビンの名前
    s := '給紙  :' + Printer.BinName;
    Memo1.Lines.Add(s);

end;

end.