📄 hotspdoc.cpp
字号:
// hotspdoc.cpp : implementation of the CHotspot2Doc class
//
#include "stdafx.h"
#include "hotspot2.h"
#include "hotspdoc.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHotspot2Doc
IMPLEMENT_DYNCREATE(CHotspot2Doc, CDocument)
BEGIN_MESSAGE_MAP(CHotspot2Doc, CDocument)
//{{AFX_MSG_MAP(CHotspot2Doc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHotspot2Doc construction/destruction
CHotspot2Doc::CHotspot2Doc()
{
m_hDIB = NULL;
m_palette = NULL;
m_regionDefined = FALSE;
m_modeTesting = FALSE;
m_toEdit = FALSE;
m_editRecord = 0;
//Open the hotspot data file.
CFileStatus status;
if (CFile::GetStatus(LINKFILENAME, status))
{
//File exists - open it.
m_datafile.Open(LINKFILENAME, CFile::modeReadWrite);
//If file contains at least one record,
//set m_toEdit flag.
if (m_datafile.GetLength() >= sizeof(HotSpotRecord))
m_toEdit = TRUE;
}
else
//File does not exist - create and open it.
m_datafile.Open(LINKFILENAME, CFile::modeCreate | CFile::modeReadWrite);
}
CHotspot2Doc::~CHotspot2Doc()
{
//Clean up memory.
if (m_hDIB != NULL)
::GlobalFree((HGLOBAL) m_hDIB);
if (m_palette != NULL)
delete m_palette;
//Close the data file.
m_datafile.Close();
}
BOOL CHotspot2Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
void CHotspot2Doc::InitBitmapData()
{
//Delete any existing palette.
if (m_palette != NULL)
{
delete m_palette;
m_palette = NULL;
}
//If there is no bitmap loaded, return.
if (m_hDIB == NULL)
return;
//Determine bitmap size.
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
m_DocSize = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB));
::GlobalUnlock((HGLOBAL) m_hDIB);
//Get the bitmap's palette.
m_palette = new CPalette;
if (m_palette == NULL) //Low on memory!
{
::GlobalFree((HGLOBAL) m_hDIB);
m_hDIB = NULL;
return;
}
if (::CreateDIBPalette(m_hDIB, m_palette) == NULL)
{
//DIB may not have a palette.
delete m_palette;
m_palette = NULL;
}
}
/////////////////////////////////////////////////////////////////////////////
// CHotspot2Doc serialization
BOOL CHotspot2Doc::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();
TRY
{
m_hDIB = ::ReadDIBFile(file);
}
CATCH (CFileException, eLoad)
{
file.Abort();
EndWaitCursor();
ReportSaveLoadException(lpszPathName, eLoad, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
m_hDIB= NULL;
return FALSE;
}
END_CATCH;
InitBitmapData();
EndWaitCursor();
if (m_hDIB == NULL)
{
CString strMsg;
strMsg = "Cannot load bitmap";
MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
return FALSE;
}
SetPathName(lpszPathName);
SetModifiedFlag(FALSE);
m_modeTesting = FALSE;
BlankCurrentHS();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CHotspot2Doc diagnostics
#ifdef _DEBUG
void CHotspot2Doc::AssertValid() const
{
CDocument::AssertValid();
}
void CHotspot2Doc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CHotspot2Doc commands
void CHotspot2Doc::SaveHotspot(void)
{
//Saves the current hot spot record to disk. If the member
//variable m_editRecord is 0, uses the first free slot in the file.
//Otherwise uses the slot specified by m_editRecord.
m_toEdit = TRUE;
if (m_editRecord == 0)
{
//Look for an empty record, which will have
//a blank image field.
char buf[8];
long fileLength = m_datafile.SeekToEnd();
m_datafile.SeekToBegin();
for (long pos = 0; pos < fileLength; pos += sizeof(HotSpotRecord))
{
m_datafile.Read(buf, 8);
if (strlen(buf) == 0) //Found an empty record.
{
//Go back to start of record and write data.
m_datafile.Seek(pos, CFile::begin);
m_datafile.Write(&m_currentHS, sizeof(HotSpotRecord));
BlankCurrentHS();
m_datafile.Flush();
return;
}
}
//We reach here only if no empty records were found.
//Write data at end of file.
m_datafile.SeekToEnd();
m_datafile.Write(&m_currentHS, sizeof(HotSpotRecord));
}
else //Writing an edited record to an existing record position.
{
m_datafile.Seek(m_editRecord * sizeof(HotSpotRecord), CFile::begin);
m_datafile.Write(&m_currentHS, sizeof(HotSpotRecord));
m_editRecord = 0;
}
BlankCurrentHS();
m_datafile.Flush();
}
void CHotspot2Doc::BlankCurrentHS(void)
{
//Blanks the current hot spot record.
m_currentHS.region = CRect(0,0,0,0);
strcpy (m_currentHS.image, "");
strcpy (m_currentHS.target, "");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -