0. Introduction
A set of methods for reading and writing application configuration is provided in the CWinApp class:
Getprofileint
Writeprofileint
GetProfileString
WriteProfileString
It is easy to read and write application configuration.
1. About the Cwinapp::setregistrykey method
After using VC + + wizards to build the MFC project, you can see this statement in InitInstance:
Setregistrykey (_t ("native application generated by Application Wizard");
This function will set up a working environment for several of the methods mentioned above, and if you write data with Writeprofileint, you will
is written to the following registry location:
Hkey_current_user\software\ Application Wizard generated local application \ Application name \
If Setregistrykey is not executed in InitInstance, writing to the data with Writeprofileint writes to the
%windir%\ the application name. ini.
2. Usage
A. If the Setregistrykey is executed in InitInstance ("The Breeze");
For the following:
Writeprofileint ("section", "Val1", 10);
The data will be written to the following path in the registry:
[hkey_current_user\software\ Wind house \ Test Application \section]
"Val1" =dword:0000000a
Note: "Test Application" is the name of the application.
B. If Setregistrykey is not executed in InitInstance
For the following:
Writeprofileint ("section", "Val1", 10);
will be written in the "%windir%\ test application. ini":
[Section]
val1=10
3. Example: Save the application's window size and location
Change the Big Hour
void Cmydlg::onsize (UINT nType, int cx, int cy)
if (m_binitdialog && cx<nx && cy<ny)
{
Save current size position
CRect Rcwindow;
GetWindowRect (&rcwindow);
Theapp.writeprofileint ("Settings", "left", rcwindow.left);
Theapp.writeprofileint ("Settings", "Top", rcwindow.top);
Theapp.writeprofileint ("Settings", "right", rcwindow.right);
Theapp.writeprofileint ("Settings", "bottom", rcwindow.bottom);
}
When you move a window
void Cmydlg::onmove (int x, int y)
{
if (m_binitdialog && x!=0 && y!=0)
{
Save current size position
CRect Rcwindow;
GetWindowRect (&rcwindow);
Theapp.writeprofileint ("Settings", "left", rcwindow.left);
Theapp.writeprofileint ("Settings", "Top", rcwindow.top);
Theapp.writeprofileint ("Settings", "right", rcwindow.right);
Theapp.writeprofileint ("Settings", "bottom", rcwindow.bottom);
}
}
Initialization
BOOL Cmydlg::oninitdialog ()
{
CRect Rcwindow;
Rcwindow.left=theapp.getprofileint ("Settings", "left", 200);
Rcwindow.top=theapp.getprofileint ("Settings", "top", 120);
Rcwindow.right=theapp.getprofileint ("Settings", "right", 800);
Rcwindow.bottom=theapp.getprofileint ("Settings", "bottom", 600);
MoveWindow (&rcwindow);
}
Save application configuration in MFC using Writeprofileint, etc.