MFC中如何将应用程序的配置信息保存到注册表中(二)

来源:互联网 发布:为知笔记 退出团队 编辑:程序博客网 时间:2024/05/21 10:34

在上一篇中介绍了几个写入注册表数据和读取注册表数据的接口,并介绍了使用方法。

这一片教你如何使得你的应用程序在下次打开时保持上一次关闭前的状态。

在上一篇添加的代码的基础上,要添加WM_CLOSE消息的响应函数,因为我们只有在窗口关闭前要保存窗口的位置信息,所以保存窗口位置到注册表的代码要写在这个消息处理函数。

代码如下:

void CTestClassDlg::OnClose() {if (AfxGetApp()->GetProfileInt("Settings", "SavePosition", 1)){//保存窗口的位置WINDOWPLACEMENT wp;GetWindowPlacement(&wp);AfxGetApp()->WriteProfileInt("Settings", "FrameStatus", wp.showCmd);AfxGetApp()->WriteProfileInt("Settings", "FrameTop",    wp.rcNormalPosition.top);AfxGetApp()->WriteProfileInt("Settings", "FrameLeft",   wp.rcNormalPosition.left);AfxGetApp()->WriteProfileInt("Settings", "FrameBottom", wp.rcNormalPosition.bottom);AfxGetApp()->WriteProfileInt("Settings", "FrameRight",  wp.rcNormalPosition.right);}CDialog::OnClose();}

在注册表中保存有应用程序关闭前的位置信息,在下一次打开的时候我们就可以取得这些数据来使得应用程序的窗口显示出关闭前的样子。

在MFC中窗口的初始化的代码一般都添加在OnInitDialog()函数中。

代码如下:

BOOL CTestClassDlg::OnInitDialog(){CDialog::OnInitDialog();................................................................// TODO: Add extra initialization hereint s, t, b, r, l;if (AfxGetApp()->GetProfileInt("Settings", "SavePosition", 1)){// only restore if there is a previously saved positionif ( -1 != (s = AfxGetApp()->GetProfileInt("Settings", "FrameStatus",   -1)) && -1 != (t = AfxGetApp()->GetProfileInt("Settings", "FrameTop",      -1)) && -1 != (l = AfxGetApp()->GetProfileInt("Settings", "FrameLeft",     -1)) && -1 != (b = AfxGetApp()->GetProfileInt("Settings", "FrameBottom",   -1)) && -1 != (r = AfxGetApp()->GetProfileInt("Settings", "FrameRight",    -1))   ) {WINDOWPLACEMENT wp;// restore the window's statuswp.showCmd = s;// restore the window's width and heightwp.rcNormalPosition.bottom = b;wp.rcNormalPosition.right = r;// the following correction is needed when the taskbar is// at the left or top and it is not "auto-hidden"RECT workArea;SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);l += workArea.left;t += workArea.top;// make sure the window is not completely out of sightint max_x = GetSystemMetrics(SM_CXSCREEN) -GetSystemMetrics(SM_CXICON);int max_y = GetSystemMetrics(SM_CYSCREEN) -GetSystemMetrics(SM_CYICON);wp.rcNormalPosition.top = min(t, max_y);wp.rcNormalPosition.left = min(l, max_x);SetWindowPlacement(&wp);}}return TRUE;  // return TRUE  unless you set the focus to a control}

运行应用程序


然后调整一下窗口的大小和位置:


关闭程序,再次打开你就会发现应用程序的窗口大小和位置和上次的是一样的。你可以测试一下!


下面是注册表的变化截图:



写的不是很详细,但是这些东西都是一层窗户纸的东西。没有必要深究,也没有什么可深究的。重要的是会用就可以了。

为了方便以后再次用到时快速的复习,也为了帮助初学的人,做了上面的简单整理!


下面是一些参考代码,可以借鉴一下!

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


0 0