[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Save printer settings

Posted on 1998-10-29
4
Medium Priority
?
662 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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

656 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