This article describes how to display a list of history files in the Implementation menu in a Windows environment, with an overview of how the Tregistry class is used.
Many Windows applications now have the ability to display lists of recently accessed files under the File menu, which makes it easy for users to revisit files they have visited. Over the past few years, this technology has become a common feature of applications that have File menu items, such as the WPS series and Office series. In previous DOS environments, programmers typically created a file to record a list of files, so are there any other methods in the Windows environment? Recently, I use C + + Builder5.0 Tregedit class to provide the successful implementation of the above functions in the registry, is described as follows:
1. Create a new project file Project1 in C + + Builder and add the following controls on Form1:
Control Name property value
Topendialog Name OpenDialog1
Tmainmenu Name MAINMNEU1
Also, add a menu item to the MainMenu1 control whose properties are
Name Caption
ITEMS1 Open File
2, in the Unit1.h
private:
Tregistry *Registry;
String Items[3];//建立显示历史文件的数组//
int ItemsCount;
void _fastcall TForm1::Display();//显示历史文件记录//
3, in the Items click event to enter the following content:
void __fastcall Tform1::items1click (tobject *sender)
{
String Tempfile,files;
Opendialog1->filter= "All Files (*.*) |*.*";
if (Opendialog1->execute ())
{
files=opendialog1->filename;//get 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];//The Open history file into the order//
items[0]=files;//the most recently opened file on the front//
}
Display ();
}
4. Set up display function in Unit.cpp
void _fastcall TForm1::D isplay ()
{
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);
}//Remove the original history file list//
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);
}//Create a New History file list//
}
5, in the Form1 show event, enter the following content:
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, in the Form1 show event, enter the following content:
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]字符串的值//
}
The above procedures are passed in the PWin98, c++builder5.0 environment.
In fact, many of the other functions of the program, such as: Automatically save the program interface size, automatic memory user password, but also the use of Tregedit in the registry to achieve. Interested readers can give it a try.