Solved

Save printer settings

Posted on 1998-10-29
4
651 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 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
URL for downloading Google Chrome for Win XP 2 278
Create a path if not exists 7 124
Firemonkey Queue list 1 51
Remove all child-forms from a parent-panel 16 12
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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