• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

DelphiMemo的记事本功能

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
Delphi Memo的记事本功能
 
 
 
 
 
这个代码实现了Windows记事本的主要功能。
新建,打开,保存,另存,退出。
文件拖拽打开文件 这主要是判断Memo内容是否修改过
 
 
unit Unit1;
 
interface
 
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtDlgs;
 
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    FileOpen: TButton;
    FileSave: TButton;
    FileSaveAs: TButton;
    FileExit: TButton;
    FileNew: TButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    procedure FileOpenClick(Sender: TObject);
    procedure FileSaveClick(Sender: TObject);
    procedure FileExitClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FileNewClick(Sender: TObject);
    procedure FileSaveAsClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    FFileName: string;
    FUpdating: Boolean;
    FDragOfs: Integer;
    FDragging: Boolean;
    procedure CheckFileSave;
    procedure SetFileName(const FileName: String);
    procedure PerformFileOpen(const AFileName: string);
    procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES;
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
uses ShellAPI;
{$R *.dfm}
 
resourcestring
  sSaveChanges = '是否将未更改保存到 %s?';
  sOverWrite = '%s 已存在。'+#13#10+'要替换它吗?';
  sTitle = '记事本';
  sUntitled = '未命名';
  sColRowInfo = 'Line: %3d   Col: %3d';
  sCommonDlgFileName = '文本文档(*.txt)|*.txt|所有文件(*.*)|*.*';
 
 
procedure TForm1.CheckFileSave;
var
  SaveRespond: Integer;
begin
  if not Memo1.Modified then
    Exit;
  SaveRespond := MessageBox(Handle, PWideChar(Format(sSaveChanges, [FFileName])
    ), PWideChar(sTitle), MB_YESNOCANCEL + MB_ICONINFORMATION);
  case SaveRespond of
    idYes:
      FileSave.click;
    idNo:
      ; { Nothing }
    idCancel:
      Abort;
  end;
end;
 
procedure TForm1.SetFileName(const FileName: String);
begin
  FFileName := FileName;
  Caption := Format('%s - %s', [ExtractFileName(FileName), sTitle]);
end;
 
procedure TForm1.PerformFileOpen(const AFileName: string);
begin
  Memo1.Lines.LoadFromFile(AFileName);
  SetFileName(AFileName);
  Memo1.SetFocus;
  Memo1.Modified := False;
end;
 
procedure TForm1.WMDropFiles(var Msg: TWMDropFiles);
var
  CFileName: array [0 .. MAX_PATH] of Char;
begin
  try
    if DragQueryFile(Msg.Drop, 0, CFileName, MAX_PATH) > 0 then
    begin
      CheckFileSave;
      PerformFileOpen(CFileName);
      Msg.Result := 0;
    end;
  finally
    DragFinish(Msg.Drop);
  end;
end;
 
procedure TForm1.FileNewClick(Sender: TObject);
begin
  CheckFileSave;
  SetFileName(sUntitled);
 
  Memo1.Lines.Clear;
  Memo1.Modified := False;
end;
 
procedure TForm1.FileOpenClick(Sender: TObject);
begin
  CheckFileSave;
 
  with TOpenDialog.Create(nil) do
  begin
    Filter := sCommonDlgFileName;
    FileName:='*.txt';
    if Execute then
    begin
      PerformFileOpen(FileName);
      Memo1.ReadOnly := ofReadOnly in Options;
    end;
  end;
end;
 
procedure TForm1.FileSaveAsClick(Sender: TObject);
begin
  with TSaveDialog.Create(nil) do
  begin
    Filter := sCommonDlgFileName;
    FileName:='*.txt';
    if Execute then
    begin
      if FileExists(FileName) then
        if MessageBox(Handle, PWideChar(Format(sOverWrite, [FFileName])),
          PWideChar(sTitle), MB_YESNOCANCEL + MB_ICONINFORMATION) <> idYes then
          Exit;
      Memo1.Lines.SaveToFile(FileName);
      SetFileName(FileName);
      Memo1.Modified := False;
    end;
  end;
end;
 
procedure TForm1.FileSaveClick(Sender: TObject);
begin
  if FFileName = sUntitled then
    FileSaveAs.click
  else
  begin
    Memo1.Lines.SaveToFile(FFileName);
    Memo1.Modified := False;
  end;
end;
 
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  CheckFileSave;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  SetFileName(sUntitled);
  DragAcceptFiles(Handle, True);
end;
 
procedure TForm1.FileExitClick(Sender: TObject);
begin
  Close;
end;
 
end.

 






鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
动态多维数组在Delphi中的应用发布时间:2022-07-18
下一篇:
delphi转换sqlserver中的bit类型发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap