Solved

Selecting a printer with code.

Posted on 1998-05-28
2
386 Views
Last Modified: 2010-04-03
As a beginner in Delphi.  How do I select a non default printer with code ?  I know you can use getprinter and printerindex etc, but am unsure how to use these functions.
Help !  

0
Comment
Question by:dmcqueen
2 Comments
 
LVL 7

Expert Comment

by:BlackMan
ID: 1348804
If you are talking standard Delphi, you should take a look at TPrinter (type it in a unit and press F1). It has a Printers and a PrinterIndex property. Below is a snip from D2 help.

if (MessageDlg('Do you want to use the default printer',
    mtInformation, mbYesNoCancel,0)=idYes) then
      Printer.PrinterIndex := -1;
0
 
LVL 1

Accepted Solution

by:
Edo082297 earned 40 total points
ID: 1348805
Hi dmc,

There are a number of different ways to do what you want to do. I am assuming that you want to print to another printer, without changing the default printer on the machine. I am assuming that you know the name of the printer (it has been chosen from a list box or something; I show a method to get them below).

procedure ChangePrinters( i : Integer );  
var
  Device, Driver, Port: array[0..79] of char;
  Dest : PChar;
  DeviceMode: THandle;
begin
  Printer.PrinterIndex := i;
  // store information about printer[i]

  Printer.GetPrinter(Device, Driver, Port, DeviceMode);
  // ("Device" is now the friendly name of the printer)

  if DeviceMode <> 0 then // if there has been no weird error
  begin
    // Lock the DeviceMode structure:
    Dest := GlobalLock(DeviceMode);

    // copy the name of the printer into the DevMode structure:
    StrCopy(Dest, Device);
   
    // !Unlock what we locked!
    GlobalUnlock(DeviceMode);
  end;
end;

What is going on here? Well, first we are loading the arrays we declared with information about the printer. This information is stored in something called the DevMode structure (look it up in Win32.hlp). You can read and write values to this structure by locking it, using GlobalLock. This tells Windows that you want to talk to the structure for a moment (Windows otherwise keeps moving it around in memory!). Unlocking is very important.

In fact, the StrCopy up there isn't the best way to change the printer, but it works for our purposes. I am trying to keep in mind that you are a beginner, so I won't overwhelm you with pointers to the DevMode structure. If you want to know a better way, let me know, and I will post a better, but more complicated, version of the above.

Regards,

Edo
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Working with hours 3 65
creating threads in delphi 1 131
Delphi: ForceDirectory plain function 7 42
I want to use librsync in my Delphi backup application. 3 58
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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 …

839 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