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.

Delphi Memo的记事本功能的更多相关文章

  1. 【Android】12.6 利用Intent实现记事本功能(NotePad)

    分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 这个例子演示如何实现一个简单的记事本功能. 该例子提前使用了后面章节将要介绍的SQLLite数据库. 二.示例-c ...

  2. JAVA 运用流编程实现简单的"记事本"功能

    一.概要 1.功能介绍 2.实现的思路及步骤代码 3.完整代码 二.功能 运用IO流和Swing实现简单的记事本功能(打开.保存.退出) 三.思路及实现步骤 1.在构造函数中画出操作界面 //创建jt ...

  3. delphi实现窗体闪烁功能

    delphi实现窗体闪烁功能 以前做窗口闪动时都没有考虑到让任务栏上的按钮闪动的问题, 现在一个客户需要任务栏按钮闪动,发现以前使用的flashwindow不能达到要求了, 查找了一下,找到flash ...

  4. delphi 10.2 ----memo 的例子 实现基本记事本功能

    unit Unit2; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  5. 教程-Delphi 调用控制面板设置功能

    应用程序运行时,有时需要对系统环境有特殊要求.例如,在Delphi数据库应用程序中可能需要进行BDE(Borland Database Engine)或ODBC数据源名称(DSN:Data Sourc ...

  6. 使用jquery.mobile和WebSQL实现记事本功能

    1.记事本列表页 1.1.页面结构与样式: <div data-role="page" id="home"> <div data-role=& ...

  7. [教学] Delphi IDE 文件搜寻功能

    Delphi IDE 提供了一个方便的文件搜寻功能,操作如下: 点 Search 选单内的 Find in Files... 例如我们想搜寻 JFile 需要引用那一个源码,可输入如下: 输入关键字: ...

  8. 第十四周课程总结&记事本功能的简单实现。

    (1)课程总结: 这周简单学习了下JDBC的内容: JDBC API 允许用户访问任何形式的表格数据,尤其是存储在关系数据库中的数据. 执行流程: (1)连接数据源,如:数据库. (2)为数据库传递查 ...

  9. [原创] delphi Memo 滚动到底部/开始 [Delphi XE、Delphi 7]

    以前控制Memo滚动到底部的操作: SendMessage(memo1.Handle,WM_VSCROLL,SB_BOTTOM,0); 或者 Memo1.SelLength:=Length(Memo1 ...

随机推荐

  1. Kafka源码分析-序列2 -Producer

    在上一篇,我们从使用方式和策略上,对消息队列做了一个宏观描述.从本篇开始,我们将深入到源码内部,仔细分析Kafka到底是如何实现一个分布式消息队列.我们的分析将从Producer端开始. 从Kafka ...

  2. 查看linux进程(强制中止进程),服务及端口号,

    进程状态查询 ps -aux [test@pan ~]$ ps -aux USER     PID    %CPU    %MEM    VSZ  RSS     TTY   STAT  START  ...

  3. 设置(TableViewController)通用框架

    本文学习于传播播客.李明杰老师.感谢

  4. 【C语言】测试系统各数据类型大小代码

    测试各系统不同数据类型大小代码 一.相关基础知识 不同环境下各数据类型大小可能不相等,(某些环境下,类型带下可以选择)故测了就知道! 二.具体内容 三.分析总结 四.实例测试 #include< ...

  5. java特点

    简单: Java语言的语法与C语言和C++语言很接近,使得大多数程序员很容易学习和使用.另一方面,Java丢弃了C++中很少使用的.很难理解的.令人迷惑的那些特性,如操作符重载.多继承.自动的强制类型 ...

  6. js烟花特效

    <!DOCTYPE html><html><head><style>body{background:#000;margin:0;}canvas{curs ...

  7. golang中设置Host Header的小Tips

    前言 笔者最近时间一直在学习和写Ruby和Go,尤其是Go,作为云计算时代的标准语言,写起来还是相当有感觉的,难过其会越来越火. 不过写的过程中,也遇到了一些小问题,本文就是分享关于go语言设置 HT ...

  8. 听同事讲 Bayesian statistics: Part 1 - Bayesian vs. Frequentist

    听同事讲 Bayesian statistics: Part 1 - Bayesian vs. Frequentist   摘要:某一天与同事下班一同做地铁,刚到地铁站,同事遇到一熟人正从地铁站出来. ...

  9. Js 表单序列化

    http://www.w3cmm.com/javascript/serialize-form.html

  10. 要将PYTHON应用于工作啦

    分析同事在线答疑的数据,考评模型还未最终给出: import time import sys import optparse #操作代码和同事名对应的文件 opfile = 'op_name.txt' ...