void CControlView::OnNMClickPlotdataTree(NMHDR *pNMHDR, LRESULT *pResult) { // TODO: 在此添加控件通知处理程序代码 *pResult = 0; UpdateData(TRUE); clicked_flag = TRUE; CSmartCarDoc * pDoc_temp=(CSmartCarDoc*)m_pDocument; for (int i=0;i<9;i++) { BOOL hehe = m_PlotTree.GetCheck(hPlot[i]); pDoc_temp->plot[i].SetShowFlag(m_PlotTree.GetCheck(hPlot[i])); } //序列化保存配置信息 CFile file (_T ("abc.txt"), CFile::modeCreate | CFile::modeWrite); CArchive ar (&file, CArchive::store); for (int i=0;i<9;i++) { pDoc_temp->plot[i].Serialize(ar); } ar.Flush(); //读完毕,关闭文件流 ar.Close(); file.Close(); *pResult = 0; }
void CSimpleReport::LayoutLoad(const char* id, const char* tag) { UINT nBytes = 0; LPBYTE pData = 0; if (!AfxGetApp()->GetProfileBinary(id, tag, &pData, &nBytes)) return; CMemFile memFile(pData, nBytes); CArchive ar (&memFile,CArchive::load); try { SerializeState(ar); } catch (COleException* pEx) { pEx->Delete (); } catch (CArchiveException* pEx) { pEx->Delete (); } ar.Close(); memFile.Close(); delete[] pData; }
BOOL CBCGPRegistry::Read(LPCTSTR pszKey, LPPOINT& lpPoint) { ASSERT(m_hKey); BOOL bSucess = FALSE; BYTE* pData = NULL; UINT uDataSize; Read (pszKey, &pData, &uDataSize); try { if (pData != NULL && m_Info.lMessage == ERROR_SUCCESS && m_Info.dwType == REG_BINARY) { CMemFile file (pData, uDataSize); CArchive ar (&file, CArchive::load); ar.m_bForceFlat = FALSE; ASSERT(ar.IsLoading()); CDWordArray dwcArray; ASSERT(dwcArray.IsSerializable()); dwcArray.Serialize(ar); ar.Close(); if (dwcArray.GetSize() == 2) { lpPoint->x = dwcArray.GetAt(0); lpPoint->y = dwcArray.GetAt(1); bSucess = TRUE; } } } catch (CMemoryException* pEx) { pEx->Delete (); TRACE(_T("Memory exception in CBCGPRegistry::Read ()!\n")); } catch (CArchiveException* pEx) { pEx->Delete (); TRACE(_T("CArchiveException exception in CBCGPRegistry::Read ()!\n")); } m_Info.dwType = REG_POINT; m_Info.dwSize = sizeof(POINT); if (pData != NULL) { delete [] pData; pData = NULL; } return bSucess; }
//**************************************************************************************** BOOL CBCGPGridSerializeManager::SerializeFrom (CFile& file) { ASSERT (m_pOwnerGrid != NULL); CArchive archive (&file, CArchive::load | CArchive::bNoFlushOnDelete); BOOL bResult = ReadFromArchive (archive); archive.Close (); return bResult; }
//**************************************************************************************** BOOL CBCGPGridSerializeManager::SerializeTo (CFile& file) { ASSERT (m_pOwnerGrid != NULL); CArchive archive (&file, CArchive::store | CArchive::bNoFlushOnDelete); BOOL bResult = WriteToArchive (archive); archive.Close (); return bResult; }
void CSimpleReport::LayoutSave(const char* id, const char* tag) { CMemFile memFile; CArchive ar (&memFile,CArchive::store); SerializeState(ar); ar.Flush(); DWORD nBytes = (DWORD)memFile.GetPosition(); LPBYTE pData = memFile.Detach(); AfxGetApp()->WriteProfileBinary(id, tag, pData, nBytes); ar.Close(); memFile.Close(); free(pData); }
void CeditorDoc::Serialize(CArchive& ar) { std::wstring filename( ar.m_strFileName ); bool bStore = ar.IsStoring() == TRUE; ar.Close(); if( bStore ) { std::ofstream os( filename ); if( !os.good() ) { error( "Could not save file, might be write protected or something." ); os.close(); return; } m_entityList->Serialize( os ); if( !os.good() ) error( "Error saving file, maybe out of space?" ); os.close(); } else { if( !m_entityList ) m_entityList = new CEditorEntityList(); std::ifstream is( filename ); if( !is.good() ) { error( "Could not read file" ); is.close(); return; } is.close(); CScriptParser parser; if( !parser.ParseFile( CW2A(filename.c_str()) ) ) { error( "Error parsing file." ); return; } m_entityList->Deserialize(&parser); } }
void CControlView::OnTvnItemChangedPlotdataTree(NMHDR *pNMHDR, LRESULT *pResult) { NMTVITEMCHANGE *pNMTVItemChange = reinterpret_cast<NMTVITEMCHANGE*>(pNMHDR); // TODO: 在此添加控件通知处理程序代码 //UpdateData(TRUE); if (Initial_flag && clicked_flag)//防止bug { CMainFrame *pMain=(CMainFrame *)AfxGetApp()->m_pMainWnd; CPlotView* pPlotView_tempt=(CPlotView*)pMain->pPlotView; CSmartCarDoc * pDoc_temp=(CSmartCarDoc*)m_pDocument; for (int i=0; i<3; i++) pPlotView_tempt->Window[i].SetShowFlag(m_PlotTree.GetCheck(hWindow[i])); for (int i=0;i<9;i++) pDoc_temp->plot[i].SetShowFlag(m_PlotTree.GetCheck(hPlot[i])); //序列化保存配置信息 CFile file (_T ("abc.txt"), CFile::modeCreate | CFile::modeWrite); CArchive ar (&file, CArchive::store); for (int i=0;i<9;i++) { pDoc_temp->plot[i].Serialize(ar); } ar.Flush(); //读完毕,关闭文件流 ar.Close(); file.Close(); //保存窗口坐标轴信息 CFile file1 (_T ("Axis.txt"), CFile::modeCreate | CFile::modeWrite); CArchive ar1 (&file1, CArchive::store); for (int i=0;i<3;i++) { pPlotView_tempt->Window[i].Serialization(ar1); } ar1.Flush(); //读完毕,关闭文件流 ar1.Close(); file1.Close(); clicked_flag = FALSE; //更新视图 pPlotView_tempt->Invalidate(FALSE); } *pResult = 0; }
void CSingleView::OnLButtonDown(UINT nFlags, CPoint point) { // step 1) 파일 객체 생성 CFile* pFile = new CFile(); ASSERT (pFile != NULL); if ( !pFile->Open("foo.dat", CFile::modeWrite|CFile::modeCreate) ) { // Handle error return; } // step 2: CArchive 객체 생성 bool bReading = false; // ... for writing CArchive* pArchive = NULL; try { pFile->SeekToBegin(); UINT uMode = (bReading ? CArchive::load : CArchive::store); pArchive = new CArchive(pFile, uMode); ASSERT (pArchive != NULL); } catch (CException* pException) { // Handle error return; } // step 3: 직렬화 맴버 함수 호출 CFoo* pFoo = new CFoo(); pFoo->Serialize( *pArchive ); delete pFoo; // step 4: 다리 객체와 파일 객체 파괴 pArchive->Close(); delete pArchive; pFile->Close(); delete pFile; CView::OnLButtonDown(nFlags, point); }
/* CPBaseDataSource method that reads the serialized data from the memory file specified by <p hData. @rdesc The list of objects serialized out of the memory file. @devnote The list returned has been dynamically allocated and it is the responsibility of the caller to free its memory. */ CFrogOleDataList * CPBaseDataSource::DeSerialize( HANDLE hData ) // @parm A handle to a memory file containing the data. { // associate global memory handle with shared memory file CSharedFile mf; mf.SetHandle(hData); CFrogOleDataList *pList = NULL; TRY { // read from shared memory file into object list CArchive ar (&mf, CArchive::load); pList = new CFrogOleDataList; pList->Serialize (ar); // disconnect archive from shared memory file ar.Close(); } CATCH(CArchiveException, archExcept) { CBExceptionReporter::ArchiveException(archExcept); delete pList; pList = NULL; }
void CMFCLineEditorDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { /* DataPoint d; d.addElement(99.9); d.addElement(88.8); g_table.setDataPoint(0,d); if(g_table.saveTable(ar, g_table.getPath())) { CSavedDlg dlg; dlg.DoModal(); } else { CSaveFailDlg dlg; dlg.DoModal(); } */// TODO: add storing code here ar.Close(); } else { /*/ TODO: add loading code here char temp[MAX_PATH]; int i=0; WideCharToMultiByte(CP_ACP, 0,ar.GetFile()->GetFilePath().GetBuffer(),-1,temp,MAX_PATH,NULL,NULL); while(temp[i]>0)i++; temp[i]=0; string val(temp); g_table.initialize(val); */ } }
void CControlView::OnInitialUpdate() { CFormView::OnInitialUpdate(); //加载窗口信息 CMainFrame* pFrame=(CMainFrame*)AfxGetMainWnd(); CPlotView *pPlotView_temp=(CPlotView*)pFrame->pPlotView; CFile file ; if(file.Open(_T ("Axis.txt"), CFile::modeRead))//打开了文档 { try { CArchive ar (&file, CArchive::load); for (int i=0;i<3;i++) { pPlotView_temp->Window[i].Serialization(ar); } ar.Flush(); ar.Close(); } catch (CArchiveException*) { //若反序列化失败 AfxMessageBox(_T("未正确读取坐标,将采用默认配置")); IsAxisFailed = TRUE; } file.Close(); } else//文档不存在,创建新文档 { AfxMessageBox(_T("坐标配置文件不存在,创建默认文件")); IsAxisFailed = TRUE; } //若曲线信息文件加载失败,创建默认文件 if (IsAxisFailed) { CAxisConfigDlg dlg_temp; dlg_temp.SaveAxisConfig(); } if (((CSmartCarDoc*)m_pDocument)->IsFailedReadConfig) { CWavePlotDlg tem_dlg; tem_dlg.p_doc = (CSmartCarDoc*)m_pDocument; tem_dlg.saveconfig(); } CString str; for (int i=1;i<25;i++) { str.Format(_T("COM%d"),i); m_ComNum.AddString(str); } str_baudrate.push_back(_T("4800")); str_baudrate.push_back(_T("9600")); str_baudrate.push_back(_T("19200")); str_baudrate.push_back(_T("38400")); str_baudrate.push_back(_T("57600")); str_baudrate.push_back(_T("115200"));//CString类型存储的是string变量的首地址,每个元素占四个字节 for (int i=0;i < static_cast<int>(str_baudrate.size());i++) { m_Baudrate.AddString(str_baudrate[i]); } m_nBaudrate = str_baudrate.size()-1; UpdateData(FALSE); CSmartCarDoc * pDoc_temp=(CSmartCarDoc*)m_pDocument; //创建句柄时将曲线名称信息显示至树状图中 hRoot = m_PlotTree.InsertItem(_T("WavePlot"), 0, 0); hWindow[0] = m_PlotTree.InsertItem(_T("Window1"), hRoot, TVI_LAST); hWindow[1] = m_PlotTree.InsertItem(_T("Window2"), hRoot, TVI_LAST); hWindow[2] = m_PlotTree.InsertItem(_T("Window3"), hRoot, TVI_LAST); hPlot[0]=m_PlotTree.InsertItem(pDoc_temp->plot[0].GetTitle(), hWindow[0], TVI_LAST); hPlot[1]=m_PlotTree.InsertItem(pDoc_temp->plot[1].GetTitle(), hWindow[0], TVI_LAST); hPlot[2]=m_PlotTree.InsertItem(pDoc_temp->plot[2].GetTitle(), hWindow[0], TVI_LAST); hPlot[3]=m_PlotTree.InsertItem(pDoc_temp->plot[3].GetTitle(), hWindow[1], TVI_LAST); hPlot[4]=m_PlotTree.InsertItem(pDoc_temp->plot[4].GetTitle(), hWindow[1], TVI_LAST); hPlot[5]=m_PlotTree.InsertItem(pDoc_temp->plot[5].GetTitle(), hWindow[1], TVI_LAST); hPlot[6]=m_PlotTree.InsertItem(pDoc_temp->plot[6].GetTitle(), hWindow[2], TVI_LAST); hPlot[7]=m_PlotTree.InsertItem(pDoc_temp->plot[7].GetTitle(), hWindow[2], TVI_LAST); hPlot[8]=m_PlotTree.InsertItem(pDoc_temp->plot[8].GetTitle(), hWindow[2], TVI_LAST); m_PlotTree.Expand(hRoot,TVE_EXPAND); m_PlotTree.Expand(hWindow[0],TVE_EXPAND); m_PlotTree.Expand(hWindow[1],TVE_EXPAND); m_PlotTree.Expand(hWindow[2],TVE_EXPAND); m_PlotTree.SetCheck(hRoot); for (int i=0;i<9;i++) { if(pDoc_temp->plot[i].GetShowFlag()) { m_PlotTree.SetCheck(hPlot[i]); } } for (int i=0;i<3;i++) { if(pPlotView_temp->Window[i].GetShowFlag()) m_PlotTree.SetCheck(hWindow[i]); } Initial_flag = TRUE; // OnBnClickedOpencom();//一开始就打开串口 // TODO: 在此添加专用代码和/或调用基类 }
//**************************************************************************************** BOOL CBCGPGridSerializeManager::DoDropRows (CBCGPGridItemID idDropTo, CBCGPGridItemID idDragFrom, BOOL bMove, BOOL bInsertAfter) { ASSERT (m_pOwnerGrid != NULL); ASSERT (m_ClipboardFormatType == CF_Rows); if (GetRangeCount () == 0) { return FALSE; // no data } // Copied rows are already removed if called from Paste or dragged from another app BOOL bNoRemove = idDragFrom.IsNull (); //--------------------- // Calculate new offset //--------------------- idDragFrom.SetNull (); if (bInsertAfter) { idDropTo.m_nRow++; } CBCGPGridItemID idTo = GetDropOffset (idDragFrom, idDropTo); ASSERT (idTo.m_nRow >= 0); ASSERT (idTo.m_nColumn >= 0); //---------------- // Mark selection: (save ranges to remove selection later) //---------------- CleanUpImplementationData (); if (bNoRemove) { m_arrCutRanges.SetSize (0); } else { m_arrCutRanges.SetSize (GetRangeCount ()); for (int i = 0; i < GetRangeCount (); i++) { CBCGPGridRange rangeOrder; if (!GetRange (i, rangeOrder)) { return FALSE; } rangeOrder.m_nTop += m_idRangesOffset.m_nRow; rangeOrder.m_nBottom += m_idRangesOffset.m_nRow; m_arrCutRanges [i] = (CMarkedGridRange) rangeOrder; if (rangeOrder.m_nTop < idDropTo.m_nRow && rangeOrder.m_nBottom >= idDropTo.m_nRow) { //idDropTo = rangeOrder.m_nTop; return FALSE; // can't insert inside range } } } //------------- // Drop ranges: //------------- for (int i = 0; i < GetRangeCount (); i++) { CBCGPGridRange rangeOrder; if (!GetRange (i, rangeOrder)) { return FALSE; } if (rangeOrder.m_nTop < 0 || rangeOrder.m_nBottom < rangeOrder.m_nTop) { return FALSE; } const int nRowOffset = -rangeOrder.m_nTop + idTo.m_nRow + (int)m_lstNewRows.GetCount (); rangeOrder.m_nTop += nRowOffset; rangeOrder.m_nBottom += nRowOffset; //------------ // Drop range: //------------ UINT nDataSize = 0; BYTE* pData = GetRangeData (i, nDataSize); if (pData == NULL || nDataSize <= 0) { return FALSE; } CMemFile f(pData, nDataSize); CArchive archive (&f, CArchive::load); if (!ReadRowsFromArchive (archive, rangeOrder)) { return FALSE; } archive.Close (); } if (!m_bSkipData) { //--------------------------- // Remove previous selection: //--------------------------- CBCGPGridItemID id; m_pOwnerGrid->SetCurSel (id, SM_NONE, FALSE); //------------------------- // Insert new rows to grid: //------------------------- InsertNewSelection (idTo.m_nRow, m_lstNewRows); if (bMove) { //--------------------- // Remove marked items (previous selection): //--------------------- if (!RemovePreviosSelection ()) { return FALSE; // can't clear non empty items } } UpdateSelectionRect (idTo); m_pOwnerGrid->AdjustLayout (); //------------------- // Set new selection: //------------------- CBCGPGridRange rangeSel = m_InsertRange; if (rangeSel.IsValid ()) { CBCGPGridItemID idFirst (rangeSel.m_nTop); CBCGPGridItemID idSecond (rangeSel.m_nBottom); m_pOwnerGrid->SetCurSel (idFirst, SM_FIRST_CLICK | SM_SINGE_SEL_GROUP, FALSE); m_pOwnerGrid->SetCurSel (idSecond, SM_SECOND_CLICK | SM_CONTINUE_SEL_GROUP, TRUE); } } CleanUpImplementationData (); return TRUE; }
//**************************************************************************************** BOOL CBCGPGridSerializeManager::DoDropItems (CBCGPGridItemID idDropTo, CBCGPGridItemID idDragFrom, BOOL bMove) { // idDropTo - index // idDragFrom - index ASSERT (m_pOwnerGrid != NULL); ASSERT (m_ClipboardFormatType == CF_Items); if (GetRangeCount () == 0) { return FALSE; // no data } //--------------------- // Calculate new offset //--------------------- CBCGPGridItemID idTo = GetDropOffset (idDragFrom, idDropTo); ASSERT (idTo.m_nRow >= 0); ASSERT (idTo.m_nColumn >= 0); //------------------------------------ // Can replace content of target area: //------------------------------------ if (!m_bSkipData) { if (!CanReplaceNewSelection (idTo, TRUE)) { return FALSE; // can't clear non empty items } } if (!m_bSkipData && bMove) { //------------------------------------- // Clear content of previous selection: //------------------------------------- if (!ClearPreviousSelection (FALSE)) { return FALSE; // can't clear non empty items } } //------------- // Drop ranges: //------------- int i = 0; for (i = 0; i < GetRangeCount (); i++) { CBCGPGridRange rangeOrder; if (!GetRange (i, rangeOrder)) { return FALSE; } rangeOrder.m_nLeft += idTo.m_nColumn; rangeOrder.m_nRight += idTo.m_nColumn; rangeOrder.m_nTop += idTo.m_nRow; rangeOrder.m_nBottom += idTo.m_nRow; if (rangeOrder.m_nBottom > m_nLastRow || rangeOrder.m_nRight > m_nLastColumn) { return FALSE; // out of grid range } //------------ // Drop range: //------------ UINT nDataSize = 0; BYTE* pData = GetRangeData (i, nDataSize); if (pData == NULL || nDataSize <= 0) { return FALSE; } CMemFile f(pData, nDataSize); CArchive archive (&f, CArchive::load | CArchive::bNoFlushOnDelete); if (!ReadItemsFromArchive (archive, rangeOrder)) { return FALSE; } archive.Close (); } if (!m_bSkipData) { //--------------------------- // Remove previous selection: //--------------------------- UpdateSelectionRect (m_idRangesOffset); //--------------- // Set selection: //--------------- BOOL bFirst = TRUE; for (i = 0; i < GetRangeCount (); i++) { CBCGPGridRange rangeOrder; if (!GetRange (i, rangeOrder)) { continue; } rangeOrder.m_nLeft += idTo.m_nColumn; rangeOrder.m_nRight += idTo.m_nColumn; rangeOrder.m_nTop += idTo.m_nRow; rangeOrder.m_nBottom += idTo.m_nRow; CBCGPGridRange rangeIndex; if (!OrderToIndex (rangeOrder, rangeIndex)) { continue; } CBCGPGridItemID idFirst (rangeIndex.m_nTop, rangeIndex.m_nLeft); CBCGPGridItemID idSecond (rangeIndex.m_nBottom, rangeIndex.m_nRight); DWORD dwSelMode = bFirst ? SM_SINGE_SEL_GROUP : SM_ADD_SEL_GROUP; bFirst = FALSE; m_pOwnerGrid->SetCurSel (idFirst, SM_FIRST_CLICK | dwSelMode, FALSE); m_pOwnerGrid->SetCurSel (idSecond, SM_SECOND_CLICK | SM_CONTINUE_SEL_GROUP, FALSE); } UpdateSelectionRect (idTo); } return TRUE; }
//**************************************************************************************** BOOL CBCGPGridSerializeManager::PrepareDataFromSelection () { ASSERT (m_pOwnerGrid != NULL); CleanUp (); if (m_pOwnerGrid != NULL) { m_nLastColumn = m_pOwnerGrid->GetColumnsInfo ().GetColumnCount (TRUE) - 1; m_nLastRow = m_pOwnerGrid->GetTotalRowCount () - 1; } if (m_ClipboardFormatType == CF_Rows) { if (!m_pOwnerGrid->NormalizeSelectionList ()) { return FALSE; } } //--------------------- // Calculate min offset //--------------------- int i = 0; CBCGPGridItemID idOffset (-1, -1); for (i = 0; i < m_pOwnerGrid->GetSelectionCount (); i++) { CBCGPGridRange rangeIndex, rangeOrder; if (m_pOwnerGrid->GetSelection (i, rangeIndex) && IndexToOrder (rangeIndex, rangeOrder)) { const int nRow = (rangeOrder.m_nTop != -1) ? rangeOrder.m_nTop: 0; const int nCol = (rangeOrder.m_nLeft != -1) ? rangeOrder.m_nLeft: 0; if (idOffset.m_nRow == -1 || nRow <= idOffset.m_nRow) { idOffset.m_nRow = nRow; } if (idOffset.m_nColumn == -1 || nCol <= idOffset.m_nColumn) { idOffset.m_nColumn = nCol; } if (idOffset.m_nRow == -1 || idOffset.m_nRow == 0 && idOffset.m_nRow == m_nLastRow) { m_bWholeColSelected = TRUE; } if (idOffset.m_nColumn == -1 || idOffset.m_nColumn == 0 && idOffset.m_nColumn == m_nLastColumn) { m_bWholeRowSelected = TRUE; } } } if (idOffset.m_nRow == -1) { idOffset.m_nRow = 0; } if (idOffset.m_nColumn == -1) { idOffset.m_nColumn = 0; } ASSERT (idOffset.m_nRow >= 0); ASSERT (idOffset.m_nColumn >= 0); m_idRangesOffset = idOffset; //------------------------------ // Prepare serialized selection: //------------------------------ for (i = 0; i < m_pOwnerGrid->GetSelectionCount (); i++) { CBCGPGridRange rangeIndex, rangeOrder; if (m_pOwnerGrid->GetSelection (i, rangeIndex) && IndexToOrder (rangeIndex, rangeOrder)) { const int nRowOffset = (rangeOrder.m_nTop != -1) ? rangeOrder.m_nTop - m_idRangesOffset.m_nRow: 0; const int nColOffset = (rangeOrder.m_nLeft != -1) ? rangeOrder.m_nLeft - m_idRangesOffset.m_nColumn: 0; CBCGPGridRange rangeRelative ( nColOffset, nRowOffset, nColOffset + (rangeOrder.m_nRight - rangeOrder.m_nLeft), nRowOffset + (rangeOrder.m_nBottom - rangeOrder.m_nTop)); try { CMemFile f; CArchive archive (&f, CArchive::store | CArchive::bNoFlushOnDelete); if (m_ClipboardFormatType == CF_Items) { WriteItemsToArchive (archive, rangeOrder); } else if (m_ClipboardFormatType == CF_Rows) { WriteRowsToArchive (archive, rangeOrder); } archive.Close (); if (f.GetLength () <= 0) { return FALSE; } UINT cbSize = (UINT)f.GetLength (); BYTE* pData = new BYTE[cbSize]; if (NULL == pData) { delete [] pData; return FALSE; } f.SeekToBegin (); if (f.Read (pData, cbSize) != cbSize) { delete [] pData; return FALSE; } AddRange (rangeRelative, cbSize, pData); } catch (CFileException* pEx) { TRACE(_T("CBCGPGridSerializeManager::PrepareDataFromSelection. File exception\r\n")); pEx->Delete (); return FALSE; } catch (CException* e) { TRACE(_T("CBCGPGridSerializeManager::PrepareDataFromSelection. Exception\r\n")); e->Delete (); return FALSE; } } } return TRUE; }