Utils.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #ifndef __UTILS_H__
  2. #define __UTILS_H__
  3. #pragma once
  4. namespace DuiLib
  5. {
  6. /////////////////////////////////////////////////////////////////////////////////////
  7. //
  8. class DUILIB_API STRINGorID
  9. {
  10. public:
  11. STRINGorID(LPCTSTR lpString);
  12. STRINGorID(unsigned int nID);
  13. LPCTSTR m_lpstr;
  14. };
  15. /////////////////////////////////////////////////////////////////////////////////////
  16. //
  17. class CDuiString;
  18. class DUILIB_API CDuiPoint : public tagPOINT
  19. {
  20. public:
  21. CDuiPoint();
  22. CDuiPoint(const POINT& src);
  23. CDuiPoint(long x, long y);
  24. CDuiPoint(LPARAM lParam);
  25. CDuiPoint(LPCTSTR pstrValue);
  26. CDuiString ToString();
  27. };
  28. /////////////////////////////////////////////////////////////////////////////////////
  29. //
  30. class DUILIB_API CDuiSize : public tagSIZE
  31. {
  32. public:
  33. CDuiSize();
  34. CDuiSize(const SIZE& src);
  35. CDuiSize(const RECT rc);
  36. CDuiSize(long cx, long cy);
  37. CDuiSize(LPCTSTR pstrValue);
  38. CDuiString ToString();
  39. };
  40. /////////////////////////////////////////////////////////////////////////////////////
  41. //
  42. class DUILIB_API CDuiRect : public tagRECT
  43. {
  44. public:
  45. CDuiRect();
  46. CDuiRect(const RECT& src);
  47. CDuiRect(long iLeft, long iTop, long iRight, long iBottom);
  48. CDuiRect(LPCTSTR pstrValue);
  49. CDuiString ToString();
  50. int GetWidth() const;
  51. int GetHeight() const;
  52. void Empty();
  53. bool IsNull() const;
  54. void Join(const RECT& rc);
  55. void ResetOffset();
  56. void Normalize();
  57. void Offset(int cx, int cy);
  58. void Inflate(int cx, int cy);
  59. void Deflate(int cx, int cy);
  60. void Union(CDuiRect& rc);
  61. };
  62. /////////////////////////////////////////////////////////////////////////////////////
  63. //
  64. class DUILIB_API CDuiString
  65. {
  66. public:
  67. enum { MAX_LOCAL_STRING_LEN = 63 };
  68. CDuiString();
  69. CDuiString(const TCHAR ch);
  70. CDuiString(const CDuiString& src);
  71. CDuiString(LPCTSTR lpsz, int nLen = -1);
  72. ~CDuiString();
  73. CDuiString ToString();
  74. void Empty();
  75. int GetLength() const;
  76. bool IsEmpty() const;
  77. TCHAR GetAt(int nIndex) const;
  78. void Append(LPCTSTR pstr);
  79. void Assign(LPCTSTR pstr, int nLength = -1);
  80. LPCTSTR GetData() const;
  81. void SetAt(int nIndex, TCHAR ch);
  82. operator LPCTSTR() const;
  83. TCHAR operator[] (int nIndex) const;
  84. const CDuiString& operator=(const CDuiString& src);
  85. const CDuiString& operator=(const TCHAR ch);
  86. const CDuiString& operator=(LPCTSTR pstr);
  87. #ifdef _UNICODE
  88. const CDuiString& operator=(LPCSTR lpStr);
  89. const CDuiString& operator+=(LPCSTR lpStr);
  90. #else
  91. const CDuiString& operator=(LPCWSTR lpwStr);
  92. const CDuiString& operator+=(LPCWSTR lpwStr);
  93. #endif
  94. CDuiString operator+(const CDuiString& src) const;
  95. CDuiString operator+(LPCTSTR pstr) const;
  96. const CDuiString& operator+=(const CDuiString& src);
  97. const CDuiString& operator+=(LPCTSTR pstr);
  98. const CDuiString& operator+=(const TCHAR ch);
  99. bool operator == (LPCTSTR str) const;
  100. bool operator != (LPCTSTR str) const;
  101. bool operator <= (LPCTSTR str) const;
  102. bool operator < (LPCTSTR str) const;
  103. bool operator >= (LPCTSTR str) const;
  104. bool operator > (LPCTSTR str) const;
  105. int Compare(LPCTSTR pstr) const;
  106. int CompareNoCase(LPCTSTR pstr) const;
  107. void MakeUpper();
  108. void MakeLower();
  109. CDuiString Left(int nLength) const;
  110. CDuiString Mid(int iPos, int nLength = -1) const;
  111. CDuiString Right(int nLength) const;
  112. int Find(TCHAR ch, int iPos = 0) const;
  113. int Find(LPCTSTR pstr, int iPos = 0) const;
  114. int ReverseFind(TCHAR ch) const;
  115. int Replace(LPCTSTR pstrFrom, LPCTSTR pstrTo);
  116. int __cdecl Format(LPCTSTR pstrFormat, ...);
  117. int __cdecl SmallFormat(LPCTSTR pstrFormat, ...);
  118. protected:
  119. LPTSTR m_pstr;
  120. TCHAR m_szBuffer[MAX_LOCAL_STRING_LEN + 1];
  121. };
  122. /////////////////////////////////////////////////////////////////////////////////////
  123. //
  124. class DUILIB_API CDuiPtrArray
  125. {
  126. public:
  127. CDuiPtrArray(int iPreallocSize = 0);
  128. CDuiPtrArray(const CDuiPtrArray& src);
  129. ~CDuiPtrArray();
  130. void Empty();
  131. void Resize(int iSize);
  132. bool IsEmpty() const;
  133. int Find(LPVOID iIndex) const;
  134. bool Add(LPVOID pData);
  135. bool SetAt(int iIndex, LPVOID pData);
  136. bool InsertAt(int iIndex, LPVOID pData);
  137. bool Remove(int iIndex, int iCount = 1);
  138. int GetSize() const;
  139. LPVOID* GetData();
  140. LPVOID GetAt(int iIndex) const;
  141. LPVOID operator[] (int nIndex) const;
  142. protected:
  143. LPVOID* m_ppVoid;
  144. int m_nCount;
  145. int m_nAllocated;
  146. };
  147. /////////////////////////////////////////////////////////////////////////////////////
  148. //
  149. class DUILIB_API CDuiValArray
  150. {
  151. public:
  152. CDuiValArray(int iElementSize, int iPreallocSize = 0);
  153. ~CDuiValArray();
  154. void Empty();
  155. bool IsEmpty() const;
  156. bool Add(LPCVOID pData);
  157. bool Remove(int iIndex, int iCount = 1);
  158. int GetSize() const;
  159. LPVOID GetData();
  160. LPVOID GetAt(int iIndex) const;
  161. LPVOID operator[] (int nIndex) const;
  162. protected:
  163. LPBYTE m_pVoid;
  164. int m_iElementSize;
  165. int m_nCount;
  166. int m_nAllocated;
  167. };
  168. /////////////////////////////////////////////////////////////////////////////////////
  169. //
  170. struct TITEM;
  171. class DUILIB_API CDuiStringPtrMap
  172. {
  173. public:
  174. CDuiStringPtrMap(int nSize = 83);
  175. ~CDuiStringPtrMap();
  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 DUILIB_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__