Link to home
Start Free TrialLog in
Avatar of coracleit
coracleit

asked on

Quick Reports Preview Form (Docking)

I am writing an application that uses an outlook style menu on the left and all forms are docked on the right hand side using:-

  Form1.show;
  Form1.ManualDock(PnlDock, nil,alNone);

This docks my forms so that they appear on the right hand side of my main form (outlook style view)

My question is, I have written a report using quick reports and when I preview it, up pops the report in its usual preview form.  I cannot seem to manually dock this form as I can with my others.

Help please

Wayne
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

You have to create your custom Preview Form using a TQRPreview...
example'll follow later....
OK let's see...

To override the QuickRep preview form you have frst to create a Custom Preview form...
Here is an example by qusoft

{.pas file}

unit prevform;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  qrprntr, ExtCtrls, Menus, Buttons, StdCtrls;

type
  TPreviewForm = class(TForm)
    Panel1: TPanel;
    QRPreview1: TQRPreview;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    Panel2: TPanel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
  private
    { Private declarations }
    FQRPrinter : TQRPrinter;
  public
    { Public declarations }
    constructor CreatePreview(AOwner : TComponent; aQRPrinter : TQRPrinter);
    property QRPrinter : TQRPrinter read FQRPrinter write FQRPrinter;
  end;

var
  PreviewForm: TPreviewForm;

implementation

{$R *.DFM}

constructor TPreviewForm.CreatePreview(AOwner : TComponent; aQRPrinter : TQRPrinter);
begin
  inherited Create(AOwner);
  QRPrinter := aQRPrinter;
  QRPreview1.QRPrinter := aQRPrinter;
  if (QRPrinter <> nil) and (QRPrinter.Title <> '') then Caption := QRPrinter.Title;
end;

procedure TPreviewForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  QRPrinter.ClosePreview(Self);
  Action := caFree;
end;

procedure TPreviewForm.SpeedButton5Click(Sender: TObject);
begin
  QRPreview1.Zoom := QRPreview1.Zoom + 20;
end;

procedure TPreviewForm.SpeedButton6Click(Sender: TObject);
begin
  QRPreview1.Zoom := QRPreview1.Zoom - 20;
end;

procedure TPreviewForm.SpeedButton2Click(Sender: TObject);
begin
  QRPreview1.PageNumber := QRPreview1.PageNumber - 1;
end;

procedure TPreviewForm.SpeedButton3Click(Sender: TObject);
begin
  QRPreview1.PageNumber := QRPreview1.PageNumber + 1;
end;

procedure TPreviewForm.SpeedButton1Click(Sender: TObject);
begin
  QRPreview1.PageNumber := 1;
end;

procedure TPreviewForm.SpeedButton4Click(Sender: TObject);
begin
  QRPreview1.PageNumber := QRPreview1.QRPrinter.PageCount;
end;

end.

{.dfm file}
 
object PreviewForm: TPreviewForm
  Left = 379
  Top = 253
  Width = 586
  Height = 410
  Caption = 'Report Preview'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsMDIChild
  OldCreateOrder = True
  Position = poDefault
  Visible = True
  OnClose = FormClose
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 29
    Height = 363
    Align = alLeft
    BevelOuter = bvNone
    TabOrder = 0
    object SpeedButton1: TSpeedButton
      Left = 2
      Top = 2
      Width = 25
      Height = 25
      Hint = 'First Page'
      Caption = '<<'
      OnClick = SpeedButton1Click
    end
    object SpeedButton2: TSpeedButton
      Left = 2
      Top = 26
      Width = 25
      Height = 25
      Hint = 'Previous Page'
      Caption = '<'
      OnClick = SpeedButton2Click
    end
    object SpeedButton3: TSpeedButton
      Left = 2
      Top = 50
      Width = 25
      Height = 25
      Hint = 'Next Page'
      Caption = '>'
      OnClick = SpeedButton3Click
    end
    object SpeedButton4: TSpeedButton
      Left = 2
      Top = 74
      Width = 25
      Height = 25
      Hint = 'Last Page'
      Caption = '>>'
      OnClick = SpeedButton4Click
    end
    object SpeedButton5: TSpeedButton
      Left = 2
      Top = 98
      Width = 25
      Height = 25
      Hint = 'Zoom in'
      Caption = '+'
      OnClick = SpeedButton5Click
    end
    object SpeedButton6: TSpeedButton
      Left = 2
      Top = 120
      Width = 25
      Height = 25
      Hint = 'Zoom out'
      Caption = '-'
      OnClick = SpeedButton6Click
    end
  end
  object QRPreview1: TQRPreview
    Left = 29
    Top = 0
    Width = 549
    Height = 363
    HorzScrollBar.Tracking = True
    VertScrollBar.Tracking = True
    Align = alClient
    TabOrder = 1
    PageNumber = 1
    Zoom = 100
  end
  object Panel2: TPanel
    Left = 0
    Top = 363
    Width = 578
    Height = 20
    Align = alBottom
    BevelOuter = bvNone
    TabOrder = 2
  end
end

Then you have to Register the New Preview Class in your Application.mainform...

so in your Outlook STyle Form menu declare the type:

TQRSimplePreviewInterface = class(TQRPreviewInterface)
  public
    function Show(AQRPrinter : TQRPrinter) : TWinControl; override;
    function ShowModal(AQRPrinter : TQRPrinter): TWinControl; override;
  end;
in implementation:
function TQRSimplePreviewInterface.Show(AQRPrinter : TQRPrinter) : TWinControl;
begin
  Result := TPreviewForm.CreatePreview(Application, AQRPrinter);
  TPreviewForm(Result).Show;
end;

function TQRSimplePreviewInterface.ShowModal(AQRPrinter : TQRPrinter) : TWinControl;
begin
  Result := TPreviewForm.CreatePreview(Application, AQRPrinter);
  TPreviewForm(Result).ShowModal;
end;

Then Register the PreviewClass in Mainform.OnCreate...

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  // Calling RegisterPreviewClass with the custom
  // interface makes the custom preview the default
  // preview for all reports.
  RegisterPreviewClass(TQRSimplePreviewInterface);
end;


This is just an example but now you can use a preview Docked Form as you need...
Avatar of coracleit
coracleit

ASKER


Thanks for the reply, I did this way back in Delphi 1 days, but the way I did it back then no longer works.

I have plugged your example into my project. It compiles, once i realised i needed the qrprntr unit (in case anyone else needs this), but when I run it I get the following error:-

Exception EInvalidOperation in module blah blah
Cannot create form. No MDI forms are currently active.

If I remove the register line in the OnCreate I still get this.

Thanks in advance
Wayne
PS I am not using MDI forms.
Sorry, that's beacuse TPreviewForm FOrmSTyle is  FormStyle = fsMDIChild (it come's from a my application)...

Change it to fsNormal....
Cheers,

Its running now.  if I do myreport.preview the custom preview appears.

Probs, The preview form appears just before the main application appears and where do I put my manualdock code now to get the preview form to appear on the right.

Thanks
Wayne
Well, you can add it here
function TQRSimplePreviewInterface.Show(AQRPrinter : TQRPrinter) : TWinControl;
begin
  Result := TPreviewForm.CreatePreview(Application, AQRPrinter);
  TPreviewForm(Result).Show;
  TpreviewForm(result).ManualDock(PnlDock, nil,alNone);
end;

->  The preview form appears just before the main application appears
change the form creation order and availability by project options....

if you want send me you project to

ferruccio (at) afsoftware dot it
Thats where I thought. I had forgotten to put main.pnlDock as its not part of the main form.

Last question I hope.

How do I close it programatically, as I need to close my forms so that the next one can show correctly on the screen.

Close is not an option for a quick report.

Wayne
you must create, preview and close your reports exactly like you'd do with the original preview, so simply call
Let's say that you've a TQuickRep called FrmReport:

FrmReport := TFrmReport.create(nil);
FrmReport.Preview; //then simply close the preview form
FrmReport.free; //this frees the preview form
Although its possible to close the form myself, I need to do it in code.

I can create the report as your code above and it works, but .free will not remove it from the screen and .close is not an option.

Wayne
DO you mean that you need to close the report itself? I don't understand exactly this, as i think that if the report is in preview mode, then it'd be possible to print it or close it by some click. BTW you can use the TQuickRep events AfterPrint or AfterPreview calling Close there....
If you imagine my outlook bar on left.

When the user clicks on a report it appears in the right windows ... fine.

This must stay until the user clicks a new option on the left, ehich could be a setup screen.  The delphi code must close the preview form and show the setup form.

Wayne
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Smart....

Thanks for help