Mega Code Archive

 
Categories / Delphi / Examples
 

A printername by any other name

The Perpetual Newbie - Log Entry #4.1 This article first appeared on http://www.undu.com I like to keep my users informed. A particular piece of information that tends to avoid trouble down the road is to have a clear label on screen as to which is the active printer. This helps avoid sending laser reports to dot matrix printers and vice-versa. And knowing where the intended destination lies allows me to change the &Print button to a &Fax button as the occasion demands it. A little thing, but it reassures the user you are in control. Unfortunately, I used a third-party DCU to get the printer name for several years. I know, I know. You aren't supposed to use any third-party tool that you don't have source for, but it worked beautifully. Until my most recent upgrade. That's when I discovered the DCU-provider was no longer supporting or updating the tool. Oops!!! I went through the TPrinter help section. Still couldn't come up with a solution. So, off to the internet I went, using www.deja.com to search for "Delphi printername." As has been pointed out to me, I would probably have been better off at http://www.tamaracka.com/search.htm. Despite the misguided start, I did encounter several emails, including one by Barry McClure of Grebar Systems, the providers of PrintDAT!, a very nice grid printer tool that I have been using happily. In the exchange was my solution, presented here as a function or two: uses Printers; {Gets FULL Printername including port designation} function gmGetPrinterName:string; begin if Printer.PrinterIndex = -1 then result := 'No Default Printer Selected' else result := Printer.Printers[Printer.PrinterIndex]; end; {Returns the Printername without port designations} function gmGetJustPrinterName:string; const s = ' on '; // include a space before and after 'on' for the pos search begin if Printer.PrinterIndex = -1 then result := 'No Default Printer Selected' else begin result := Printer.Printers[Printer.PrinterIndex]; if pos(s,result) > 0 then result := copy(result,1,pos(s,result)-1); // shaves off port end; end; Gary Mugford Idea Mechanic, Bramalea ON Canada mugford@aztec-net.com