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

📄 bmpviewerdoc.cpp

📁 vc++ study
💻 CPP
字号:
// BmpViewerDoc.cpp : implementation of the CBmpViewerDoc class
//

#include "stdafx.h"
#include "BmpViewer.h"

#include "BmpViewerDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBmpViewerDoc

IMPLEMENT_DYNCREATE(CBmpViewerDoc, CDocument)

BEGIN_MESSAGE_MAP(CBmpViewerDoc, CDocument)
	//{{AFX_MSG_MAP(CBmpViewerDoc)
		// 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()

/////////////////////////////////////////////////////////////////////////////
// CBmpViewerDoc construction/destruction

CBmpViewerDoc::CBmpViewerDoc()
{
	// TODO: add one-time construction code here

}

CBmpViewerDoc::~CBmpViewerDoc()
{
}





/////////////////////////////////////////////////////////////////////////////
// CBmpViewerDoc serialization

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

/////////////////////////////////////////////////////////////////////////////
// CBmpViewerDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CBmpViewerDoc commands



BOOL CBmpViewerDoc::OnNewDocument() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDocument::OnNewDocument();
}

void CBmpViewerDoc::OnCloseDocument() 
{
	// TODO: Add your specialized code here and/or call the base class
	CloseDIB();
	if(lpBmpFile)
	{
		delete[] lpBmpFile;
		lpBmpFile=NULL;
	}
	CDocument::OnCloseDocument();
}

BOOL CBmpViewerDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	// TODO: Add your specialized creation code here
	CFile f;
	CFileException e;
	CString s;
	TCHAR message[256];
	if (!f.Open(lpszPathName,CFile::modeRead,&e))
	{
		e.GetErrorMessage(message,256);
		CString s;
		s.Format(IDS_LOADFILEFAIL,message);
		AfxMessageBox(s);
		return FALSE;
	}

	sizeBmpFile=f.GetLength();
	lpBmpFile=new char[sizeBmpFile];

	if (!lpBmpFile)
	{
		f.Close();
		s.Format(IDS_LOADFILEFAIL,"文件太大");
		AfxMessageBox(s);
		return FALSE;
	}
	f.Read(lpBmpFile,sizeBmpFile);
	f.Close();

	pbfh= (BITMAPFILEHEADER *) lpBmpFile;
	if((lpBmpFile[0]!='B') || (lpBmpFile[1]!='M'))
	{
		s.Format(IDS_LOADFILEFAIL,"BMP文件个是错误");
		AfxMessageBox(s);
		return FALSE;
	}

	pbih=(BITMAPINFOHEADER *) (lpBmpFile+sizeof(BITMAPFILEHEADER));
	if(pbih->biSize!=sizeof(BITMAPINFOHEADER))
	{
		s.Format(IDS_LOADFILEFAIL,"BMP文件格式错误");
		AfxMessageBox(s);
		return FALSE;
	}

	OpenDIB();
	return TRUE;
}

void CBmpViewerDoc::OpenDIB()
{
  m_hDD=DrawDibOpen();
}

void CBmpViewerDoc::CloseDIB()
{
  if(m_hDD)
  {
	  DrawDibClose(m_hDD);
	  m_hDD=0;
  }
}

void CBmpViewerDoc::Draw(CDC *pDC)
{
  DrawDibRealize(m_hDD,pDC->GetSafeHdc(),TRUE);
  DrawDibDraw(m_hDD,pDC->GetSafeHdc(),
	          0,0,
			  pbih->biWidth,pbih->biHeight,
			  pbih,NULL,0,0,
			  pbih->biWidth,pbih->biHeight,
			  DDF_BACKGROUNDPAL);
}


HDRAWDIB CBmpViewerDoc::GetDrawDib()
{
  return m_hDD;
}

CSize CBmpViewerDoc::GetImageSize()
{
  return CSize(pbih->biWidth,pbih->biHeight);
}



void CBmpViewerDoc::GetInfo(CString &s)
{
  s.Format("%dx%dx",pbih->biWidth,pbih->biHeight);
  switch(pbih->biBitCount)
  {
  case 1: s+="两色";break;
  case 4: s+="16色";break;
  case 8: s+="256色";break;
  case 16:s+="64K色";break;
  case 24:s+="16M色";break;
}

⌨️ 快捷键说明

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