WriteProfileInt

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]

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();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值