2022年文件预览程序源代码 .pdf
编程实现文件预览以下为程序代码TXTDLGTEMPLATE DIALOG 0, 0, 316, 76 STYLE 0 x404L | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS FONT 8, MS Sans Serif LTEXT , 1119, 0, 0, 204, 76, SS_LEFT | WS_CHILD | NOT WS_VISIBLE | WS_GROUP unit txtDialog; interface uses Windows, Messages, SysUtils, Classes, StdCtrls, ExtCtrls, Buttons, Dialogs, ShellAPI, Controls; type TtxtDialog = class(TOpenDialog) private Private declarations FtxtPanel: TPanel; FtxtLabel: TLabel; FPreviewButton: TSpeedButton; FtxtCtrl: TMemo; protected Protected declarations procedure PreviewClick(Sender: TObject); virtual; procedure DoSelectionChange; override; procedure DoShow; override; property txtCtrl: TMemo read FtxtCtrl; public Public declarations constructor Create(AOwner: TComponent); override; function Execute: Boolean; override; published Published declarations end; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 5 页 - - - - - - - - - implementation $R txtDialog.res TtxtDialog constructor TtxtDialog.Create(AOwner: TComponent); begin inherited Create(AOwner); /文件扩展名过滤器 Filter := 文本文件 (*.txt)|*.txt; /定义一个主Panel-FtxtPanel,其他所有的构件都建立在这个Panel 上 FtxtPanel := TPanel.Create(Self); with FtxtPanel do begin Name := txtPanel; Caption := ; SetBounds(204, 5, 169, 200); BevelOuter := bvNone; BorderWidth := 2; TabOrder := 1; FtxtLabel := TLabel.Create(Self); with FtxtLabel do begin Name := txtLabel; Caption := 文本文件预览 ; Font.Charset := GB2312_CHARSET; Font.Name := 宋体 ; Font.Size := 9; SetBounds(6, 6, 157, 23); Align := alTop; AutoSize := False; Parent := FtxtPanel; end; /文件预览按钮 FPreviewButton := TSpeedButton.Create(Self); with FPreviewButton do begin Name := PreviewButton; SetBounds(77, 1, 23, 22); Enabled := False; /从资源文件中读出TXTGLYPH 图像,用于SpeedButton 的图像 Glyph.LoadFromResourceName(HInstance, TXTGLYPH); Hint := 查看该文件 ; ParentShowHint := False; ShowHint := True; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 5 页 - - - - - - - - - /OnClick事件,调用记事本打开该文本文件 OnClick := PreviewClick; Parent := FtxtPanel; end; /添加一个Memo元件,用于显示文件内容 FtxtCtrl := TMemo.Create(Self); with FtxtCtrl do begin Name := Memo; SetBounds(6, 29, 157, 145); Align:=alClient; /OnDblClick事件 OnDblClick := PreviewClick; TabOrder := 2; ReadOnly:=True; Text:=; ScrollBars:=ssBoth; Parent := FtxtPanel; end; end; end; procedure TtxtDialog.DoSelectionChange; var Valided: Boolean; f : TextFile; s : string; function ValidFile(const FileName: string): Boolean; begin Result := GetFileAttributes(PChar(FileName) $FFFFFFFF; end; begin FtxtCtrl.Lines.Clear; Valided := FileExists(FileName) and ValidFile(FileName) and (UpperCase(ExtractFileExt(Filename)=.TXT); if Valided then try /打开文件,逐行读入Memo的 Lines ,完成预览 AssignFile(f,Filename); FileMode:=fmOpenRead; Reset(f); while not EOF(f) do begin Readln(f,s); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 5 页 - - - - - - - - - FtxtCtrl.Lines.Add(s); end; CloseFile(f); FPreviewButton.Enabled := True; except Valided := False; end; if not Valided then FPreviewButton.Enabled := False; inherited DoSelectionChange; end; procedure TtxtDialog.DoShow; var PreviewRect, StaticRect: TRect; begin /GetClientRect是 Windows API,求出了整个Client区的大小 GetClientRect(Handle, PreviewRect); /GetStaticRect是 TOpenDialog 提供的方法,求出了标准区的大小 StaticRect := GetStaticRect; /将 PreviewRect缩小在 StaticRect的右边 /应该使用PreviewRect.Left := StaticRect.Right,此处保留了ExtDlgs.pas中语句 PreviewRect.Left := StaticRect.Left + (StaticRect.Right - StaticRect.Left); Inc(PreviewRect.Top, 4); /添加预览内容 FtxtPanel.BoundsRect := PreviewRect; FPreviewButton.Left := FtxtCtrl.BoundsRect.Right - FPreviewButton.Width - 2; FtxtPanel.ParentWindow := Handle; inherited DoShow; end; function TtxtDialog.Execute: Boolean; begin /传递自定义对话框TXTDLGTEMPLATE if NewStyleControls and not (ofOldStyleDialog in Options) then Template := TXTDLGTEMPLATE else Template := nil; Result := inherited Execute; end; procedure TtxtDialog.PreviewClick(Sender: TObject); begin /使用记事本打开该文本文件名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 5 页 - - - - - - - - - if UpperCase(ExtractFileExt(Filename)=.TXT then ShellExecute(Application.Handle,open,PChar(Filename),SW_SHOW); end; end. var Demo : TtxtDialog; begin Demo:=TtxtDialog.Create(Form1); Demo.Execute; Demo.Free; end; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 5 页 - - - - - - - - -