Author: Tomas Rutkauskas
How is it possible to add all paperbins from the active printer in a listbox (like
in the PrinterSetupDialog)?
Answer:
1 uses
2 WinSpool;
3
4 procedure GetBinnames(sl: TStrings);
5 type
6 TBinName = array[0..23] of Char;
7 TBinNameArray = array[1..High(Integer) div Sizeof(TBinName)] of TBinName;
8 PBinnameArray = ^TBinNameArray;
9 TBinArray = array[1..High(Integer) div Sizeof(Word)] of Word;
10 PBinArray = ^TBinArray;
11 var
12 Device, Driver, Port: array[0..255] of Char;
13 hDevMode: THandle;
14 i, numBinNames, numBins, temp: Integer;
15 pBinNames: PBinnameArray;
16 pBins: PBinArray;
17 begin
18 Printer.PrinterIndex := -1;
19 Printer.GetPrinter(Device, Driver, Port, hDevmode);
20 numBinNames := WinSpool.DeviceCapabilities(Device, Port, DC_BINNAMES, nil, nil);
21 numBins := WinSpool.DeviceCapabilities(Device, Port, DC_BINS, nil, nil);
22 if numBins <> numBinNames then
23 begin
24 raise Exception.Create('DeviceCapabilities reports different number of bins and
25 '+ 'bin names!');
26 end;
27 if numBinNames > 0 then
28 begin
29 pBins := nil;
30 GetMem(pBinNames, numBinNames * Sizeof(TBinname));
31 GetMem(pBins, numBins * Sizeof(Word));
32 try
33 WinSpool.DeviceCapabilities(Device, Port, DC_BINNAMES, Pchar(pBinNames), nil);
34 WinSpool.DeviceCapabilities(Device, Port, DC_BINS, Pchar(pBins), nil);
35 sl.clear;
36 for i := 1 to numBinNames do
37 begin
38 temp := pBins^[i];
39 sl.addObject(pBinNames^[i], TObject(temp));
40 end;
41 finally
42 FreeMem(pBinNames);
43 if pBins <> nil then
44 FreeMem(pBins);
45 end;
46 end;
47 end;
48
49 //Called like this:
50
51 GetBinnames(listbox1.items);
|