怎么把对象保存到文件中 vc
发布网友
发布时间:2022-09-10 15:42
我来回答
共5个回答
热心网友
时间:2024-05-08 14:47
#include "StdAfx.h"
#include "..\include\skinbuttonstate.h"
////////////////////////////////////////////////////
////////////////////////////////////////////////////
IMPLEMENT_SERIAL(CSkinScrollBarState,CObject,1)
CSkinScrollBarState::CSkinScrollBarState(void)
: m_bArrow1(FALSE)
, m_bArrow2(FALSE)
, m_bScrollBar(FALSE)
, m_bThurmb(FALSE)
{
m_imageArrow1= new CSkinImageRect();
m_imageArrow2= new CSkinImageRect();
m_imageScrollBar= new CSkinImageSection();
m_imageThurmb= new CSkinImageSection();
}
CSkinScrollBarState::~CSkinScrollBarState(void)
{
if(m_imageArrow1)
delete m_imageArrow1;
if(m_imageArrow2)
delete m_imageArrow2;
if(m_imageScrollBar)
delete m_imageScrollBar;
if(m_imageThurmb)
delete m_imageThurmb;
m_imageArrow1 = NULL;
m_imageArrow2 = NULL;
m_imageScrollBar = NULL;
m_imageThurmb = NULL;
}
void CSkinScrollBarState::Serialize(CArchive &ar)
{
CObject::Serialize(ar);
if(ar.IsStoring())
{
ar << m_bArrow1 << m_bArrow2 << m_bScrollBar << m_bThurmb;
}
else
{
ar >> m_bArrow1 >> m_bArrow2 >> m_bScrollBar >> m_bThurmb;
}
if(m_bArrow1)
m_imageArrow1->Serialize(ar);
if(m_bArrow2)
m_imageArrow2->Serialize(ar);
if(m_bScrollBar)
m_imageScrollBar->Serialize(ar);
if(m_bThurmb)
m_imageThurmb->Serialize(ar);
}
////////////////////////////////////////////////////
IMPLEMENT_SERIAL(CSkinWindowFrame,CObject,1)
CSkinWindowFrame::CSkinWindowFrame(void)
{
}
CSkinWindowFrame::~CSkinWindowFrame(void)
{
}
void CSkinWindowFrame::Serialize(CArchive &ar)
{
CObject::Serialize(ar);
m_imageTop.Serialize(ar);
m_imageBottom.Serialize(ar);
m_imageLeft.Serialize(ar);
m_imageRight.Serialize(ar);
}
bool CSkinWindowFrame::DrawTop(CDC *pDC,SkinRect rectDst)
{
if(m_imageTop.m_strImageName.IsEmpty())
return false;
else
{
m_imageTop.Draw(pDC,rectDst);
return true;
}
}
bool CSkinWindowFrame::DrawBottom(CDC *pDC,SkinRect rectDst)
{
if(m_imageBottom.m_strImageName.IsEmpty())
return false;
else
{
m_imageBottom.Draw(pDC,rectDst);
return true;
}
}
bool CSkinWindowFrame::DrawLeft(CDC *pDC,SkinRect rectDst)
{
if(m_imageLeft.m_strImageName.IsEmpty())
return false;
else
{
m_imageLeft.Draw(pDC,rectDst);
return true;
}
}
bool CSkinWindowFrame::DrawRight(CDC *pDC,SkinRect rectDst)
{
if(m_imageRight.m_strImageName.IsEmpty())
return false;
else
{
m_imageRight.Draw(pDC,rectDst);
return true;
}
}
////////////////////////////////////////////////////
IMPLEMENT_SERIAL(CSkinMenuBar,CObject,1)
CSkinMenuBar::CSkinMenuBar(void)
{
}
CSkinMenuBar::~CSkinMenuBar(void)
{
}
void CSkinMenuBar::Serialize(CArchive &ar)
{
CObject::Serialize(ar);
m_imageNormal.Serialize(ar);
m_imageActive.Serialize(ar);
m_imagePressed.Serialize(ar);
m_textNormal.Serialize(ar);
m_textGrayed.Serialize(ar);
m_textActive.Serialize(ar);
m_textPressed.Serialize(ar);
if(ar.IsLoading())
ar>>m_colorActive>>m_colorPressed;
else
ar<<m_colorActive<<m_colorPressed;
}
热心网友
时间:2024-05-08 14:48
用序列化技术。CArchive。
其实保存对象,实际就是保存类成员变量。具体可参考msdn:
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_vclib/html/1db772b1-ad55-4fcf-b133-126cca082510.htm
或
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_vclib/html/a829b6dd-bc31-47e0-8108-fbb946722db9.htm
热心网友
时间:2024-05-08 14:48
这涉及到C++的串行化过程。重载Doc类的Serialize函数,在其中将Doc的成员变量串行和并行化:
if (ar.IsStoring())
{
// TODO: add storing code here
ar<<m_var1<<m_var2<<m_var3;
}
else
{
// TODO: add loading code here
ar>>m_var1>>m_var2>>m_var3;
}
这里假设m_var1,m_var2,m_var3是Doc类的成员变量。记住if-else结构中的变量顺序一定要一致!
热心网友
时间:2024-05-08 14:49
这涉及到C++的串行化过程。重载Doc类的Serialize函数,在其中将Doc的成员变量串行和并行化
热心网友
时间:2024-05-08 14:50
路过,呵呵,都不懂你的问题,学习下。