Ado 实现C++对象的存取
2006-07-23 11:19:25 来源:WEB开发网本文示例源代码或素材下载
其实我最讨厌写东西了,最近忙死了,呵呵,前一段时间在做一个图形程序时,需要把C++对象保存到数据库里,刚开始真让我头疼啊,琢磨了一个下午,终于给做出来了,废话不说了,还是把自己的一些体会与各位同任一起分享!!!!
一、新建一个继承于 CObject 的子类 CLine;
头文件:Line.h class CLine : public CObject?
实现文件:Line.cpp
{
private :
LOGPEN m_logPen; //画笔
COLORREF m_crBackColor;
CArray<CPoint, CPoint &> m_PointArray; //标记类对应框
public:
int GetSize();
CPoint GetPoint(int pos);
void DrawLine(CDC *pDC,CPoint pt1,CPoint pt2,CRect rc);
void DrawBackGround(CDC *pDC,CRect rect);
void DrawPoint(CDC *pDC, CRect rect);
void SetWidth(int iWidth);
COLORREF GetColor();
void SetColor(COLORREF color);
COLORREF GetBkColor();
void SetBkColor(COLORREF color);
void AddPoint(CPoint point);
void Clear();
CLine();
virtual ~CLine();
virtual void Serialize(CArchive &ar);
CLine& operator=(CLine &src);
DECLARE_SERIAL(CLine)??
};//////////////////////////////////////////////////////////////////////
// Line.cpp: implementation of the CLine class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TestAdo.h"
#include "Line.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
IMPLEMENT_SERIAL(CLine,CObject,1)
CLine::CLine()
{
Clear();
}
CLine::~CLine()
{
}
//重写 =
CLine& CLine::operator=(CLine &src)
{
if(this!=&src)
{
m_logPen = src.m_logPen;
m_crBackColor = src.m_crBackColor;
}
return *this;?
}
//串行化操作
void CLine::Serialize(CArchive &ar)
{
if (ar.IsStoring())
{
ar << DWORD(m_crBackColor);
ar.Write(&m_logPen, sizeof(LOGPEN));
}
else
{
DWORD dw;
ar >> dw; m_crBackColor = COLORREF(dw);
ar.Read(&m_logPen, sizeof(LOGPEN));
}
m_PointArray.Serialize(ar);
}
void CLine::Clear()
{
m_crBackColor = RGB(255,255,255);
m_logPen.lopnStyle = PS_SOLID;
m_logPen.lopnWidth.x = 1;
m_logPen.lopnWidth.y = 1;
m_logPen.lopnColor = RGB(0, 0, 0);
m_PointArray.RemoveAll();
}
void CLine::AddPoint(CPoint point)
{
m_PointArray.Add(point);
}
void CLine::SetColor(COLORREF color)
{
m_logPen.lopnColor = color;
}
COLORREF CLine::GetColor()
{
return m_logPen.lopnColor;
}
void CLine::SetBkColor(COLORREF color)
{
m_crBackColor = color;
}
COLORREF CLine::GetBkColor()
{
return m_crBackColor;
}
void CLine::SetWidth(int iWidth)
{
m_logPen.lopnWidth.x = iWidth;
m_logPen.lopnWidth.y = iWidth;
}
//绘线条
void CLine::DrawPoint(CDC *pDC, CRect rect)
{
int len = m_PointArray.GetSize();
if (len <=0) return;
CPen pen;
pen.CreatePenIndirect(&m_logPen);
CPen *pOldPen = pDC->SelectObject(&pen);
CPoint pt = m_PointArray.GetAt(0);
pDC->MoveTo(pt);
for (int i=1; i< len; i++)
{
pt = m_PointArray.GetAt(i);
pDC->LineTo(pt);
}
pDC->SelectObject(pOldPen);
pOldPen = NULL;
pen.DeleteObject();
}
void CLine::DrawBackGround(CDC *pDC, CRect rect)
{
CBrush brushCtl;
brushCtl.CreateSolidBrush(GetBkColor());
pDC->Rectangle(rect);
pDC->FillRect(rect,&brushCtl) ;
brushCtl.DeleteObject();
}
void CLine::DrawLine(CDC *pDC,CPoint pt1, CPoint pt2, CRect rc)
{
CPen pen;
pen.CreatePenIndirect(&m_logPen);
CPen *pOldPen = pDC->SelectObject(&pen);
pDC->MoveTo(pt1);
pDC->LineTo(pt2);
pDC->SelectObject(pOldPen);
pOldPen = NULL;
pen.DeleteObject();
}
CPoint CLine::GetPoint(int pos)
{
if (pos>=0 && pos<m_PointArray.GetSize())?
{
return m_PointArray.GetAt(pos);
}
return CPoint(0,0);
}
int CLine::GetSize()
{
return m_PointArray.GetSize();
}
更多精彩
赞助商链接