⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dialogex.pas

📁 数据备份与恢复源码,给需要的一个参考,可以直接运行
💻 PAS
字号:
unit DialogEx;

interface

uses
  Windows, Classes, Sysutils, Controls, Dialogs, Comctrls, Stdctrls, Extctrls,
  Graphics, CommDlg, Forms;

type

  TOpenPreviewDialog = class;

  TPreviewNotify = procedure(Sender: TOpenPreviewDialog; FileName: string) of object;

  TOpenPreviewDialog = class(TOpenDialog)
  private
    FScrollBox: TScrollBox;
    FPreviewText: TRichEdit;
    FHintPanel: TPanel;
    FHintLabel: TLabel;
    FOnPreview: TPreviewNotify;
    function GetHint: string;
    procedure SetHint(value: string);
  protected
    procedure DoShow; override;
    procedure DoClose; override;
    procedure DoSelectionChange; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function Execute: Boolean; override;
    property Hint: string read GetHint write SetHint;
    property PreviewText: TRichEdit read FPreviewText write FPreviewText;
    property OnPreview: TPreviewNotify read FOnPreview write FOnPreview;
  end;

procedure Register;

implementation

{$R DialogEx.Res}

constructor TOpenPreviewDialog.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Options := Options - [ofEnableSizing];
  FScrollBox := TScrollBox.Create(Self);
  with FScrollBox do
  begin
    Name := 'ScrollBox';
    Ctl3D := True;
    BorderStyle := bsSingle;
    FPreviewText := TRichEdit.Create(Self);
    with FPreviewText do
    begin
      Parent := FScrollBox;
      Align := alClient;
      Alignment := taLeftJustify;
      ScrollBars := ssBoth;
      BorderStyle := bsNone;
      WantReturns := False;
      ReadOnly := True;
      Font.Charset := GB2312_CHARSET;
      Font.Name := '宋体';
      Font.Size := 9;
    end;
  end;

  FHintPanel := TPanel.Create(Self);
  with FHintPanel do
  begin
    Name := 'HintPanel';
    Caption := '';
    Ctl3D := False;
    BevelOuter := bvNone;
    BevelInner := bvNone;
    BorderStyle := bsNone;
    BorderWidth := 0;
    FHintLabel := TLabel.Create(Self);
    with FHintLabel do
    begin
      Parent := FHintPanel;
      Name := 'HintLabel';
      Caption := '';
      Align := alClient;
      AutoSize := False;
      LayOut := tlCenter;
      Font.Assign(FPreviewText.Font);
      Font.Style := Font.Style + [fsBold];
    end;
  end;
end;

destructor TOpenPreviewDialog.Destroy;
begin
  FHintLabel.Free;
  FHintPanel.Free;
  FPreviewText.Free;
  FScrollBox.Free;
  inherited Destroy;
end;

procedure TOpenPreviewDialog.DoShow;
var
  PreviewRect, LabelRect, StaticRect: TRect;
begin
  inherited;
  GetClientRect(Handle, PreviewRect);
  StaticRect := GetStaticRect;
  with PreviewRect do
  begin
    Left := StaticRect.Right;
    Right := Right - 8;
    Top := Top + 35;
    Bottom := Bottom - 8;
  end;

  with FScrollBox do
  begin
    BringToFront;
    BoundsRect := PreviewRect;
    Color := clBtnFace;
    ParentFont := True;
  end;
  FScrollBox.ParentWindow := Handle;

  GetClientRect(Handle, LabelRect);
  LabelRect.Left := PreviewRect.Left;
  LabelRect.Right := PreviewRect.Right;
  LabelRect.Bottom := PreviewRect.Top - 1;
  FHintPanel.BoundsRect := LabelRect;
  FHintPanel.ParentWindow := Handle;

  with FPreviewText do
  begin
    Color := clWhite;
    Visible := True;
    Clear;
    Text := '';
    ParaGraph.Alignment := taLeftJustify;
  end;
end;

function TOpenPreviewDialog.Execute: Boolean;
begin
  if ofOldStyleDialog in Options then Options := Options - [ofOldStyleDialog];
  Template := 'EXTEMPLATE';
  Result := inherited Execute;
end;

procedure TOpenPreviewDialog.DoClose;
begin
  inherited DoClose;
  Application.HideHint;
end;

procedure TOpenPreviewDialog.DoSelectionChange;
var
  FullName: string;
  function ValidFile(const FileName: string): Boolean;
  begin
    Result := GetFileAttributes(PChar(FileName)) <> $FFFFFFFF;
  end;
begin
  inherited DoSelectionChange;
  FPreviewText.Lines.Clear;
  FullName := FileName;
  if FileExists(FullName) and ValidFile(FullName) and Assigned(FOnPreview) then
  begin
    try
      FOnPreview(Self, FullName);
    except
    end;
  end;
  FPreviewText.Invalidate;
end;

function TOpenPreviewDialog.GetHint: string;
begin
  Result := FHintLabel.Caption;
end;

procedure TOpenPreviewDialog.SetHint(value: string);
begin
  FHintLabel.Caption := Value;
end;

procedure Register;
begin
  RegisterComponents('CPub', [TOpenPreviewDialog]);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -