CWinApp类中提供了一组用于读写应用程序配置的方法:
GetProfileInt
WriteProfileInt
GetProfileString
WriteProfileString
可方便的用于读写应用程序配置。
1.关于CWinApp::SetRegistryKey方法
用VC++的向导建立MFC项目之后,在InitInstance中可以看到这样的语句:
SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
该函数将为以上提到的几个方法建立工作环境,此时如果用WriteProfileInt写入数据,将会
被写入到如下注册表位置:
HKEY_CURRENT_USER\Software\应用程序向导生成的本地应用程序\应用程序名称\
如果在InitInstance中不执行SetRegistryKey,则用WriteProfileInt写入数据时,将写入到
%windir%\应用程序名称.ini中。
2.用法
a.如果在InitInstance中执行了SetRegistryKey("应用程序向导生成的本地应用程序");
则对于:
WriteProfileInt("section","val1",10);
将在注册表中如下路径写入数据:
[HKEY_CURRENT_USER\Software\应用程序向导生成的本地应用程序\应用程序名称\section]
"val1"=dword:0000000a
b.如果在InitInstance中没执行SetRegistryKey
则对于:
WriteProfileInt("section","val1",10);
将在“%windir%\测试应用程序.ini”中写入:
[section]
GetProfileInt
WriteProfileInt
GetProfileString
WriteProfileString
可方便的用于读写应用程序配置。
1.关于CWinApp::SetRegistryKey方法
用VC++的向导建立MFC项目之后,在InitInstance中可以看到这样的语句:
SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
该函数将为以上提到的几个方法建立工作环境,此时如果用WriteProfileInt写入数据,将会
被写入到如下注册表位置:
HKEY_CURRENT_USER\Software\应用程序向导生成的本地应用程序\应用程序名称\
如果在InitInstance中不执行SetRegistryKey,则用WriteProfileInt写入数据时,将写入到
%windir%\应用程序名称.ini中。
2.用法
a.如果在InitInstance中执行了SetRegistryKey("应用程序向导生成的本地应用程序");
则对于:
WriteProfileInt("section","val1",10);
将在注册表中如下路径写入数据:
[HKEY_CURRENT_USER\Software\应用程序向导生成的本地应用程序\应用程序名称\section]
"val1"=dword:0000000a
b.如果在InitInstance中没执行SetRegistryKey
则对于:
WriteProfileInt("section","val1",10);
将在“%windir%\测试应用程序.ini”中写入:
[section]
val1=10
void CMainFrame::ActivateFrame(int nCmdShow)
{
if (m_bFirst)
{
m_bFirst = FALSE;
WINDOWPLACEMENT* pWndpl = new WINDOWPLACEMENT;
pWndpl->length = sizeof(WINDOWPLACEMENT);
CWinApp* pApp = AfxGetApp();
//恢复窗口位置
pWndpl->flags = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
_T("FLAGS"), 0);
pWndpl->showCmd = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
_T("SHOWCMD"), 0);
nCmdShow = pWndpl->showCmd;
pWndpl->ptMinPosition.x = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
_T("MINX"), 0);
pWndpl->ptMinPosition.y = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
_T("MINY"), 0);
pWndpl->ptMaxPosition.x = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
_T("MAXX"), 0);
pWndpl->ptMaxPosition.y = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
_T("MAXY"), 0);
pWndpl->rcNormalPosition.top = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
_T("TOP"), 0);
pWndpl->rcNormalPosition.left = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
_T("LEFT"), 0);
pWndpl->rcNormalPosition.right = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
_T("RIGHT"), 0);
pWndpl->rcNormalPosition.bottom = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
_T("BOTTOM"), 0);
//设置窗口位置
SetWindowPlacement(pWndpl);
delete pWndpl;
}
CFrameWnd::ActivateFrame(nCmdShow);
}
void CMainFrame::OnClose()
{
WINDOWPLACEMENT* pWndpl = new WINDOWPLACEMENT;
pWndpl->length = sizeof(WINDOWPLACEMENT);
//获得窗口位置
GetWindowPlacement(pWndpl);
CWinApp* pApp = AfxGetApp();
//保存窗口位置
pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("FLAGS"),
pWndpl->flags);
pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("SHOWCMD"),
pWndpl->showCmd);
pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MINX"),
pWndpl->ptMinPosition.x);
pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MINY"),
pWndpl->ptMinPosition.y);
pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MAXX"),
pWndpl->ptMaxPosition.x);
pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MAXY"),
pWndpl->ptMaxPosition.y);
pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("TOP"),
pWndpl->rcNormalPosition.left);
pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("LEFT"),
pWndpl->rcNormalPosition.top);
pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("RIGHT"),
pWndpl->rcNormalPosition.right);
pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("BOTTOM"),
pWndpl->rcNormalPosition.bottom);
delete pWndpl;
CFrameWnd::OnClose();
}