Google
Showing posts with label Printer. Show all posts
Showing posts with label Printer. Show all posts

Monday, March 19, 2007

determine if a printer is a Dot-Matrix or Laser

{$APPTYPE CONSOLE}
uses Windows, Printers, WinSpool, Variants;
{
Using only API calls, determinate which type is the active printer:
Dot-Matrix or Laser (or InkJet)

This example is distributed "AS IS", WITHOUT WARRANTY OF ANY KIND,
either express or implied. You use it at your own risk!
}

function IsPrinterMatrix: Boolean;
var
DeviceMode: THandle;
Device, Driver, Port: array [0..79] of Char;
pDevice, pDriver, pPort: PChar;
begin

// Determinate that active printer is a Dot-Marix
Result:= False;
pDevice := @Device;
pDriver := @Driver;
pPort := @Port;

Device := #0;
Driver := #0;
Port := #0;

Printer.GetPrinter(pDevice, pDriver, pPort, DeviceMode);

// Printer can be dot-matrix when number of colors is maximum 16
// and when printer is capable to print only for TRUETYPE
// fonts as graphics (dot-matrix and PCL printers are capable for that).

if (GetDeviceCaps(Printer.Handle,NUMCOLORS)<=16) and
(DeviceCapabilities(pDevice, pPort,DC_TRUETYPE,nil,nil) = DCTT_BITMAP)
then
Result := True;
end;

begin
writeln ('Active printer is ', Printer.Printers[Printer.PrinterIndex]);

if IsPrinterMatrix then
writeln('This is a Dot-Matrix printer')
else
writeln('This is a LaserJet or InkJet printer');
end.

retrieve the Port of a given Printer (W2K, XP)

uses
Registry;

//...

function Get_Printerport(Printername: string): string;
var
Reg: TRegistry;
p: Integer;
begin
Reg := TRegistry.Create;
with Reg do
begin
RootKey := HKEY_CURRENT_USER;
if OpenKey('\Software\Microsoft\Windows NT\CurrentVersion\Devices\', True) then
begin
if
ValueExists(Printername) then
begin
// Im reg Key steht so etwas wie "winspool,LPT1:"
// The reg Key value may look like "winspool,LPT1:"
Result := ReadString(Printername);
p := Pos(',', Result);
Result := Copy(Result, p + 1,Length(Result) - p);
end;
end;
CloseKey;
end;
end;