Quickreport: Controlling Previews

Controlling the preview.

This article shows how to alter the properties of the preview window AFTER it is created and displayed.

Sample code -

Controlling a preview using a timer.
This code snippet shows how the preview form may be controlled using a timer to to fire API calls that can re-size, or minimise and maximise a showing preview. The zoom state and other properties may also be set

The app has 2 forms, Mainform and RepForm which has a quickrep called QuickRep1. The code is rather bare - but the principles are illustrated.

The most important thing is that this -

repform.QuickRep1.QRPrinter.Client.Parent.Handle

is the handle of the preview window.

This button on the main form kicks-off the preview and sets the timer on mainform. Set the interval of the timer to 50000 in design mode.

procedure TMainform.SpeedButton1Click(Sender: TObject);
begin
    timer1.enabled := false;
    repform.Table1.open;
    // this can be as small as you like - provided the
    // tick routine checks that the preview is showing
    timer1.Interval := 3000; // millisecs
    timer1.enabled := true;
    repform.QuickRep1.Preview;
    repform.Table1.close;
end;

Reposition and/or resize the window. See WinAPI help for the possibilities of the SetWindowPlacement call.

procedure Tmainform.Timer1Timer(Sender: TObject);
var
    pStru : WINDOWPLACEMENT;
begin
    // here we should check if the preview is showing
    // and exit if not. A delay of 1 or 2 seconds is
    // enough usually to allow this.
    // This test might fail - but it hasn't yet
    if not assigned(repform.QuickRep1.QRPrinter) then exit;

    timer1.enabled := false; // we've finished with the timer.

    // Doing it the easy way. Resetting the WindowState may not be
    // necessary.
    TForm( repform.QuickRep1.QRPrinter.Client.parent).WindowState := wsNormal;
    repform.QuickRep1.QRPrinter.Client.parent.width := 800;
    repform.QuickRep1.QRPrinter.Client.parent.height := 600;
    repform.QuickRep1.QRPrinter.Client.parent.top := 50;
    repform.QuickRep1.QRPrinter.Client.parent.left := 50;
    exit;

    // How to do it with API calls. This way is smoother.
    // set the Window position structure
    pstru.length := sizeof( WINDOWPLACEMENT);// mandatory
    pstru.showCmd := SW_RESTORE; // could be SW_SHOWMAXIMIZED etc.
    pstru.flags := WPF_SETMINPOSITION;
    // set the rect in screen pixels for a move/resize
    pstru.rcNormalPosition := rect( 100, 100, 850, 700 );
    SetWindowPlacement( repform.QuickRep1.QRPrinter.Client.parent.Handle, @pstru );
end;

Setting the toolbar buttons

Use a longer time delay to do this - because the buttons are reset in the QR code when the report is done.

This code may be put in the AfterPrint event of the report and the timer given  a short interval.

// add some vars in the proc above
var
    prevFrm : TForm;
    tBar, tBut, tPrev : TComponent;
..
..
    // this code goes into the proc above
    // find the buttons and set them
    prevfrm := TForm( repform.QuickRep1.QRPrinter.Client.parent);
    for j := 0 to prevfrm.controlcount-1 do
    begin
        tBar := prevfrm.controls[ j ];
        if tBar is TToolbar then
        begin
            // Vanish the toolbar !!
            // TToolbar(tBar).visible := false;
            // break; // if vanishing
            for k := 0 to TToolbar(tBar).controlcount-1 do
            begin
                tBut := TToolbar(tBar).controls[ k ];
                if tBut is TToolbutton then // could test the name here
                TToolbutton(tBut).enabled := false;
            end;
    end;
end;

Setting the preview component properties

// insert into the procedure above

// set preview component
tPrev := repform.QuickRep1.QRPrinter.Client;
TQRPreview(tPrev).ZoomToWidth;
TQRPreview(tPrev).Zoom := 80;
TQRPreview(tPrev).color := clRed;