Solved

Selecting a printer with code.

Posted on 1998-05-28
2
372 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

932 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

10 Experts available now in Live!

Get 1:1 Help Now