📄 atlmisc.h
字号:
return rect;
}
inline CRect CRect::operator -(POINT pt) const
{
CRect rect(*this);
::OffsetRect(&rect, -pt.x, -pt.y);
return rect;
}
inline CRect CRect::operator +(SIZE size) const
{
CRect rect(*this);
::OffsetRect(&rect, size.cx, size.cy);
return rect;
}
inline CRect CRect::operator -(SIZE size) const
{
CRect rect(*this);
::OffsetRect(&rect, -size.cx, -size.cy);
return rect;
}
inline CRect CRect::operator +(LPCRECT lpRect) const
{
CRect rect(this);
rect.InflateRect(lpRect);
return rect;
}
inline CRect CRect::operator -(LPCRECT lpRect) const
{
CRect rect(this);
rect.DeflateRect(lpRect);
return rect;
}
inline CRect CRect::operator &(const RECT& rect2) const
{
CRect rect;
::IntersectRect(&rect, this, &rect2);
return rect;
}
inline CRect CRect::operator |(const RECT& rect2) const
{
CRect rect;
::UnionRect(&rect, this, &rect2);
return rect;
}
inline BOOL CRect::SubtractRect(LPCRECT lpRectSrc1, LPCRECT lpRectSrc2)
{ return ::SubtractRect(this, lpRectSrc1, lpRectSrc2); }
inline void CRect::NormalizeRect()
{
int nTemp;
if (left > right)
{
nTemp = left;
left = right;
right = nTemp;
}
if (top > bottom)
{
nTemp = top;
top = bottom;
bottom = nTemp;
}
}
inline void CRect::MoveToY(int y)
{
bottom = Height() + y;
top = y;
}
inline void CRect::MoveToX(int x)
{
right = Width() + x;
left = x;
}
inline void CRect::MoveToXY(int x, int y)
{
MoveToX(x);
MoveToY(y);
}
inline void CRect::MoveToXY(POINT pt)
{
MoveToX(pt.x);
MoveToY(pt.y);
}
inline void CRect::InflateRect(LPCRECT lpRect)
{
left -= lpRect->left;
top -= lpRect->top;
right += lpRect->right;
bottom += lpRect->bottom;
}
inline void CRect::InflateRect(int l, int t, int r, int b)
{
left -= l;
top -= t;
right += r;
bottom += b;
}
inline void CRect::DeflateRect(LPCRECT lpRect)
{
left += lpRect->left;
top += lpRect->top;
right -= lpRect->right;
bottom -= lpRect->bottom;
}
inline void CRect::DeflateRect(int l, int t, int r, int b)
{
left += l;
top += t;
right -= r;
bottom -= b;
}
inline CRect CRect::MulDiv(int nMultiplier, int nDivisor) const
{
return CRect(
::MulDiv(left, nMultiplier, nDivisor),
::MulDiv(top, nMultiplier, nDivisor),
::MulDiv(right, nMultiplier, nDivisor),
::MulDiv(bottom, nMultiplier, nDivisor));
}
#endif //!_WTL_NO_WTYPES
///////////////////////////////////////////////////////////////////////////////
// CString - String class
#ifndef _WTL_NO_CSTRING
struct CStringData
{
long nRefs; // reference count
int nDataLength;
int nAllocLength;
// TCHAR data[nAllocLength]
TCHAR* data()
{ return (TCHAR*)(this + 1); }
};
// Globals
// For an empty string, m_pchData will point here
// (note: avoids special case of checking for NULL m_pchData)
// empty string data (and locked)
_declspec(selectany) int rgInitData[] = { -1, 0, 0, 0 };
_declspec(selectany) CStringData* _atltmpDataNil = (CStringData*)&rgInitData;
_declspec(selectany) LPCTSTR _atltmpPchNil = (LPCTSTR)(((BYTE*)&rgInitData) + sizeof(CStringData));
class CString
{
public:
// Constructors
CString();
CString(const CString& stringSrc);
CString(TCHAR ch, int nRepeat = 1);
CString(LPCSTR lpsz);
CString(LPCWSTR lpsz);
CString(LPCSTR lpch, int nLength);
CString(LPCWSTR lpch, int nLength);
CString(const unsigned char* psz);
// Attributes & Operations
// as an array of characters
int GetLength() const;
BOOL IsEmpty() const;
void Empty(); // free up the data
TCHAR GetAt(int nIndex) const; // 0 based
TCHAR operator [](int nIndex) const; // same as GetAt
void SetAt(int nIndex, TCHAR ch);
operator LPCTSTR() const; // as a C string
// overloaded assignment
const CString& operator =(const CString& stringSrc);
const CString& operator =(TCHAR ch);
#ifdef _UNICODE
const CString& operator =(char ch);
#endif
const CString& operator =(LPCSTR lpsz);
const CString& operator =(LPCWSTR lpsz);
const CString& operator =(const unsigned char* psz);
// string concatenation
const CString& operator +=(const CString& string);
const CString& operator +=(TCHAR ch);
#ifdef _UNICODE
const CString& operator +=(char ch);
#endif
const CString& operator +=(LPCTSTR lpsz);
friend CString __stdcall operator +(const CString& string1, const CString& string2);
friend CString __stdcall operator +(const CString& string, TCHAR ch);
friend CString __stdcall operator +(TCHAR ch, const CString& string);
#ifdef _UNICODE
friend CString __stdcall operator +(const CString& string, char ch);
friend CString __stdcall operator +(char ch, const CString& string);
#endif
friend CString __stdcall operator +(const CString& string, LPCTSTR lpsz);
friend CString __stdcall operator +(LPCTSTR lpsz, const CString& string);
// string comparison
int Compare(LPCTSTR lpsz) const; // straight character
int CompareNoCase(LPCTSTR lpsz) const; // ignore case
int Collate(LPCTSTR lpsz) const; // NLS aware
int CollateNoCase(LPCTSTR lpsz) const; // ignore case
// simple sub-string extraction
CString Mid(int nFirst, int nCount) const;
CString Mid(int nFirst) const;
CString Left(int nCount) const;
CString Right(int nCount) const;
CString SpanIncluding(LPCTSTR lpszCharSet) const;
CString SpanExcluding(LPCTSTR lpszCharSet) const;
// upper/lower/reverse conversion
void MakeUpper();
void MakeLower();
void MakeReverse();
// trimming whitespace (either side)
void TrimRight();
void TrimLeft();
// remove continuous occurrences of chTarget starting from right
void TrimRight(TCHAR chTarget);
// remove continuous occcurrences of characters in passed string,
// starting from right
void TrimRight(LPCTSTR lpszTargets);
// remove continuous occurrences of chTarget starting from left
void TrimLeft(TCHAR chTarget);
// remove continuous occcurrences of characters in
// passed string, starting from left
void TrimLeft(LPCTSTR lpszTargets);
// advanced manipulation
// replace occurrences of chOld with chNew
int Replace(TCHAR chOld, TCHAR chNew);
// replace occurrences of substring lpszOld with lpszNew;
// empty lpszNew removes instances of lpszOld
int Replace(LPCTSTR lpszOld, LPCTSTR lpszNew);
// remove occurrences of chRemove
int Remove(TCHAR chRemove);
// insert character at zero-based index; concatenates
// if index is past end of string
int Insert(int nIndex, TCHAR ch);
// insert substring at zero-based index; concatenates
// if index is past end of string
int Insert(int nIndex, LPCTSTR pstr);
// delete nCount characters starting at zero-based index
int Delete(int nIndex, int nCount = 1);
// searching (return starting index, or -1 if not found)
// look for a single character match
int Find(TCHAR ch) const; // like "C" strchr
int ReverseFind(TCHAR ch) const;
int Find(TCHAR ch, int nStart) const; // starting at index
int FindOneOf(LPCTSTR lpszCharSet) const;
// look for a specific sub-string
int Find(LPCTSTR lpszSub) const; // like "C" strstr
int Find(LPCTSTR lpszSub, int nStart) const; // starting at index
// Concatentation for non strings
const CString& Append(int n)
{
const int cchBuff = 12;
TCHAR szBuffer[cchBuff];
wsprintf(szBuffer,_T("%d"),n);
ConcatInPlace(SafeStrlen(szBuffer), szBuffer);
return *this;
}
// simple formatting
BOOL __cdecl Format(LPCTSTR lpszFormat, ...);
BOOL __cdecl Format(UINT nFormatID, ...);
BOOL FormatV(LPCTSTR lpszFormat, va_list argList);
// formatting for localization (uses FormatMessage API)
BOOL __cdecl FormatMessage(LPCTSTR lpszFormat, ...);
BOOL __cdecl FormatMessage(UINT nFormatID, ...);
// Windows support
BOOL LoadString(UINT nID); // load from string resource (255 chars max.)
#ifndef _UNICODE
// ANSI <-> OEM support (convert string in place)
void AnsiToOem();
void OemToAnsi();
#endif
#ifndef _ATL_NO_COM
// OLE BSTR support (use for OLE automation)
BSTR AllocSysString() const;
BSTR SetSysString(BSTR* pbstr) const;
#endif //!_ATL_NO_COM
// Access to string implementation buffer as "C" character array
LPTSTR GetBuffer(int nMinBufLength);
void ReleaseBuffer(int nNewLength = -1);
LPTSTR GetBufferSetLength(int nNewLength);
void FreeExtra();
// Use LockBuffer/UnlockBuffer to turn refcounting off
LPTSTR LockBuffer();
void UnlockBuffer();
// Implementation
public:
~CString();
int GetAllocLength() const;
static BOOL __stdcall _IsValidString(LPCWSTR lpsz, int nLength = -1)
{
if(lpsz == NULL)
return FALSE;
#ifndef _WIN32_WCE
return !::IsBadStringPtrW(lpsz, nLength);
#else // CE specific
nLength;
return TRUE;
#endif //_WIN32_WCE
}
static BOOL __stdcall _IsValidString(LPCSTR lpsz, int nLength = -1)
{
if(lpsz == NULL)
return FALSE;
#ifndef _WIN32_WCE
return !::IsBadStringPtrA(lpsz, nLength);
#else // CE specific
nLength;
return TRUE;
#endif //_WIN32_WCE
}
protected:
LPTSTR m_pchData; // pointer to ref counted string data
// implementation helpers
CStringData* GetData() const;
void Init();
void AllocCopy(CString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
BOOL AllocBuffer(int nLen);
void AssignCopy(int nSrcLen, LPCTSTR lpszSrcData);
BOOL ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data, int nSrc2Len, LPCTSTR lpszSrc2Data);
void ConcatInPlace(int nSrcLen, LPCTSTR lpszSrcData);
void CopyBeforeWrite();
BOOL AllocBeforeWrite(int nLen);
void Release();
static void PASCAL Release(CStringData* pData);
static int PASCAL SafeStrlen(LPCTSTR lpsz);
static int __stdcall _LoadString(UINT nID, LPTSTR lpszBuf, UINT nMaxBuf)
{
#ifdef _DEBUG
// LoadString without annoying warning from the Debug kernel if the
// segment containing the string is not present
#if (_ATL_VER >= 0x0700)
if (::FindResource(ATL::_AtlBaseModule.GetResourceInstance(), MAKEINTRESOURCE((nID>>4) + 1), RT_STRING) == NULL)
#else //!(_ATL_VER >= 0x0700)
if (::FindResource(_Module.GetResourceInstance(), MAKEINTRESOURCE((nID>>4) + 1), RT_STRING) == NULL)
#endif //!(_ATL_VER >= 0x0700)
{
lpszBuf[0] = '\0';
return 0; // not found
}
#endif //_DEBUG
#if (_ATL_VER >= 0x0700)
int nLen = ::LoadString(ATL::_AtlBaseModule.GetResourceInstance(), nID, lpszBuf, nMaxBuf);
#else //!(_ATL_VER >= 0x0700)
int nLen = ::LoadString(_Module.GetResourceInstance(), nID, lpszBuf, nMaxBuf);
#endif //!(_ATL_VER >= 0x0700)
if (nLen == 0)
lpszBuf[0] = '\0';
return nLen;
}
static const CString& __stdcall _GetEmptyString()
{
return *(CString*)&_atltmpPchNil;
}
// CString conversion helpers
static int __cdecl _wcstombsz(char* mbstr, const wchar_t* wcstr, size_t count)
{
if (count == 0 && mbstr != NULL)
return 0;
int result = ::WideCharToMultiByte(CP_ACP, 0, wcstr, -1, mbstr, (int)count, NULL, NULL);
ATLASSERT(mbstr == NULL || result <= (int)count);
if (result > 0)
mbstr[result - 1] = 0;
return result;
}
static int __cdecl _mbstowcsz(wchar_t* wcstr, const char* mbstr, size_t count)
{
if (count == 0 && wcstr != NULL)
return 0;
int result = ::MultiByteToWideChar(CP_ACP, 0, mbstr, -1, wcstr, (int)count);
ATLASSERT(wcstr == NULL || result <= (int)count);
if (result > 0)
wcstr[result - 1] = 0;
return result;
}
// Helpers to avoid CRT startup code
#ifdef _ATL_MIN_CRT
static TCHAR* _cstrchr(const TCHAR* p, TCHAR ch)
{
//strchr for '\0' should succeed
while (*p != 0)
{
if (*p == ch)
break;
p = ::CharNext(p);
}
return (TCHAR*)((*p == ch) ? p : NULL);
}
static TCHAR* _cstrrchr(const TCHAR* p, TCHAR ch)
{
const TCHAR* lpsz = NULL;
while (*p != 0)
{
if (*p == ch)
lpsz = p;
p = ::CharNext(p);
}
return (TCHAR*)lpsz;
}
static TCHAR* _cstrrev(TCHAR* pStr)
{
// Optimize NULL, zero-length, and single-char case.
if ((pStr == NULL) || (pStr[0] == '\0') || (pStr[1] == '\0'))
return pStr;
TCHAR* p = pStr;
while (*p != 0)
{
TCHAR* pNext = ::CharNext(p);
if(pNext > p + 1)
{
char p1 = *(char*)p;
*(char*)p = *(char*)(p + 1);
*(char*)(p + 1) = p1;
}
p = pNext;
}
p--;
TCHAR* q = pStr;
while (q < p)
{
TCHAR t = *q;
*q = *p;
*p = t;
q++;
p--;
}
return (TCHAR*)pStr;
}
static TCHAR* _cstrstr(const TCHAR* pStr, const TCHAR* pCharSet)
{
int nLen = lstrlen(pCharSet);
if (nLen == 0)
return (TCHAR*)pStr;
const TCHAR* pRet = NULL;
const TCHAR* pCur = pStr;
while((pCur = _cstrchr(pCur, *pCharSet)) != NULL)
{
if(memcmp(pCur, pCharSet, nLen * sizeof(TCHAR)) == 0)
{
pRet = pCur;
break;
}
pCur = ::CharNext(pCur);
}
return (TCHAR*) pRet;
}
static int _cstrspn(const TCHAR* pStr, const TCHAR* pCharSet)
{
int nRet = 0;
TCHAR* p = (TCHAR*)pStr;
while (*p != 0)
{
TCHAR* pNext = ::CharNext(p);
if(pNext > p + 1)
{
if(_cstrchr_db(pCharSet, *p, *(p + 1)) == NULL)
break;
nRet += 2;
}
else
{
if(_cstrchr(pCharSet, *p) == NULL)
break;
nRet++;
}
p = pNext;
}
return nRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -