Jump to content
Fórum Script Brasil
  • 0

Como criar um editor de textos


davidzaum

Question

1 answer to this question

Recommended Posts

  • 0

exemplo:

program Notepad;

uses 
 Forms, 
 UnitMain in 'UnitMain.pas'{FormMain},
 Unit1 in 'Unit1.pas'{AboutBox}; 

 {$R *.RES} 

 begin 
 Application.Initialize; 
 Application.CreateForm(TFormMain, FormMain);
 Application.CreateForm(TAboutBox, AboutBox); 
 Application.Run; 
 end. 

unit UnitMain; 

interface

uses 
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
 Dialogs, Menus, StdCtrls, ComCtrls; 
  
type 
 TFormMain = class(TForm1)
 RichEdit1: TRichEdit; 
 MainMenu1: TMainMenu; 
 MenuFile: TMenuItem; 
 ItemNew: TMenuItem; 
 ItemOpen: TMenuItem; 
 ItemSave: TMenuItem; 
 ItemSaveAs: TMenuItem;

 N2: TMenuItem; 
 ItemPrint: TMenuItem; 
 N4: TMenuItem; 
 ItemClose: TMenuItem; 
 SaveDialog1: TSaveDialog;
 OpenDialog1: TOpenDialog; 
 N1: TMenuItem; 
 ItemFont: TMenuItem; 
 FontDialog1: TFontDialog; 
 PopupMenu1:TPopupMenu; 
 ItemUndo: TMenuItem;
 N3: TMenuItem; 
 ItemRedo: TMenuItem; 
 ItemCut: TMenuItem; 
 ItemCopy: TMenuItem; 
 ItemPaste: TMenuItem; 
 ItemDelete: TMenuItem;
 N5: TMenuItem; 
 ItemSelectAll:TMenuItem; 
 N6: TMenuItem; 
 ItemFindText: TMenuItem; 
 ItemFindAndReplace: TMenuItem; 
 FindDialog1: TFindDialog;
 MenuEdit: TMenuItem; 
 ItemUndo1:TMenuItem; 
 ItemRedo1: TMenuItem; 
 N7: TMenuItem; 
 ItemCut1: TMenuItem; 
 ItemCopy1: TMenuItem;
 ItemPaste1: TMenuItem; 
 ItemDelete1: TMenuItem; 
 N8: TMenuItem; 
 ItemSelectAll1: TMenuItem; 
 N9: TMenuItem; 
 ItemFind1: TMenuItem; 
 ItemFindAndPlace1: TMenuItem;
 N10: TMenuItem; 
 ItemAutoWarp1: TMenuItem; 
 N11: TMenuItem; 
 ItemAutoWarp: TMenuItem; 
 ReplaceDialog1: TReplaceDialog; 
 N12: TMenuItem; 
 N13: TMenuItem;
 N14: TMenuItem; 
 procedure ItemNewClick(Sender: Tobject); 
 procedure ItemOpenClick(Sender: Tobject); 
 procedure ItemSaveClick(Sender: Tobject); 
 procedure ItemPrintClick(Sender: Tobject); 
 procedure ItemCloseClick(Sender: Tobject); 
 procedure ItemFondClick(Sender: Tobject);
 procedure PopupMenu1Popup(Sender: Tobject); 
 procedure ItemUndoClick(Sender: Tobject); 
 procedure ItemRedoClick(Sender: Tobject); 
 procedure ItemCutClick(Sender: Tobject); 
 procedure ItemCopyClick(Sender: Tobject); 
 procedure ItemPasteClick(Sender: Tobject); 
 procedure ItemDelectClick(Sender: Tobject);
 procedure ItemSelectClick(Sender: Tobject); 
 procedure ItemFindTextClick(Sender: Tobject); 
 procedure FindDialog1Find(Sender: Tobject); 
 procedure ItemAutoWarp1Click(Sender: Tobject); 
 procedure ItemFindAndReplaceClick(Sender: Tobject); 
 procedure ReplaceDialog1Replace(Sender: Tobject);
 procedure FormCreat(Sender: Tobject); 
 procedure MenuFileClick(Sender: Tobject); 
 procedure FromCloseQuery(Sender: Tobject; var CanClose: Boolean); 
 procedure N14Click(Sender: Tobject); 
 private 
 sFileName:String;
 Function CheckhasModified:Boolean;
 Function SaveAsFile:Boolean;
 Function SaveFile:Boolean;
 Function MyOpenFile(FileName:String):Boolean;
 { Private declarations } 
 public
 { Public declarations } 
 end; 

var 
 FormMain: TFormMain; 

implementation 

uses RichEdit,Unit1; 

{$R *.dfm} 

function TFormMain.CheckHasModified: Boolean;
begin 
 Result:=not RichEdit1.Modified; 
 if not Result then 
 begin 
 Case Application.MessageBox('??????!??????','??',
 MB_YESNO+mB_ICONQUESTION) OF 
 IDYES:Result:=SaveFile; 
 IDNO:Result:=True; 
 end; 
 end; 
end;

 procedure TFormMain.ItemNewClick(Sender: TObject); 
 begin
 If CheckHasModified Then 
 begin 
 RichEdit1.Lines.Clear;
 RichEdit1.Modified:=False; 
 sFileName:='???'; 
 end; 
end;

function TformMain.SaveAsFile:Boolean;
begin
 Result:=False; 
 if SaveDialog1.Execute then 
 begin 
 RichEdit1.Lines.SaveToFile(SaveDialog1.FileName0; 
 RichEdit1.Modified:=False;
 sFileName:=SaveDialog1.FileName; 
 Result:=True; 
 end; 
end;

procedure TFormMain.ItemOpenClick(Sender: TObject);
begin
 if OpenDialog1.Execute then 
 MyOpenFile(OpenDialog1.FileName); 
end;

Procedure TFormMain.ItemSaveClick(Sender: TObject);
begin
 SaveFile; 
end; 

function TFormMain.SaveFile: Boolean; 
begin
 If sFileName='???' then 
 Result:=SaveAsFile 
 else 
 begin 
 RichEdit1.Lines.SaveToFile(sFileName); 
 RichEdit1.Modified:=False;
 Result:=True; 
 end; 
end;

 procedure TFormMain.ItemSaveAsClick(Sender:TObject); 
 begin
 SaveAsFile;
 end; 

 procedure TFormMain.ItemPrintClick(Sender:TObject); 
 begin
 RichEdit1.Print(sFileName);
 end; 

 procedure TFormMain.ItemCloseClick(Sender: TObject); 
 begin
 Close; 
 end;

 procedure TFormMain.ItemFontClick(Sender: TObject); 
 begin
 FontDialog1.Font.Assign(RichEdit1.Font); 
 if FontDialog1.Execute then 
 RichEdit1.Font.Assign(FontDialog1.Font);
 end; 

 procedure TFormMain.PopupMenu1Popup(Sender: TObject); 
 begin
 ItemUndo.Enabled:=(RichEdit1.Perform(EM_CANUNDO,0,0<>0); 
 ItemRedo.Enabled:=(RichEdit1.Perform(EM_CANREDO,0,0<>0);
 ItemCut.Enabled:=(RichEdit1.Perform(EM_SELECTIONTYPE,0,0)<>0); 
 ItemCopy.Enabled:=ItemCut.Enabled; 
 ItemPaste.Enabled:=(RichEdit1.Perform(EM_CANPASTE,0,0)<>0); 
 ItemAutoWarp.Checked:=RichEdit1.WordWrap; 
 ItemUndo1.Enabled:=(RichEdit1.Perform(EM_CANUNDO,0,0<>0); 
 ItemRedo1.Enabled:=(RichEdit1.Perform(EM_CANREDO,0,0<>0);
 ItemCut1.Enabled:=(RichEdit1.Perform(EM_SELECTIONTYPE,0,0)<>0); 
 ItemCopy1.Enabled:=ItemCut.Enabled; 
 ItemPaste1.Enabled:=(RichEdit1.Perform(EM_CANPASTE,0,0)<>0); 
 ItemAutoWarp1.Checked:=RichEdit1.WordWrap; 
 end; 

 procedure TFormMain.ItemUndoClick(Sender: TObject); 
 begin
 RichEdit1.Perform(EM_UNDO,0,0); 
 end; 

 procedure TFormMain.ItemRedoClick(Sender:TObject);
 begin
 RichEdit1.Perform(EM_REDO,0,0); 
 end; 

 procedure TFormMain.ItemCutClick(Sender: TObject); 
 begin
 RichEdit1.Perform(WM_CUT,0,0); 
 end; 

 procedure TFormMain.ItemCopyClick(Sender:TObject); 
 begin
 RichEdit1.Perform(WM_COPY,0,0);
 end; 

 procedure TFormMain.ItemPasteClick(Sender:TObject); 
 begin
 RichEdit1.Perform(WM_Paste,0,0); 
 end;

 procedure TFormMain.ItemDeleteClick(Sender: TObject);
 begin
 RichEdit1.Perform(WM_CLEAR,0,0); 
 end; 

 procedure TFormMain.ItemSelectAllClick(Sender: TObject); 
 Var P:TPoint;
 begin 
 P:=Point(0,0); 
 P:=RichEdit1.ClientToScreen(P); 
 FindDialog1.Position := P;
 FindDialog1.Execute; 
 end; 

 procedure TFormMain.FindDialog1Find(Sender: TObject); 
 var
 FoundAt: LongInt;
 StartPos, ToEnd: Integer; 
 st:TSearchTypes; 
 begin 
 with RichEdit1 do 
 begin 
 StartPos :=SelStart + SelLength;
 ToEnd := Length(Richedit1.Text) - StartPos; 
 st:=[]; 
 if frMatchCase in FindDialog1.Options then 
 st:=st+[stMatchCase]; 
 if frWholeWord in FindDialog1.Options then 
 st:=st+[stWholeWord];
 FoundAt := FindText(FindDialog1.FindText,StartPos,Toend,st); 
 if FoundAt <> -1 then 
 begin 
 SelStart := FoundAt; 
 SelLength := Length(FindDialog1.FindText); 
 end else
 ShowMessage('????!') 
 end; 
 end; 

 procedure TFormMain.ItemAutoWarp1Click(Sender: TObject);
 begin
 RichEdit1.WordWrap:=not RichEdit1.WordWrap; 
 ItemAutoWarp1.Checked:=RichEdit1.WordWrap; 
 ItemAutoWarp.Checked:=RichEdit1.WordWrap; 
 end;

 Function TFormMain.MyOpenFile(FileName:String):Boolean; 
 begin
 Result:=False; 
 if CheckHasModified then
 begin 
 try 
 RichEdit1.Lines.LoadFromFile(FileName); 
 RichEdit1.Modified:=False 
 sFileName:=FileName;
 Result:=True; 
 except 
 on E:Exception do 
 ShowMessage(E.Message); 
 end;
 end; 
 end; 

 procedure TFormMain.ItemFindAndREplaceClick(Sender:TObject); 
 Var P:TPoint;
 begin 
 P:=Point(0,0); 
 P:=RichEdit1.ClientToScreen(P); 
 ReplaceDialog1.Position := P; 
 ReplaceDialog1.Execute;
 end; 

 procedure TFormMain.ReplaceDialog1Replace(Sender: TObject); 
 Var P:TPoint;
 begin
 P:=Point(0,0); 
 P:=RichEdit1.ClientToScreen(P); 
 ReplaceDialog1.Position := P; 
 ReplaceDialog1.Execute; 
 end;

 procedure TFormMain.ReplaceDialog1Replace(Sender: TObject): 
 var
 FoundAt: LongInt; 
 StartPos, ToEnd: Integer;
 st:TSearchTypes; 
 begin 
 with RichEdit1 do 
 begin 
 StartPos := SelStart + SelLength;
 ToEnd := Length(RichEdit1.Text) - StartPos; 
 st:=[]; 
 if frMatchCase in ReplaceDialog1.Options then 
 st:=st+[stMatchCase]; 
 if frWholeWord in ReplaceDialog1.Options.then
 st:=st+[stWholeWord]; 
 FoundAt := FindText(Replacedialog1.FindText, StartPos, ToEnd, st); 
 While FoundAt <> -1 do 
 begin 
 SelStart := FoundAt;
 SelLength := Length(ReplaceDialog1.FindText); 
 Perform(EM_REPLACESEL,1,Interger(PChar(Replacedialog1.ReplaceText))); 
 SelStart := FoundAt; 
 SelLength := Length(ReplaceDialog1.ReplaceText); 
 if not (frReplaceAll in ReplaceDialog1.Options) then
 Break; 
 StartPos:=SelStart; 
 ToEnd := Length(RichEdit1..Text) - StartPos; 
 FoundAt := FindText(ReplaceDialog1.FindText, StartPos,ToEnd,st); 
 end;
 FoundAt:= FindText(ReplaceDialog1.FindText,StartPos,ToEnd,st); 

 if FoundAt=-1 then
 ShowMessage('????!'); 
 end;
end;

 procedure TFormMain.FormCreate(Sender: TObject); 
 Var s1FileName,TemStr,ParamString:String;
 i,FromIndex,ToIndex,iPox:Integer;
 Reg:TRegistry; 
 begin 
 try 
 Reg:=TRegistry.Create;
 try 
 Reg.RootKey:=HKEY_CLASSES_ROOT; 
 Reg.OpenKey('*\shell\MyNotePad',True); 
 Reg.WriteString('','????????');
 Reg.CloseKey; 
 Reg.OpenKey('*\Shell\MyNotePad\Command',True); 
 Reg.WriteString('','"'+ParamStr(0)+'" "%1"'); 
 Reg.CloseKey;
 finally 
 Reg.Free; 
 end; 
 except
 end;
 if ParamCount>=1 then 
 begin 
 i:=1;
 ParamString:=ParamStr(i); 
 While (ParamString[1]='/') and(i<=ParamCount) do 
 begin 
 Inc(i);
 ParamString:=ParamStr(i); 
 end; 
 FromIndex:=i; 
 ParamString:=ParamStr(i);
 if ParamString[1]='/' then 
 Exit; 
 While i<=ParamCount do 
 begin
 if ParamString[1]='/' then 
 Break; 
 Inc(i); 
 ParamString:=ParamStr(i);
 end; 
 ToIndex:=i; 
 s1FileName:=GetCommandLine; 
 ParamString:=ParamStr(FromIndex);
 iPos:=Pos(ParamString,s1Filename); 
 TemStr:=ParamString; 
 Delete(s1Filename,1,iPos-1+length(TemStr));
 For i:=FromIndex+1 to ToIndex do 
 begin 
 ParamString:=ParamStr(i);
 iPos:=pos(ParamString,s1FileName); 
 TemStr:=TemStr+copy(s1FileName,1,iPos+length(ParamString)); 
 end;
 s1FileName:=TemStr; 
 MyOpenFile(s1FileName); 
 For i:=1 to ParamCount do
 begin 
 if (ParamStr(i)='/p') or(ParamStr(i)='/p' then 
 begin
 if Application.MessageBox('????','??',MB_YESNO+MB_ICONQUESTION) 
 =IDYes then 
 RichEdit1.Print(sFileName);
 Break; 
 end; 
 end
 end; 

end;

 procedure TFormMain.MenuFileClick(Sender: Tobject); 
 begin
 ItemSave.Enabled:=RichEdit1.Modified; 
 end;

 procedure TFormMain.FormCloseQuery(Sender: TObject; VAr CanClose:Boolean);
 begin
 CanClose:=not RichEdit1.Modified;
 if not CanClose then
 begin
 Case Application.MessageBox('??????!??????','??',
 MB_YESNOCANCEL+MB_ICONQUESTION) of
 IDYES:CanClose:=SaveFile;
 IDNo:CanClose:=True;
 end;
 end;
 end;

 procedure TFormMain.N14Click(Sender:TObject);
 begin
 AboutBox.showmodal;
 end;
end.

abraço

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...