2008. október 4., szombat
Check if the current printer is ready to print in color
Problem/Question/Abstract:
How can I find out whether the current printer is ready to print in colour, rather than just capable of printing in colour?
Answer:
Solve 1:
This works for some but not all printers, depending on the driver capabilities:
{ ... }
var
Dev, Drv, Prt: array[0..255] of Char;
DM1: PDeviceMode;
DM2: PDeviceMode;
Sz: Integer;
DevM: THandle;
begin
Printer.PrinterIndex := -1;
Printer.GetPrinter(Dev, Drv, Prt, DevM);
DM1 := nil;
DM2 := nil;
Sz := DocumentProperties(0, 0, Dev, DM1^, DM2^, 0);
GetMem(DM1, Sz);
DocumentProperties(0, 0, Dev, DM1^, DM2^, DM_OUT_BUFFER);
if DM1^.dmColor > 1 then
Label1.Caption := Dev + ': Color'
else
Label1.Caption := Dev + ': Black and White';
if DM1^.dmFields and DM_Color <> 0 then
Label2.Caption := 'Printer supports color printing'
else
Label2.Caption := 'Printer does not support color printing';
FreeMem(DM1);
end;
Solve 2:
function IsColorPrinter: bool;
var
Device: array[0..MAX_PATH] of char;
Driver: array[0..MAX_PATH] of char;
Port: array[0..MAX_PATH] of char;
hDMode: THandle;
PDMode: PDEVMODE;
begin
result := False;
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device, Driver, Port, hDMode);
if hDMode <> 0 then
begin
pDMode := GlobalLock(hDMode);
if pDMode <> nil then
begin
if ((pDMode^.dmFields and dm_Color) = dm_Color) then
begin
result := True;
end;
GlobalUnlock(hDMode);
end;
end;
end;
function SetPrinterColorMode(InColor: bool): bool;
var
Device: array[0..MAX_PATH] of char;
Driver: array[0..MAX_PATH] of char;
Port: array[0..MAX_PATH] of char;
hDMode: THandle;
PDMode: PDEVMODE;
begin
result := False;
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device, Driver, Port, hDMode);
if hDMode <> 0 then
begin
pDMode := GlobalLock(hDMode);
if pDMode <> nil then
begin
if (pDMode^.dmFields and dm_Color) = dm_Color then
begin
if (InColor) then
begin
pDMode^.dmColor := DMCOLOR_COLOR;
end
else
begin
pDMode^.dmColor := DMCOLOR_MONOCHROME;
end;
result := True;
end;
GlobalUnlock(hDMode);
Printer.PrinterIndex := Printer.PrinterIndex;
end;
end;
end;
Solve 3:
It is usually better to use DeviceCapabilities to examine what the printer supports. Unfortunately this will only work on Win2K and XP, not on older platforms.
uses
printers, winspool;
function PrinterSupportsColor: Boolean;
var
Device, Driver, Port: array[0..255] of Char;
hDevMode: THandle;
begin
Printer.GetPrinter(Device, Driver, Port, hDevmode);
Result := WinSpool.DeviceCapabilities(Device, Port, DC_COLORDEVICE, nil, nil) <> 0;
end;
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése