📄 nicuidoc.cpp
字号:
// nicuiDoc.cpp : implementation of the CNicuiDoc class
//
#include "stdafx.h"
#include "nicui.h"
#include "nicuiDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNicuiDoc
IMPLEMENT_DYNCREATE(CNicuiDoc, CDocument)
BEGIN_MESSAGE_MAP(CNicuiDoc, CDocument)
//{{AFX_MSG_MAP(CNicuiDoc)
ON_COMMAND(ID_FILE_REOPEN, OnFileReopen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNicuiDoc construction/destruction
CNicuiDoc::CNicuiDoc()
{
// TODO: add one-time construction code here
// 默认背景色,灰色
m_refColorBKG = 0x00808080;
// 初始化变量
m_hDIB = NULL;
m_palDIB = NULL;
m_sizeDoc = CSize(1,1);
}
CNicuiDoc::~CNicuiDoc()
{
// 判断DIB对象是否存在
if (m_hDIB != NULL)
{
// 清除DIB对象
::GlobalFree((HGLOBAL) m_hDIB);
}
// 判断调色板是否存在
if (m_palDIB != NULL)
{
// 清除调色板
delete m_palDIB;
}
}
BOOL CNicuiDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CNicuiDoc serialization
void CNicuiDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CNicuiDoc diagnostics
#ifdef _DEBUG
void CNicuiDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CNicuiDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CNicuiDoc commands
BOOL CNicuiDoc::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);
// 返回FALSE
return FALSE;
}
DeleteContents();
// 更改光标形状
BeginWaitCursor();
// 尝试调用ReadDIBFile()读取图像
TRY
{
m_hDIB = ::ReadDIBFile(file);
}
CATCH (CFileException, eLoad)
{
// 读取失败
file.Abort();
// 恢复光标形状
EndWaitCursor();
// 报告失败
ReportSaveLoadException(lpszPathName, eLoad,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
// 设置DIB为空
m_hDIB = NULL;
// 返回FALSE
return FALSE;
}
END_CATCH
// 初始化DIB
InitDIBData();
// 恢复光标形状
EndWaitCursor();
// 判断读取文件是否成功
if (m_hDIB == NULL)
{
// 失败,可能非BMP格式
CString strMsg;
strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
// 提示出错
MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
// 返回FALSE
return FALSE;
}
// 设置文件名称
SetPathName(lpszPathName);
// 初始化胀标记为FALSE
SetModifiedFlag(FALSE);
// 返回TRUE
return TRUE;
}
BOOL CNicuiDoc::OnSaveDocument(LPCTSTR 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);
// 返回FALSE
return FALSE;
}
// 尝试调用SaveDIB保存图像
BOOL bSuccess = FALSE;
TRY
{
// 更改光标形状
BeginWaitCursor();
// 尝试保存图像
bSuccess = ::SaveDIB(m_hDIB, file);
// 关闭文件
file.Close();
}
CATCH (CException, eSave)
{
// 失败
file.Abort();
// 恢复光标形状
EndWaitCursor();
ReportSaveLoadException(lpszPathName, eSave,
TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);
// 返回FALSE
return FALSE;
}
END_CATCH
// 恢复光标形状
EndWaitCursor();
// 重置胀标记为FALSE
SetModifiedFlag(FALSE);
if (!bSuccess)
{
// 保存失败,可能是其它格式的DIB,可以读取但是不能保存
// 或者是SaveDIB函数有误
CString strMsg;
strMsg = "无法保存BMP图像!";
// 提示出错
MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
}
return bSuccess;
}
BOOL CNicuiDoc::CanCloseFrame(CFrameWnd* pFrame)
{
// TODO: Add your specialized code here and/or call the base class
return CDocument::CanCloseFrame(pFrame);
}
void CNicuiDoc::DeleteContents()
{
// TODO: Add your specialized code here and/or call the base class
CDocument::DeleteContents();
}
void CNicuiDoc::OnFileReopen()
{
// TODO: Add your command handler code here
// 重新打开图像,放弃所有修改
// 判断当前图像是否已经被改动
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();
// 尝试调用ReadDIBFile()读取图像
TRY
{
m_hDIB = ::ReadDIBFile(file);
}
CATCH (CFileException, eLoad)
{
// 读取失败
file.Abort();
// 恢复光标形状
EndWaitCursor();
// 报告失败
ReportSaveLoadException(strPathName, eLoad,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
// 设置DIB为空
m_hDIB = NULL;
// 返回
return;
}
END_CATCH
// 初始化DIB
InitDIBData();
// 判断读取文件是否成功
if (m_hDIB == NULL)
{
// 失败,可能非BMP格式
CString strMsg;
strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
// 提示出错
MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
// 恢复光标形状
EndWaitCursor();
// 返回
return;
}
// 初始化胀标记为FALSE
SetModifiedFlag(FALSE);
// 刷新
UpdateAllViews(NULL);
// 恢复光标形状
EndWaitCursor();
// 返回
return;
}
void CNicuiDoc::ReplaceHDIB(HDIB hDIB)
{
// 替换DIB,在功能粘贴中用到该函数
// 判断DIB是否为空
if (m_hDIB != NULL)
{
// 非空,则清除
::GlobalFree((HGLOBAL) m_hDIB);
}
// 替换成新的DIB对象
m_hDIB = hDIB;
}
void CNicuiDoc::InitDIBData()
{
// 初始化DIB对象
// 判断调色板是否为空
if (m_palDIB != NULL)
{
// 删除调色板对象
delete m_palDIB;
// 重置调色板为空
m_palDIB = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -