unit1.cpp

来自「C++Builder高级应用开发指南随书源码」· C++ 代码 · 共 148 行

CPP
148
字号
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
   FileList=new TStringList;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ExitItemClick(TObject *Sender)
{
   Close();     
}
//---------------------------------------------------------------------------
void TForm1::AddFile(AnsiString FileName)
{
   int i;
   for(i=0;i<FileList->Count;i++)//如果历史文件列表中已经有了则将其先去掉
   {
      if(FileName==FileList->Strings[i])
      {
         FileList->Delete(i);
         break;
      }
   }
   FileList->Insert(0,FileName);//将当前文件名加入为第一个
   if(FileList->Count>FileCount)//如果历史文件数目超过最大数目就去掉最后一个
   {
      FileList->Delete(FileCount);
   }
}
//---------------------------------------------------------------------------
void TForm1::LoadFromReg()
{
   TRegistry *Registry;
   Registry =new TRegistry;
   AnsiString temp;
   int i;
   Registry->RootKey=HKEY_LOCAL_MACHINE;
   Registry->OpenKey("SOFTWARE\\EXAMPLE\\HISTORYFILES\\",TRUE);
   //在注册表中打开主键,如果该主键不存在则新建该主键//
   for(i=0;i<FileCount;i++)
   {
      temp=AnsiString("Item")+AnsiString(i);
      temp=Registry->ReadString(temp);
      if(!temp.IsEmpty())//如果不是空字符串
      {
          FileList->Add(temp);
      }
      else
      {
         break;
      }
   }
   DisplayFiles();//在菜单项上显示历史文件列表
   delete Registry;
}
//---------------------------------------------------------------------------
void TForm1::WriteToReg()
{
   TRegistry *Registry;
   Registry =new TRegistry;
   AnsiString temp;
   int i;
   Registry->RootKey=HKEY_LOCAL_MACHINE;
   Registry->OpenKey("SOFTWARE\\EXAMPLE\\HISTORYFILES\\",TRUE);
   //在注册表中打开主键,如果该主键不存在则新建该主键//
   for(i=0;i<FileList->Count;i++)
   {
      temp=AnsiString("Item")+AnsiString(i);
      Registry->WriteString(temp,FileList->Strings[i]);
   }
   for(i=FileList->Count;i<FileCount;i++)
   {
      temp=AnsiString("Item")+AnsiString(i);
      Registry->WriteString(temp,"");
   }
   delete Registry;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OpenItemClick(TObject *Sender)
{
   if(OpenDialog1->Execute())
   {
      Memo1->Lines->LoadFromFile(OpenDialog1->FileName);
      AddFile(OpenDialog1->FileName);
      DisplayFiles();
   }
}
//---------------------------------------------------------------------------
void TForm1::DisplayFiles()
{
   TMenuItem *MenuItem;
   int i;
   int Count;

   Count=MainMenu1->Items->Items[0]->Count;
   for(i=2;i<Count-1;i++)//如果当前菜单有历史文件列表,将其清空
   {
       MainMenu1->Items->Items[0]->Delete(2);//每次只需要删除第三项即可
   }
   for(i=0;i<FileList->Count;i++)//将历史文件列表加入菜单项
   {
      MenuItem=new TMenuItem(MainMenu1->Items->Items[0]);
      MenuItem->Caption="&"+AnsiString(i+1)+" "+FileList->Strings[i];
      MenuItem->OnClick=HistoryFileClick;//新加入的菜单项的OnClick事件处理函数
      MainMenu1->Items->Items[0]->Insert(i+2,MenuItem);
   }
   if(FileList->Count>0)//加入下分割线
   {
      MenuItem=new TMenuItem(MainMenu1->Items->Items[0]);
      MenuItem->Caption="-";
      MainMenu1->Items->Items[0]->Insert(MainMenu1->Items->Items[0]->Count-1,MenuItem);
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormShow(TObject *Sender)
{
   LoadFromReg();     
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
   WriteToReg();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HistoryFileClick(TObject *Sender)
{
   AnsiString TheFileName;
   TheFileName=((TMenuItem *)Sender)->Caption;
   TheFileName.Delete(1,3);//删除文件名字符串前的数字和空格
   Memo1->Lines->LoadFromFile(TheFileName);
   AddFile(TheFileName);
   DisplayFiles();
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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