procedure TForm1.FormCreate(Sender: TObject);
var
WinIni: TIniFile;
DevList: TStringList;
device: string;
i, p: integer;
begin
WinIni := TIniFile.Create('WIN.INI');
// Get the current default printer
device := WinIni.ReadString('windows', 'device', ',,');
if device = '' then device := ',,';
p := Pos(',', device);
DDevice.Name := Copy(device, 1, p-1);
device := Copy(device, p+1, Length(device)-p);
p := Pos(',', device);
DDevice.Driver := Copy(device, 1, p-1);
DDevice.Port := Copy(device, p+1, Length(device)-p);
// Get the printers list
DevList := TStringList.Create;
WinIni.ReadSectionValues('Devices', DevList);
// Store the printers list in a dynamic array
SetLength(Devices, DevList.Count);
for i := 0 to DevList.Count - 1 do begin
device := DevList[i];
p := Pos('=', device);
Devices[i].Name := Copy(device, 1, p-1);
device := Copy(device, p+1, Length(device)-p);
p := Pos(',', device);
Devices[i].Driver := Copy(device, 1, p-1);
Devices[i].Port := Copy(device, p+1, Length(device)-p);
// Add the printer to the ListBox
ListBox1.Items.Add(Devices[i].Name
+ ' (' + Devices[i].Port + ')');
// Is the current default printer?
if (CompareText(Devices[i].Name, DDevice.Name) = 0) and
(CompareText(Devices[i].Driver, DDevice.Driver) = 0) and
(CompareText(Devices[i].Port, DDevice.Port) = 0) then
ListBox1.ItemIndex := i; // Make it the selected printer
end;
WinIni.Free;
end;
|