如何在mfc中进行对象序列化? [英] How to do the Object Serialization in mfc?

查看:303
本文介绍了如何在mfc中进行对象序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我已经从列表控件创建了gridcontrol。现在我想保存列表控件的对象,当我打开应用程序时我想再次加载它。



所以,请帮助我...







谢谢。

Hello,

i have created gridcontrol from list control. now i want to save the object of the list control and when i open the application i want to load it again.

So, please help me...



Thanks.

推荐答案

背景信息MFC 序列化: Microsoft Visual C ++ / MFC文件处理:序列化 [ ^ ]。


经过大量搜索我得到了我的解决方案就是这样。



1)拿CArray。

i已经从CObject创建了一个CSave类。

在头文件中我必须声明这样。

class CSave:public CObject

{

public:

CSave();

CSave(CSave& item);

CSave& operator =(CS ave& item);

虚拟~CSave();

公开:

CString str [120];

}



2)在源文件(.cpp)中声明如下



After Searching a lot i got my solution and it is like this.

1) Take CArray.
i have created a CSave class from CObject.
In the header file i have to declare like this.
class CSave : public CObject
{
public:
CSave();
CSave(CSave &item);
CSave &operator =(CSave &item);
virtual ~CSave();
public:
CString str[120];
}

2) In the source file(.cpp) declare like this

CSave::CSave(CSave &item)
{
    int j=0;
    for(int Row=0;Row<20;Row++)
    {
        for(int Col=0;Col<6;Col++,j++)
        {
            str[j]=item.str[j];
        }
    }
}





3)对于序列化,请这样做





3) For Serialization do like this

void CSave::Serialize(CArchive& ar)
{
    CObject::Serialize(ar);

    if( ar.IsStoring() )
    {
        int j=0;
        for(int Row=0;Row<20;Row++)
        {
            for(int Col=0;Col<6;Col++,j++)
            {
                ar<<str[j];
            }
        }
    }

     else
    {
        int j=0;
        for(int Row=0;Row<20;Row++)
        {
            for(int Col=0;Col<6;Col++,j++)
            {
                ar>>str[j];
            }
        }
    }
   
}







4)在对话框源文件(.cpp)中添加此项以将数据添加到序列化....






4)In the dialog source file (.cpp) add this for adding data to serialization....

CSave itm;
    int j=0;
    for(int Row=0;Row<20;Row++)
    {
        for(int Col=0;Col<6;Col++,j++)
        {
            itm.str[j]=m_lstDemo.GetItemText(Row,Col);
        }
    }
    CSave *item = new CSave(itm);
    ListOfData.Add(item);







5)用于读取数据..




5) For Reading data back ..

CSave *itm=reinterpret_cast<CSave *>(ListOfData[0]);

    int j=0;
    for(int Row=0;Row<20;Row++)

    {

        for(int Col=0;Col<6;Col++,j++)

        {

            m_lstDemo.SetItemText(Row,Col,itm->str[j]);
        }
    }





就是这样......



That's it...


这篇关于如何在mfc中进行对象序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆