国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

在CB環境中實現在菜單中顯示歷史文件列表

2019-11-17 05:28:49
字體:
來源:轉載
供稿:網友

  本文介紹了如何在Windows環境下實現菜單中顯示歷史文件列表,同時概要介紹了Tregistry類的使用方法。
 現在,在許多Windows應用程序都具有這樣一個功能:可以在文件菜單下面顯示列出最近訪問過的文件,這使用戶很輕易再次訪問曾經訪問過的文件。在過去幾年中,這項技術以成為擁有文件菜單項的應用程序的共同特色:如wps系列和Office系列。在以前的DOS環境中,程序員一般通過創建一個文件用來記錄文件列表;那么在Windows環境中,還有其他的方法嗎?最近筆者利用C++ Builder5.0 C/S版提供的Tregedit類成功在注冊表中實現了上述功能,現介紹如下:

1、在C++ Builder中新建一個工程文件PRoject1,并在Form1上添加如下控件:

控件名稱         屬性         值

TOpenDialog       Name         OpenDialog1
TMainMenu         Name         MainMneu1

同時在 MainMenu1控件中增加一個菜單項,其屬性為

Name             Caption
Items1           打開文件

2、在unit1.h中

private:
  Tregistry *Registry;
  String Items[3];//建立顯示歷史文件的數組//
  int ItemsCount;
  void _fastcall TForm1::Display();//顯示歷史文件記錄//



3、在Items的Click事件中輸入如下內容:

void __fastcall TForm1::Items1Click(Tobject *Sender)
{
String TempFile,Files;
OpenDialog1->Filter="All Files(*.*)*.*";
if(OpenDialog1->Execute())
  {
  Files=OpenDialog1->FileName;//取得文件名//
 for(int i=0;i<3;i++)
    TempFile=Items[0];
  if(ItemsCount<3)
    ItemsCount++;
  for(int i=ItemsCount-1;i>0;i--)
    Items[i]=Items[i-1];//對打開的歷史文件進排序//
  Items[0]=Files;//使最近打開的文件在最前面//
  }
Display();
}

4、在unit.cpp中建立Display函數

void _fastcall TForm1::Display()

{

TMenuItem *NewItem;

while(MainMenu1->Items->Items[MainMenu1->Items->Count-1]->Count>2)
{
MainMenu1->Items->Items[MainMenu1->Items->Count-1]->
Delete(MainMenu1->Items->Items[MainMenu1->Items->Count-1]->Count-1);
}//除去原有的歷史文件列表//
for(int i=0;i<ItemsCount;i++)
  {
  NewItem=new TMenuItem(MainMenu1);
  NewItem->Caption=Items[i];
  MainMenu1->Items->Items[MainMenu1->Items->Count-1]->Insert(MainMenu1->Items->Items[MainMenu1->Items->Count-1]->Count,NewItem);

  }//建立新的歷史文件列表//
}

5、在Form1的Show事件中輸入如下內容:

void __fastcall TForm1::FormShow(Tobject *Sender)
{
Registry =new Tregistry;
ItemsCount=0;
Registry->RootKey=HKEY_LOCAL_MACHINE;
Registry->OpenKey("SOFTWARE//MYCOMPANY//Remember",TRUE); //在注冊表中打開主鍵,假如該主鍵不存在則新建該主鍵//
Items[0]=Registry->ReadString("Item1");//讀items[i]字符串的值//
ItemsCount++;
Items[1]=Registry->ReadString("Item2");
ItemsCount++;
Items[2]=Registry->ReadString("Item3");
ItemsCount++;
}

6、在Form1的Show事件中輸入如下內容:

void __fastcall TForm1::FormClose(Tobject *Sender, TCloseAction &Action)
{
if(ItemsCount<3)
  for(int i=ItemsCount+1;i<=3;i++)
    Items[i]="";
Registry->WriteString("Item1",Items[0]);
Registry->WriteString("Item2",Items[1]);
Registry->WriteString("Item3",Items[2]); //向注冊表寫入items[i]字符串的值//
}
  以上程序在PWin98、C++Builder5.0環境中通過。
  其實許多程序的其他功能,如:自動保存程序界面大小、自動記憶用戶口令、也是利用Tregedit在注冊表中實現的。有愛好的讀者可以試一試。 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新密市| 弥勒县| 高碑店市| 开封市| 沙河市| 新竹市| 久治县| 漳平市| 兴城市| 芷江| 辉南县| 新宁县| 宁陕县| 博湖县| 新绛县| 东港市| 二连浩特市| 温州市| 鄂托克旗| 陆良县| 金门县| 东乌| 上犹县| 光泽县| 山东省| 金秀| 逊克县| 沂水县| 紫金县| 旬邑县| 临西县| 陆良县| 临湘市| 海城市| 星子县| 乌拉特前旗| 芦山县| 赤城县| 长阳| 汽车| 塘沽区|