[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Selecting a printer with code.

Posted on 1998-05-28
2
Medium Priority
?
404 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
[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 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 160 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 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