Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Save printer settings

Posted on 1998-10-29
4
646 Views
Last Modified: 2012-06-27
How do I save settings of:
selected printer from printsetupdialogbox in the register.
I have tried to use printer.printerindex but I always get printer index #1 or default printer index #-1
and  I also want my program to use my saved printersetting when it starts up,
please recall with some code
0
Comment
Question by:samone
4 Comments
 
LVL 3

Expert Comment

by:Matvey
ID: 1344990
#1 must be the default printer. You can set a different default printer in the Printers folder, is this what you want to do? This issue has propably something to do with the SetPrinter API, but I never used it, so I can advice to look in the Help for this key or the MSDN.
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 100 total points
ID: 1344991
Ok, first let the user choose his printer and put this one in the registery or Ini-file.

TPrinters.Printers contains all available printers.

So TPrinter.PrinterIndex is the currently selected printer.
(-1 for default printer)

If you want to change your printer then look at the index of the printer in printers and fill this one in the printerindex :

TPrinter.PrinterIndex := 2;

Thats all!

Now, you can store the index, but I think it's better to store the printer string in your ini or registery file. Because then your save too if the user added a printer.

Ok, here is some code :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, IniFiles, Printers;

type
  TForm1 = class(TForm)
    Load: TButton;
    Save: TButton;
    ListBox1: TListBox;
    Print: TButton;
    Show: TButton;
    procedure FormCreate(Sender: TObject);
    procedure LoadClick(Sender: TObject);
    procedure SaveClick(Sender: TObject);
    procedure PrintClick(Sender: TObject);
    procedure ShowClick(Sender: TObject);
  private
    { Private declarations }
    PrinterName : ShortString;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var inifile : TIniFile;
begin
 // load printer string
 inifile := TInifile.Create('c:\test.ini');
 try
  PrinterName := Inifile.ReadString('Printer', 'Name', 'None');
 finally
  inifile.Free;
 end;
end;

procedure TForm1.LoadClick(Sender: TObject);
var Printer : TPrinter;
begin
 // get available printers
 Printer := TPrinter.Create;
 try
  ListBox1.Items.Assign(Printer.Printers);
 finally
  Printer.Free;
 end;
end;

procedure TForm1.SaveClick(Sender: TObject);
var IniFile : TIniFile;
begin
 // save selected printer name in ini-file
 if Listbox1.ItemIndex <> -1 then begin
  inifile := TInifile.Create('c:\test.ini');
  try
   Inifile.WriteString('Printer', 'Name', Listbox1.Items[Listbox1.ItemIndex]);
  finally
   inifile.Free;
  end;
 end;
end;

procedure TForm1.PrintClick(Sender: TObject);
var Printer : TPrinter;
    i       : integer;
    PrinterIndex : integer;
begin
 PrinterIndex := -2;
 i := 0;
 // before printing search printer
 Printer := TPrinter.Create;
 if PrinterName <>'None' then begin
  while (PrinterIndex=-2) and (i < Printer.Printers.Count) do begin
   if Printer.Printers[i] = PrinterName then begin
    PrinterIndex := i;
   end;
  end;
 end;
 Printer.PrinterIndex := PrinterIndex;
 // do printstuff here.
 Printer.Free;
end;

procedure TForm1.ShowClick(Sender: TObject);
begin
 ShowMessage(PrinterName);
end;

end.


ps. Windows uses a default printer so that every printer uses this printer. This means the user has only have to change is in the configuration panel. Offcourse if you use special printers for a special program you better put such an option into your program.

Regards, Zif.
0
 
LVL 3

Expert Comment

by:bryan7
ID: 1344992
This is how to use the windows registry:

      Include     REGISTRY     in the Uses clause

      then i.e:   THIS will SAVE to the registry

      procedure TForm1.BitBtn1Click(Sender: TObject);
      Var r: TRegistry;
            a: byte;
            b: byte;
      begin
      a:= 7;
      b:= 12;
      r := TRegistry.Create;
              with r do
              try
                 CreateKey('NameOfMyProgram');
                 OpenKey('NameOfMyProgram', False);
                 WriteBinaryData('Option1',a,1);
                 WriteBinaryData('Option2',b,1);
                 CloseKey;
              finally
                 free;
              end;

      ans   THIS will READ from the registry

      procedure TForm1.BitBtn2Click(Sender: TObject);
      Var r: TRegistry;
            a: byte;
            b: byte;
      begin
      r := TRegistry.Create;
              with r do
              try
                 CreateKey('NameOfMyProgram');
                 OpenKey('NameOfMyProgram', False);
                 ReadBinaryData('Option1',a,1);
                 ReadBinaryData('Option2',b,1);
                 CloseKey;
              finally
                 free;
              end;

This way you can WRITE and READ in the registry.

bryan
0
 

Expert Comment

by:glabax
ID: 1344993
to zifnab :

here is a code witch doesn't work with the printer.printerIndex could you tell me why ???
var
  i,nb_pr:integer;
  chaine:string;
  bool:boolean;
  imprimante:tprinter;
begin
  imprimante:=tprinter.create;
  nb_pr:=imprimante.Printers.Count;
  i:=1;
  bool:=false;
  while (i<=nb_pr) and (not bool) do
  begin
     chaine:=trim(imprimante.Printers[i-1]);
     if copy(chaine,7,8)='DFX-5000' then  // here, the
                                          // printer is founded.
     begin
           bool:=true;
     end;
     i:=i+1;
  end;
  i:=i-1;
  imprimante.PrinterIndex:=i-1;
  r_facture.print;
  r_facture.preview;    // It's a Qreport
  imprimante.free;
end;
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question