📄 pstoolbar.pas
字号:
(* GREATIS PRINT SUITE *)
(* unit version 1.85.010 *)
(* Copyright (C) 2001-2007 Greatis Software *)
(* http://www.greatis.com/delphicb/printsuite/ *)
(* http://www.greatis.com/delphicb/printsuite/faq/ *)
(* http://www.greatis.com/bteam.html *)
unit PSToolbar;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, Buttons, StdCtrls, PSPreview, PSCombo, PSCommon;
type
TButtonKind = (
btnPrint,
btnPrintDialog,
btnPrinterSetupDialog,
btnFirstPage,
btnPrevPage,
btnNextPage,
btnLastPage,
btnWholePage,
btnPageWidth,
btnZoomOut,
btnZoomIn,
btnRefresh,
btnClose,
btnUser);
TButtons = set of TButtonKind;
TButtonClickEvent = procedure (Sender: TObject; Button: TButtonKind; var AllowDefault: Boolean) of object;
TCustomPreviewToolbar = class(TCustomPanel)
private
{ Private declarations }
FPreview: TCustomPreview;
FBitmaps: TBitmap;
FButtonsArray: array[TButtonKind] of TSpeedButton;
FButtons: TButtons;
FFlat: Boolean;
FScaleCombo: TPreviewComboBox;
FScaleBox: Boolean;
FScaleBoxPos: TButtonKind;
FUserButtonEnabled: Boolean;
FUserButtonGlyph: TBitmap;
FUserButtonHint: string;
FOnUpdate: TNotifyEvent;
FOnUserButtonClick: TNotifyEvent;
FOnButtonClick: TButtonClickEvent;
procedure SetPreview(const Value: TCustomPreview);
procedure SetButtons(const Value: TButtons);
procedure SetFlat(const Value: Boolean);
procedure SetScaleBox(const Value: Boolean);
procedure SetScaleBoxPos(const Value: TButtonKind);
procedure SetUserButtonEnabled(const Value: Boolean);
procedure SetUserButtonGlyph(const Value: TBitmap);
procedure SetUserButtonHint(const Value: string);
procedure GetBitmapPart(Bitmap: TBitmap; Index: Integer);
function GetButtonLeft(Button: TButtonKind): Integer;
procedure OnButtonClickEvent(Sender: TObject);
protected
{ Protected declarations }
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
property Preview: TCustomPreview read FPreview write SetPreview;
property Buttons: TButtons read FButtons write SetButtons default [btnPrint,btnPrinterSetupDialog,btnFirstPage,btnPrevPage,btnNextPage,btnLastPage,btnZoomOut,btnZoomIn];
property Flat: Boolean read FFlat write SetFlat default True;
property ScaleBox: Boolean read FScaleBox write SetScaleBox default True;
property ScaleBoxPos: TButtonKind read FScaleBoxPos write SetScaleBoxPos default btnZoomOut;
property UserButtonEnabled: Boolean read FUserButtonEnabled write SetUserButtonEnabled default True;
property UserButtonGlyph: TBitmap read FUserButtonGlyph write SetUserButtonGlyph;
property UserButtonHint: string read FUserButtonHint write SetUserButtonHint;
property OnUpdate: TNotifyEvent read FOnUpdate write FOnUpdate;
property OnUserButtonClick: TNotifyEvent read FOnUserButtonClick write FOnUserButtonClick;
property OnButtonClick: TButtonClickEvent read FOnButtonClick write FOnButtonClick;
property Align default alTop;
property ParentShowHint;
property ShowHint;
property Visible;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Update; override;
procedure UpdateButtons;
procedure EnableButtons;
procedure ButtonClick(Button: TButtonKind); virtual;
end;
TPreviewToolbar = class(TCustomPreviewToolbar)
published
{ Published declarations }
property Preview;
property Buttons;
property Flat;
property ScaleBox;
property ScaleBoxPos;
property UserButtonEnabled;
property UserButtonGlyph;
property UserButtonHint;
property OnUpdate;
property OnUserButtonClick;
property OnButtonClick;
property Align;
property ParentShowHint;
property ShowHint;
property Visible;
end;
procedure Register;
implementation
{$R PSTOOLBAR.RES}
const
ButtonSize = 24;
GlyphSize = 16;
{$IFDEF PSTRIAL}
EnableZoom = False;
{$ELSE}
EnableZoom = True;
{$ENDIF}
procedure TCustomPreviewToolbar.SetPreview(const Value: TCustomPreview);
begin
if Value<>FPreview then
begin
if Assigned(FPreview) then FPreview.DeleteControlNotification(Self);
FPreview:=Value;
if Assigned(FPreview) then FPreview.AddControlNotification(Self);
FScaleCombo.Preview:=FPreview;
UpdateButtons;
end;
end;
procedure TCustomPreviewToolbar.SetButtons(const Value: TButtons);
begin
if Value<>FButtons then
begin
FButtons:=Value;
UpdateButtons;
end;
end;
procedure TCustomPreviewToolbar.SetFlat(const Value: Boolean);
var
B: TButtonKind;
begin
if Value<>FFlat then
begin
FFlat:=Value;
for B:=Low(TButtonKind) to High(TButtonKind) do
FButtonsArray[B].Flat:=FFlat;
end;
end;
procedure TCustomPreviewToolbar.SetScaleBox(const Value: Boolean);
begin
if Value<>FScaleBox then
begin
FScaleBox:=Value;
UpdateButtons;
end;
end;
procedure TCustomPreviewToolbar.SetScaleBoxPos(const Value: TButtonKind);
begin
if Value<>FScaleBoxPos then
begin
FScaleBoxPos:=Value;
UpdateButtons;
end;
end;
procedure TCustomPreviewToolbar.SetUserButtonEnabled(const Value: Boolean);
begin
if Value<>FUserButtonEnabled then
begin
FUserButtonEnabled:=Value;
FButtonsArray[btnUser].Enabled:=FUserButtonEnabled;
end;
end;
procedure TCustomPreviewToolbar.SetUserButtonGlyph(const Value: TBitmap);
begin
FUserButtonGlyph.Assign(Value);
FButtonsArray[btnUser].Glyph:=FUserButtonGlyph;
end;
procedure TCustomPreviewToolbar.SetUserButtonHint(const Value: string);
begin
if Value<>FUserButtonHint then
begin
FUserButtonHint:=Value;
FButtonsArray[btnUser].Hint:=FUserButtonHint;
end;
end;
procedure TCustomPreviewToolbar.GetBitmapPart(Bitmap: TBitmap; Index: Integer);
begin
with Bitmap,Canvas do
begin
CopyMode:=cmSrcCopy;
Width:=GlyphSize;
Height:=GlyphSize;
Bitmap.Canvas.CopyRect(
Rect(0,0,GlyphSize,GlyphSize),
FBitmaps.Canvas,
Rect(Index*GlyphSize,0,Succ(Index)*GlyphSize,GlyphSize));
end;
end;
function TCustomPreviewToolbar.GetButtonLeft(Button: TButtonKind): Integer;
begin
Result:=Succ(Integer(Button)*ButtonSize);
end;
procedure TCustomPreviewToolbar.Update;
begin
inherited;
EnableButtons;
if Assigned(FOnUpdate) then FOnUpdate(Self);
end;
procedure TCustomPreviewToolbar.UpdateButtons;
var
V: Integer;
B: TButtonKind;
begin
V:=0;
for B:=Low(TButtonKind) to High(TButtonKind) do
begin
FScaleCombo.Visible:=FScaleBox;
with FButtonsArray[B] do
begin
Visible:=(B in FButtons) or (csDesigning in ComponentState);
if Visible then
begin
Left:=GetButtonLeft(TButtonKind(V));
Inc(V);
end;
if (B>=FScaleBoxPos) and (FScaleBox or (csDesigning in ComponentState)) then
Left:=Left+FScaleCombo.Width+4;
end;
if (B=FScaleBoxPos) and (FScaleBox or (csDesigning in ComponentState)) then
FScaleCombo.Left:=GetButtonLeft(TButtonKind(Pred(V)))+2;
end;
FButtonsArray[btnUser].Glyph:=FUserButtonGlyph;
EnableButtons;
end;
procedure TCustomPreviewToolbar.EnableButtons;
var
E: Boolean;
begin
E:=Assigned(Preview) and
Assigned(Preview.PrintJob) and
Preview.PrintJob.PrinterOK and not (csDesigning in ComponentState);
if E then
with Preview,PrintJob do
begin
FButtonsArray[btnFirstPage].Enabled:=PageIndex>1;
FButtonsArray[btnPrevPage].Enabled:=PageIndex>1;
FButtonsArray[btnNextPage].Enabled:=PageIndex<PageCount;
FButtonsArray[btnLastPage].Enabled:=PageIndex<PageCount;
FButtonsArray[btnZoomOut].Enabled:=(ViewScale>ScaleArray[Low(ScaleArray)]) and EnableZoom;
FButtonsArray[btnZoomIn].Enabled:=(ViewScale<ScaleArray[High(ScaleArray)]) and EnableZoom;
FScaleCombo.Enabled:=EnableZoom;
end;
end;
procedure TCustomPreviewToolbar.OnButtonClickEvent(Sender: TObject);
begin
ButtonClick(TButtonKind((Sender as TSpeedButton).Tag));
end;
procedure TCustomPreviewToolbar.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if (Operation=opRemove) and Assigned(FPreview) and (AComponent=FPreview) then
Preview:=nil;
end;
constructor TCustomPreviewToolbar.Create(AOwner: TComponent);
var
B: TButtonKind;
begin
inherited;
ControlStyle:=ControlStyle-[csSetCaption];
Height:=26;
Align:=alTop;
BevelInner:=bvNone;
BevelOuter:=bvNone;
FBitmaps:=TBitmap.Create;
FBitmaps.LoadFromResourceName(HInstance,'SMALL');
FScaleBox:=True;
FUserButtonEnabled:=True;
FUserButtonGlyph:=TBitmap.Create;
FButtons:=[btnPrint,btnPrinterSetupDialog,btnFirstPage,btnPrevPage,btnNextPage,btnLastPage,btnZoomIn,btnZoomOut];
FScaleBoxPos:=btnZoomOut;
Caption:='';
for B:=Low(TButtonKind) to High(TButtonKind) do
begin
FButtonsArray[B]:=TSpeedButton.Create(Self);
with FButtonsArray[B] do
begin
Margin:=-1;
Width:=ButtonSize;
Height:=ButtonSize;
Top:=1;
if B<btnUser then GetBitmapPart(Glyph,Integer(B));
Tag:=Integer(B);
ParentShowHint:=True;
if B<btnUser then Hint:=HintArray[Integer(B)];
OnClick:=OnButtonClickEvent;
Parent:=Self;
end;
if B=FScaleBoxPos then
begin
FScaleCombo:=TPreviewComboBox.Create(Self);
with FScaleCombo do
begin
Top:=(Self.Height-Height) div 2;
Hint:=strScaleComboHint;
Parent:=Self;
end;
end;
end;
Flat:=True;
UpdateButtons;
end;
destructor TCustomPreviewToolbar.Destroy;
begin
FBitmaps.Free;
FUserButtonGlyph.Free;
if Assigned(FPreview) then FPreview.DeleteControlNotification(Self);
inherited;
end;
procedure TCustomPreviewToolbar.ButtonClick(Button: TButtonKind);
var
AllowDefault: Boolean;
begin
AllowDefault:=True;
if Assigned(FOnButtonClick) then FOnButtonClick(Self,Button,AllowDefault);
if AllowDefault then
begin
case Button of
btnUser:
if Assigned(FOnUserButtonClick) then FOnUserButtonClick(Self);
btnClose:
if Owner is TCustomForm then TCustomForm(Owner).Close;
end;
if Assigned(Preview) and Assigned(Preview.PrintJob) then
with Preview,PrintJob do
case Button of
btnPrint: Print;
btnPrintDialog: PrintDialog;
btnPrinterSetupDialog: PrinterSetupDialog;
btnFirstPage: PageIndex:=1;
btnPrevPage: PageIndex:=PageIndex-1;
btnNextPage: PageIndex:=PageIndex+1;
btnLastPage: PageIndex:=PageCount;
btnWholePage: ViewMode:=vmWholePage;
btnPageWidth: ViewMode:=vmPageWidth;
btnZoomOut: ZoomOut;
btnZoomIn: ZoomIn;
btnRefresh: Update;
end;
end;
EnableButtons;
end;
procedure Register;
begin
RegisterComponents('Print Suite', [TPreviewToolbar]);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -