📄 afx.h
字号:
// can also use set_terminate which is part of C++ standard library
// (these are provided for backward compatibility)
void AFXAPI AfxTerminate();
typedef void (AFXAPI* AFX_TERM_PROC)();
AFX_TERM_PROC AFXAPI AfxSetTerminate(AFX_TERM_PROC);
#endif
/////////////////////////////////////////////////////////////////////////////
// Exception macros using try, catch and throw
// (for backward compatibility to previous versions of MFC)
#ifndef _AFX_OLD_EXCEPTIONS
#define TRY { AFX_EXCEPTION_LINK _afxExceptionLink; try {
#define CATCH(class, e) } catch (class* e) \
{ ASSERT(e->IsKindOf(RUNTIME_CLASS(class))); \
_afxExceptionLink.m_pException = e;
#define AND_CATCH(class, e) } catch (class* e) \
{ ASSERT(e->IsKindOf(RUNTIME_CLASS(class))); \
_afxExceptionLink.m_pException = e;
#define END_CATCH } }
#define THROW(e) throw e
#define THROW_LAST() (AfxThrowLastCleanup(), throw)
// Advanced macros for smaller code
#define CATCH_ALL(e) } catch (CException* e) \
{ { ASSERT(e->IsKindOf(RUNTIME_CLASS(CException))); \
_afxExceptionLink.m_pException = e;
#define AND_CATCH_ALL(e) } catch (CException* e) \
{ { ASSERT(e->IsKindOf(RUNTIME_CLASS(CException))); \
_afxExceptionLink.m_pException = e;
#define END_CATCH_ALL } } }
#define END_TRY } catch (CException* e) \
{ ASSERT(e->IsKindOf(RUNTIME_CLASS(CException))); \
_afxExceptionLink.m_pException = e; } }
#else //_AFX_OLD_EXCEPTIONS
/////////////////////////////////////////////////////////////////////////////
// Exception macros using setjmp and longjmp
// (for portability to compilers with no support for C++ exception handling)
#define TRY \
{ AFX_EXCEPTION_LINK _afxExceptionLink; \
if (::setjmp(_afxExceptionLink.m_jumpBuf) == 0)
#define CATCH(class, e) \
else if (::AfxCatchProc(RUNTIME_CLASS(class))) \
{ class* e = (class*)_afxExceptionLink.m_pException;
#define AND_CATCH(class, e) \
} else if (::AfxCatchProc(RUNTIME_CLASS(class))) \
{ class* e = (class*)_afxExceptionLink.m_pException;
#define END_CATCH \
} else { ::AfxThrow(NULL); } }
#define THROW(e) AfxThrow(e)
#define THROW_LAST() AfxThrow(NULL)
// Advanced macros for smaller code
#define CATCH_ALL(e) \
else { CException* e = _afxExceptionLink.m_pException;
#define AND_CATCH_ALL(e) \
} else { CException* e = _afxExceptionLink.m_pException;
#define END_CATCH_ALL } }
#define END_TRY }
#endif //_AFX_OLD_EXCEPTIONS
/////////////////////////////////////////////////////////////////////////////
// Standard Exception classes
class CMemoryException : public CSimpleException
{
DECLARE_DYNAMIC(CMemoryException)
public:
CMemoryException();
// Implementation
public:
CMemoryException(BOOL bAutoDelete);
CMemoryException(BOOL bAutoDelete, UINT nResourceID);
virtual ~CMemoryException();
};
class CNotSupportedException : public CSimpleException
{
DECLARE_DYNAMIC(CNotSupportedException)
public:
CNotSupportedException();
// Implementation
public:
CNotSupportedException(BOOL bAutoDelete);
CNotSupportedException(BOOL bAutoDelete, UINT nResourceID);
virtual ~CNotSupportedException();
};
class CArchiveException : public CException
{
DECLARE_DYNAMIC(CArchiveException)
public:
enum {
none,
generic,
readOnly,
endOfFile,
writeOnly,
badIndex,
badClass,
badSchema
};
// Constructor
CArchiveException(int cause = CArchiveException::none,
LPCTSTR lpszArchiveName = NULL);
// Attributes
int m_cause;
CString m_strFileName;
// Implementation
public:
virtual ~CArchiveException();
#ifdef _DEBUG
virtual void Dump(CDumpContext& dc) const;
#endif
virtual BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
PUINT pnHelpContext = NULL);
};
class CFileException : public CException
{
DECLARE_DYNAMIC(CFileException)
public:
enum {
none,
generic,
fileNotFound,
badPath,
tooManyOpenFiles,
accessDenied,
invalidFile,
removeCurrentDir,
directoryFull,
badSeek,
hardIO,
sharingViolation,
lockViolation,
diskFull,
endOfFile
};
// Constructor
CFileException(int cause = CFileException::none, LONG lOsError = -1,
LPCTSTR lpszArchiveName = NULL);
// Attributes
int m_cause;
LONG m_lOsError;
CString m_strFileName;
// Operations
// convert a OS dependent error code to a Cause
static int PASCAL OsErrorToException(LONG lOsError);
static int PASCAL ErrnoToException(int nErrno);
// helper functions to throw exception after converting to a Cause
static void PASCAL ThrowOsError(LONG lOsError, LPCTSTR lpszFileName = NULL);
static void PASCAL ThrowErrno(int nErrno, LPCTSTR lpszFileName = NULL);
// Implementation
public:
virtual ~CFileException();
#ifdef _DEBUG
virtual void Dump(CDumpContext&) const;
#endif
virtual BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
PUINT pnHelpContext = NULL);
};
/////////////////////////////////////////////////////////////////////////////
// Standard exception throws
void AFXAPI AfxThrowMemoryException();
void AFXAPI AfxThrowNotSupportedException();
void AFXAPI AfxThrowArchiveException(int cause,
LPCTSTR lpszArchiveName = NULL);
void AFXAPI AfxThrowFileException(int cause, LONG lOsError = -1,
LPCTSTR lpszFileName = NULL);
/////////////////////////////////////////////////////////////////////////////
// File - raw unbuffered disk file I/O
class CFile : public CObject
{
DECLARE_DYNAMIC(CFile)
public:
// Flag values
enum OpenFlags {
modeRead = 0x0000,
modeWrite = 0x0001,
modeReadWrite = 0x0002,
shareCompat = 0x0000,
shareExclusive = 0x0010,
shareDenyWrite = 0x0020,
shareDenyRead = 0x0030,
shareDenyNone = 0x0040,
modeNoInherit = 0x0080,
modeCreate = 0x1000,
modeNoTruncate = 0x2000,
typeText = 0x4000, // typeText and typeBinary are used in
typeBinary = (int)0x8000 // derived classes only
};
enum Attribute {
normal = 0x00,
readOnly = 0x01,
hidden = 0x02,
system = 0x04,
volume = 0x08,
directory = 0x10,
archive = 0x20
};
enum SeekPosition { begin = 0x0, current = 0x1, end = 0x2 };
enum { hFileNull = -1 };
// Constructors
CFile();
CFile(int hFile);
CFile(LPCTSTR lpszFileName, UINT nOpenFlags);
// Attributes
UINT m_hFile;
operator HFILE() const;
virtual DWORD GetPosition() const;
BOOL GetStatus(CFileStatus& rStatus) const;
virtual CString GetFileName() const;
virtual CString GetFileTitle() const;
virtual CString GetFilePath() const;
virtual void SetFilePath(LPCTSTR lpszNewName);
// Operations
virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags,
CFileException* pError = NULL);
static void PASCAL Rename(LPCTSTR lpszOldName,
LPCTSTR lpszNewName);
static void PASCAL Remove(LPCTSTR lpszFileName);
static BOOL PASCAL GetStatus(LPCTSTR lpszFileName,
CFileStatus& rStatus);
static void PASCAL SetStatus(LPCTSTR lpszFileName,
const CFileStatus& status);
DWORD SeekToEnd();
void SeekToBegin();
// backward compatible ReadHuge and WriteHuge
DWORD ReadHuge(void* lpBuffer, DWORD dwCount);
void WriteHuge(const void* lpBuffer, DWORD dwCount);
// Overridables
virtual CFile* Duplicate() const;
virtual LONG Seek(LONG lOff, UINT nFrom);
virtual void SetLength(DWORD dwNewLen);
virtual DWORD GetLength() const;
virtual UINT Read(void* lpBuf, UINT nCount);
virtual void Write(const void* lpBuf, UINT nCount);
virtual void LockRange(DWORD dwPos, DWORD dwCount);
virtual void UnlockRange(DWORD dwPos, DWORD dwCount);
virtual void Abort();
virtual void Flush();
virtual void Close();
// Implementation
public:
virtual ~CFile();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
enum BufferCommand { bufferRead, bufferWrite, bufferCommit, bufferCheck };
virtual UINT GetBufferPtr(UINT nCommand, UINT nCount = 0,
void** ppBufStart = NULL, void** ppBufMax = NULL);
protected:
BOOL m_bCloseOnDelete;
CString m_strFileName;
};
/////////////////////////////////////////////////////////////////////////////
// STDIO file implementation
class CStdioFile : public CFile
{
DECLARE_DYNAMIC(CStdioFile)
public:
// Constructors
CStdioFile();
CStdioFile(FILE* pOpenStream);
CStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags);
// Attributes
FILE* m_pStream; // stdio FILE
// m_hFile from base class is _fileno(m_pStream)
// Operations
// reading and writing strings
virtual void WriteString(LPCTSTR lpsz);
virtual LPTSTR ReadString(LPTSTR lpsz, UINT nMax);
virtual BOOL ReadString(CString& rString);
// Implementation
public:
virtual ~CStdioFile();
#ifdef _DEBUG
void Dump(CDumpContext& dc) const;
#endif
virtual DWORD GetPosition() const;
virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags,
CFileException* pError = NULL);
virtual UINT Read(void* lpBuf, UINT nCount);
virtual void Write(const void* lpBuf, UINT nCount);
virtual LONG Seek(LONG lOff, UINT nFrom);
virtual void Abort();
virtual void Flush();
virtual void Close();
// Unsupported APIs
virtual CFile* Duplicate() const;
virtual void LockRange(DWORD dwPos, DWORD dwCount);
virtual void UnlockRange(DWORD dwPos, DWORD dwCount);
};
////////////////////////////////////////////////////////////////////////////
// Memory based file implementation
class CMemFile : public CFile
{
DECLARE_DYNAMIC(CMemFile)
public:
// Constructors
CMemFile(UINT nGrowBytes = 1024);
CMemFile(BYTE* lpBuffer, UINT nBufferSize, UINT nGrowBytes = 0);
// Operations
void Attach(BYTE* lpBuffer, UINT nBufferSize, UINT nGrowBytes = 0);
BYTE* Detach();
// Advanced Overridables
protected:
virtual BYTE* Alloc(DWORD nBytes);
virtual BYTE* Realloc(BYTE* lpMem, DWORD nBytes);
virtual BYTE* Memcpy(BYTE* lpMemTarget, const BYTE* lpMemSource, UINT nBytes);
virtual void Free(BYTE* lpMem);
virtual void GrowFile(DWORD dwNewLen);
// Implementation
protected:
UINT m_nGrowBytes;
DWORD m_nPosition;
DWORD m_nBufferSize;
DWORD m_nFileSize;
BYTE* m_lpBuffer;
BOOL m_bAutoDelete;
public:
virtual ~CMemFile();
#ifdef _DEBUG
virtual void Dump(CDumpContext& dc) const;
virtual void AssertValid() const;
#endif
virtual DWORD GetPosition() const;
BOOL GetStatus(CFileStatus& rStatus) const;
virtual LONG Seek(LONG lOff, UINT nFrom);
virtual void SetLength(DWORD dwNewLen);
virtual UINT Read(void* lpBuf, UINT nCount);
virtual void Write(const void* lpBuf, UINT nCount);
virtual void Abort();
virtual void Flush();
virtual void Close();
virtual UINT GetBufferPtr(UINT nCommand, UINT nCount = 0,
void** ppBufStart = NULL, void** ppBufMax = NULL);
// Unsupported APIs
virtual CFile* Duplicate() const;
virtual void LockRange(DWORD dwPos, DWORD dwCount);
virtual void UnlockRange(DWORD dwPos, DWORD dwCount);
};
////////////////////////////////////////////////////////////////////////////
// Local file searches
class CFileFind : public CObject
{
public:
CFileFind();
virtual ~CFileFind();
// Attributes
public:
DWORD GetLength() const;
#if defined(_X86_) || defined(_ALPHA_)
__int64 GetLength64() const;
#endif
virtual CString GetFileName() const;
virtual CString GetFilePath() const;
virtual CString GetFileTitle() const;
virtual CString GetFileURL() const;
virtual CString GetRoot() const;
virtual BOOL GetLastWriteTime(FILETIME* pTimeStamp) const;
virtual BOOL GetLastAccessTime(FILETIME* pTimeStamp) const;
virtual BOOL GetCreationTime(FILETIME* pTimeStamp) const;
virtual BOOL GetLastWriteTime(CTime& refTime) const;
virtual BOOL GetLastAccessTime(CTime& refTime) const;
virtual BOOL GetCreationTime(CTime& refTime) const;
virtual BOOL MatchesMask(DWORD dwMask) const;
virtual BOOL IsDots() const;
// these aren't virtual because they all use MatchesMask(), which is
BOOL IsReadOnly() const;
BOOL IsDirectory() const;
BOOL IsCompressed() const;
BOOL IsSystem() const;
BOOL IsHidden() const;
BOOL IsTemporary() const;
BOOL IsNormal() const;
BOOL IsArchived() const;
// Operations
void Close();
virtual BOOL FindFile(LPCTSTR pstrName = NULL, DWORD dwUnused = 0);
virtual BOOL FindNextFile();
protected:
virtual void CloseContext();
// Implementation
protected:
void* m_pFoundInfo;
void* m_pNextInfo;
HANDLE m_hContext;
BOOL m_bGotLast;
CString m_strRoot;
TCHAR m_chDirSeparator; // not '\\' for Internet classes
#ifdef _DEBUG
void Dump(CDumpContext& dc) const;
void AssertValid() const;
#endif
DECLARE_DYNAMIC(CFileFind)
};
/////////////////////////////////////////////////////////////////////////////
// CTimeSpan and CTime
class CTimeSpan
{
public:
// Constructors
CTimeSpan();
CTimeSpan(time_t time);
CTimeSpan(LONG lDays, int nHours, int nMins, int nSecs);
CTimeSpan(const CTimeSpan& timeSpanSrc);
const CTimeSpan& operator=(const CTimeSpan& timeSpanSrc);
// Attributes
// extract parts
LONG GetDays() const; // total # of days
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -