0
我正在處理基於對話框的MFC應用程序,它由複選框,滾動條,按鈕,編輯控件等組成。我試圖將應用程序的當前狀態保存到.txt文件中,然後在應用程序再次啓動時將其加載回來。我使用CArchive類來序列化數據。使用序列化在基於對話框的MFC應用程序中保存並加載滾動條狀態。
//保存應用程序設置
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)」一樣。有人能指導我如何做這件事嗎?