📄 edgecontourdoc.cpp
字号:
// EdgeContourDoc.cpp : implementation of the CEdgeContourDoc class
//
#include "stdafx.h"
#include "EdgeContour.h"
#include "EdgeContourDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEdgeContourDoc
IMPLEMENT_DYNCREATE(CEdgeContourDoc, CDocument)
BEGIN_MESSAGE_MAP(CEdgeContourDoc, CDocument)
//{{AFX_MSG_MAP(CEdgeContourDoc)
ON_COMMAND(ID_FILE_REOPEN, OnFileReopen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEdgeContourDoc construction/destruction
CEdgeContourDoc::CEdgeContourDoc()
{
// TODO: add one-time construction code here
// 默认背景色,灰色
m_refColorBKG = 0x00808080;
// 初始化变量
m_pDibImage = NULL;
m_hDIB = NULL;
m_palDIB = NULL;
m_sizeDoc = CSize(1,1);
}
CEdgeContourDoc::~CEdgeContourDoc()
{
// 判断DIB对象是否存在
if (m_hDIB != NULL)
{
// 清除DIB对象
::GlobalFree((HGLOBAL) m_hDIB);
}
// 判断调色板是否存在
if (m_palDIB != NULL)
{
// 清除调色板
delete m_palDIB;
m_palDIB = NULL;
}
// 判断DibImage对象是否存在
if (m_pDibImage != NULL)
{
// 清除DibImage对象
delete m_pDibImage;
m_pDibImage = NULL;
}
}
BOOL CEdgeContourDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CEdgeContourDoc serialization
void CEdgeContourDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CEdgeContourDoc diagnostics
#ifdef _DEBUG
void CEdgeContourDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CEdgeContourDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEdgeContourDoc commands
void CEdgeContourDoc::ReplaceHDIB(HDIB hDIB)
{
// 替换DIB,在功能粘贴中用到该函数
if (m_hDIB != NULL)
{
::GlobalFree((HGLOBAL) m_hDIB);
}
m_hDIB = hDIB;
}
void CEdgeContourDoc::InitDIBData()
{
// 初始化DIB对象
if (m_palDIB != NULL)
{
delete m_palDIB;
m_palDIB = NULL;
}
if (m_hDIB == NULL)
{
return;
}
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
// 判断图像是否过大
if (m_pDibImage->DIBWidth(lpDIB) > INT_MAX ||m_pDibImage->DIBHeight(lpDIB)
> INT_MAX)
{
::GlobalUnlock((HGLOBAL) m_hDIB);
::GlobalFree((HGLOBAL) m_hDIB);
m_hDIB = NULL;
CString strMsg;
strMsg = "BMP图像太大!";
MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
return;
}
m_sizeDoc = CSize((int) m_pDibImage->DIBWidth(lpDIB),
(int) m_pDibImage->DIBHeight(lpDIB));
::GlobalUnlock((HGLOBAL) m_hDIB);
// 创建新调色板
m_palDIB = new CPalette;
if (m_palDIB == NULL)
{
::GlobalFree((HGLOBAL) m_hDIB);
m_hDIB = NULL;
return;
}
if (m_pDibImage->CreateDIBPalette(m_hDIB, m_palDIB) == NULL)
{
delete m_palDIB;
m_palDIB = NULL;
return;
}
}
BOOL CEdgeContourDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
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();
if(m_pDibImage != NULL)
{
delete m_pDibImage;
m_pDibImage = NULL;
}
m_pDibImage = new CDibImage;
TRY
{
m_hDIB = m_pDibImage->ReadDIBFile(file);
}
CATCH (CFileException, eLoad)
{
file.Abort();
EndWaitCursor();
ReportSaveLoadException(lpszPathName, eLoad,FALSE,
AFX_IDP_FAILED_TO_OPEN_DOC);
m_hDIB = NULL;
if(m_pDibImage != NULL)
{
delete m_pDibImage;
m_pDibImage = NULL;
}
return FALSE;
}
END_CATCH
InitDIBData();
EndWaitCursor();
// 判断读取文件是否成功
if (m_hDIB == NULL)
{
CString strMsg;
strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
if(m_pDibImage != NULL)
{
delete m_pDibImage;
m_pDibImage = NULL;
}
return FALSE;
}
SetPathName(lpszPathName); // 设置文件名称
SetModifiedFlag(FALSE); // 初始化胀标记为FALSE
return TRUE;
}
BOOL CEdgeContourDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
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;
}
BOOL bSuccess = FALSE;
TRY
{
BeginWaitCursor();
bSuccess = m_pDibImage->SaveDIB(m_hDIB, file);
file.Close();
}
CATCH (CException, eSave)
{
file.Abort();
EndWaitCursor();
ReportSaveLoadException(lpszPathName,eSave,TRUE,
AFX_IDP_FAILED_TO_SAVE_DOC);
return FALSE;
}
END_CATCH
EndWaitCursor();
SetModifiedFlag(FALSE);
if (!bSuccess)
{
// 保存失败,可能是其它格式的DIB,可以读取但是不能保存
// 或者是SaveDIB函数有误
CString strMsg;
strMsg = "无法保存BMP图像!";
MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
}
return bSuccess;
}
void CEdgeContourDoc::OnFileReopen()
{
// 重新打开图像,放弃所有修改
// 判断当前图像是否已经被改动
if (IsModified())
{
if (MessageBox(NULL, "重新打开图像将丢失所有改动!是否继续?",
"系统提示", MB_ICONQUESTION | MB_YESNO) == IDNO)
{
return;
}
}
CFile file;
CFileException fe;
CString strPathName;
strPathName = GetPathName();
if (!file.Open(strPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
{
ReportSaveLoadException(strPathName,&fe,FALSE,AFX_IDP_FAILED_TO_OPEN_DOC);
return;
}
BeginWaitCursor();
if(m_pDibImage != NULL)
{
delete m_pDibImage;
m_pDibImage = NULL;
}
m_pDibImage = new CDibImage;
TRY
{
m_hDIB = m_pDibImage->ReadDIBFile(file);
}
CATCH (CFileException, eLoad)
{
file.Abort();
EndWaitCursor();
ReportSaveLoadException(strPathName,eLoad,FALSE,AFX_IDP_FAILED_TO_OPEN_DOC);
m_hDIB = NULL;
if(m_pDibImage != NULL)
{
delete m_pDibImage;
m_pDibImage = NULL;
}
return;
}
END_CATCH
InitDIBData();
if (m_hDIB == NULL)
{
CString strMsg;
strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
EndWaitCursor();
if(m_pDibImage != NULL)
{
delete m_pDibImage;
m_pDibImage = NULL;
}
return;
}
SetModifiedFlag(FALSE);
UpdateAllViews(NULL);
EndWaitCursor();
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -