小弟请教,在Delphi中如何用代码检测打印机是否存在?

解决方案 »

  1.   

    var
      Device: array[0..255] of Char;
      Driver: array[0..255] of char;
      Port: array[0..255] of char;
      s : array[0..255] of Char;
      hDeviceMode: THandle;begin
    Printer.PrinterIndex := ComboBox1.ItemIndex;
      Printer.GetPrinter (Device, Driver, Port, hDeviceMode);
      StrCopy (s, Device);
      StrCat (s, ',');
      StrCat (s, Driver);
      StrCat (s, ',');
      StrCat (s, Port);
      WriteProfileString ('windows', 'device', s);
      StrCopy (s, 'windows');
      SendMessage (HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@s));
      ComboBox1.Items := Printer.Printers; {populates ComboBox}
      ComboBox1.ItemIndex := Printer.PrinterIndex;
      //showmessage(inttostr(combobox1.ItemIndex));
      showmessage(combobox1.Items[1].Text);
      //showmessage(combobox1.);
    if var
      Device: array[0..255] of Char;
      Driver: array[0..255] of char;
      Port: array[0..255] of char;
      s : array[0..255] of Char;
      hDeviceMode: THandle;begin
    Printer.PrinterIndex := ComboBox1.ItemIndex;
      Printer.GetPrinter (Device, Driver, Port, hDeviceMode);
      StrCopy (s, Device);
      StrCat (s, ',');
      StrCat (s, Driver);
      StrCat (s, ',');
      StrCat (s, Port);
      WriteProfileString ('windows', 'device', s);
      StrCopy (s, 'windows');
      SendMessage (HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@s));
      ComboBox1.Items := Printer.Printers; {populates ComboBox}
      ComboBox1.ItemIndex := Printer.PrinterIndex;
       if combobox1.Items.Text=' ' then showmessage('no printere');
      

  2.   

    var
      Device: array[0..255] of Char;
      Driver: array[0..255] of char;
      Port: array[0..255] of char;
      s : array[0..255] of Char;
      hDeviceMode: THandle;begin
    Printer.PrinterIndex := ComboBox1.ItemIndex;
      Printer.GetPrinter (Device, Driver, Port, hDeviceMode);
      StrCopy (s, Device);
      StrCat (s, ',');
      StrCat (s, Driver);
      StrCat (s, ',');
      StrCat (s, Port);
      WriteProfileString ('windows', 'device', s);
      StrCopy (s, 'windows');
      SendMessage (HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@s));
      ComboBox1.Items := Printer.Printers; {populates ComboBox}
      ComboBox1.ItemIndex := Printer.PrinterIndex;
    if combobox1.Items.Text=' ' then showmessage('no printere');
      

  3.   

    uses  Printersprocedure TfrmJLMain.N13Click(Sender: TObject);
    var
      tt: TPrinter;
    begin
     { try
        PrinterSetupDialog1.Execute;
      except
        //
      end; }
      tt := TPrinter.Create;  if tt.Printers.Count <= 0 then
        ShowMessage('错误:没有找到打印机!')
      else
        PrinterSetupDialog1.Execute;  tt.Free;
    end;