?
Solved

Save printer settings

Posted on 1998-10-29
4
Medium Priority
?
656 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
[X]
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
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 300 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

770 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