?
Solved

Selecting a printer with code.

Posted on 1998-05-28
2
Medium Priority
?
397 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

770 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