Pergunta

I am working on a dialog based MFC application that consists of check boxes, scroll bars, buttons, edit control etc. I am trying to save the application's current state into a .txt file and then load it back when the application starts again. I use the CArchive class to serialize the data.

// Saving the application settings

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

}

//Loading the Setting from a File

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

}

I am able to save and load back the check boxes, data in the text boxes and also radio buttons state but I am finding it hard to restore the scroll bar last saved position.

//Code for Scroll bar control

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

}

I am also not knowing how to link a static text to a scroll bar. I mean if I have the slider at the middle, it should say something like "Slider at 50 (Range: 0-100)". Can some one direct me on how about doing this?

Foi útil?

Solução

When I search the MFC source files for CScrollBar::Serialize nothing is found. So it does not appear to support serialization. I suggest you maintain scroll bar state in a few member variables of your dialog class and serialize those values. Or you could derive your own class from CScrollBar and override the Serialize method. After loading the file use the saved variables to call SetScrollRange and SetScrollPos.

The code you show for writing to a static text control looks almost like what you need: Something like this:

szPosition.Format("Slider at %d", CurPos);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top