Page 1 of 1

TChart and MFC (CArchive specifically).

Posted: Thu Jun 28, 2007 8:24 am
by 9530487
Hi all,

I need to save my TCharts within a COleDocument derived class. At the moment it is done via serialisation and I have separate .TEE files that contain the graph definitions. I really need to store everything in the single file so I can implement some new functionality.

So far I am having little success.

Many thanks in advance,

Tony.

PS. I have been experimenting with the following code.

//Saving
COleVariant tempvariant = pTChart->GetExport().GetAsNative().SaveToStream(TRUE);
archive << tempvariant;

//LoadingCOleVariant tempvariant =
COleVariant tempvariant;
archive >> tempvariant;
pTChart->GetImport().LoadFromStream(tempvariant);

Posted: Mon Jul 02, 2007 10:04 am
by Pep
Hi Tony,

I remeber a customer which did similar thing ( at least the use of the streams), please find below a copy of its post and the code he used.
What I wanted to do was to print/preview
an image of the chart displayed with TeeChart onto the printer by
using ComponentOne's VSView printer component.

If anyone knows about an easier way, please let me know.

Regards,
Holger Persch

To the completeness enclosed the source code.
===============================
void CVSViewTestChartDlg::OnCopyChartToPrinter()
{
// export the chart as metafile
CMetafileExport metaFileExport = m_Chart.GetExport().GetAsMetafile();

COleVariant vtStream;
vtStream = metaFileExport.SaveToStream();
ASSERT(vtStream.vt & (VT_ARRAY | VT_UI1));

// attach the stream to SafeArray
COleSafeArray safeArray;
safeArray.Attach(vtStream);
ASSERT(safeArray.GetDim() == 1);

// calculate the stream's size
LONG lLBound, lUBound, cElements;
safeArray.GetLBound(1, &lLBound);
safeArray.GetUBound(1, &lUBound);
cElements = lUBound - lLBound + 1;

// access the data from the SafeArray
LPBYTE pArrayData;
safeArray.AccessData((LPVOID *)&pArrayData);

// alloc memory for the metafile data
HGLOBAL hGlobal = GlobalAlloc(GHND, cElements);
if (hGlobal != NULL)
{
// get a pointer to the data ...
LPBYTE pBytes = (LPBYTE)GlobalLock(hGlobal);
if (pBytes != NULL)
{
// ... and copy it now
memcpy(pBytes, pArrayData, cElements);
// don't forget to unlock the data
GlobalUnlock(hGlobal);

CPictureHolder pictureHolder;
CComPtr<IStream> spStream;

// create a stream from the memory data
if (S_OK == CreateStreamOnHGlobal(hGlobal, FALSE, &spStream))
{
// load the picture from the stream interface
if (S_OK == OleLoadPicture(spStream, 0, FALSE, IID_IPicture,
(void**)&pictureHolder.m_pPict))
{
// get the PictureDisp interface
LPPICTUREDISP ipPicture = pictureHolder.GetPictureDispatch();

// at last draw the picture onto the printer (VSView printer component)
COleVariant vtNA(0L, VT_ERROR);
m_Printer.StartDoc();
m_Printer.GetMargins();

m_Printer.DrawPicture(ipPicture, m_Printer.GetX1(), m_Printer.GetY1(),
COleVariant(m_Printer.GetX2().dblVal -
m_Printer.GetX1().dblVal),
COleVariant(m_Printer.GetY2().dblVal -
m_Printer.GetY1().dblVal),
vtNA, vtNA);
m_Printer.EndDoc();

// release the interface
ipPicture->Release();
}
}
}

// clean up
GlobalFree(hGlobal);
}

// unaccess the data from the SafeArray
safeArray.UnaccessData();
}

Posted: Mon Jul 02, 2007 11:16 am
by 9530487
Many thanks, that was just what I needed.

All the best,

Tony.