delphi_基于RichEdit的简易文本编辑器

2017-12-19 18:58:08来源:CSDN作者:qq_32869085人点击

分享

#简陋的界面


#代码如下

unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls, ComCtrls;type  TForm1 = class(TForm)    redt1: TRichEdit;    btn1: TButton;    btn2: TButton;    btn3: TButton;    btn4: TButton;    btn5: TButton;    btn6: TButton;    btn7: TButton;    btn8: TButton;    btn9: TButton;    btn10: TButton;    dlgOpen1: TOpenDialog;    dlgSave1: TSaveDialog;    dlgFont1: TFontDialog;    procedure btn1Click(Sender: TObject);    procedure btn2Click(Sender: TObject);    procedure btn3Click(Sender: TObject);    procedure btn4Click(Sender: TObject);    procedure btn5Click(Sender: TObject);    procedure btn6Click(Sender: TObject);    procedure redt1SelectionChange(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure btn10Click(Sender: TObject);    procedure btn8Click(Sender: TObject);    procedure btn9Click(Sender: TObject);    procedure btn7Click(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;var  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);begin  redt1.CopyToClipboard;  //复制end;procedure TForm1.btn2Click(Sender: TObject);begin  redt1.CutToClipboard; //剪切end;procedure TForm1.btn3Click(Sender: TObject);begin  redt1.SelText:='';     //删除end;procedure TForm1.btn4Click(Sender: TObject);begin  redt1.PasteFromClipboard; //粘贴end;procedure TForm1.btn5Click(Sender: TObject);begin  redt1.Undo; //取消,仅对有些操作有效end;procedure TForm1.btn6Click(Sender: TObject);begin  redt1.HideSelection:=False; //选定文本反底显示  redt1.SelectAll; //选定所以文本end;procedure TForm1.redt1SelectionChange(Sender: TObject);begin  if redt1.SelLength=0  then begin           //如果未做选定,则“复制”“删除”“剪切”按钮无效    btn1.Enabled:=False;    btn2.Enabled:=False;    btn3.Enabled:=False;  end  else begin      //如果选择了文本,则有效    btn1.Enabled:=True;    btn2.Enabled:=True;    btn3.Enabled:=True;  end;end;procedure TForm1.FormCreate(Sender: TObject);begin  if redt1.SelLength=0  then begin           //如果未做选定,则“复制”“删除”“剪切”按钮无效    btn1.Enabled:=False;    btn2.Enabled:=False;    btn3.Enabled:=False;  end  else begin      //如果选择了文本,则有效    btn1.Enabled:=True;    btn2.Enabled:=True;    btn3.Enabled:=True;  end;end;procedure TForm1.btn10Click(Sender: TObject);    //字体begin  dlgFont1.Font.Assign(redt1.DefAttributes);  //打开对话框中的字体与RichEdit中的默认字体一样  if dlgFont1.Execute then       //判断是否执行  redt1.SelAttributes.Assign(dlgFont1.Font);end;procedure TForm1.btn8Click(Sender: TObject);         //保存begin  dlgSave1.Filter:='RTF FIle (*.RTF)|*.RTF';  dlgSave1.DefaultExt:='RTF';      //保存为RTF格式  if redt1.Modified then    if dlgSave1.Execute then      begin        redt1.Lines.SaveToFile(dlgSave1.FileName);        redt1.Modified:=False;      end;end;procedure TForm1.btn9Click(Sender: TObject);       //新建var i:Word;begin  if redt1.Modified         //如果RichEdit中内容被修改  then begin    i:=MessageDlg('文件被修改,是否保存?',mtConfirmation,[mbyes,mbNo,mbCancel],1);    if i=mryes then    begin      btn8Click(Sender);      redt1.Text:='';      redt1.Modified:=False;                //新建后,设置标志    end;    {      if dlgSave1.Execute then      begin        //redt1.Lines.SaveToFile(dlgSave1.FileName);        btn8Click(Sender);                         //保存后再新建        redt1.Text:='';        redt1.Modified:=False;                //新建后,设置标志      end;      }    if i=mrno then    begin      redt1.Text:='';                            //不保存,直接新建      redt1.Modified:=False;                     //新建后,返回标志    end;  end  else begin                                     //如果RichEdit中的内容没有被修改,则直接新建    redt1.Text:='';    redt1.Modified:=False;                       //新建后,设置标志  end;end;procedure TForm1.btn7Click(Sender: TObject); //打开var i:Word;begin  dlgOpen1.Filter:='RTF FIle (*.RTF)|*.RTF';  dlgOpen1.DefaultExt:='RTF';      //保存为RTF格式  if redt1.Modified         //如果RichEdit中内容被修改  then begin    i:=MessageDlg('文件被修改,是否保存?',mtConfirmation,[mbyes,mbNo,mbCancel],1);    if i=mryes then    begin      btn8Click(Sender);      if dlgOpen1.Execute then        redt1.Lines.LoadFromFile(dlgOpen1.FileName);      redt1.Modified:=False;    //打开后,设置标志    end;    if i=mrno then      if dlgOpen1.Execute then    begin      redt1.Lines.LoadFromFile(dlgOpen1.FileName);                            //不保存,直接打开      redt1.Modified:=False;                     //打开后,返回标志    end;  end  else  begin    if dlgOpen1.Execute then                                   //如果RichEdit中的内容没有被修改,则直接打开    redt1.Lines.LoadFromFile(dlgOpen1.FileName);    redt1.Modified:=False;                       //新建后,设置标志  end;end;end.


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台