projDoc.cpp
上传用户:ywlong9188
上传日期:2022-05-31
资源大小:2656k
文件大小:6k
- // ProjDoc.cpp : implementation of the CProjDoc class
- //
- #include "stdafx.h"
- #include "Proj.h"
- #include "ProjDoc.h"
- #include "ProjView.h"
- #include "drawobj.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CProjDoc
- IMPLEMENT_DYNCREATE(CProjDoc, CDocument)
- BEGIN_MESSAGE_MAP(CProjDoc, CDocument)
- //{{AFX_MSG_MAP(CProjDoc)
- ON_COMMAND(ID_VIEW_PAPERCOLOR, OnViewPaperColor)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CProjDoc construction/destruction
- CProjDoc::CProjDoc()
- {
- m_nMapMode = MM_ANISOTROPIC;
- m_paperColor = RGB(192, 192, 192);
- ComputePageSize();
- char path[100];
- CString temp;
- GetCurrentDirectory(100,path);
- temp.Format("%s\grf.lib",path);
- m_filename.Format("%s\lib",path);
- CreateDirectory(m_filename,NULL);
- m_directory = m_filename;
- m_filename=m_filename+"\grf.lib";
- if(MoveFile(temp,m_filename) == TRUE)
-
- ::MessageBox(NULL,"成功加载图形库!","图形组态",MB_ICONEXCLAMATION+MB_OK);
- if(!IsExist())
-
- ::MessageBox(NULL,"没有发现图形库!","图形组态",MB_ICONEXCLAMATION+MB_OK);
- }
- CProjDoc::~CProjDoc()
- {
- while(m_customList.GetHeadPosition())
- delete m_customList.RemoveHead();
- }
- BOOL CProjDoc::IsExist()
- {
- CFile file;
- if(file.Open(m_filename,CFile::modeReadWrite))
- {
- file.Close();
- return TRUE;
- }
- return FALSE;
- }
- BOOL CProjDoc::CreateLib()
- {
- CFile file;
- if(file.Open(m_filename,CFile::modeCreate))
- {
- file.Close();
- return TRUE;
- }
- return FALSE;
- }
- BOOL CProjDoc::LoadFromLib()
- {
- CFile file;
- try{
- if(!file.Open(m_filename,CFile::modeReadWrite))
- {
- return FALSE;
- }
-
- while(m_customList.GetHeadPosition())
- delete m_customList.RemoveHead();
-
- try{
-
- CArchive ar(&file,CArchive::load);
- m_customList.Serialize(ar);
- }
- catch(CException* e)
- {
- //e->ReportError();
- e->Delete();
- }
- }
- catch(CException* e)
- {
- e->Delete();
- if(file.m_hFile != CFile::hFileNull)
- file.Close();
- AfxMessageBox("Can't open graphics library!");
- return FALSE;
- }
- if(file.m_hFile != CFile::hFileNull)
- file.Close();
- return TRUE;
- }
- void CProjDoc::StoreToLib()
- {
- CFile file;
- try{
- if(!file.Open(m_filename,CFile::modeReadWrite))
- {
- AfxMessageBox("Can't open graphics library!");
- return ;
- }
- CArchive ar(&file,CArchive::store);
- ar.m_pDocument = this;
- m_customList.Serialize(ar);
- }
- catch(CException* e)
- {
- e->Delete();
- if(file.m_hFile != CFile::hFileNull)
- file.Close();
- AfxMessageBox("Can't open graphics library!");
- return ;
- }
- if(file.m_hFile != CFile::hFileNull)
- file.Close();
- }
- BOOL CProjDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
- m_paperColor = RGB(192,192,192);
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CProjDoc serialization
- void CProjDoc::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- ar << m_paperColor;
- }
- else
- {
- ar >> m_paperColor;
- }
- m_objects.Serialize(ar);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CProjDoc diagnostics
- #ifdef _DEBUG
- void CProjDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CProjDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CProjDoc commands
- void CProjDoc::Draw(CDC* pDC, CProjView* pView)
- {
- POSITION pos = m_objects.GetHeadPosition();
- while (pos != NULL)
- {
- CDrawObj* pObj = m_objects.GetNext(pos);
- if( pObj != NULL)
- pObj->Draw(pDC);
- if (pView->m_bActive && !pDC->IsPrinting() && pView->IsSelected(pObj))
- if( pObj != NULL)
- pObj->DrawTracker(pDC, CDrawObj::selected);
- }
- }
- void CProjDoc::AddToList(CDrawObj* pObj)
- {
- m_customList.AddTail(pObj);
- SetModifiedFlag();
- }
- void CProjDoc::Add(CDrawObj* pObj)
- {
- m_objects.AddTail(pObj);
- pObj->m_pDocument = this;
- SetModifiedFlag();
- }
- void CProjDoc::RemoveFromList(CDrawObj* pObj)
- {
- POSITION pos = m_customList.Find(pObj);
- if (pos != NULL){
- CDrawObj* p = m_customList.GetAt(pos);
- m_customList.RemoveAt(pos);
- p->Remove();
- }
- // set document modified flag
- SetModifiedFlag();
- }
- void CProjDoc::Remove(CDrawObj* pObj)
- {
- // Find and remove from document
- POSITION pos = m_objects.Find(pObj);
- if (pos != NULL)
- m_objects.RemoveAt(pos);
- // set document modified flag
- SetModifiedFlag();
- // call remove for each view so that the view can remove from m_selection
- pos = GetFirstViewPosition();
- while (pos != NULL)
- ((CProjView*)GetNextView(pos))->Remove(pObj);
- }
- // point is in logical coordinates
- CDrawObj* CProjDoc::ObjectAt(const CPoint& point)
- {
- CRect rect(point, CSize(1, 1));
- POSITION pos = m_objects.GetTailPosition();
- while (pos != NULL)
- {
- CDrawObj* pObj = m_objects.GetPrev(pos);
- if(pObj != NULL )
- if (pObj->Intersects(rect))
- return pObj;
- }
- return NULL;
- }
- void CProjDoc::ComputePageSize()
- {
- CSize new_size(2000, 1500); // 8.5" x 11" default
- /*CPrintDialog dlg(FALSE);
- if (AfxGetApp()->GetPrinterDeviceDefaults(&dlg.m_pd))
- {
- // GetPrinterDC returns a HDC so attach it
- CDC dc;
- HDC hDC= dlg.CreatePrinterDC();
- ASSERT(hDC != NULL);
- dc.Attach(hDC);
- // Get the size of the page in loenglish
- int n =dc.GetDeviceCaps(HORZSIZE);
- int m= dc.GetDeviceCaps(VERTSIZE);
- new_size.cx = MulDiv(dc.GetDeviceCaps(HORZSIZE), 1000, 254);
- new_size.cy = MulDiv(dc.GetDeviceCaps(VERTSIZE), 1000, 254);
- }*/
- // if size changed then iterate over views and reset
- if (new_size != m_size)
- {
- m_size = new_size;
- POSITION pos = GetFirstViewPosition();
- while (pos != NULL)
- ((CProjView*)GetNextView(pos))->SetPageSize(m_size);
- }
- }
- void CProjDoc::OnViewPaperColor()
- {
- CColorDialog dlg;
- if (dlg.DoModal() != IDOK)
- return;
- m_paperColor = dlg.GetColor();
- SetModifiedFlag();
- UpdateAllViews(NULL);
- }
- /////////////////////////////////////////////////////////////////////////////
- void CProjDoc::DeleteContents()
- {
-
- while(m_objects.GetHeadPosition())
- {
- delete m_objects.RemoveHead();
- }
- CDrawObj::c_identify = 1;
- }