Support for the latest file list in the MFC framework
The standard framework program established by MFC has the ability to record recent operation files. the paths of these recent files are recorded in the Registry and added to the File menu when the program is running.
There isCrecentfilelist *M_precentfilelist; the pointer manages this information.
The following process is analyzed and similar methods are used to save some other recent data with a fixed number.
1. Create a crecentfilelist object, read records, save records, and destroy objects.
① Creation and record reading
If loadstdprofilesettings is called in initinstance () in the cwinapp derived class, crecentfilelist is created, so the program can manage the list of recent files.
At the same time, read the previous records from the registry.
VoidCwinapp: loadstdprofilesettings (uintNmaxmru)// The default value is 4.
{
...
If(Nmaxmru! =0)
{
// Create a crecentfilelist object and initialize it to manage nmaxmru File Information
M_precentfilelist=NewCrecentfilelist (0,_ Afxfilesection,
_ Afxfileentry, nmaxmru );
// Read records
M_precentfilelist->Readlist ();
}
...
}
Key data member in the crecentfilelist object
Crecentfilelist contains a cstring pointerCstring *M_arrnames;It is used to point to a cstring object array, which records the most recent file name.
In additionIntM_nsizeIndicates the number of records.
When an object is created, the constructor completes initialization, including the establishment of the cstring array.
② Record storage and crecentfilelist destruction.
In exitinstance (), savestdprofilesettings () will be called. In savestdprofilesettings (), m_precentfilelist-> writelist (); operations are available to save the record.
DeleteM_precentfilelist; destroys an object.
Crecentfilelist: crecentfilelist (uintNstart,LpctstrLpszsection,
LpctstrLpszentryformat,IntNsize,IntNmaxdisplen)
{
Assert (nsize! =0 );
M_arrnames=NewCstring [nsize];// Create a cstring array.
M_nsize=Nsize;
M_nstart=Nstart;
M_strsectionname=Lpszsection;
M_strentryformat=Lpszentryformat;
M_nmaxdisplaylength=Nmaxdisplen;
}
3. Add record
When the document is saved, setpathname (...) is called, and setpathname (...) is called to call addtorecentfilelist in the application class.
In addtorecentfilelist, execute m_precentfilelist-> Add (lpszpathname) to add the file name to the string array.
VoidCrecentfilelist: add (lpctstrLpszpathname)
{
Assert (m_arrnames! =Null );
Assert (lpszpathname! =Null );
Assert (afxisvalidstring (lpszpathname ));
//FullyQualifyThePathName
TcharSztemp [_ max_path];
Afxfullpath (sztemp,Lpszpathname );// Obtain the full file path
//Search to see if this file name already exists
For(IntImru=0;Imru<M_nSize-1;
Imru ++)
{
If(Afxcomparepath (m_arrnames [imru],Sztemp ))
Break;//Imru