Solved

Get Default Printer

Posted on 2006-11-17
3
1,758 Views
Last Modified: 2012-06-27
Hi Experts,

Can anybody show me code line(s) how to get the default printer in delphi 6?
Say provided a form and a button, when the user clicks on it, it will
display the default printer (on the machine where this program is running)
together with the default paper size and paper length.
Note that from time to time, the default printer may not be the one locally attached on this computer. It might be from another machine/computer.

TIA,
yorge
0
Comment
Question by:yorge
[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
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
atul_parmar earned 75 total points
ID: 17963925
Very easy,

uses
  printers;

....

begin
  ShowMessage(Printer.Printers[Printer.PrinterIndex]);
end;

to know the paper size use Printer.Paperwidth and Paperheight properties.
0
 
LVL 10

Assisted Solution

by:atul_parmar
atul_parmar earned 75 total points
ID: 17963968
more precisely,
Printer.PrinterIndex := -1; // sets the printer to default; -1 for default printer
then
ShowMessage(Printer.Printers[Printer.PrinterIndex]);
...



0
 
LVL 1

Expert Comment

by:PaulShearing
ID: 23872210
Thanks atul. I have found that without first setting the default printer a reference to Printer.PrinterIndex can cause a runtime crash deep in ntdll.dll. Without setting the PrinterIndex to -1 my code would fail on the line:

If (Printer.PrinterIndex > 0) Then

My Delphi code to get the default printer name now looks like this:

Function getDefaultPrinterName(): String;
Begin
   Printer.PrinterIndex := -1;     // set to default
   If (Printer.PrinterIndex > 0) Then
      Try
         Result := Printer.Printers[Printer.PrinterIndex]
      Except
         Result := 'Unknown'
      End
   else
      Result := 'Unknown'
End;


0

Featured Post

Independent Software Vendors: 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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…

749 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