Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ VC++
Dear Friends,
 
Im implementing serialization in the document/view architecture. In that im drawing multiple lines using mouse click event.
 
void CserializationTestDoc::Serialize(CArchive& ar)
{
    CserializationTestView s;
    if (ar.IsStoring())
    {
        // TODO: add storing code here
        ar<<s.l[0]<<s.l[1]<<s.PtLine;
    }
    else
    {
        // TODO: add loading code here
        ar>>s.l[0]>>s.l[1]>>s.PtLine;
    }
}
 
Here l[0],l[1] and PtLine are the variables which containing the values of points to draw the lines. Is that enough to include the variables in the serialize function in the document class. In the else statement how to call the OnDraw function.
 
Thankyou,
Posted 9-Dec-12 22:03pm
Edited 10-Dec-12 1:27am
v4
Comments
Richard MacCutchan at 10-Dec-12 4:38am
   
Assuming that is all the data in your document then yes, that should be enough. However, I suspect there is a lot more data somewhere that you are not showing.
J.Surjith Kumar at 10-Dec-12 5:10am
   
Im using that variables in the view class..
 
void CserializationTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
l[0]=point.x;
l[1]=point.y;
flag=1;
CView::OnLButtonDown(nFlags, point);
}
 
void CserializationTestView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
PtLine=CPoint(point.x,point.y);
/*if(flag==1)
{
dc.MoveTo(l[0],l[1]);
dc.LineTo(PtLine);
}*/
Invalidate();
CView::OnLButtonUp(nFlags, point);
}
 
void CserializationTestView::OnDraw(CDC* pDC)
{
CserializationTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->MoveTo(l[0],l[1]);
pDC->LineTo(PtLine);

// TODO: add draw code for native data here
}
J.Surjith Kumar at 10-Dec-12 5:12am
   
There is no error but the value of that variable was not retrieved when i open the file.
J.Surjith Kumar at 10-Dec-12 6:15am
   
How the serialize function knows that the object ar is for storing or loading. Can u explain how it works internally in the doc/view architecture without creating the CFile object and CArchive object.
Richard MacCutchan at 10-Dec-12 9:39am
   
The documentation for CArchive explains how it works and how you should implement it. One thing you should look at is your basic design here; you should not be mixing your serialization and drawing code in the same places. The MFC SDI template is based on the MVC model, the idea being that you separate the manipulation of your data from its display. Your serialization code should be inside your CDocument class, and your drawing code should be inside your view's OnDraw() method.
 
I am sure that I have explained this to you more than once already. Are you sure you are not posting the same questions under a different CodeProject name?
J.Surjith Kumar at 10-Dec-12 9:52am
   
I need to know how to collectively store the points and draw the lines using moveto and lineto functions. For every OnLButtonDown and OnLButtonUp one line have to be drawn. Finally collections of lines will be drawn how to store all the lines and how to use serialize concept according to that.
Richard MacCutchan at 10-Dec-12 12:34pm
   
1. Capture the user's input, mouse or keyboard, in your view class and collect the points and save them in a list, vector array, etc., depending on requirements.
2. In your view's OnDraw() function you should iterate through the points, drawing the various lines as you do so, remembering to take account of the view being scrolled beyond the starting point.
3. In your serialization code, save or load the points to/from the CArchive so that you have a permanent copy of them.
 
If you want more information then look at this tutorial.
J.Surjith Kumar at 11-Dec-12 4:34am
   
help me out to resolve from these errors.
JackDingler at 10-Dec-12 16:06pm
   
you don't need t he CArchive to keep a runtime copy of your data. You simply need to have your data in your doc class.
 
In your view, access the doc, and display the data it stores.
 
The serialization method is used to save and load your drawing from a file.
J.Surjith Kumar at 11-Dec-12 3:27am
   
I got the result by using CArray.
here is my code..
 
CArray<cpoint,cpoint> ptAr;
ptAr.Add(CPoint(0,0));
ptAr.Add(CPoint(10,10));
CPoint *pCp = (CPoint*)ptAr.GetData();
pDC->Polyline( pCp, ptAr.GetSize() );
 
I got this working to draw the lines dynamically.
 
but in the serialization im getting an error.
 
void CserializationTest3Doc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
ar<<pCp<<ptAr;
}
else
{
// TODO: add loading code here
ar>>pCp>>ptAr;
}
}
 
error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'CArchive' (or there is no acceptable conversio)
 
error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'CPoint *' (or there is no acceptable conversion)
 
warning C4800: 'CPoint *' : forcing value to bool 'true' or 'false' (performance warning)
Richard MacCutchan at 11-Dec-12 4:52am
   
What is pCp supposed to contain, and why are you trying to save a pointer - it has no meaning in that context? Why not just send the CArray containing all the points to the archive?
J.Surjith Kumar at 11-Dec-12 5:13am
   
now im trying to save only the CArray object ptAr but it shows the same kind of error.
J.Surjith Kumar at 11-Dec-12 5:05am
   
ok can you give me some example.
Richard MacCutchan at 11-Dec-12 5:36am
   
Sorry I do not have MFC on my system so I cannot check this on your behalf. I cannot see why your << operators are not being recognised. Take a look at this MSDN page, to see if that helps.
J.Surjith Kumar at 11-Dec-12 5:46am
   
Thanks Richard Its working :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100