⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 note.pas

📁 用Delphi编的一个简单的记事本,这里面实现了一些简单的功能~!
💻 PAS
字号:
unit note;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus,childform, ComCtrls, ToolWin, ImgList, StdCtrls,inifiles;

type
  TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Exit: TMenuItem;
    N3: TMenuItem;
    Print: TMenuItem;
    N4: TMenuItem;
    Save: TMenuItem;
    Open: TMenuItem;
    New: TMenuItem;
    Window: TMenuItem;
    Show1: TMenuItem;
    N8: TMenuItem;
    Tile1: TMenuItem;
    Help: TMenuItem;
    About1: TMenuItem;
    Close: TMenuItem;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    ToolBar1: TToolBar;
    tb_new: TToolButton;
    tb_open: TToolButton;
    tb_save: TToolButton;
    ToolButton4: TToolButton;
    tb_undo: TToolButton;
    tb_cut: TToolButton;
    tb_copy: TToolButton;
    ImageList1: TImageList;
    ToolButton8: TToolButton;
    tb_print: TToolButton;
    tb_preview: TToolButton;
    ToolBar2: TToolBar;
    ToolButton11: TToolButton;
    tb_paste: TToolButton;
    tb_bold: TToolButton;
    tb_Italic: TToolButton;
    tb_UnderLine: TToolButton;
    tb_color: TToolButton;
    ToolButton18: TToolButton;
    tb_left: TToolButton;
    tb_center: TToolButton;
    tb_right: TToolButton;
    StatusBar1: TStatusBar;
    ComboBox1: TComboBox;
    PrinterSetupDialog1: TPrinterSetupDialog;
    PrintDialog1: TPrintDialog;
    N1: TMenuItem;
    ComboBox2: TComboBox;
    ToolButton1: TToolButton;
    ColorDialog1: TColorDialog;
    PopupMenu1: TPopupMenu;
    FontDialog1: TFontDialog;
    procedure CloseClick(Sender: TObject);
    procedure NewClick(Sender: TObject);
    procedure OpenClick(Sender: TObject);
    procedure SaveClick(Sender: TObject);
    procedure Tile1Click(Sender: TObject);
    procedure Cascade1Click(Sender: TObject);
    procedure ArrangeAllClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure PrintSetupClick(Sender: TObject);
    procedure PrintClick(Sender: TObject);
    procedure MyClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ComboBox1Change(Sender: TObject);
    procedure tb_boldClick(Sender: TObject);//动态菜单OnClick事件响应
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  fname:string;
  achild:Tformchild;

  f1,f2,f3,f4,lines:tmenuitem;
  n:byte;//自动生成的子菜单个数

  myinifile:Tinifile;
implementation

{$R *.dfm}
procedure TMainForm.MyClick(Sender: TObject);//动态菜单OnClick事件响应
var
  i,p,l:integer;
  find:boolean;
  s:string;

begin
    s:=TMenuItem(Sender).Caption;      // 动态菜单标题
    p:=pos(':',TMenuItem(Sender).Caption);    //":"在菜单标题中的位置
    l:=length(TMenuItem(Sender).Caption);    //菜单标题长度
    fname:=copy(s,p-1,l-p+2);           //  取出菜单标题里面的文件路径

    i:=mdiChildCount;
    find:=false;
    while not find and (i>=0) do
      if mdiChildren[i].Caption=fname then
      begin
        mdiChildren[i].Show;
        find:=true;
        fname:=opendialog1.FileName;
      end
      else
        i:=i-1;
    if not find then
    begin
      achild:=tformchild.Create(application);
      achild.RichEdit1.Lines.LoadFromFile(fname);
      achild.Caption:=fname;
    
    end;

end;

procedure TMainForm.CloseClick(Sender: TObject);
begin
  self.ActiveMDIChild.Close;
end;

procedure TMainForm.NewClick(Sender: TObject);
begin
  achild:=tformChild.Create(application);
  achild.RichEdit1.Clear;
  getdir(0,fname);
  if length(fname)=3 then
    fname:=fname+'文档'+inttostr(MdiChildCount)
  else
    fname:=fname+'\文档'+inttostr(mdichildcount);
  achild.Caption:=fname;

end;

procedure TMainForm.OpenClick(Sender: TObject);
var
  i:integer;

  find:boolean;
begin
  if opendialog1.Execute and fileExists(opendialog1.FileName) then
  begin
    i:=mdiChildCount;
    find:=false;
    while not find and (i>=0) do
      if mdiChildren[i].Caption=opendialog1.FileName then
      begin
        mdiChildren[i].Show;
        find:=true;
        fname:=opendialog1.FileName;
      end
      else
        i:=i-1;
    if not find then
    begin
      achild:=tformchild.Create(application);
      achild.RichEdit1.Lines.LoadFromFile(opendialog1.FileName);
      achild.Caption:=opendialog1.FileName;
      fname:=opendialog1.FileName;
    end;
  end;


  //动态创建菜单

   try
     
     lines:=tMenuItem.Create(self);
     lines.Caption:='-';
     if n=1 then
     begin
       file1.Insert(file1.Count-1,lines);
       myinifile.WriteString('line','分割线',lines.Caption);        //写入ini文件
     end;

     f1:=tMenuItem.Create(self);
     f1.Caption:=inttostr(n)+'  '+fname;
     file1.Insert(file1.Count-2,f1);
     myinifile.WriteString('path'+inttostr(n),'文件路径',f1.Caption);  //写入ini文件
  
     f1.OnClick:=myclick;
   except
     f1.Free;
     lines.Free;

     raise;
   end;
  n:=n+1;
  if n=5 then n:=1;
end;

procedure TMainForm.SaveClick(Sender: TObject);
var
  curChild:Tformchild;
begin
  curChild:=self.ActiveMDIchild as Tformchild;
  if pos('文档',fname)<>0 then
  begin
    if savedialog1.Execute then
    begin
      fname:=savedialog1.FileName;

      curchild.richedit1.Lines.savetofile(fname);
      curchild.Caption:=fname;

    end;
  end
  else
  begin
    fname:=self.ActiveMDIChild.Caption;

    curchild.richedit1.Lines.savetofile(fname);
    curchild.Caption:=fname;
  end;

end;

procedure TMainForm.Tile1Click(Sender: TObject);
begin
 Tile;
end;

procedure TMainForm.Cascade1Click(Sender: TObject);
begin
  cascade ;
end;

procedure TMainForm.ArrangeAllClick(Sender: TObject);
begin
  ArrangeIcons;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  combobox1.Items:=screen.Fonts;
  n:=1;
end;

procedure TMainForm.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  combobox1.Canvas.FillRect(rect);
  combobox1.Canvas.Font.Name:=combobox1.Items[index];
  combobox1.Canvas.Font.Size:=0;
  combobox1.Canvas.TextOut(rect.Left+1,rect.Top,combobox1.Items[index])   
end;

procedure TMainForm.PrintSetupClick(Sender: TObject);
begin
  try
    PrinterSetupDialog1.Execute
  except
    Application.MessageBox(Pchar('无法找到默认的打印机'+#13+#10+'请确认打印机已安装正确。'),Pchar(Application.Title),MB_ICONEXCLAMATION);
  end;
end;

procedure TMainForm.PrintClick(Sender: TObject);
begin
  try
    formchild.RichEdit1.Print(FName);
  except
    Application.MessageBox(Pchar('无法找到默认的打印机'+#13+#10+'请确认打印机已安装正确。'),Pchar(Application.Title),MB_ICONEXCLAMATION);
  end;
end;

procedure TMainForm.FormShow(Sender: TObject);
begin
  myinifile:=tinifile.Create('files.ini');
  try
    



    lines:=tmenuitem.Create(self);

    lines.Caption:=myinifile.ReadString('line','分割线','');
    if lines.Caption<>'' then
      file1.Insert(file1.Count-1,lines);

    f1:=tmenuItem.Create(self);
    f1.Caption:=myinifile.ReadString('path1','文件路径','');
    if f1.Caption<>'' then
      file1.Insert(file1.Count-2,f1);

   
    f2:=tmenuItem.Create(self);
    f2.Caption:=myinifile.ReadString('path2','文件路径','');
    if f1.Caption<>'' then
      file1.Insert(file1.Count-2,f2);

    f3:=tmenuItem.Create(self);
    f3.Caption:=myinifile.ReadString('path3','文件路径','');
    if f1.Caption<>'' then
      file1.Insert(file1.Count-2,f3);

    f4:=tmenuItem.Create(self);
    f4.Caption:=myinifile.ReadString('path4','文件路径','');
    if f1.Caption<>'' then
      file1.Insert(file1.Count-2,f4);

    f1.OnClick:=myclick;
    f2.OnClick:=myclick;
    f3.OnClick:=myclick;
    f4.OnClick:=myclick;
  except
    lines.Free;
    f1.Free;
  end;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  myinifile.Destroy;
end;

procedure TMainForm.ComboBox1Change(Sender: TObject);
begin
   FormChild.richedit1.SelAttributes.Name:=combobox1.Text;

end;

procedure TMainForm.tb_boldClick(Sender: TObject);
begin
     if tb_bold.Down then
     FormChild.richedit1.SelAttributes.Style:=FormChild.richedit1.SelAttributes.Style+[fsbold]
 else
     FormChild.richedit1.SelAttributes.Style:=FormChild.richedit1.SelAttributes.Style-[fsbold];

end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -