Changeset 455 for PrintPreview/UPrintPreview.pas
- Timestamp:
- Nov 28, 2012, 7:44:19 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
PrintPreview/UPrintPreview.pas
r266 r455 7 7 uses 8 8 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; 10 11 11 12 const … … 30 31 FOnNewPage: TNotifyEvent; 31 32 FOnPrint: TNotifyEvent; 32 FOnPrintFooter: TNotifyEvent;33 //FOnPrintFooter: TNotifyEvent; 33 34 FZoom: Double; 34 35 FPageNumber: Integer; … … 73 74 TPrintPreviewForm = class(TForm) 74 75 AClose: TAction; 76 AToolbarShowCaption: TAction; 77 AZoomFitToWidth: TAction; 78 AZoomFitToHeight: TAction; 75 79 ALastPage: TAction; 76 80 AFirstPage: TAction; … … 83 87 APrint: TAction; 84 88 ActionList1: TActionList; 85 Button1: TButton; 86 Button2: TButton; 87 Button3: TButton; 88 Button4: TButton; 89 ComboBoxZoom: TComboBox; 89 90 EditPageNumber: TEdit; 90 91 Image1: TImage; 91 92 ImageList1: TImageList; 93 MenuItem1: TMenuItem; 92 94 PageSetupDialog1: TPageSetupDialog; 95 Panel1: TPanel; 96 PopupMenuToolbar: TPopupMenu; 93 97 PrintDialog1: TPrintDialog; 94 98 PrinterSetupDialog1: TPrinterSetupDialog; … … 97 101 ToolBar1: TToolBar; 98 102 ToolButton1: TToolButton; 103 ToolButton10: TToolButton; 104 ToolButton11: TToolButton; 105 ToolButton12: TToolButton; 106 ToolButton13: TToolButton; 107 ToolButton14: TToolButton; 108 ToolButton15: TToolButton; 99 109 ToolButton2: TToolButton; 100 110 ToolButton3: TToolButton; … … 113 123 procedure APrinterSetupExecute(Sender: TObject); 114 124 procedure APrintExecute(Sender: TObject); 125 procedure AToolbarShowCaptionExecute(Sender: TObject); 126 procedure AZoomFitToHeightExecute(Sender: TObject); 127 procedure AZoomFitToWidthExecute(Sender: TObject); 115 128 procedure AZoomInExecute(Sender: TObject); 116 129 procedure AZoomOutExecute(Sender: TObject); 130 procedure ComboBoxZoomChange(Sender: TObject); 117 131 procedure EditPageNumberChange(Sender: TObject); 118 132 procedure FormCreate(Sender: TObject); … … 120 134 procedure FormResize(Sender: TObject); 121 135 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); 122 144 procedure ScrollBarHorizChange(Sender: TObject); 123 145 procedure ScrollBarVertChange(Sender: TObject); 124 146 private 147 DragStart: Boolean; 148 DragStartPos: TPoint; 149 DragScrollBarPos: TPoint; 125 150 FPrintPreview: TPrintPreview; 126 procedure ReloadPageNumber;151 procedure UpdateInterface; 127 152 public 128 153 PageNumber: Integer; 154 MinZoom: Double; 155 MaxZoom: Double; 156 procedure EraseBackground(DC: HDC); override; 129 157 procedure Redraw; 130 158 property PrintPreview: TPrintPreview read FPrintPreview … … 285 313 286 314 procedure TPrintPreview.Preview; 287 const288 DefaultMargin = 10;289 315 begin 290 316 if Assigned(FOnPrint) then begin … … 349 375 PrintDialog1.FromPage := 1; 350 376 PrintDialog1.ToPage := PrintPreview.PageCount; 351 if PrintDialog1.Execute then 377 if PrintDialog1.Execute then begin 352 378 if Assigned(FPrintPreview) then PrintPreview.Print; 379 end; 380 end; 381 382 procedure TPrintPreviewForm.AToolbarShowCaptionExecute(Sender: TObject); 383 begin 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; 394 end; 395 396 procedure TPrintPreviewForm.AZoomFitToHeightExecute(Sender: TObject); 397 begin 398 PrintPreview.Zoom := 1 / (Printer.PageHeight / SizeDivider / Image1.Height) * 0.95; 399 ScrollBarVert.Position := 0; 400 Redraw; 401 UpdateInterface; 402 end; 403 404 procedure TPrintPreviewForm.AZoomFitToWidthExecute(Sender: TObject); 405 begin 406 PrintPreview.Zoom := 1 / (Printer.PageWidth / SizeDivider / Image1.Width) * 0.95; 407 ScrollBarHoriz.Position := 0; 408 Redraw; 409 UpdateInterface; 353 410 end; 354 411 355 412 procedure TPrintPreviewForm.AZoomInExecute(Sender: TObject); 356 begin 357 PrintPreview.Zoom := PrintPreview.Zoom * 1.25; 358 Redraw; 413 var 414 NewZoom: Double; 415 begin 416 NewZoom := PrintPreview.Zoom * 1.25; 417 if NewZoom > MaxZoom then NewZoom := MaxZoom; 418 PrintPreview.Zoom := NewZoom; 419 Redraw; 420 UpdateInterface; 359 421 end; 360 422 361 423 procedure TPrintPreviewForm.AZoomOutExecute(Sender: TObject); 362 begin 363 PrintPreview.Zoom := PrintPreview.Zoom / 1.25; 364 EditPageNumberChange(Self); 365 Redraw; 424 var 425 NewZoom: Double; 426 begin 427 NewZoom := PrintPreview.Zoom / 1.25; 428 if NewZoom < MinZoom then NewZoom := MinZoom; 429 PrintPreview.Zoom := NewZoom; 430 Redraw; 431 UpdateInterface; 432 end; 433 434 procedure TPrintPreviewForm.ComboBoxZoomChange(Sender: TObject); 435 var 436 ZoomText: string; 437 NewZoomInt: Integer; 438 NewZoom: Double; 439 begin 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; 366 451 end; 367 452 … … 376 461 PageNumber := PrintPreview.PageCount - 1; 377 462 end; 378 ReloadPageNumber;463 UpdateInterface; 379 464 end; 380 465 381 466 procedure TPrintPreviewForm.FormCreate(Sender: TObject); 382 467 begin 383 DoubleBuffered := True; 468 //DoubleBuffered := True; 469 Panel1.DoubleBuffered := True; 470 AToolbarShowCaption.Checked := True; 471 MinZoom := 0.1; 472 MaxZoom := 3; 384 473 end; 385 474 … … 418 507 419 508 PrintDialog1.MaxPage := PrintPreview.Pages.Count; 420 ReloadPageNumber; 421 Redraw; 509 UpdateInterface; 510 Redraw; 511 end; 512 513 procedure TPrintPreviewForm.Image1MouseDown(Sender: TObject; 514 Button: TMouseButton; Shift: TShiftState; X, Y: Integer); 515 begin 516 DragStartPos := Point(X, Y); 517 DragStart := True; 518 DragScrollBarPos := Point(ScrollBarHoriz.Position, ScrollBarVert.Position); 519 end; 520 521 procedure TPrintPreviewForm.Image1MouseMove(Sender: TObject; 522 Shift: TShiftState; X, Y: Integer); 523 begin 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; 530 end; 531 532 procedure TPrintPreviewForm.Image1MouseUp(Sender: TObject; 533 Button: TMouseButton; Shift: TShiftState; X, Y: Integer); 534 begin 535 DragStart := False; 536 end; 537 538 procedure TPrintPreviewForm.Image1MouseWheel(Sender: TObject; 539 Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; 540 var Handled: Boolean); 541 begin 542 if WheelDelta > 0 then AZoomIn.Execute 543 else if WheelDelta < 0 then AZoomOut.Execute; 422 544 end; 423 545 … … 432 554 end; 433 555 434 procedure TPrintPreviewForm. ReloadPageNumber;556 procedure TPrintPreviewForm.UpdateInterface; 435 557 begin 436 558 EditPageNumber.Text := IntToStr(PageNumber); 437 559 Redraw; 438 560 ANextPage.Enabled := PageNumber < (PrintPreview.PageCount - 1); 561 ALastPage.Enabled := PageNumber < (PrintPreview.PageCount - 1); 439 562 APreviousPage.Enabled := PageNumber > 0; 563 AFirstPage.Enabled := PageNumber > 0; 564 ComboBoxZoom.Text := IntToStr(Trunc(PrintPreview.Zoom * 100)) + '%'; 565 end; 566 567 procedure TPrintPreviewForm.EraseBackground(DC: HDC); 568 begin 569 //inherited EraseBackground(DC); 440 570 end; 441 571 442 572 procedure TPrintPreviewForm.Redraw; 443 573 var 444 SourceRect: TRect;574 //SourceRect: TRect; 445 575 DestRect: TRect; 446 576 Page: TPrintPage; 577 PageSize: TPoint; 447 578 begin 448 579 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; 456 591 try 457 592 Image1.Picture.Bitmap.SetSize(Image1.Width, Image1.Height); 458 593 Image1.Picture.Bitmap.BeginUpdate(True); 459 594 with Image1.Picture.Bitmap, Canvas do begin 460 Brush.Color := cl Black;595 Brush.Color := clGray; 461 596 Brush.Style := bsSolid; 462 FillRect(Rect(0, 0, Width,Height));597 FillRect(Rect(0, 0, Image1.Picture.Bitmap.Width, Image1.Picture.Bitmap.Height)); 463 598 Draw(DestRect.Left, DestRect.Top, Page.Bitmap); 464 599 //CopyRect(DestRect, Page.Bitmap.Canvas, SourceRect); … … 485 620 Round(PageSetupDialog1.Margins.Bottom / 100)); 486 621 UpdateMargins; 622 PrintPreview.Preview; 487 623 Redraw; 488 624 end; … … 494 630 Dec(PageNumber); 495 631 if PageNumber < 0 then PageNumber := 0; 496 ReloadPageNumber;632 UpdateInterface; 497 633 end; 498 634 … … 502 638 if PageNumber >= PrintPreview.PageCount then 503 639 PageNumber := PrintPreview.PageCount; 504 ReloadPageNumber;640 UpdateInterface; 505 641 end; 506 642 … … 508 644 begin 509 645 PageNumber := PrintPreview.PageCount - 1; 510 ReloadPageNumber;646 UpdateInterface; 511 647 end; 512 648 … … 514 650 begin 515 651 PageNumber := 0; 516 ReloadPageNumber;652 UpdateInterface; 517 653 end; 518 654
Note:
See TracChangeset
for help on using the changeset viewer.