procedure TMainForm.FormCreate(Sender: TObject);
begin
  { Copy the printer names to the combobox and set the combobox to
    show the currently selected default printer }
  cbPrinters.Items.Assign(Printer.Printers);
  cbPrinters.Text := Printer.Printers[Printer.PrinterIndex];
  // Update the label to reflect the default printer 
  lblPrinter.Caption := Printer.Printers[Printer.PrinterIndex];
end;procedure TMainForm.cbPrintersChange(Sender: TObject);
var
  IniFile: TIniFile;
  TempStr1, TempStr2: String;
  S: array[0..64] of char;
begin
  with Printer do
  begin
    // Set the new printer based on the ComboBox's selected printer
    PrinterIndex := cbPrinters.ItemIndex;
    // Store the printer name into a temporary string
    TempStr1 := Printers[PrinterIndex];
    // Delete the unnecessary portion of the printer name
    System.Delete(TempStr1, Pos(' on ', TempStr1), Length(TempStr1));
    // Create a TIniFile class
    IniFile := TIniFile.Create('WIN.INI');
    try
      // Retrieve the device name of the selected printer
      TempStr2 := IniFile.ReadString('Devices', TempStr1, '');
      // Change the default printer to that chosen by the user
      IniFile.WriteString('windows', 'device', TempStr1 + ',' + TempStr2);
      // Tell all windows that the default printer changed. 
      StrCopy(S, 'windows');
      SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@S));
    finally
      IniFile.Free;
    end;
  end;
  // Update the label to reflect the new printer selection 
  lblPrinter.Caption := Printer.Printers[Printer.PrinterIndex];
end;

解决方案 »

  1.   

    procedure Tfrmprintsetup.btbtnokClick(Sender: TObject);
    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
      if combobox1.Tag<>0 then
      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));
      end;
      close;
    end;
    在use中加上messages