www.pudn.com > MySee_Snake.rar > MySeeDoc.cpp
// MySeeDoc.cpp : implementation of the CMySeeDoc class
//
#include "stdafx.h"
#include "MySee.h"
#include "MySeeDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMySeeDoc
IMPLEMENT_DYNCREATE(CMySeeDoc, CDocument)
BEGIN_MESSAGE_MAP(CMySeeDoc, CDocument)
//{{AFX_MSG_MAP(CMySeeDoc)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMySeeDoc construction/destruction
CMySeeDoc::CMySeeDoc()
{
// TODO: add one-time construction code here
}
CMySeeDoc::~CMySeeDoc()
{
if (flag==1)
{
HeapFree(GetProcessHeap(),0,pbi);
HeapFree(GetProcessHeap(),0,lpBuf);
}
}
BOOL CMySeeDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMySeeDoc serialization
void CMySeeDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CMySeeDoc diagnostics
#ifdef _DEBUG
void CMySeeDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CMySeeDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMySeeDoc commands
void CMySeeDoc::OnFileOpen()
{
// TODO: Add your command handler code here
//读取BMP文件到文档
LPCTSTR lpszFilter="BMP Files(*.bmp)|*.bmp|任何文件|*.*||";
CFileDialog dlg1(true,lpszFilter,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,lpszFilter,NULL);
CString filename;
CFile file;
//打开文件对话框
if (dlg1.DoModal()==IDOK)
{
filename=dlg1.GetPathName();
if (file.Open(filename,CFile::modeRead|CFile::shareDenyNone,NULL)==0)
{
//读取文件失败
AfxMessageBox("无法打开文件!",MB_OK,0);
return;
}
//读取文件头BITMAPFILEHEADER
file.Read(&bf,sizeof(bf));
//判断是否是BMP文件
if (bf.bfType!=0x4d42)//'BM'
{
AfxMessageBox("not bmp file!",MB_OK,0);
return;
}
//判断是否损坏
if (file.GetLength()!=bf.bfSize)
{
AfxMessageBox("damaged file,please check it again!",MB_OK,0);
return;
}
//读取文件信息头BITMAPINFOHEADER
file.Read(&bi,sizeof(bi));
//计算调色板数目
numQuad=0;
if (bi.biBitCount<24)
{
numQuad=1<