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

📄 viewdibdoc.cpp

📁 < 精通Visual C++图象处理编程>>(第3版)第八章配套源码,希望对学过此书的同学或开发人员有帮助
💻 CPP
字号:
// ViewDIBDoc.cpp : implementation of the CViewDIBDoc class
//

#include "stdafx.h"
#include "ViewDIB.h"

#include "ViewDIBDoc.h"
#include "LoadTifDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CViewDIBDoc

IMPLEMENT_DYNCREATE(CViewDIBDoc, CDocument)

BEGIN_MESSAGE_MAP(CViewDIBDoc, CDocument)
	//{{AFX_MSG_MAP(CViewDIBDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CViewDIBDoc construction/destruction

CViewDIBDoc::CViewDIBDoc()
{
	m_pDib = new CDib;
}

CViewDIBDoc::~CViewDIBDoc()
{
	delete m_pDib;
}

BOOL CViewDIBDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CViewDIBDoc serialization

void CViewDIBDoc::Serialize(CArchive& ar)
{
	m_pDib->Serialize(ar);
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CViewDIBDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CViewDIBDoc commands

BOOL CViewDIBDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	char drive[_MAX_DRIVE];   
	char dir[_MAX_DIR];
	char fname[_MAX_FNAME];   
	char ext[_MAX_EXT];
	_splitpath(lpszPathName, drive, dir, fname, ext);   

	if (! stricmp(ext, ".gif")) // GIF file
	{
		CGif gif;
		if (! gif.Load(lpszPathName))
			return FALSE;

		HDIB hDIB = CopyHandle(gif.GetDib()->GetHandle());
		if (hDIB == NULL)
			return FALSE;

		m_pDib->Attach(hDIB);
	
		return TRUE;
	}
	else if (! stricmp(ext, ".jpg") ||
			 ! stricmp(ext, ".jpe") ||
			 ! stricmp(ext, ".jpeg") ) // JPEG file
	{
		CJpeg jpeg;
		if (! jpeg.Load(lpszPathName))
			return FALSE;

		HDIB hDIB = CopyHandle(jpeg.GetDib()->GetHandle());
		if (hDIB == NULL)
			return FALSE;

		m_pDib->Attach(hDIB);
	
		return TRUE;
	}
	else if (! stricmp(ext, ".pcx")) // PCX file
	{
		CPcx pcx;
		if (! pcx.Load(lpszPathName))
			return FALSE;

		HDIB hDIB = CopyHandle(pcx.GetDib()->GetHandle());
		if (hDIB == NULL)
			return FALSE;

		m_pDib->Attach(hDIB);
	
		return TRUE;
	}
	else if (! stricmp(ext, ".tga")) // TGA file
	{
		CTga tga;
		if (! tga.Load(lpszPathName))
			return FALSE;

		HDIB hDIB = CopyHandle(tga.GetDib()->GetHandle());
		if (hDIB == NULL)
			return FALSE;

		m_pDib->Attach(hDIB);
	
		return TRUE;
	}
	else if (! stricmp(ext, ".tif") ||
			 ! stricmp(ext, ".tiff")) // TIF file
	{
		CTif tif;
		if (! tif.Load(lpszPathName))
			return FALSE;

		CLoadTifDlg loadTifDlg(lpszPathName, tif.m_wIFDNum);
		if (loadTifDlg.DoModal() != IDOK)
			return FALSE;

		int *nPageList = new int[tif.m_wIFDNum];
		int nLoadPageNum = 0;
		if (loadTifDlg.m_bLoadAll)
		{
			nLoadPageNum = tif.m_wIFDNum;
			for (int i=0; i<nLoadPageNum; i++)
				nPageList[i] = i;
		}
		else
		{
			CString s = loadTifDlg.m_strPages;
			int nLen = s.GetLength();
			for (int i=0;i<nLen;++i)
			{
				if (s[i] == ' ' || s[i] == 32)
					s.Delete(i);
				nLen = s.GetLength();
			}
			while (1)
			{
				int n = s.Find(',');
				if (n == -1)
					break;

				CString strPage = s.Left(n);
				s.Delete(0, n+1);

				int nPage = atoi((LPCSTR)strPage) - 1;
				if (nPage >=0 && nPage < tif.m_wIFDNum)
					nPageList[nLoadPageNum++] = nPage;

				if (nLoadPageNum >= tif.m_wIFDNum)
					break;
			}
			if (nLoadPageNum < tif.m_wIFDNum)
			{
				int nPage = atoi((LPCSTR)s) - 1;
				if (nPage >=0 && nPage < tif.m_wIFDNum)
					nPageList[nLoadPageNum++] = nPage;
			}
		}

		if (nLoadPageNum == 0)
			return FALSE;

		CViewDIBApp* pApp = (CViewDIBApp *)AfxGetApp();
		for (int i=nLoadPageNum-1; i>0; i--)
		{
			CString strTitle;
			CString s = lpszPathName;
			s += " p%d";
			strTitle.Format((LPCTSTR)s, nPageList[i]+1);
			pApp->AddFile(CopyHandle(tif.m_DibList[nPageList[i]].GetHandle()), (LPCTSTR)strTitle);
		}

		// set the first selected page as current document
		HDIB hDIB = CopyHandle(tif.m_DibList[nPageList[0]].GetHandle());
		if (hDIB == NULL)
			return FALSE;
		m_pDib->Attach(hDIB);

		CString s = lpszPathName;
		s += " p%d";
		m_strTifPathName.Format((LPCTSTR)s, nPageList[0]+1);
		m_bLoadTif = TRUE;
		pApp->AddToRecentFileList(lpszPathName);
		SetModifiedFlag(TRUE);
	
		return TRUE;
	}

	return CDocument::OnOpenDocument(lpszPathName);
}

⌨️ 快捷键说明

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