www.pudn.com > yuzhishuanfa.zip > ImageDoc.cpp
// ImageDoc.cpp : implementation of the CImageDocA class
//
#include "limits.h"
#include "stdafx.h"
#include "dibapi.h"
#include "ImageDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CImageDocA
IMPLEMENT_DYNCREATE(CImageDocA, CDocument)
BEGIN_MESSAGE_MAP(CImageDocA, CDocument)
//{{AFX_MSG_MAP(CImageDocA)
// 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
ON_COMMAND(ID_FILE_SEND_MAIL, OnFileSendMail)
ON_UPDATE_COMMAND_UI(ID_FILE_SEND_MAIL, OnUpdateFileSendMail)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CImageDocA construction/destruction
CImageDocA::CImageDocA()
{
// TODO: add one-time construction code here
m_hDIB = NULL;
m_palDIB = NULL;
m_sizeDoc = CSize(1,1); // dummy value to make CScrollView happy
m_bGray=FALSE;
}
CImageDocA::~CImageDocA()
{
if (m_hDIB != NULL)
{
::GlobalFree((HGLOBAL) m_hDIB);
}
if (m_palDIB != NULL)
{
delete m_palDIB;
}
}
BOOL CImageDocA::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CImageDocA serialization
void CImageDocA::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CImageDocA diagnostics
#ifdef _DEBUG
void CImageDocA::AssertValid() const
{
CDocument::AssertValid();
}
void CImageDocA::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CImageDocA commands
//更新位图数据
void CImageDocA::ReplaceHDIB(HDIB hDIB)
{
if (m_hDIB != NULL)
{
::GlobalFree((HGLOBAL) m_hDIB);
}
m_hDIB = hDIB;
InitDIBData();
SetModifiedFlag(TRUE);
//取得视图
CImageViewA *pView=NULL;
POSITION curViewPos = GetFirstViewPosition();
if(curViewPos != NULL)
pView = (CImageViewA *)(GetNextView(curViewPos));
if(pView != NULL)
{
pView->SetScrollSizes(MM_TEXT, GetDocSize());
UpdateAllViews(NULL);
}
}
//1999-01-29,鲍捷
void CImageDocA::ReplaceHDIB(HBITMAP hBitmap)
{
ReplaceHDIB((HDIB)BitmapToDIB(hBitmap,NULL));
}
//设置位图数据属性 :m_palDIB,m_hDIB,m_sizeDoc
void CImageDocA::InitDIBData()
{
if (m_palDIB != NULL)
{
delete m_palDIB;
m_palDIB = NULL;
}
if (m_hDIB == NULL)
{
return;
}
// Set up document size
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX)
{
::GlobalUnlock((HGLOBAL) m_hDIB);
::GlobalFree((HGLOBAL) m_hDIB);
m_hDIB = NULL;
CString strMsg="图象文件过大";
// strMsg.LoadString(IDS_DIB_TOO_BIG);
MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
return;
}
m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB));
::GlobalUnlock((HGLOBAL) m_hDIB);
// Create copy of palette
m_palDIB = new CPalette;
if (m_palDIB == NULL)
{
// we must be really low on memory
::GlobalFree((HGLOBAL) m_hDIB);
m_hDIB = NULL;
return;
}
if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL)
{
// DIB may not have a palette
delete m_palDIB;
m_palDIB = NULL;
return;
}
}
BOOL CImageDocA::OnOpenDocument(LPCTSTR lpszPathName)
{
//if (!COleServerDoc::OnOpenDocument(lpszPathName))
// return FALSE;
// TODO: Add your specialized creation code here
//打开文件
CFile file;
CFileException fe;
if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
{
ReportSaveLoadException(lpszPathName, &fe,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
return FALSE;
}
DeleteContents(); //确保文档空
BeginWaitCursor(); //设置等待光标
// replace calls to Serialize with ReadDIBFile function
//读位图
TRY
{
m_hDIB = ::ReadDIBFile(file);
}
//异常处理
CATCH (CFileException, eLoad)
{
file.Abort(); // will not throw an exception
EndWaitCursor();
ReportSaveLoadException(lpszPathName, eLoad,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
m_hDIB = NULL;
return FALSE;
}
END_CATCH
//初始化位图数据
InitDIBData();
EndWaitCursor();
//若不是DIB文件,显示出错信息
if (m_hDIB == NULL)
{
// may not be DIB format
CString strMsg="装载图象时发生错误!";
// strMsg.LoadString(IDS_CANNOT_LOAD_DIB);
MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
return FALSE;
}
// 设置当前文件路径,并加入MRU
SetPathName(lpszPathName);
// 文件开始时未修改
SetModifiedFlag(FALSE); // start off with unmodified
return TRUE;
}
BOOL CImageDocA::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
//COleServerDoc::OnSaveDocument(lpszPathName);
// TODO: Add your specialized code here and/or call the base class
CFile file;
CFileException fe;
if (!file.Open(lpszPathName, CFile::modeCreate |
CFile::modeReadWrite | CFile::shareExclusive, &fe))
{
ReportSaveLoadException(lpszPathName, &fe,
TRUE, AFX_IDP_INVALID_FILENAME);
return FALSE;
}
// replace calls to Serialize with SaveDIB function
BOOL bSuccess = FALSE;
TRY
{
BeginWaitCursor();
bSuccess = ::SaveDIB(m_hDIB, file);
file.Close();
}
CATCH (CException, eSave)
{
file.Abort(); // will not throw an exception
EndWaitCursor();
ReportSaveLoadException(lpszPathName, eSave,
TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);
return FALSE;
}
END_CATCH
EndWaitCursor();
SetPathName(lpszPathName);
SetModifiedFlag(FALSE); // back to unmodified
if (!bSuccess)
{
// may be other-style DIB (load supported but not save)
// or other problem in SaveDIB
CString strMsg="保存图象时发生错误!";
// strMsg.LoadString(IDS_CANNOT_SAVE_DIB);
MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
}
return bSuccess;
}
/////////////////////////////////////////////////////////////////////////////
// CImageDocA serialization
/////////////////////////////////////////////////////////////////////////////
// CImageDocA commands
//1999-01-30 鲍捷,更新视图
void CImageDocA::UpdateView()
{
//取得视图
CImageViewA *pView=NULL;
POSITION curViewPos = GetFirstViewPosition();
if(curViewPos != NULL)
pView = (CImageViewA *)(GetNextView(curViewPos));
if(pView != NULL)
{
pView->SetScrollSizes(MM_TEXT, GetDocSize());
UpdateAllViews(NULL);
}
}
//1999-02-02,鲍捷,获取文档的视图
CImageViewA * CImageDocA::GetView()
{
CImageViewA *pView=NULL;
POSITION curViewPos = GetFirstViewPosition();
if(curViewPos != NULL)
pView = (CImageViewA *)(GetNextView(curViewPos));
return pView;
}