好友
阅读权限25
听众
最后登录1970-1-1
|
小可爱~
发表于 2017-1-28 21:33
本帖最后由 小可爱~ 于 2017-1-28 22:51 编辑
这是界面
这是line的头文件
[C++] 纯文本查看 复制代码 #pragma once
#include "afx.h"
class CLine :
public CObject
{
public:
CPoint m_poFrom;
CPoint m_poTo;
COLORREF m_clrLine;
public:
CLine()
{
}
CLine::CLine(CPoint from, CPoint to)
{
this->m_poFrom = from;
this->m_poTo = to;
}
CLine::CLine(CPoint from, CPoint to, COLORREF clrLine)
{
this->m_poFrom = from;
this->m_poTo = to;
this->m_clrLine = clrLine;
}
void setCLine(CPoint from, CPoint to)
{
this->m_poFrom = from;
this->m_poTo = to;
}
void setCLine(CPoint from, CPoint to, COLORREF clrLine)
{
this->m_poFrom = from;
this->m_poTo = to;
this->m_clrLine = clrLine;
}
CLine::~CLine()
{
}
DECLARE_SERIAL(CLine)
virtual void Serialize(CArchive& ar);
};
这是line.cpp文件源码
[C++] 纯文本查看 复制代码 #include "stdafx.h"
#include "Line.h"
IMPLEMENT_SERIAL(CLine, CObject, 2 | VERSIONABLE_SCHEMA)
void CLine::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
if (ar.IsStoring())
{ // storing code
ar << m_poFrom << m_poTo << m_clrLine;
}
else
{ // loading code
UINT nSchema = ar.GetObjectSchema();
switch (nSchema)
{
case 1://这个是版本一的输出
ar >> m_poFrom >> m_poTo;
m_clrLine = RGB(0, 0, 0);
break;
case 2:
ar >> m_poFrom >> m_poTo >> m_clrLine;
break;
default:
AfxThrowArchiveException(CArchiveException::badSchema);
break;
}
}
}
这是主要源码
[C++] 纯文本查看 复制代码
void C串行化CArchiveDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CFile file(_T("demo.txt"), CFile::modeWrite | CFile::modeCreate);
int a = 10;
int b = 100;
CArchive ArcFile(&file, CArchive::store);
//现在需要保存 任何数据类型都是比较简单的事
ArcFile << a << b;//写入变量的值
m_line.setCLine(CPoint(10, 10), CPoint(100, 100));
ArcFile << &m_line;
//line.Serialize(ArcFile);//写入对象的属性 //使用这个方法的话就不能做多版本了
ArcFile.Close();
file.Close();
MessageBox(TEXT("串行化成功"), TEXT("提示:"), 0);
}
void C串行化CArchiveDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
CFile file(_T("demo.txt"), CFile::modeRead);
CArchive ar(&file, CArchive::load);
int a, b;
ar >> a >> b;//获取文件内部的变量 a b 的值
CLine *liTmp = &m_line;
ar >> liTmp;
//line.Serialize(ar);//获取文件的 line 对象的数据
m_edit.Empty();
CString string;
string.Format(_T("a = %d\r\n"), a);
m_edit.Append(string);
string.Format(_T("b = %d\r\n"), b);
m_edit.Append(string);
string.Format(_T("Line form = (%d, %d), to = (%d, %d)\r\n"), liTmp->m_poFrom.x, liTmp->m_poFrom.y,
liTmp->m_poTo.x, liTmp->m_poTo.y);
m_edit.Append(string);
ar.Close();
file.Close();
UpdateData(FALSE);
}
void C串行化CArchiveDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
CFile file(_T("file.txt"), CFile::modeReadWrite | CFile::modeCreate);
CArchive ar(&file, CArchive::store);
int a, b;
a = 10;
b = 100;
ar << a << b;
m_line.setCLine(CPoint(10, 10), CPoint(100, 100), COLORREF(RGB(0, 255, 0)));
ar << &m_line;
ar.Close();
file.Close();
MessageBox(_T("串行话成功"), _T("提示:"), 0);
}
void C串行化CArchiveDlg::OnBnClickedButton4()
{
// TODO: 在此添加控件通知处理程序代码
CFile file(_T("file.txt"), CFile::modeRead);
CArchive ar(&file, CArchive::load);
int a, b;
ar >> a >> b;
CLine *pLiTmp = &m_line;
ar >> pLiTmp;
CString string;
string.Format(_T("a = %d, b = %d\n"), a, b);
m_edit.Empty();
m_edit.Append(string);
CString strColor;
switch (pLiTmp->m_clrLine)
{
case RGB(255, 0, 0):
strColor.Format(_T("红色"));
break;
case RGB(0, 255, 0):
strColor.Format(_T("绿色"));
break;
case RGB(0, 0, 255):
strColor.Format(_T("蓝色"));
break;
}
string.Format(_T("line form = (%d, %d), to = (%d, %d), color = "), pLiTmp->m_poFrom.x,
pLiTmp->m_poFrom.y, pLiTmp->m_poTo.x, pLiTmp->m_poTo.y);
string.Append(strColor);
m_edit.Append(string);
ar.Close();
file.Close();
UpdateData(FALSE);
}
HBRUSH C串行化CArchiveDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何特性
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
switch (pWnd->GetDlgCtrlID())
{
case IDC_EDIT1:
pDC->SetTextColor(m_line.m_clrLine);
break;
default:
break;
}
return hbr;
} |
|