C ++ Builder implements menu display of historical file list

Source: Internet
Author: User
The C ++ Builder Menu displays the list of historical files-Linux general technology-Linux programming and kernel information. For more information, see the following. This section describes how to display a list of historical files in a menu in a Windows environment and describes how to use the TRegistry Class.

Currently, many Windows applications have the following function: You can view recently accessed files under the File menu, which makes it easy for users to access files that have been accessed again. Over the past few years, this technology has become a common feature of applications with file menu items: Wps series and Office series. In the previous DOS environment, programmers generally create a file to record the file list. Is there any other method in Windows? Recently, I have successfully implemented the above functions in the registry using the TRegedit class provided by C ++ Builder5.0 C/S. The introduction is 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

At the same time, add a menu item to the MainMenu1 control, whose attribute is

Name Caption

Items1 open a file

2. In unit1.h

Private:

TRegistry * Registry;

String Items [3]; // create an array for displaying historical files //

Int ItemsCount;

Void _ fastcall TForm1: Display (); // Display historical file records //

3. Enter the following content in the Items Click event:

Void _ fastcall TForm1: Items1Click (TObject * Sender)

{



String TempFile, Files;

OpenDialog1-> Filter = "All Files (*. *) | *.*";

If (OpenDialog1-> Execute ())

{

Files = OpenDialog1-> FileName; // get the file name //

For (int I = 0; I <3; I ++)

TempFile = Items [0];

If (ItemsCount <3)

ItemsCount ++;

For (int I = ItemsCount-1; I> 0; I --)

Items = Items [I-1]; // sorts Open history files //

Items [0] = Files; // keep the most recently opened file at the beginning //

}

Display ();

}

4. Create the Display function in unit. cpp.

Void _ fastcall TForm1: Display ()

{

TMenuItem * NewItem;

While (MainMenu1-> Items [MainMenu1-> Items-> Count-1]-> Count> 2)

{

MainMenu1-> Items [MainMenu1-> Items-> Count-1]->

Delete (MainMenu1-> Items [MainMenu1-> Items-> Count-1]-> Count-1 );

} // Remove the original historical file list //

For (int I = 0; I
{

NewItem = new TMenuItem (MainMenu1 );

NewItem-> Caption = Items;

MainMenu1-> Items [MainMenu1-> Items-> Count-1]-> Insert

(MainMenu1-> Items [MainMenu1-> Items-> Count-1]-> Count, NewItem );

} // Create a new historical file list //

}

5. Enter the following content in the Show event of Form1:

Void _ fastcall TForm1: FormShow (TObject * Sender)

{

Registry = new TRegistry;

ItemsCount = 0;

Registry-> RootKey = HKEY_LOCAL_MACHINE;

Registry-> OpenKey ("SOFTWARE \ MYCOMPANY \ Remember", TRUE );

// Open the primary key in the registry. If the primary key does not exist, create the primary key //

Items [0] = Registry-> ReadString ("Item1"); // read itemsString Value //

ItemsCount ++;

Items [1] = Registry-> ReadString ("Item2 ");

ItemsCount ++;

Items [2] = Registry-> ReadString ("Item3 ");

ItemsCount ++;



}

6. Enter the following content in the Show event of Form1:

Void _ fastcall TForm1: FormClose (TObject * Sender, TCloseAction & Action)

{

If (ItemsCount <3)

For (int I = ItemsCount + 1; I <= 3; I ++)

Items= "";

Registry-> WriteString ("Item1", Items [0]);

Registry-> WriteString ("Item2", Items [1]);

Registry-> WriteString ("Item3", Items [2]);

// Write items to the RegistryString Value //



}

The above programs are passed in the PWin98 and C ++ Builder5.0 environments.

In fact, many other functions of the program, such as: automatically save the program interface size, automatically remember the user password, also use

TRegedit is implemented in the registry. If you are interested, try it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.