Ignore:
Timestamp:
Nov 28, 2012, 7:44:19 AM (12 years ago)
Author:
chronos
Message:
  • Modified: PrintPreview should use TVectorCanvas instead of raster TCanvas dependent on DPI of printer. But some font function as TextWidth and TextHeight cannot be easily implemented without use of OS API.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • PrintPreview/UPrintPreview.pas

    r266 r455  
    77uses
    88  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
    9   ComCtrls, ExtCtrls, ActnList, PrintersDlgs, Contnrs, Printers, StdCtrls;
     9  ComCtrls, ExtCtrls, ActnList, PrintersDlgs, Contnrs, Printers, StdCtrls,
     10  Menus, LCLType, types;
    1011
    1112const
     
    3031    FOnNewPage: TNotifyEvent;
    3132    FOnPrint: TNotifyEvent;
    32     FOnPrintFooter: TNotifyEvent;
     33    //FOnPrintFooter: TNotifyEvent;
    3334    FZoom: Double;
    3435    FPageNumber: Integer;
     
    7374  TPrintPreviewForm = class(TForm)
    7475    AClose: TAction;
     76    AToolbarShowCaption: TAction;
     77    AZoomFitToWidth: TAction;
     78    AZoomFitToHeight: TAction;
    7579    ALastPage: TAction;
    7680    AFirstPage: TAction;
     
    8387    APrint: TAction;
    8488    ActionList1: TActionList;
    85     Button1: TButton;
    86     Button2: TButton;
    87     Button3: TButton;
    88     Button4: TButton;
     89    ComboBoxZoom: TComboBox;
    8990    EditPageNumber: TEdit;
    9091    Image1: TImage;
    9192    ImageList1: TImageList;
     93    MenuItem1: TMenuItem;
    9294    PageSetupDialog1: TPageSetupDialog;
     95    Panel1: TPanel;
     96    PopupMenuToolbar: TPopupMenu;
    9397    PrintDialog1: TPrintDialog;
    9498    PrinterSetupDialog1: TPrinterSetupDialog;
     
    97101    ToolBar1: TToolBar;
    98102    ToolButton1: TToolButton;
     103    ToolButton10: TToolButton;
     104    ToolButton11: TToolButton;
     105    ToolButton12: TToolButton;
     106    ToolButton13: TToolButton;
     107    ToolButton14: TToolButton;
     108    ToolButton15: TToolButton;
    99109    ToolButton2: TToolButton;
    100110    ToolButton3: TToolButton;
     
    113123    procedure APrinterSetupExecute(Sender: TObject);
    114124    procedure APrintExecute(Sender: TObject);
     125    procedure AToolbarShowCaptionExecute(Sender: TObject);
     126    procedure AZoomFitToHeightExecute(Sender: TObject);
     127    procedure AZoomFitToWidthExecute(Sender: TObject);
    115128    procedure AZoomInExecute(Sender: TObject);
    116129    procedure AZoomOutExecute(Sender: TObject);
     130    procedure ComboBoxZoomChange(Sender: TObject);
    117131    procedure EditPageNumberChange(Sender: TObject);
    118132    procedure FormCreate(Sender: TObject);
     
    120134    procedure FormResize(Sender: TObject);
    121135    procedure FormShow(Sender: TObject);
     136    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
     137      Shift: TShiftState; X, Y: Integer);
     138    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
     139      );
     140    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
     141      Shift: TShiftState; X, Y: Integer);
     142    procedure Image1MouseWheel(Sender: TObject; Shift: TShiftState;
     143      WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
    122144    procedure ScrollBarHorizChange(Sender: TObject);
    123145    procedure ScrollBarVertChange(Sender: TObject);
    124146  private
     147    DragStart: Boolean;
     148    DragStartPos: TPoint;
     149    DragScrollBarPos: TPoint;
    125150    FPrintPreview: TPrintPreview;
    126     procedure ReloadPageNumber;
     151    procedure UpdateInterface;
    127152  public
    128153    PageNumber: Integer;
     154    MinZoom: Double;
     155    MaxZoom: Double;
     156    procedure EraseBackground(DC: HDC); override;
    129157    procedure Redraw;
    130158    property PrintPreview: TPrintPreview read FPrintPreview
     
    285313
    286314procedure TPrintPreview.Preview;
    287 const
    288   DefaultMargin = 10;
    289315begin
    290316  if Assigned(FOnPrint) then begin
     
    349375  PrintDialog1.FromPage := 1;
    350376  PrintDialog1.ToPage := PrintPreview.PageCount;
    351   if PrintDialog1.Execute then
     377  if PrintDialog1.Execute then begin
    352378    if Assigned(FPrintPreview) then PrintPreview.Print;
     379  end;
     380end;
     381
     382procedure TPrintPreviewForm.AToolbarShowCaptionExecute(Sender: TObject);
     383begin
     384  AToolbarShowCaption.Checked := not AToolbarShowCaption.Checked;
     385  if AToolbarShowCaption.Checked then begin
     386    ToolBar1.ButtonHeight := 42;
     387    ToolBar1.ButtonWidth := 42;
     388    ToolBar1.ShowCaptions := True;
     389  end else begin
     390    ToolBar1.ButtonHeight := 22;
     391    ToolBar1.ButtonWidth := 23;
     392    ToolBar1.ShowCaptions := False;
     393  end;
     394end;
     395
     396procedure TPrintPreviewForm.AZoomFitToHeightExecute(Sender: TObject);
     397begin
     398  PrintPreview.Zoom := 1 / (Printer.PageHeight / SizeDivider / Image1.Height) * 0.95;
     399  ScrollBarVert.Position := 0;
     400  Redraw;
     401  UpdateInterface;
     402end;
     403
     404procedure TPrintPreviewForm.AZoomFitToWidthExecute(Sender: TObject);
     405begin
     406  PrintPreview.Zoom := 1 / (Printer.PageWidth / SizeDivider / Image1.Width) * 0.95;
     407  ScrollBarHoriz.Position := 0;
     408  Redraw;
     409  UpdateInterface;
    353410end;
    354411
    355412procedure TPrintPreviewForm.AZoomInExecute(Sender: TObject);
    356 begin
    357   PrintPreview.Zoom := PrintPreview.Zoom * 1.25;
    358   Redraw;
     413var
     414  NewZoom: Double;
     415begin
     416  NewZoom := PrintPreview.Zoom * 1.25;
     417  if NewZoom > MaxZoom then NewZoom := MaxZoom;
     418  PrintPreview.Zoom := NewZoom;
     419  Redraw;
     420  UpdateInterface;
    359421end;
    360422
    361423procedure TPrintPreviewForm.AZoomOutExecute(Sender: TObject);
    362 begin
    363   PrintPreview.Zoom := PrintPreview.Zoom / 1.25;
    364   EditPageNumberChange(Self);
    365   Redraw;
     424var
     425  NewZoom: Double;
     426begin
     427  NewZoom := PrintPreview.Zoom / 1.25;
     428  if NewZoom < MinZoom then NewZoom := MinZoom;
     429  PrintPreview.Zoom := NewZoom;
     430  Redraw;
     431  UpdateInterface;
     432end;
     433
     434procedure TPrintPreviewForm.ComboBoxZoomChange(Sender: TObject);
     435var
     436  ZoomText: string;
     437  NewZoomInt: Integer;
     438  NewZoom: Double;
     439begin
     440  ZoomText := Trim(ComboBoxZoom.Text);
     441  if Pos('%', ZoomText) > 0 then
     442    ZoomText := Copy(ZoomText, 1, Pos('%', ZoomText) - 1);
     443  if TryStrToInt(ZoomText, NewZoomInt) then begin
     444    NewZoom := NewZoomInt / 100;
     445    if NewZoom < MinZoom then NewZoom := MinZoom;
     446    if NewZoom > MaxZoom then NewZoom := MaxZoom;
     447    PrintPreview.Zoom := NewZoom;
     448    EditPageNumberChange(Self);
     449    Redraw;
     450  end;
    366451end;
    367452
     
    376461      PageNumber := PrintPreview.PageCount - 1;
    377462  end;
    378   ReloadPageNumber;
     463  UpdateInterface;
    379464end;
    380465
    381466procedure TPrintPreviewForm.FormCreate(Sender: TObject);
    382467begin
    383   DoubleBuffered := True;
     468  //DoubleBuffered := True;
     469  Panel1.DoubleBuffered := True;
     470  AToolbarShowCaption.Checked := True;
     471  MinZoom := 0.1;
     472  MaxZoom := 3;
    384473end;
    385474
     
    418507
    419508  PrintDialog1.MaxPage := PrintPreview.Pages.Count;
    420   ReloadPageNumber;
    421   Redraw;
     509  UpdateInterface;
     510  Redraw;
     511end;
     512
     513procedure TPrintPreviewForm.Image1MouseDown(Sender: TObject;
     514  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
     515begin
     516  DragStartPos := Point(X, Y);
     517  DragStart := True;
     518  DragScrollBarPos := Point(ScrollBarHoriz.Position, ScrollBarVert.Position);
     519end;
     520
     521procedure TPrintPreviewForm.Image1MouseMove(Sender: TObject;
     522  Shift: TShiftState; X, Y: Integer);
     523begin
     524  if DragStart then begin
     525    ScrollBarHoriz.Position := DragScrollBarPos.X - Trunc((X - DragStartPos.x) *
     526      (ScrollBarHoriz.Max - ScrollBarHoriz.Min) / Width / PrintPreview.Zoom);
     527    ScrollBarVert.Position := DragScrollBarPos.Y - Trunc((Y - DragStartPos.Y) *
     528      (ScrollBarVert.Max - ScrollBarVert.Min) / Height / PrintPreview.Zoom);
     529  end;
     530end;
     531
     532procedure TPrintPreviewForm.Image1MouseUp(Sender: TObject;
     533  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
     534begin
     535  DragStart := False;
     536end;
     537
     538procedure TPrintPreviewForm.Image1MouseWheel(Sender: TObject;
     539  Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
     540  var Handled: Boolean);
     541begin
     542  if WheelDelta > 0 then AZoomIn.Execute
     543  else if WheelDelta < 0 then AZoomOut.Execute;
    422544end;
    423545
     
    432554end;
    433555
    434 procedure TPrintPreviewForm.ReloadPageNumber;
     556procedure TPrintPreviewForm.UpdateInterface;
    435557begin
    436558  EditPageNumber.Text := IntToStr(PageNumber);
    437559  Redraw;
    438560  ANextPage.Enabled := PageNumber < (PrintPreview.PageCount - 1);
     561  ALastPage.Enabled := PageNumber < (PrintPreview.PageCount - 1);
    439562  APreviousPage.Enabled := PageNumber > 0;
     563  AFirstPage.Enabled := PageNumber > 0;
     564  ComboBoxZoom.Text := IntToStr(Trunc(PrintPreview.Zoom * 100)) + '%';
     565end;
     566
     567procedure TPrintPreviewForm.EraseBackground(DC: HDC);
     568begin
     569  //inherited EraseBackground(DC);
    440570end;
    441571
    442572procedure TPrintPreviewForm.Redraw;
    443573var
    444   SourceRect: TRect;
     574  //SourceRect: TRect;
    445575  DestRect: TRect;
    446576  Page: TPrintPage;
     577  PageSize: TPoint;
    447578begin
    448579  Page := TPrintPage(PrintPreview.Pages[PageNumber]);
    449   SourceRect := Rect(0, 0,
    450     Page.Bitmap.Canvas.Width,
    451     Page.Bitmap.Canvas.Height);
    452   DestRect.Left := -Round(ScrollBarHoriz.Position / ScrollBarHoriz.Max * Width * PrintPreview.Zoom);
    453   DestRect.Top := -Round(ScrollBarVert.Position / ScrollBarVert.Max * Height * PrintPreview.Zoom);
    454   DestRect.Right := DestRect.Left + Round(Page.Bitmap.Canvas.Width * PrintPreview.Zoom);
    455   DestRect.Bottom := DestRect.Top + Round(Page.Bitmap.Canvas.Height * PrintPreview.Zoom);
     580  //SourceRect := Rect(0, 0,
     581  //  Page.Bitmap.Canvas.Width,
     582  //  Page.Bitmap.Canvas.Height);
     583  PageSize := Point(Round(Page.Bitmap.Canvas.Width),
     584    Round(Page.Bitmap.Canvas.Height));
     585  DestRect.Left := (Image1.Width - PageSize.X) div 2 - Round(ScrollBarHoriz.Position /
     586    (ScrollBarHoriz.Max - ScrollBarHoriz.Min) * Image1.Width * PrintPreview.Zoom);
     587  DestRect.Top := (Image1.Height - PageSize.Y) div 2 - Round(ScrollBarVert.Position /
     588    (ScrollBarVert.Max - ScrollBarVert.Min) * Image1.Height * PrintPreview.Zoom);
     589  DestRect.Right := DestRect.Left + PageSize.X;
     590  DestRect.Bottom := DestRect.Top + PageSize.Y;
    456591  try
    457592    Image1.Picture.Bitmap.SetSize(Image1.Width, Image1.Height);
    458593    Image1.Picture.Bitmap.BeginUpdate(True);
    459594    with Image1.Picture.Bitmap, Canvas do begin
    460       Brush.Color := clBlack;
     595      Brush.Color := clGray;
    461596      Brush.Style := bsSolid;
    462       FillRect(Rect(0, 0, Width, Height));
     597      FillRect(Rect(0, 0, Image1.Picture.Bitmap.Width, Image1.Picture.Bitmap.Height));
    463598      Draw(DestRect.Left, DestRect.Top, Page.Bitmap);
    464599      //CopyRect(DestRect, Page.Bitmap.Canvas, SourceRect);
     
    485620        Round(PageSetupDialog1.Margins.Bottom / 100));
    486621      UpdateMargins;
     622      PrintPreview.Preview;
    487623      Redraw;
    488624    end;
     
    494630  Dec(PageNumber);
    495631  if PageNumber < 0 then PageNumber := 0;
    496   ReloadPageNumber;
     632  UpdateInterface;
    497633end;
    498634
     
    502638  if PageNumber >= PrintPreview.PageCount then
    503639    PageNumber := PrintPreview.PageCount;
    504   ReloadPageNumber;
     640  UpdateInterface;
    505641end;
    506642
     
    508644begin
    509645  PageNumber := PrintPreview.PageCount - 1;
    510   ReloadPageNumber;
     646  UpdateInterface;
    511647end;
    512648
     
    514650begin
    515651  PageNumber := 0;
    516   ReloadPageNumber;
     652  UpdateInterface;
    517653end;
    518654
Note: See TracChangeset for help on using the changeset viewer.