⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagedoc.cpp

📁 一个跟Cppunit一样好用的单元测试工具
💻 CPP
字号:
// imageDoc.cpp : implementation of the CImageDoc class
//

#include "stdafx.h"
#include "image.h"

#include "imageDoc.h"
#include "BitmapFile.h"
#include "imageView.h"
#include "PenWidthsDlg.h"
#include "drawshape.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CImageDoc

IMPLEMENT_DYNCREATE(CImageDoc, CDocument)

BEGIN_MESSAGE_MAP(CImageDoc, CDocument)
	//{{AFX_MSG_MAP(CImageDoc)
	ON_COMMAND(ID_PEN_WIDTH, OnPenWidth)
	ON_COMMAND(ID_PEN_COLOR, OnPenColor)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImageDoc construction/destruction

CImageDoc::CImageDoc()
{
	// TODO: add one-time construction code here	
	m_pBitmap = NULL;
	m_pPenCurrent = NULL;
}

CImageDoc::~CImageDoc()
{
	delete m_pBitmap;
	delete m_pPenCurrent;
}

BOOL CImageDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;
	m_pBitmap = new CBitmapFile();
	CView* pOnlyView = GetTheView();
	if(pOnlyView)
	{
		pOnlyView->InvalidateRect(NULL);
	}
	InitDocument();
	OmitEdit();
	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)	
	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CImageDoc serialization

void CImageDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CImageDoc diagnostics

#ifdef _DEBUG
void CImageDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CImageDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CImageDoc commands

BOOL CImageDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	// TODO: Add your specialized creation code here
	m_pBitmap->LoadBitmap(lpszPathName);	
	CView* pOnlyView = GetTheView();
	if(pOnlyView)
	{
		pOnlyView->InvalidateRect(NULL);
	}
	InitDocument();
	return TRUE;
}

BOOL CImageDoc::OnSaveDocument(LPCTSTR lpszPathName) 
{
	// TODO: Add your specialized code here and/or call the base class
	CDocument::OnSaveDocument(lpszPathName);
	return	m_pBitmap->SaveBitmap(lpszPathName);	
}

void CImageDoc::InitDocument()
{
	m_nCurPenWidth = 2;
	m_CurColor = RGB(0, 0, 0);
	m_pPenCurrent = new CPen();
	if(!m_pPenCurrent->CreatePen(PS_SOLID, m_nCurPenWidth, m_CurColor))
	{
		AfxMessageBox("Create default pen failed!");
	}
	CImageView *pActiveView = (CImageView*)(GetTheView());
	if(pActiveView)
		pActiveView->SetScale(1.0f);
}

void CImageDoc::OmitContents()
{
	while(!m_ShapeList.IsEmpty())
	{
		delete m_ShapeList.RemoveHead();
	}
}

void CImageDoc::OmitEdit()
{
	CImageView* pView = (CImageView*)GetTheView();
	if (pView)
	{
		if (pView->GetPreText())
		{
			 delete pView->m_pEdit;
			 pView->m_pEdit = NULL;
		}
	}
}

void CImageDoc::ReplacePen()
{
	m_pPenCurrent->DeleteObject();
	m_pPenCurrent->CreatePen(PS_SOLID, m_nCurPenWidth, m_CurColor);
}

CView* CImageDoc::GetTheView()
{
	POSITION pos = GetFirstViewPosition( );
	if (pos)
	{
		//get the point to the view associate with the current document
		return GetNextView(pos); 
	}
	else
	{
		return NULL;
	}
}

void CImageDoc::OnPenWidth() 
{
	// TODO: Add your command handler code here
	CPenWidthsDlg dlg;
	dlg.m_nCurrentWidth = m_nCurPenWidth;
	if(dlg.DoModal()==IDOK)
	{
		m_nCurPenWidth = dlg.m_nCurrentWidth;
		ReplacePen();
	}
}

void CImageDoc::OnPenColor() 
{
	// TODO: Add your command handler code here
	CColorDialog dlg;
	if(dlg.DoModal()==IDOK)
	{
		m_CurColor = dlg.GetColor();
		ReplacePen();
	}
}

void CImageDoc::DeleteContents() 
{
	// TODO: Add your specialized code here and/or call the base class
	OmitContents();
	CDocument::DeleteContents();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -