?
Solved

How can I select the printer with printer?

Posted on 2006-04-07
5
Medium Priority
?
775 Views
Last Modified: 2011-09-20
Hello guys.

I have three printers installed in my computer, One of them is for a special printer for labels, I can leave this printers as default in my system only to set the drivers when I use the system for labels. In my cod I am using a this line
Printer.PrinterIndex := Printer.Printers.IndexOf('NAME_OF_MY_PRINTER'), but it is not working fine I think that something is missing, maybe to load another driver. Below you can see the code of my system. Maybe, there is a way to change default printer and after printing I can come back it.


 Printer.PrinterIndex := Printer.Printers.IndexOf('ETIQUETACLIENTE');
  With Printer do
  Begin
    Orientation := poLandscape;
    BeginDoc;
   // Canvas.StretchDraw(Rect(0,30,130,130),Image1.Picture.Graphic);
    Canvas.Pen.Width := 2;
    Canvas.Font.Name := 'Times New Roman';
    Canvas.Font.Size := 16;
    Canvas.TextOut(150,40, Cli);
    Canvas.MoveTo (150,90);
    Canvas.LineTo (600,90);
    Canvas.TextOut(150,110 ,'Romaneio :' + Rom);

    Canvas.Font.Size := 12;
    Canvas.TextOut(10,195,'Quantidade: ' + Qtd);
    Canvas.TextOut(10,255,'Código....: ' + Cod);
    Canvas.TextOut(10,315,'Descrição.: ' + Desc);
    Canvas.TextOut(10,375,'Locação...: ' + loc);

    EndDoc;
  End;
0
Comment
Question by:hidrau
5 Comments
 
LVL 14

Assisted Solution

by:Pierre Cornelius
Pierre Cornelius earned 400 total points
ID: 16401555
This demo will show you how to select a specific printer. Alternatively you could select a printer using the Printer.Getprinter function.

PAS
===
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    PrinterSetupDialog1: TPrinterSetupDialog;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  PrinterSetupDialog1.Execute;
  ShowMessage('Printer selected: '+ Printer.Printers[Printer.PrinterIndex]);
end;

end.


DFM
===
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 197
  Height = 91
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 16
    Top = 8
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object PrinterSetupDialog1: TPrinterSetupDialog
    Left = 112
    Top = 8
  end
end

Regards
Pierre
0
 
LVL 5

Accepted Solution

by:
Scay7 earned 800 total points
ID: 16403151
For this example i place a printersetupdialog, button and a memo on a form.
For the onlick event for the button i have the following:


procedure TForm1.Button1Click(Sender: TObject);
var
oldprinter : integer;
begin
memo1.Lines.Add('Pre Printer was:');
memo1.Lines.Add(Printer.Printers[Printer.PrinterIndex]);
  oldprinter := Printer.PrinterIndex;
memo1.Lines.Add('');
memo1.Lines.Add('Printer Setup Started.');
  printersetupdialog1.Execute;
memo1.Lines.Add('New Selected Printer is:');
memo1.Lines.Add(Printer.Printers[Printer.PrinterIndex]);
memo1.Lines.Add('');

  // add your printing stuff here

  printer.PrinterIndex := oldprinter;
memo1.Lines.Add('Printer Set back to:');
memo1.Lines.Add(Printer.Printers[Printer.PrinterIndex]);
end;


This should get the DEFAULT printer save it, then with the PRINTERSETUPDIALOG select the new printer afterwards change the printer back to the default one...

Peace Scay7
0
 
LVL 6

Assisted Solution

by:saravananvg
saravananvg earned 400 total points
ID: 16406215
Hello sir,

  You could check the following site for various operations you can perform on the printer.

http://www.swissdelphicenter.ch/torry/printing.php

with regards,
padmaja.
0
 
LVL 1

Assisted Solution

by:HanZulu
HanZulu earned 400 total points
ID: 16415379
Changing the index for the printerindex works fine when you select
1 of the printers from a list.

try to select each one of the printers using their id and not indexof.
this code works fine with a 2 printer computer.

for default :

procedure TForm1.FormCreate(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to printer.printers.Count -1 do
    lstPrinters.items.Add (printer.printers[i]);
  printer.PrinterIndex := -1 ;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Print;
end;

for first printer

procedure TForm1.FormCreate(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to printer.printers.Count -1 do
    lstPrinters.items.Add (printer.printers[i]);
  printer.PrinterIndex := 1 ;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Print;
end;

for second printer

procedure TForm1.FormCreate(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to printer.printers.Count -1 do
    lstPrinters.items.Add (printer.printers[i]);
  printer.PrinterIndex := 0 ;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Print;
end;

Works fine.
0
 
LVL 5

Assisted Solution

by:Scay7
Scay7 earned 800 total points
ID: 16430763
quote "Maybe, there is a way to change default printer and after printing I can come back it."

which is what the below code does...

Hanzulu:

procedure TForm1.Button1Click(Sender: TObject);
var oldprinter : integer;
begin
  oldprinter := Printer.PrinterIndex;  //<-- saves the default printer, which any PC has no matter how many printers there  
                                                    //      are. and i save that index...
  printersetupdialog1.Execute; //<-- you select the printer you want to print to changing from the default one.

  // add your printing stuff here, printing with the new set printer

  printer.PrinterIndex := oldprinter; //<-- set the default printer back, cause i saved its index...
end;

why make so many procedures for a simple question ?  :PP

Peace Scay7
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month16 days, 6 hours left to enroll

850 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