Arsip: Set default printer


by kaka-delphi in Sistem more 14 years ago 2581
Ingin ngeset default printer pake koding delphi ... ? Berikut cuplikan source code nya :
type
TDevice = record
Name, Driver, Port: string;
end;
var
Devices: array of TDevice;
DDevice: TDevice; // current default printer
Syntax Highlighted with https://pascal-id.org/syntax
Pada event OnCreate TForm, tulis skrip beriut :
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;
Syntax Highlighted with https://pascal-id.org/syntax
Contoh penggunaan nya, tambakan komponen TButton dan TListBox. Pada event OnClick TButton, sisipkan kode berikut :
procedure TForm1.Button1Click(Sender: TObject);
var
WinIni: TIniFile;
begin
if ListBox1.ItemIndex = -1 then exit;
DDevice := Devices[ListBox1.ItemIndex];
WinIni := TIniFile.Create('WIN.INI');
WinIni.WriteString('windows', 'device', DDevice.Name
+ ',' + DDevice.Driver + ',' + DDevice.Port);
WinIni.Free;
SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0,
LPARAM(pchar('windows')));
end;
Syntax Highlighted with https://pascal-id.org/syntax
Pada event OnClick TListBox, sisipkan kode berikut :
procedure TForm1.ListBox1Click(Sender: TObject);
begin
Button1Click(Sender);
end;
Syntax Highlighted with https://pascal-id.org/syntax


berikut skrinsyutnya :


Untuk contoh project, dapat di download di PulicFolder dengan nama SetDefPrinter.rar
Local Business Directory, Search Engine Submission & SEO Tools FreeWebSubmission.com SonicRun.com