Solved

Selecting a printer with code.

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

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

17 Experts available now in Live!

Get 1:1 Help Now