Solved

Get Default Printer

Posted on 2006-11-17
3
1,616 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
  • 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
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…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now