Utils.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #ifndef __UTILS_H__
  2. #define __UTILS_H__
  3. #pragma once
  4. namespace DuiLib
  5. {
  6. /////////////////////////////////////////////////////////////////////////////////////
  7. //
  8. class STRINGorID
  9. {
  10. public:
  11. STRINGorID(LPCTSTR lpString) : m_lpstr(lpString)
  12. { }
  13. STRINGorID(UINT nID) : m_lpstr(MAKEINTRESOURCE(nID))
  14. { }
  15. LPCTSTR m_lpstr;
  16. };
  17. /////////////////////////////////////////////////////////////////////////////////////
  18. //
  19. class UILIB_API CDuiPoint : public tagPOINT
  20. {
  21. public:
  22. CDuiPoint();
  23. CDuiPoint(const POINT& src);
  24. CDuiPoint(int x, int y);
  25. CDuiPoint(LPARAM lParam);
  26. };
  27. /////////////////////////////////////////////////////////////////////////////////////
  28. //
  29. class UILIB_API CDuiSize : public tagSIZE
  30. {
  31. public:
  32. CDuiSize();
  33. CDuiSize(const SIZE& src);
  34. CDuiSize(const RECT rc);
  35. CDuiSize(int cx, int cy);
  36. };
  37. /////////////////////////////////////////////////////////////////////////////////////
  38. //
  39. class UILIB_API CDuiRect : public tagRECT
  40. {
  41. public:
  42. CDuiRect();
  43. CDuiRect(const RECT& src);
  44. CDuiRect(int iLeft, int iTop, int iRight, int iBottom);
  45. int GetWidth() const;
  46. int GetHeight() const;
  47. void Empty();
  48. bool IsNull() const;
  49. void Join(const RECT& rc);
  50. void ResetOffset();
  51. void Normalize();
  52. void Offset(int cx, int cy);
  53. void Inflate(int cx, int cy);
  54. void Deflate(int cx, int cy);
  55. void Union(CDuiRect& rc);
  56. };
  57. /////////////////////////////////////////////////////////////////////////////////////
  58. //
  59. class UILIB_API CStdPtrArray
  60. {
  61. public:
  62. CStdPtrArray(int iPreallocSize = 0);
  63. CStdPtrArray(const CStdPtrArray& src);
  64. ~CStdPtrArray();
  65. void Empty();
  66. void Resize(int iSize);
  67. bool IsEmpty() const;
  68. int Find(LPVOID iIndex) const;
  69. bool Add(LPVOID pData);
  70. bool SetAt(int iIndex, LPVOID pData);
  71. bool InsertAt(int iIndex, LPVOID pData);
  72. bool Remove(int iIndex);
  73. int GetSize() const;
  74. LPVOID* GetData();
  75. LPVOID GetAt(int iIndex) const;
  76. LPVOID operator[] (int nIndex) const;
  77. protected:
  78. LPVOID* m_ppVoid;
  79. int m_nCount;
  80. int m_nAllocated;
  81. };
  82. /////////////////////////////////////////////////////////////////////////////////////
  83. //
  84. class UILIB_API CStdValArray
  85. {
  86. public:
  87. CStdValArray(int iElementSize, int iPreallocSize = 0);
  88. ~CStdValArray();
  89. void Empty();
  90. bool IsEmpty() const;
  91. bool Add(LPCVOID pData);
  92. bool Remove(int iIndex);
  93. int GetSize() const;
  94. LPVOID GetData();
  95. LPVOID GetAt(int iIndex) const;
  96. LPVOID operator[] (int nIndex) const;
  97. protected:
  98. LPBYTE m_pVoid;
  99. int m_iElementSize;
  100. int m_nCount;
  101. int m_nAllocated;
  102. };
  103. /////////////////////////////////////////////////////////////////////////////////////
  104. //
  105. class UILIB_API CDuiString
  106. {
  107. public:
  108. enum { MAX_LOCAL_STRING_LEN = 63 };
  109. CDuiString();
  110. CDuiString(const TCHAR ch);
  111. CDuiString(const CDuiString& src);
  112. CDuiString(LPCTSTR lpsz, int nLen = -1);
  113. ~CDuiString();
  114. void Empty();
  115. int GetLength() const;
  116. bool IsEmpty() const;
  117. TCHAR GetAt(int nIndex) const;
  118. void Append(LPCTSTR pstr);
  119. void Assign(LPCTSTR pstr, int nLength = -1);
  120. LPCTSTR GetData() const;
  121. void SetAt(int nIndex, TCHAR ch);
  122. operator LPCTSTR() const;
  123. TCHAR operator[] (int nIndex) const;
  124. const CDuiString& operator=(const CDuiString& src);
  125. const CDuiString& operator=(const TCHAR ch);
  126. const CDuiString& operator=(LPCTSTR pstr);
  127. #ifdef _UNICODE
  128. const CDuiString& CDuiString::operator=(LPCSTR lpStr);
  129. const CDuiString& CDuiString::operator+=(LPCSTR lpStr);
  130. #else
  131. const CDuiString& CDuiString::operator=(LPCWSTR lpwStr);
  132. const CDuiString& CDuiString::operator+=(LPCWSTR lpwStr);
  133. #endif
  134. CDuiString operator+(const CDuiString& src) const;
  135. CDuiString operator+(LPCTSTR pstr) const;
  136. const CDuiString& operator+=(const CDuiString& src);
  137. const CDuiString& operator+=(LPCTSTR pstr);
  138. const CDuiString& operator+=(const TCHAR ch);
  139. bool operator == (LPCTSTR str) const;
  140. bool operator != (LPCTSTR str) const;
  141. bool operator <= (LPCTSTR str) const;
  142. bool operator < (LPCTSTR str) const;
  143. bool operator >= (LPCTSTR str) const;
  144. bool operator > (LPCTSTR str) const;
  145. int Compare(LPCTSTR pstr) const;
  146. int CompareNoCase(LPCTSTR pstr) const;
  147. void MakeUpper();
  148. void MakeLower();
  149. CDuiString Left(int nLength) const;
  150. CDuiString Mid(int iPos, int nLength = -1) const;
  151. CDuiString Right(int nLength) const;
  152. int Find(TCHAR ch, int iPos = 0) const;
  153. int Find(LPCTSTR pstr, int iPos = 0) const;
  154. int ReverseFind(TCHAR ch) const;
  155. int Replace(LPCTSTR pstrFrom, LPCTSTR pstrTo);
  156. int __cdecl Format(LPCTSTR pstrFormat, ...);
  157. int __cdecl SmallFormat(LPCTSTR pstrFormat, ...);
  158. protected:
  159. LPTSTR m_pstr;
  160. TCHAR m_szBuffer[MAX_LOCAL_STRING_LEN + 1];
  161. };
  162. /////////////////////////////////////////////////////////////////////////////////////
  163. //
  164. struct TITEM
  165. {
  166. CDuiString Key;
  167. LPVOID Data;
  168. struct TITEM* pPrev;
  169. struct TITEM* pNext;
  170. };
  171. class UILIB_API CStdStringPtrMap
  172. {
  173. public:
  174. CStdStringPtrMap(int nSize = 83);
  175. ~CStdStringPtrMap();
  176. void Resize(int nSize = 83);
  177. LPVOID Find(LPCTSTR key, bool optimize = true) const;
  178. bool Insert(LPCTSTR key, LPVOID pData);
  179. LPVOID Set(LPCTSTR key, LPVOID pData);
  180. bool Remove(LPCTSTR key);
  181. void RemoveAll();
  182. int GetSize() const;
  183. LPCTSTR GetAt(int iIndex) const;
  184. LPCTSTR operator[] (int nIndex) const;
  185. protected:
  186. TITEM** m_aT;
  187. int m_nBuckets;
  188. int m_nCount;
  189. };
  190. /////////////////////////////////////////////////////////////////////////////////////
  191. //
  192. class UILIB_API CWaitCursor
  193. {
  194. public:
  195. CWaitCursor();
  196. ~CWaitCursor();
  197. protected:
  198. HCURSOR m_hOrigCursor;
  199. };
  200. /////////////////////////////////////////////////////////////////////////////////////
  201. //
  202. class CVariant : public VARIANT
  203. {
  204. public:
  205. CVariant()
  206. {
  207. VariantInit(this);
  208. }
  209. CVariant(int i)
  210. {
  211. VariantInit(this);
  212. this->vt = VT_I4;
  213. this->intVal = i;
  214. }
  215. CVariant(float f)
  216. {
  217. VariantInit(this);
  218. this->vt = VT_R4;
  219. this->fltVal = f;
  220. }
  221. CVariant(LPOLESTR s)
  222. {
  223. VariantInit(this);
  224. this->vt = VT_BSTR;
  225. this->bstrVal = s;
  226. }
  227. CVariant(IDispatch *disp)
  228. {
  229. VariantInit(this);
  230. this->vt = VT_DISPATCH;
  231. this->pdispVal = disp;
  232. }
  233. ~CVariant()
  234. {
  235. VariantClear(this);
  236. }
  237. };
  238. }// namespace DuiLib
  239. #endif // __UTILS_H__