我正在开发一个基于对话框的 MFC 应用程序,该应用程序由复选框、滚动条、按钮、编辑控件等组成。我正在尝试将应用程序的当前状态保存到 .txt 文件中,然后在应用程序再次启动时将其加载回来。我使用 CArchive 类来序列化数据。
// 保存应用程序设置
void CTestCalculatorDlg::OnSave()
{
this->UpdateData();
CFile f1;
CFileDialog l_SampleDlg(FALSE,".txt",NULL,0,"Text Files (*.txt)|*.txt|INI Files (*.ini)|*.ini|All Files (*.*)|*.*||");
if (l_SampleDlg.DoModal() == IDOK)
{
f1.Open(l_SampleDlg.GetPathName(), CFile::modeCreate | CFile::modeWrite);
CArchive ar(&f1,CArchive::store); //create an archive object and tie it to the file object
ar << N1 << m_OperandLeft << N2 << m_OperandRight << Res << m_Resulting << Typ << operation << m_operation << IsitChecked << m_checking; //serialize the data for the two edit boxes
ar.Close();
}
else
return;
f1.Close();
}
//从文件中加载设置
void CTestCalculatorDlg::OnOpen()
{
this -> UpdateData();
CFile f1;
CFileDialog l_SampleDlg(TRUE,".txt",NULL,0,"TXT Files (*.txt)|*.txt|INI Files (*.ini)|*.ini|All Files (*.*)|*.*||");
if (l_SampleDlg.DoModal() == IDOK)
{
if (f1.Open(l_SampleDlg.GetPathName(), CFile::modeRead) == FALSE)
return;
//create an archive object and tie it to the file object
CArchive ar(&f1,CArchive::load);
//serialize the data for the two edit boxes
ar >> N1 >> m_OperandLeft >> N2 >> m_OperandRight >> Res >> m_Resulting >> Typ >> operation >> m_operation >> IsitChecked >> m_checking >> m_Scrollingbar >> currScroll;;
ar.Close();
}
f1.Close();
this -> UpdateData(FALSE);
}
我能够保存和加载复选框、文本框中的数据以及单选按钮状态,但我发现很难恢复滚动条上次保存的位置。
//滚动条控制代码
void CTestCalculatorDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int CurPos = m_ScrollBar.GetScrollPos();
switch (nSBCode)
{
case SB_LEFT: // Scroll to far left.
CurPos = 0;
break;
case SB_RIGHT: // Scroll to far right.
CurPos = 100;
break;
case SB_ENDSCROLL: // End scroll.
break;
case SB_LINELEFT: // Scroll left.
if (CurPos > 0)
CurPos--;
break;
m_ScrollBar.SetScrollPos(CurPos);
CString szPosition;
int currScroll;
szPosition.Format("%d", CurPos);
SetDlgItemText(IDC_DECIMAL, szPosition);
currScroll = m_ScrollBar.GetScrollPos();
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}
我也不知道如何将静态文本链接到滚动条。我的意思是如果我在中间有滑块,它应该说“滑块在 50(范围:0-100)”。有人可以指导我如何做到这一点吗?