DelphiTXT文档编辑器
EXE:http://files.cnblogs.com/xe2011/Text_EditorRelease2013-12-20-185320.rar
Delphi XE5 PAS:http://files.cnblogs.com/xe2011/Text_Editor_Pascal2013-12-20-185320.rar
字符处理单元
// 字符串处理功能 unit StringFunctions; interface uses
Windows, Messages, SysUtils, Variants, Classes, Forms, Dialogs, StdCtrls,
Commctrl; type
TStringFunction = class(TObject)
private
function IsUpper(ch: char): boolean;
function IsLower(ch: char): boolean;
function ToUpper(ch: char): char;
function ToLower(ch: char): char;
public
procedure ReplaceSelText(Edit: TCustomEdit; const s: String);
procedure UpperSelText(Edit: TCustomEdit);
procedure LowerSelText(Edit: TCustomEdit); function UpperFistLetter(Memo: TMemo): string; procedure ClearBlankLine(Memo: TMemo);
procedure ClearBlankSpace(Memo: TMemo);
procedure ClearNum(Memo: TMemo);
procedure ClearLetter(Memo: TMemo); procedure InsertNumber(Memo: TMemo);
procedure InsertString(Memo: TMemo; str: string);
procedure InsertComment(Memo: TMemo);
procedure BatchReplaceString(Memo: TMemo); procedure JustOneLine(Memo: TMemo);
procedure ReLine(Memo: TMemo; n: Integer); procedure TextToHtml(sTextFile, sHtmlFile: string); function Proper(const s: string): string;
function CNWordsCount(text: string): Integer;
function ENWordsCount(text: string): Integer; end; var
StrFunction: TStringFunction; implementation // 让代码设置Memo后可以让memo在Ctrl+Z撤销有效
procedure TStringFunction.ReplaceSelText(Edit: TCustomEdit; const s: String);
begin
SendMessage(Edit.Handle, EM_REPLACESEL, , LPARAM(PChar(s)));
// Edit.Perform(EM_REPLACESEL, , LPARAM(PChar(s)));
end; // Edit显示行号
// ------------------------------------------------------------------------------
// 去除空行
// Memo1.Text := StringReplace(Memo1.Text, ####, ##, [rfReplaceAll]);
{
//无法撤销
//空行的去掉
//本行只有空格的也去掉
//全选
//复制到剪切板上
}
procedure TStringFunction.ClearBlankLine(Memo: TMemo);
var
i: Integer;
list: TStringList;
begin
with Memo do
begin
if Lines.Count > then
begin
list := TStringList.Create;
for i := to Lines.Count - do
if (Trim(Lines[i]) <> '') then
list.Add(Lines[i]);
SelectAll;
ReplaceSelText(Memo, list.text);
list.Free;
end;
end;
end; // 去除空格
// 将 空格替换为空
procedure TStringFunction.ClearBlankSpace(Memo: TMemo);
var
s: string;
begin
s := StringReplace(Memo.Lines.text, ' ', '', [rfReplaceAll]);
s := StringReplace(s, ' ', '', [rfReplaceAll]); //中文的空格 Memo.SelectAll;
ReplaceSelText(Memo,s);
end; // 去除一字符串中的所有的数字
//
procedure TStringFunction.ClearNum(Memo: TMemo);
var
str: string;
i: Integer;
begin
str := '';
for i := to Length(str) do
Memo.text := StringReplace(Memo.Lines.text, str[i], '', [rfReplaceAll]); { rfReplaceAll
TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
}
end; // 去除一字符串中的所有的字母
procedure TStringFunction.ClearLetter(Memo: TMemo);
var
str: string;
i: Integer;
begin
str := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
for i := to Length(str) do
Memo.text := StringReplace(Memo.Lines.text, str[i], '', [rfReplaceAll]);
end; // 批量替换关键字
procedure TStringFunction.BatchReplaceString(Memo: TMemo);
var
i: Integer;
begin
for i := to Length(Memo.Lines.text) do
Memo.text := StringReplace(Memo.Lines.text, Memo.Lines[i], '',
[rfReplaceAll]);
ClearBlankSpace(Memo);
end; // ------------------------------------------------------------------------------
// 全角转半角
// 符号有哪些
procedure ConvertQtoB;
begin end; // 半角转换全角
procedure ConvertBtoQ;
begin end; { 转换选中的文本大写 }
procedure TStringFunction.UpperSelText(Edit: TCustomEdit);
var
x, y: Integer;
begin
With Edit do
begin
x := SelStart;
y := SelLength;
if SelText <> '' then
begin
ReplaceSelText(Edit, UpperCase(SelText));
SelStart := x;
SelLength := y;
end
else
begin
Edit.SelectAll;
ReplaceSelText(Edit, UpperCase(Edit.text));
end;
end;
end; { 转换选中的文本小写 }
procedure TStringFunction.LowerSelText(Edit: TCustomEdit);
var
x, y: Integer;
begin
With Edit do
begin
x := SelStart;
y := SelLength;
if SelText <> '' then
begin
ReplaceSelText(Edit, LowerCase(SelText));
SelStart := x;
SelLength := y;
end
else
begin
Edit.SelectAll;
ReplaceSelText(Edit, LowerCase(Edit.text));
end;
end;
end; { 判断字符是否是大写字符 }
function TStringFunction.IsUpper(ch: char): boolean;
begin
Result := ch in ['A' .. 'Z'];
end; { 判断字符是否是小写字符 }
function TStringFunction.IsLower(ch: char): boolean;
begin
Result := ch in ['a' .. 'z'];
end; { 转换为大写字符 }
function TStringFunction.ToUpper(ch: char): char;
begin
Result := chr(ord(ch) and $DF);
end; { 转换为小写字符 }
function TStringFunction.ToLower(ch: char): char;
begin
Result := chr(ord(ch) or $);
end; { Capitalizes First Letter Of Every Word In S 单语首字母大写 }
function TStringFunction.Proper(const s: string): string;
var
i: Integer;
CapitalizeNextLetter: boolean;
begin
Result := LowerCase(s);
CapitalizeNextLetter := True;
for i := to Length(Result) do
begin
if CapitalizeNextLetter and IsLower(Result[i]) then
Result[i] := ToUpper(Result[i]);
CapitalizeNextLetter := Result[i] = ' ';
end;
end; { //// Memo选中的首字母大写 }
function TStringFunction.UpperFistLetter(Memo: TMemo): string;
var
lst: TStrings;
i: Integer;
s: string;
begin
lst := TStrings.Create;
lst := Memo.Lines; for i := to lst.Count - do
begin
s := s + StrFunction.Proper(lst[i] + ##);
end;
Memo.SelectAll;
StrFunction.ReplaceSelText(Memo, s);
// Memo.SelectAll;
end; // ------------------------------------------------------------------------------
procedure TStringFunction.InsertNumber(Memo: TMemo);
var
lst: TStrings;
i: Integer;
s: string;
begin
lst := TStrings.Create;
lst := Memo.Lines; for i := to lst.Count - do
begin
s := s + Format('%d %s'##, [i, lst[i]]);;
end;
Memo.SelectAll;
ReplaceSelText(Memo, s);
// Memo.SelectAll;
end; procedure TStringFunction.InsertString(Memo: TMemo; str: string);
var
lst: TStrings;
i: Integer;
s: string;
begin
lst := TStrings.Create;
lst := Memo.Lines; for i := to lst.Count - do
begin
s := s + Format('%s%s'##, [lst[i], str]);;
end;
Memo.SelectAll;
ReplaceSelText(Memo, s);
end; // 注释和取消注释
// 获得选中的文本的起始行和结束行
// 可以通过 MOUSEDOWN MOUSEUP得到
procedure TStringFunction.InsertComment(Memo: TMemo);
var
str: string;
x, y: Integer;
begin
str := Memo.SelText;
x := Memo.SelStart;
y := Memo.SelLength; if str = '' then
Exit;
// Memo.SetSelText('//' +str);
Memo.SelText := '//' + str;
Memo.SelStart := x + ;
Memo.SelLength := y + ; end; // ------------------------------------------------------------------------------
// 合并成一行
procedure TStringFunction.JustOneLine(Memo: TMemo);
var
s: string;
i: Integer;
begin
for i := to Memo.Lines.Count - do
s := s + Memo.Lines[i];
Memo.SelectAll;
ReplaceSelText(Memo, s);
end; // ------------------------------------------------------------------------------
// 重新分行
{
var
n: Integer;
begin
n := StrToInt(InputBox('重新分行', '每行几个字符', '8'));
ReLine(Memo1, n);
end;
}
procedure TStringFunction.ReLine(Memo: TMemo; n: Integer);
var
s: string;
i, j, k: Integer;
L: TStringList;
begin
L := TStringList.Create;
j := ; for k := to Memo.Lines.Count - do
s := s + Memo.Lines[k]; if Trim(s) <> '' then
begin
for i := to (Length(s) div n) do // 几行
begin
j := j + n;
L.Add(Copy(s, j - n, n)); // COPY 的第一位不是0是1 // 每行的字符
end;
end;
Memo.SelectAll;
ReplaceSelText(Memo, L.text);
L.Free;
end; // ------------------------------------------------------------------------------
// 获得汉字字符个数
function TStringFunction.CNWordsCount(text: string): Integer;
var
i, sum, c: Integer;
begin
Result := ; c := ;
sum := Length(text); if sum = then
Exit;
for i := to sum do
begin
if ord(text[i]) >= then
begin
Inc(c);
end;
end;
Result := c;
end; // 获得非汉字字符个数
function TStringFunction.ENWordsCount(text: string): Integer;
var
i, sum, e: Integer;
begin
Result := ;
e := ;
sum := Length(text);
if sum = then
Exit;
for i := to sum do
begin
if (ord(text[i]) >= ) and (ord(text[i]) <= ) then
begin
Inc(e);
end;
end;
Result := e;
end; {
TextToHtml('C:\1.txt','c:\2.htm');
}
procedure TStringFunction.TextToHtml(sTextFile, sHtmlFile: string);
var
aText: TStringList;
aHtml: TStringList;
i: Integer;
begin
aText := TStringList.Create;
try
aText.LoadFromFile(sTextFile);
aHtml := TStringList.Create;
try
aHtml.Clear;
aHtml.Add('<html>');
aHtml.Add('<body>');
for i := to aText.Count - do
aHtml.Add(aText.Strings[i] + '<br>');
aHtml.Add('</body>');
aHtml.Add('</html>');
aHtml.SaveToFile(sHtmlFile);
finally
aHtml.Free;
end;
finally
aText.Free;
end;
end; Initialization StrFunction := TStringFunction.Create; Finalization StrFunction.Free; end.
主窗体单元
unit TextEditor; interface uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.ComCtrls,
Vcl.StdActns, Vcl.ActnList, Vcl.ExtActns, System.Actions, Vcl.ExtCtrls,
Vcl.ExtDlgs, INIFILES; function GetUserDefaultUILanguage(): Integer; external 'Kernel32.DLL'; type
TTextEditorForm = class(TForm)
Memo1: TMemo;
StatusBar1: TStatusBar;
MainMenu1: TMainMenu;
mni_File: TMenuItem;
FileNew: TMenuItem;
FileOpen: TMenuItem;
FileSave: TMenuItem;
FileSaveAs: TMenuItem;
mni_PageSetup: TMenuItem;
mni_Print: TMenuItem;
mni_Exit: TMenuItem;
mni_Edit: TMenuItem;
mni_Undo: TMenuItem;
mni_Cut: TMenuItem;
mni_Copy: TMenuItem;
mni_Paste: TMenuItem;
mni_Delete: TMenuItem;
mni_Find: TMenuItem;
mni_FindNext: TMenuItem;
mni_Replace: TMenuItem;
mni_GoTo: TMenuItem;
mni_SelectAll: TMenuItem;
mni_DateTime: TMenuItem;
mni_Format: TMenuItem;
mni_Font: TMenuItem;
mni_WordWrap: TMenuItem;
mni_View: TMenuItem;
mni_StatusBar: TMenuItem;
mni_Help: TMenuItem;
mni_ViewHelp: TMenuItem;
mni_About: TMenuItem;
mni_SetTopMoset: TMenuItem;
FindDialog1: TFindDialog;
ReplaceDialog1: TReplaceDialog;
Tools: TMenuItem;
Options1: TMenuItem;
N2: TMenuItem;
Convert1: TMenuItem;
Clear1: TMenuItem;
Bat1: TMenuItem;
Rnd1: TMenuItem;
Total1: TMenuItem;
upperCase: TMenuItem;
Lowercase: TMenuItem;
N3: TMenuItem;
UpperFirstLatter: TMenuItem;
InsertNumber: TMenuItem;
Comment: TMenuItem;
NoBank: TMenuItem;
BatNoKeyWord: TMenuItem;
N5: TMenuItem;
NoSpace: TMenuItem;
NoNumber: TMenuItem;
NoLetter: TMenuItem;
CombineOne: TMenuItem;
ReLine: TMenuItem;
N7: TMenuItem;
BatCreateFile: TMenuItem;
BatCreateFolder: TMenuItem;
BatCreateRename: TMenuItem;
N4: TMenuItem;
N1: TMenuItem;
HTMLTXT1: TMenuItem;
XTHTML1: TMenuItem;
RecentFiles: TMenuItem;
ClearFiles: TMenuItem;
N6: TMenuItem;
ComplexText1: TMenuItem;
SimpleText1: TMenuItem;
mni_InsertRightStr: TMenuItem;
ASCIIHEX1: TMenuItem;
HEXASCII1: TMenuItem;
N8: TMenuItem;
procedure FormResize(Sender: TObject);
procedure mni_WordWrapClick(Sender: TObject);
procedure mni_AboutClick(Sender: TObject);
procedure mni_FontClick(Sender: TObject);
procedure mni_DateTimeClick(Sender: TObject);
procedure mni_GoToClick(Sender: TObject);
procedure mni_StatusBarClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SaveConfig(Sender: TObject);
procedure LoadConfig(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure mni_PrintClick(Sender: TObject);
procedure mni_SetTopMosetClick(Sender: TObject);
procedure Memo1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure act_SetCaretPosExecute(Sender: TObject);
procedure Memo1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FindDialog1Find(Sender: TObject);
procedure mni_DeleteClick(Sender: TObject);
procedure mni_PasteClick(Sender: TObject);
procedure mni_CopyClick(Sender: TObject);
procedure mni_CutClick(Sender: TObject);
procedure ReplaceDialog1Replace(Sender: TObject);
procedure ReplaceDialog1Find(Sender: TObject);
procedure mni_FindNextClick(Sender: TObject);
procedure mni_FindClick(Sender: TObject);
procedure mni_ReplaceClick(Sender: TObject);
procedure mni_EditClick(Sender: TObject);
procedure mni_UndoClick(Sender: TObject);
procedure mni_PageSetupClick(Sender: TObject);
procedure mni_ExitClick(Sender: TObject);
procedure Memo1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure mni_SelectAllClick(Sender: TObject);
procedure Memo1KeyPress(Sender: TObject; var Key: Char);
procedure FileNewClick(Sender: TObject);
procedure FileOpenClick(Sender: TObject);
procedure FileSaveClick(Sender: TObject);
procedure FileSaveAsClick(Sender: TObject);
procedure Total1Click(Sender: TObject);
procedure upperCaseClick(Sender: TObject);
procedure LowercaseClick(Sender: TObject);
procedure UpperFirstLatterClick(Sender: TObject);
procedure NoSpaceClick(Sender: TObject);
procedure NoBankClick(Sender: TObject);
procedure CombineOneClick(Sender: TObject);
procedure ReLineClick(Sender: TObject);
procedure HTMLTXT1Click(Sender: TObject);
procedure InsertNumberClick(Sender: TObject);
procedure mni_InsertRightStrClick(Sender: TObject);
procedure NoNumberClick(Sender: TObject);
procedure NoLetterClick(Sender: TObject);
procedure mni_ViewHelpClick(Sender: TObject);
private
{ Private declarations }
FFileName: string;
procedure CheckFileSave;
procedure SetFileName(const FileName: String);
procedure PerformFileOpen(const AFileName: string);
procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES; // ------------------------------------------------------------------------------
// procedure WMDROPFILES(var MSg: TMessage); message WM_DROPFILES;
procedure GoToMemoLineDialog(Memo: TMemo);
procedure SetUiCHS();
procedure SetUiEN();
procedure MemoPrinter(Memo: TMemo; TitleStr: string = '无标题'); // ------------------------------------------------------------------------------ public
{ Public declarations }
end; var
TextEditorForm: TTextEditorForm;
FindStr: string;
bStatueBar: Boolean = False;
AppINI: string;
// ------------------------------------------------------------------------------ implementation uses
ShellApi, Registry, Printers, Clipbrd, StrUtils,
Search, StringFunctions, Encrypt, Config, SimpleConvert;
{$R *.dfm} resourcestring
sSaveChanges = '是否将未更改保存到 %s?';
sOverWrite = '%s 已存在。' + ## + '要替换它吗?';
sTitle = '记事本';
sUntitled = '未命名';
sColRowInfo = '行: %3d 列: %3d';
sLine = '行'; //
scol = '列';
sGoToTitle = '转到指定行'; // 轮到行的 输入对话框的标题
sGoToTips = '行号(&L):'; //
sMsgBoxTitle = '行数超过了总行数';
sFileDlgFilter = '文本文档(*.txt)|*.txt|所有文件(*.*)|*.*'; // 打开和保存的文本是一样的 procedure TextToHtml(Memo: TMemo);
var
lst: TStringList;
i: Integer;
begin
try
lst := TStringList.Create;
with lst do
begin
Clear;
Add('<html>');
Add('<body>');
for i := to Memo.Lines.Count - do
Add(Memo.Lines[i] + '<br>');
Add('</body>');
Add('</html>');
Memo.text := text;
ShowMessage(text);
end;
finally
lst.Free;
end;
end; procedure TTextEditorForm.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 TTextEditorForm.CombineOneClick(Sender: TObject);
begin
strfunction.JustOneLine(Memo1);
end; procedure TTextEditorForm.SetFileName(const FileName: String);
begin
FFileName := FileName;
Caption := Format('%s - %s', [ExtractFileName(FileName), sTitle]);
end; procedure TTextEditorForm.PerformFileOpen(const AFileName: string);
begin
Memo1.Lines.LoadFromFile(AFileName);
SetFileName(AFileName);
Memo1.SetFocus;
Memo1.Modified := False;
end; procedure TTextEditorForm.WMDropFiles(var Msg: TWMDropFiles);
var
CFileName: array [ .. MAX_PATH] of Char;
begin
try
if DragQueryFile(Msg.Drop, , CFileName, MAX_PATH) > then
begin
CheckFileSave;
PerformFileOpen(CFileName);
Msg.Result := ;
end;
finally
DragFinish(Msg.Drop);
end;
end; { ReplaceDialog Find }
procedure TTextEditorForm.ReLineClick(Sender: TObject);
var
n: Integer;
begin
n := StrToInt(InputBox('重新分行', '每行几个字符', ''));
strfunction.ReLine(Memo1, n);
end; procedure TTextEditorForm.ReplaceDialog1Find(Sender: TObject);
begin
with Sender as TReplaceDialog do
if not SearchMemo(Memo1, FindText, Options) then
MessageBox(Handle, PWideChar(Concat('找不到"', FindText, '"')), '记事本',
MB_ICONINFORMATION);
end; { ReplaceDialog Replace }
procedure TTextEditorForm.ReplaceDialog1Replace(Sender: TObject);
var
Found: Boolean;
begin
with ReplaceDialog1 do
begin
{ Replace }
if (frReplace in Options) and (Memo1.SelText = FindText) then
Memo1.SelText := ReplaceText;
Found := SearchMemo(Memo1, FindText, Options); { Replace All }
if (frReplaceAll in Options) then
begin
Memo1.SelStart := ;
while Found do
begin
if (Memo1.SelText = FindText) then
Memo1.SelText := ReplaceText;
Found := SearchMemo(Memo1, FindText, Options);
end;
if not Found then
SendMessage(Memo1.Handle, WM_VSCROLL, SB_TOP, );
end; if (not Found) and (frReplace in Options) then
MessageBox(Handle, PWideChar(Concat('找不到"', FindText, '"')), '记事本',
MB_ICONINFORMATION);
end; end; procedure TTextEditorForm.FileNewClick(Sender: TObject);
begin
CheckFileSave;
SetFileName(sUntitled); Memo1.Lines.Clear;
Memo1.Modified := False;
end; procedure TTextEditorForm.FileOpenClick(Sender: TObject);
begin
CheckFileSave; with TOpenDialog.Create(nil) do
begin
Filter := sFileDlgFilter;
FileName := '*.txt';
if Execute then
begin
PerformFileOpen(FileName);
Memo1.ReadOnly := ofReadOnly in Options;
end;
end;
end; procedure TTextEditorForm.FileSaveClick(Sender: TObject);
begin
if FFileName = sUntitled then
FileSaveAs.click
else
begin
Memo1.Lines.SaveToFile(FFileName);
Memo1.Modified := False;
end;
end; procedure TTextEditorForm.FileSaveAsClick(Sender: TObject);
begin
with TSaveDialog.Create(nil) do
begin
Filter := sFileDlgFilter;
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 TTextEditorForm.FindDialog1Find(Sender: TObject);
begin
with Sender as TFindDialog do
begin
FindStr := FindText;
if not SearchMemo(Memo1, FindText, Options) then
MessageBox(Handle, PWideChar(Concat('找不到"', FindText, '"')), '记事本',
MB_ICONINFORMATION);
end;
end; procedure TTextEditorForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
SaveConfig(Sender);
Action := caFree; CheckFileSave;
end; procedure TTextEditorForm.FormCreate(Sender: TObject);
begin AppINI := ExtractFilePath(Application.ExeName) + 'Text Editor.ini'; SetFileName(sUntitled);
DragAcceptFiles(Handle, True);
// FindDialog1.Options := [frDown, frHideWholeWord];
// ReplaceDialog1.Options := [frDown, frHideWholeWord];
with Memo1 do
begin
HideSelection := False;
ScrollBars := ssVertical;
Align := alClient;
end; act_SetCaretPosExecute(Sender); if GetUserDefaultUILanguage() = $ then
SetUiCHS // Caption:='简体中文';
else
SetUiEN; // Caption:='英文'; // Caption := Form1Title;
LoadConfig(Sender);
bStatueBar := mni_StatusBar.Checked; if mni_WordWrap.Checked then
begin
mni_WordWrap.click;
mni_WordWrap.Checked := True;
// 可以自动换行
Memo1.ScrollBars := ssVertical;
Memo1.WordWrap := True;
mni_GoTo.Enabled := False;
mni_StatusBar.Checked := False;
mni_StatusBar.Enabled := False;
StatusBar1.Visible := False;
end
else
begin
// 不能换行
Memo1.ScrollBars := ssBoth;
Memo1.WordWrap := False;
mni_GoTo.Enabled := True;
mni_StatusBar.Enabled := True;
StatusBar1.Visible := bStatueBar;
end; bStatueBar := mni_StatusBar.Checked;
mni_StatusBar.Checked := bStatueBar;
StatusBar1.Panels[].Width := ( * StatusBar1.Width) div ;
end; procedure TTextEditorForm.FormResize(Sender: TObject);
begin
StatusBar1.Panels[].Width := ( * StatusBar1.Width) div ;
// SaveConfig(Sender);
end; procedure TTextEditorForm.GoToMemoLineDialog(Memo: TMemo);
var
LineIndex1, LineLength1, selStart1, Line, i: Integer;
begin
selStart1 := ;
Line := StrToInt(InputBox(sGoToTitle, sGoToTips,
IntToStr(Memo.CaretPos.Y + ))) - ; if (Line > ) and (Line <= Memo.Lines.Count) then
for i := to Line - do
begin
LineIndex1 := SendMessage(Memo.Handle, EM_LINEINDEX, i, );
LineLength1 := SendMessage(Memo.Handle, EM_LINELENGTH, LineIndex1, ) + ;
selStart1 := selStart1 + LineLength1;
end
else if Line = then
Memo.SelStart := selStart1
else
MessageBox(Handle, PWideChar('行数超出了总行数'), PWideChar('记事本 - 跳行'), );
Memo.SelStart := selStart1;
end; procedure TTextEditorForm.HTMLTXT1Click(Sender: TObject);
begin
TextToHtml(Memo1);
end; procedure TTextEditorForm.InsertNumberClick(Sender: TObject);
begin
strfunction.InsertNumber(Memo1);
end; procedure TTextEditorForm.LowercaseClick(Sender: TObject);
begin
strfunction.LowerSelText(Memo1);
end; procedure TTextEditorForm.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
{ 你猜在编辑菜单中为何不使用系统的HotKey而在这里用手动来实现快捷键
去除声音
}
if (Shift = [ssCtrl]) and (Key = $) then // 按下<Ctrl+F>
mni_Find.click; if (Key = vk_F3) and mni_FindNext.Enabled then // F3
mni_FindNext.click; if (Shift = [ssCtrl]) and (Key = $) then // Ctrl+H
mni_Replace.click; if (Shift = [ssCtrl]) and (Key = $) and (not Memo1.WordWrap) then // Ctrl+G
mni_GoTo.click; if (Shift = [ssCtrl]) and (Key = $) then // Ctrl+A
mni_SelectAll.click; if (Key = vk_F5) then // F5
mni_DateTime.click;
end; procedure TTextEditorForm.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
// F,H,G,A
if (Key = #) or (Key = #) { or (Key = #8) } or (Key = #) then
Key := #;
end; procedure TTextEditorForm.Memo1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
act_SetCaretPosExecute(Sender);
end; procedure TTextEditorForm.Memo1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin act_SetCaretPosExecute(Sender);
end; // ------------------------------------------------------------------------------
{ Edit Menu Item Enable }
procedure TTextEditorForm.mni_EditClick(Sender: TObject);
begin
mni_Find.Enabled := (Memo1.text <> '');
mni_FindNext.Enabled := (Memo1.text <> '') and (FindStr <> '');
mni_Replace.Enabled := (Memo1.text <> ''); mni_GoTo.Enabled := not Memo1.WordWrap;
mni_Undo.Enabled := Memo1.Modified;
mni_Cut.Enabled := (Memo1.SelLength > );
mni_Copy.Enabled := (Memo1.SelLength > );
mni_Paste.Enabled := Clipboard.HasFormat(CF_TEXT);
mni_Delete.Enabled := (Memo1.text <> '');
// mni_SelectAll.Enabled:= ( Memo1.SelLength <> Length(Memo1.Text) );
end; procedure TTextEditorForm.mni_AboutClick(Sender: TObject);
begin
ShellAbout(Handle, PWideChar('记事本'),
'Roman E-Main:450640526@qq.com 2013年6月15日17:46:18',
Application.Icon.Handle);
end; procedure TTextEditorForm.mni_CopyClick(Sender: TObject);
begin
Memo1.CopyToClipboard
end; procedure TTextEditorForm.mni_CutClick(Sender: TObject);
begin
Memo1.CutToClipboard;
end; procedure TTextEditorForm.mni_DeleteClick(Sender: TObject);
begin
// 没选中也能删除的
// 快捷键del去掉就可以正常使用了
Memo1.ClearSelection;
end; procedure TTextEditorForm.mni_SelectAllClick(Sender: TObject);
begin
Memo1.SelectAll;
end; procedure TTextEditorForm.mni_DateTimeClick(Sender: TObject);
begin
Memo1.SetSelText((FormatDateTime('hh:mm yyyy/m/dd', now))); // 插入时间/日期
end; procedure TTextEditorForm.mni_ExitClick(Sender: TObject);
begin
Close;
end; // 调用查找对话框
procedure TTextEditorForm.mni_FindClick(Sender: TObject);
begin
with FindDialog1 do
begin
Left := Self.Left + ;
Top := Self.Top + ;
FindText := Memo1.SelText;
Execute;
end;
end; { ReplaceDialog1.Execute }
procedure TTextEditorForm.mni_ReplaceClick(Sender: TObject);
begin
with ReplaceDialog1 do
begin
Left := Self.Left + ;
Top := Self.Top + ;
FindText := Memo1.SelText;
Execute;
end;
end; { Find Next }
procedure TTextEditorForm.mni_FindNextClick(Sender: TObject);
begin
if not SearchMemo(Memo1, FindStr, FindDialog1.Options) then
MessageBox(Handle, PWideChar(Concat('找不到"', FindStr, '"')), '记事本',
MB_ICONINFORMATION);
end; procedure TTextEditorForm.mni_FontClick(Sender: TObject);
begin
with TFontDialog.Create(nil) do
begin
Font := Memo1.Font;
Options := [fdApplyButton];
if Execute() then
Memo1.Font := Font;
end;
end; procedure TTextEditorForm.mni_GoToClick(Sender: TObject);
begin
GoToMemoLineDialog(Memo1);
end; procedure TTextEditorForm.mni_InsertRightStrClick(Sender: TObject);
var
str: string;
begin
str := InputBox('提示', '放在每行的最右边的字符串', ' - Hello Roman');
strfunction.InsertString(Memo1, str);
end; procedure TTextEditorForm.mni_PageSetupClick(Sender: TObject);
begin
With TPageSetupDialog.Create(nil) do
Execute;
end; procedure TTextEditorForm.mni_PasteClick(Sender: TObject);
begin
Memo1.PasteFromClipboard;
end; procedure TTextEditorForm.mni_PrintClick(Sender: TObject);
begin
MemoPrinter(Memo1); // 标题修改为文件名
end; procedure TTextEditorForm.mni_StatusBarClick(Sender: TObject);
begin
if mni_StatusBar.Checked then
begin
bStatueBar := True;
StatusBar1.Visible := True;
end else
begin
StatusBar1.Visible := False;
bStatueBar := False;
end;
end; procedure TTextEditorForm.mni_UndoClick(Sender: TObject);
begin
Memo1.Undo;
end; procedure TTextEditorForm.mni_ViewHelpClick(Sender: TObject);
begin
ShellExecute(Handle, 'open', 'http://www.cnblogs.com/xe2011/p/3483809.html',
, , SW_SHOWNORMAL);
end; procedure TTextEditorForm.mni_WordWrapClick(Sender: TObject);
begin
if mni_WordWrap.Checked then
begin
Memo1.ScrollBars := ssVertical; // 自动换行
Memo1.WordWrap := True; // 转到 和 状态栏不可用 和状态栏菜单不可用 check为false
mni_GoTo.Enabled := False; // ----------------------------------------
mni_StatusBar.Enabled := False;
mni_StatusBar.Checked := False;
StatusBar1.Visible := False;
end
else
begin
Memo1.ScrollBars := ssBoth; // 取消自动换行
Memo1.WordWrap := False; mni_GoTo.Enabled := True; // ----------------------------------------
mni_StatusBar.Enabled := True;
mni_StatusBar.Checked := bStatueBar;
StatusBar1.Visible := bStatueBar;
end;
// if bStatueBar=True then Caption:='True';
// if bStatueBar=False then Caption:='False'; end; procedure TTextEditorForm.NoBankClick(Sender: TObject);
begin
strfunction.ClearBlankLine(Memo1);
end; procedure TTextEditorForm.NoLetterClick(Sender: TObject);
begin
strfunction.ClearLetter(Memo1);
end; procedure TTextEditorForm.NoNumberClick(Sender: TObject);
begin
strfunction.ClearNum(Memo1);
end; procedure TTextEditorForm.NoSpaceClick(Sender: TObject);
begin
strfunction.ClearBlankSpace(Memo1);
end; procedure TTextEditorForm.mni_SetTopMosetClick(Sender: TObject);
begin
if mni_SetTopMoset.Checked then
FormStyle := fsStayOnTop
else
FormStyle := fsNormal;
end; procedure TTextEditorForm.SetUiCHS();
begin
// SetUICH
// ------------------------------------------
mni_File.Caption := '文件(&F)';
FileNew.Caption := '新建(&N)';
FileOpen.Caption := '打开(&O)...';
FileSave.Caption := '保存(&S)';
FileSaveAs.Caption := '另存为(&A)...';
mni_PageSetup.Caption := '页面设置(&U)...';
mni_Print.Caption := '打印(&P)...';
mni_Exit.Caption := '退出(&X)';
// ------------------------------------------
mni_Edit.Caption := '编辑(&E)';
mni_Undo.Caption := '撤消(&U) Ctrl+Z';
mni_Cut.Caption := '剪切(&T) Ctrl+X';
mni_Copy.Caption := '复制(&C) Ctrl+C';
mni_Paste.Caption := '粘贴(&P) Ctrl+V';
mni_Delete.Caption := '删除(&L)) Del';
mni_Find.Caption := '查找(F)... Ctrl+F';
mni_FindNext.Caption := '查找下一个(&N) F3';
mni_Replace.Caption := '替换(&R)... Ctrl+H';
mni_GoTo.Caption := '转到(&G)... Ctrl+G';
mni_SelectAll.Caption := '全选(&A) Ctrl+A';
mni_DateTime.Caption := '时间/日期(&D) F5';
// ------------------------------------------
mni_Format.Caption := '格式(&O)';
mni_WordWrap.Caption := '自动换行(&W)';
mni_Font.Caption := '字体(&F)...';
// ------------------------------------------
mni_View.Caption := '查看(&V)';
mni_StatusBar.Caption := '状态栏(&S)';
mni_SetTopMoset.Caption := '置顶(&T)';
// ------------------------------------------
mni_Help.Caption := '帮助(&H)';
mni_ViewHelp.Caption := '查看帮助(&H)';
mni_About.Caption := '关于记事本(&A)'; // // ------------------------------------------
// Form1Title := '无标题 - 记事本';
// Line := '行'; //
// col := '列';
// sGoToTitle := '转到指定行'; // 轮到行的 输入对话框的标题
// sGoToTips := '行号(&L):'; //
// MsgBoxTitle := '行数超过了总行数';
// MsgBoxHint := '记事本 - 跳行';
// shellAboutText := '关于 - 记事本';
// FileDialogFilter := '文本文档(*.txt)|*.txt|所有文件(*.*)|*.*'; end; procedure TTextEditorForm.SetUiEN();
begin
// SetUIENGLISH
// ------------------------------------------
mni_File.Caption := '&File';
FileNew.Caption := '&New';
FileOpen.Caption := '&Open...';
FileSave.Caption := '&Save';
FileSaveAs.Caption := 'Save &As...';
mni_PageSetup.Caption := 'Page Set&up...';
mni_Print.Caption := '&Print...';
mni_Exit.Caption := 'E&xit';
// ------------------------------------------
mni_Edit.Caption := '&Edit';
mni_Undo.Caption := '&Undo Ctrl+Z';
mni_Cut.Caption := 'Cu&t Ctrl+X';
mni_Copy.Caption := '&Copy Ctrl+C';
mni_Paste.Caption := '&Paste) Ctrl+V';
mni_Delete.Caption := '&Delete Del';
mni_Find.Caption := '&Find... Ctrl+F';
mni_FindNext.Caption := 'Find &Next F3';
mni_Replace.Caption := '&Replace... Ctrl+H';
mni_GoTo.Caption := '&Go To... Ctrl+G';
mni_SelectAll.Caption := 'Select &All Ctrl+A';
mni_DateTime.Caption := 'Time/&Date F5';
// ------------------------------------------
mni_Format.Caption := 'F&ormat';
mni_WordWrap.Caption := '&Word Wrap';
mni_Font.Caption := '&Font...';
// ------------------------------------------
mni_View.Caption := '&View';
mni_StatusBar.Caption := '&StatueBar';
mni_SetTopMoset.Caption := '&TopMost';
// ------------------------------------------
mni_Help.Caption := '&Help';
mni_ViewHelp.Caption := 'View H&elp';
mni_About.Caption := '&About Notepad'; // // ------------------------------------------
// Form1Title := 'Untitled - Notepad';
// Line := 'Ln'; //
// col := 'Col';
// sGoToTitle := 'Go To Line'; // 轮到行的 输入对话框的标题
// sGoToTips := '&Line Number:'; //
// MsgBoxTitle := 'The line number is beyond the total number of lines';
// MsgBoxHint := 'Notepad - Goto Line';
// shellAboutText := ' - Notepad';
// FileDialogFilter := 'Text File(*.txt)|*.txt|All File(*.*)|*.*';
end; procedure TTextEditorForm.Total1Click(Sender: TObject);
var
Line: Integer;
Count: Integer;
en, cn: Integer;
s: string;
begin
with Memo1 do
begin
Line := Memo1.Lines.Count;
Count := Length(Memo1.Lines.text);
en := strfunction.ENWordsCount(text);
cn := strfunction.CNWordsCount(text);
end;
s := Format
('行 %d '#'字符(全部 包括空格) %d '#'字符(非汉字 无空格) %d '#'汉字(汉字字符) %d',
[Line, Count, en, cn]);
Application.MessageBox(PWideChar(s), '统计 - 记事本', MB_ICONINFORMATION);
end; procedure TTextEditorForm.upperCaseClick(Sender: TObject);
begin
strfunction.UpperSelText(Memo1);
end; procedure TTextEditorForm.UpperFirstLatterClick(Sender: TObject);
begin
strfunction.UpperFistLetter(Memo1);
end; // Printers
procedure TTextEditorForm.MemoPrinter(Memo: TMemo; TitleStr: string = '无标题');
var
Left: Integer;
Top: Integer;
i, j, X, Y: Integer; // PageHeight,
PagesStr: String;
posX, posY, Posx1, posY1: Integer;
PrintDialog1: TPrintDialog;
begin
Left := ;
Top := ;
Y := Top; //
X := Left; //
j := ;
PrintDialog1 := TPrintDialog.Create(Application);
if PrintDialog1.Execute then
begin
if Memo1.text = '' then
Exit; // 文本为空 本次操作不会被执行 With Printer do
begin
BeginDoc; // 另存的打印的文件名 如何实现 默认为 .jnt
// Form2.Show;
Canvas.Font := Memo.Font;
// -------------------------------------------------------------------------
// 打印文件名的标题
// TitleStr:='无标题';
posX := (PageWidth div ) - Length(TitleStr) * ; // x+;
posY := (PageHeight * ) div ; // 第N页的标题
PagesStr := Format('第 %d 页', [Printer.PageNumber]);
Posx1 := (PageWidth div ) - Length(PagesStr) * ;
posY1 := (PageHeight * ) div ;
// -------------------------------------------------------------------------
for i := to Memo.Lines.Count - do
begin
Canvas.TextOut(X, Y, Memo.Lines[i]); // TextOut(Left,Top,string);
Y := Y + Memo.Font.Size * ;
// Memo.Font.Size*为行间距 第1行与第2行的间距,和3,与4,... if (Y > PageHeight - Top) then
begin
Canvas.TextOut(posX, posY, TitleStr);
for j := to Printer.PageNumber do
begin
PagesStr := Format('第 %d 页', [j]);
Canvas.TextOut(Posx1, posY1, PagesStr);
// Form2.Label1.Caption := System.Concat(' 正在打印', ##, TitleStr,
// ##, Format('第 %d 页', [j]));
// if Form2.Tag = then
// begin
// Abort;
// Exit;
// end;
end;
NewPage;
Y := Top;
end;
end;
Canvas.TextOut(posX, posY, TitleStr);
Canvas.TextOut(Posx1, posY1, Format('第 %d 页', [j]));
// Form2.Close;
EndDoc;
end;
end;
end; procedure TTextEditorForm.LoadConfig(Sender: TObject);
begin
ReadformState('MainForm', AppINI, Self); with TIniFile.Create(AppINI) do
begin
Memo1.Font.Name := ReadString('Memo', 'FontName', '宋体');
Memo1.Font.Size := ReadInteger('Memo', 'Size', );
mni_StatusBar.Checked := ReadBool('Other', 'StatueBarChecked', True);
mni_WordWrap.Checked := ReadBool('Other', 'WordWrapChecked', True);
Free;
end;
end; procedure TTextEditorForm.SaveConfig(Sender: TObject);
begin
WriteformState('MainForm', AppINI, Self); with TIniFile.Create(AppINI) do
begin
WriteString('Memo', 'FontName', Memo1.Font.Name);
WriteInteger('Memo', 'Size', Memo1.Font.Size);
WriteBool('Other', 'StatueBarChecked', mni_StatusBar.Checked);
WriteBool('Other', 'WordWrapChecked', mni_WordWrap.Checked);
Free;
end;
end; procedure TTextEditorForm.act_SetCaretPosExecute(Sender: TObject);
begin
if GetUserDefaultUILanguage() = $ then // SetUiCHS // Caption:='简体中文';
StatusBar1.Panels[].text := Format(' %s %d %s,%s %d %s ',
[sLine, Memo1.CaretPos.Y + , scol, sLine, Memo1.CaretPos.X + , scol])
else
// SetUiEN; //Caption:='英文';
StatusBar1.Panels[].text := Format(' %s %d ,%s %d ',
[sLine, Memo1.CaretPos.Y + , scol, Memo1.CaretPos.X + ]);
end; end.
DelphiTXT文档编辑器的更多相关文章
- Linux_文档编辑器_简介
1. vi 2. vim 3. ubuntu 有一个 自己的图形化的 文档编辑器,用起来比较方便: gedit 4. 5.
- PowerDesigner(九)-模型文档编辑器(生成项目文档)(转)
模型文档编辑器 PowerDesigner的模型文档(Model Report)是基于模型的,面向项目的概览文档,提供了灵活,丰富的模型文档编辑界面,实现了设计,修改和输出模型文档的全过程. 模型文 ...
- Web页面引入文档编辑器报风险
Web页面引入文档编辑器会报风险,则需要以下操作: <system.web> <httpRuntime requestValidationMode="2.0" / ...
- 使用Swing实现简易而不简单的文档编辑器
本文通过Swing来实现文档简易而不简单的文档编辑器,该文档编辑器的功能包括: 设置字体样式:粗体,斜体,下划线,可扩展 设置字体:宋体,黑体,可扩展 设置字号:12,14,18,20,30,40, ...
- 在线HTML文档编辑器使用入门之图片上传与图片管理的实现
在线HTML文档编辑器使用入门之图片上传与图片管理的实现: 官方网址: http://kindeditor.net/demo.php 开发步骤: 1.开发中只需要导入选中的文件(通常在 webapp ...
- [Qt及Qt Quick开发实战精解] 第1章 多文档编辑器
这一章的例子是对<Qt Creator快速人门>基础应用篇各章节知识的综合应用, 也是一个规范的实例程序.之所以说其规范,是因为在这个程序中,我们对菜单什么时候可用/什么时候不可用.关 ...
- 基于slate构建文档编辑器
基于slate构建文档编辑器 slate.js是一个完全可定制的框架,用于构建富文本编辑器,在这里我们使用slate.js构建专注于文档编辑的富文本编辑器. 描述 Github | Editor DE ...
- MKDOCS在线文档编辑器
http://www.mkdocs.org/ api接口文档编写 ,效果非常不错
- 在线帮助文档编辑器gitbook
https://www.gitbook.com/editor/windows https://yuzeshan.gitbooks.io/gitbook-studying/content/howtous ...
随机推荐
- Sql狗血的Bit类型赋值与取值
Bit 数据类型在 SQL Server 数据库中以存储 1.0 进行存储. 往数据库中添加,修改 bit 类型的字段时,只能用 0 或者 1. 关于修改 Bit 类型的字段 1.若使用 SQL 语句 ...
- CF192div2-C - Purification
题意: 从给定的图中找出某些点,这些点能够消除同一行和同一列的“怪物”.求使得最少的点的位置. 关键:要想消除整张的图的妖怪,必须选中n个点(对于n行n列来说)!!!!!!!!!!! 做法:对于每一行 ...
- 李洪强漫谈iOS开发[C语言-022]-算术运算符
- 【HDOJ】1022 Train Problem I
栈和队列训练题目. #include <stdio.h> #include <string.h> #define MAXNUM 1005 char in[MAXNUM]; ch ...
- FRM-40401 No changes to save error
FAQ: I have updated a table from the form and written a update statement in the when-button-pressed ...
- BZOJ2741: 【FOTILE模拟赛】L
2741: [FOTILE模拟赛]L Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 1170 Solved: 303[Submit][Status] ...
- MFC窗口重绘
Invalidate()与 UpdateAllViews()有什么分别 Invalidate()是让程序重画窗口. UpdateAllViews()是在DOC/VIEW结构中, 当一个视图的数据改变后 ...
- easyui plugin —— etreegrid:CRUD Treegrid
昨天写了一个ko+easyui的同样的实现,感觉写的太乱,用起来十分麻烦,于是今天照着edatagrid,写了一个etreegrid,这样再用ko绑定就方便多了. 使用很简单,$(tableId).e ...
- 宣布正式发布 Azure 媒体服务内容保护服务
Mingfei Yan Azure媒体服务项目经理 我们非常高兴地宣布正式发布 Azure 媒体服务内容保护服务.这包括 Microsoft PlayReady许可服务和 AES明文密钥交付服务!此外 ...
- Xcode6 运行程序后,右侧Debug区域的Memory显示空白解决方法
http://chenyh-blog.com/%E8%9B%8B%E7%96%BC%E7%9A%84%E5%86%85%E5%AD%98-%E7%AC%AC%E4%B8%89%E7%AF%87-sdw ...