Change printer settings by code

edited February 2012 in FastReport 4.0
I need to use the standard print dialog of Delphi instead the one included in FastReport to choose and setup the printer from the code but the printer settings (like color, bin, copies number) are not applied.
I'm using this code:
var
  AIndex : Integer;

begin
  frxReport.LoadFromFile(DllGlobalData.RootPath + SVAL__REPORT_FILE_NAME);
  ..........
  ..........
  if not PrintDialog.Execute then Exit;

  frxReport.PrepareReport(True);
  frxreport.PrintOptions.ShowDialog := False;

  AIndex := frxPrinters.IndexOf(Printer.Printers[Printer.PrinterIndex]);
  if AIndex <> -1 then
  begin
    frxPrinters.PrinterIndex := AIndex;
    frxreport.PrintOptions.Printer := frxPrinters[AIndex].Name;
    frxreport.SelectPrinter;
    frxReport.Print;
  end;
end;

Using the following procedure (after the line "if not PrintDialog.Execute then Exit;") I can see that the setup is correctly applied to the Printer:
procedure CheckSettings;
var
  hDevMode: THandle;
  Device, Driver, Port: array[0..1024] of Char;
  DeviceMode: PDevMode;

begin
  Printer.GetPrinter(Device, Driver, Port, hDevMode);
  if hDevMode <> 0 then
  begin
    DeviceMode := GlobalLock(hDevMode);
    case DeviceMode^.dmColor of
      DMCOLOR_COLOR: Label1.Caption := 'Color';
      DMCOLOR_MONOCHROME: Label1.Caption := 'Monochrome';
    else
      Label1.Caption := 'Unknown';
    end;
    GlobalUnlock(hDevMode);
  end;
end;

What is wrong ?

P.S. I'm using the version shipped with RAD Studio XE2

Comments

  • gpigpi
    edited 5:26PM
    var
      AIndex : Integer;
      hDevMode: THandle;
      Device, Driver, Port: array[0..1024] of Char;
      DeviceMode: PDevMode;
    
    begin
    
      if not PrintDialog1.Execute then Exit;
    
      frxReport1.PrepareReport(True);
      frxreport1.PrintOptions.ShowDialog := False;
    
      AIndex := frxPrinters.IndexOf(Printer.Printers[Printer.PrinterIndex]);
    
      if AIndex <> -1 then
      begin
        frxPrinters.PrinterIndex := AIndex;
        frxreport1.PrintOptions.Printer := frxPrinters[AIndex].Name;
        frxreport1.SelectPrinter;
    
        Printer.GetPrinter(Device, Driver, Port, hDevMode);
        if hDevMode <> 0 then
          DeviceMode := GlobalLock(hDevMode);
    
        TfrxPrinter(frxPrinters.Printer).DeviceMode.dmColor := DeviceMode^.dmColor;
        GlobalUnlock(hDevMode);
        //frxPrinters.Printer.PropertiesDlg;
        frxReport1.Print;
      end;
    
    end;
    
  • edited 5:26PM
    Many, many thanks.
    All works right now !

    Thank you very much again.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.