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

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

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

在上一篇添加的代码的基础上,要添加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 here
    int s, t, b, r, l;

    if (AfxGetApp()->GetProfileInt("Settings", "SavePosition", 1))
    {
        // only restore if there is a previously saved position
        if ( -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 status
            wp.showCmd = s;

            // restore the window's width and height
            wp.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 sight
            int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值