Solved

Save printer settings

Posted on 1998-10-29
4
635 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
Comment Utility
#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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now