Win32Helper.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 2.3.15
  5. * Author : Bruce Liang
  6. * Website : http://www.jessma.org
  7. * Project : https://github.com/ldcsaa
  8. * Blog : http://www.cnblogs.com/ldcsaa
  9. * Wiki : http://www.oschina.net/p/hp-socket
  10. * QQ Group : 75375912
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #pragma once
  25. #include "GeneralHelper.h"
  26. // RECT 帮助宏
  27. #define RC_WIDTH(rc) (rc.right - rc.left)
  28. #define RC_HEIGHT(rc) (rc.bottom - rc.top)
  29. #define RC_CENTER_X(rc) ((rc.right + rc.left) / 2)
  30. #define RC_CENTER_Y(rc) ((rc.bottom + rc.top) / 2)
  31. /************************************************************************/
  32. /* 消息映射帮助宏 */
  33. /************************************************************************/
  34. /* see: WindowsX.h */
  35. #define HANDLE_SYS_MSG(hwnd, message, fn) HANDLE_MSG(hwnd, message, fn)
  36. /* LRESULT Cls_OnMessage(HWND hwnd, WPARAM wParam, LPARAM lParam) */
  37. #define HANDLE_USER_MSG(hwnd, message, fn) \
  38. case (message): return (LRESULT)(fn)((hwnd), (wParam), (lParam))
  39. #define FORWARD_USER_MSG(hwnd, message, wParam, lParam, fn) \
  40. (LRESULT)(fn)((hwnd), (message), (wParam), (lParam))
  41. #define GET_WND_PROC_INTERNAL(theClass, flag) ((WNDPROC)theClass##flag##WndProc)
  42. #define GET_DLG_PROC_INTERNAL(theClass, flag) ((DLGPROC)theClass##flag##DlgProc)
  43. #define DECLARE_MSG_MAP_INTERNAL(theClass, flag) \
  44. static LRESULT CALLBACK theClass##flag##WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  45. #define DECLARE_DLG_MSG_MAP_INTERNAL(theClass, flag) \
  46. static BOOL CALLBACK theClass##flag##DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  47. #define BEGIN_MSG_MAP_INTERNAL(theClass, flag) \
  48. LRESULT theClass##flag##WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) \
  49. { \
  50. LRESULT result = 0; \
  51. \
  52. switch(msg) \
  53. {
  54. #define BEGIN_DLG_MSG_MAP_INTERNAL(theClass, flag) \
  55. BOOL theClass##flag##DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) \
  56. { \
  57. BOOL retVal = TRUE; \
  58. LRESULT result = 0; \
  59. \
  60. switch(msg) \
  61. {
  62. // 窗口过程为类中的静态成员函数
  63. #define GET_WND_PROC(theClass) GET_WND_PROC_INTERNAL(theClass, ::)
  64. #define GET_DLG_PROC(theClass) GET_DLG_PROC_INTERNAL(theClass, ::)
  65. #define DECLARE_MSG_MAP(theClass) \
  66. public: \
  67. DECLARE_MSG_MAP_INTERNAL(theClass, ::)
  68. #define DECLARE_DLG_MSG_MAP(theClass) \
  69. public: \
  70. DECLARE_DLG_MSG_MAP_INTERNAL(theClass, ::)
  71. #define BEGIN_MSG_MAP(theClass) BEGIN_MSG_MAP_INTERNAL(theClass, ::)
  72. #define BEGIN_DLG_MSG_MAP(theClass) BEGIN_DLG_MSG_MAP_INTERNAL(theClass, ::)
  73. /* 消息处理函数的声明请参考: <WindowsX.h> 的 HANDLE_MSG */
  74. #define ADD_MSG_MAP(msg, fn) \
  75. case (msg): result = HANDLE_##msg((hWnd), (wParam), (lParam), (fn)); break;
  76. /* LRESULT Cls_OnMessage(HWND hwnd, WPARAM wParam, LPARAM lParam) */
  77. #define ADD_USER_MSG_MAP(msg, fn) \
  78. case (msg): result = (LRESULT)(fn)((hWnd), (wParam), (lParam)); break;
  79. #define END_MSG_MAP() \
  80. default: \
  81. result = ::DefWindowProc(hWnd, msg, wParam, lParam); \
  82. } \
  83. \
  84. return result; \
  85. }
  86. #define END_DLG_MSG_MAP() \
  87. default: \
  88. retVal = FALSE; \
  89. } \
  90. \
  91. if(retVal) \
  92. SetDlgMsgResult(hWnd, msg, result); \
  93. \
  94. return retVal; \
  95. }
  96. // 窗口过程为全局函数
  97. #define GET_GLOBAL_WND_PROC(theClass) GET_WND_PROC_INTERNAL(theClass, _)
  98. #define DECLARE_GLOBAL_MSG_MAP(theClass) DECLARE_MSG_MAP_INTERNAL(theClass, _)
  99. #define BEGIN_GLOBAL_MSG_MAP(theClass) BEGIN_MSG_MAP_INTERNAL(theClass, _)
  100. #define END_GLOBAL_MSG_MAP() END_MSG_MAP()
  101. #define GET_GLOBAL_DLG_PROC(theClass) GET_DLG_PROC_INTERNAL(theClass, _)
  102. #define DECLARE_GLOBAL_DLG_MSG_MAP(theClass) DECLARE_DLG_MSG_MAP_INTERNAL(theClass, _)
  103. #define BEGIN_GLOBAL_DLG_MSG_MAP(theClass) BEGIN_DLG_MSG_MAP_INTERNAL(theClass, _)
  104. #define END_GLOBAL_DLG_MSG_MAP() END_DLG_MSG_MAP()
  105. // 绑定对象指针到窗口
  106. #define ATTACH_OBJ_PTR_TO_WINDOW(hwnd, objPtr) ::SetWindowLongPtr(hwnd, GWL_USERDATA, (LONG_PTR)objPtr)
  107. #define GET_OBJ_PTR_FROM_WINDOW(hwnd, theClass) (theClass*)(LONG_PTR)::GetWindowLongPtr(hwnd, GWL_USERDATA)
  108. #define DEFINE_OBJ_PTR_FROM_WINDOW(hwnd, theClass, pObj) \
  109. theClass* pObj = GET_OBJ_PTR_FROM_WINDOW(hwnd, theClass); \
  110. ASSERT(pObj);
  111. /************************************************************************/
  112. /* 应用程序唯一实例 */
  113. /************************************************************************/
  114. class COnlyOneApp
  115. {
  116. public:
  117. BOOL IsFirstApp () {return m_bIsFirstApp;}
  118. DWORD GetProcID () {return m_dwProcID;}
  119. COnlyOneApp(LPCTSTR pszAppFlag)
  120. : m_dwProcID(0), m_bIsFirstApp(FALSE)
  121. {
  122. m_hMap = ::CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, sizeof(DWORD), pszAppFlag);
  123. if(m_hMap)
  124. {
  125. if(::GetLastError() != ERROR_ALREADY_EXISTS)
  126. {
  127. m_bIsFirstApp = TRUE;
  128. m_dwProcID = ::GetCurrentProcessId();
  129. LPVOID lpBuff = ::MapViewOfFile(m_hMap, FILE_MAP_WRITE, 0, 0, sizeof(DWORD));
  130. ASSERT(lpBuff);
  131. memcpy(lpBuff, &m_dwProcID, sizeof(DWORD));
  132. ::UnmapViewOfFile(lpBuff);
  133. }
  134. else
  135. {
  136. m_bIsFirstApp = FALSE;
  137. LPVOID lpBuff = ::MapViewOfFile(m_hMap, FILE_MAP_READ, 0, 0, sizeof(DWORD));
  138. ASSERT(lpBuff);
  139. memcpy(&m_dwProcID, lpBuff, sizeof(DWORD));
  140. ::UnmapViewOfFile(lpBuff);
  141. }
  142. }
  143. else
  144. {
  145. ASSERT(FALSE);
  146. }
  147. }
  148. ~COnlyOneApp() {if(m_hMap) {::CloseHandle(m_hMap); m_hMap = nullptr;}}
  149. private:
  150. HANDLE m_hMap;
  151. DWORD m_dwProcID;
  152. BOOL m_bIsFirstApp;
  153. DECLARE_NO_COPY_CLASS(COnlyOneApp)
  154. };
  155. class COnlyOneWndApp
  156. {
  157. public:
  158. BOOL IsFirstApp() {return m_hwndPre == nullptr;}
  159. HWND GetPreInstanceWindow() {return m_hwndPre;}
  160. COnlyOneWndApp(LPCTSTR lpszClassName, LPCTSTR lpszWindowName = nullptr)
  161. {
  162. m_hwndPre = ::FindWindow(lpszClassName, lpszWindowName);
  163. }
  164. private:
  165. HWND m_hwndPre;
  166. DECLARE_NO_COPY_CLASS(COnlyOneWndApp)
  167. };
  168. /************************************************************************/
  169. /* 句柄 (HANDLE) 智能包装器 */
  170. /************************************************************************/
  171. template<HANDLE NULL_VALUE>
  172. class auto_handle
  173. {
  174. public:
  175. auto_handle(HANDLE h = NULL_VALUE) {set(h);}
  176. auto_handle(auto_handle<NULL_VALUE>& other) {set(other.release());}
  177. ~auto_handle() {reset();}
  178. auto_handle<NULL_VALUE>& reset(HANDLE h = NULL_VALUE)
  179. {
  180. if(h != m_h)
  181. {
  182. if(is_valid())
  183. VERIFY(::CloseHandle(m_h));
  184. set(h);
  185. }
  186. return *this;
  187. }
  188. auto_handle<NULL_VALUE>& reset(auto_handle<NULL_VALUE>& other)
  189. {
  190. if(this != &other)
  191. reset(other.release());
  192. return *this;
  193. }
  194. HANDLE release()
  195. {
  196. HANDLE h = get();
  197. set(NULL_VALUE);
  198. return h;
  199. }
  200. operator HANDLE () const {return m_h;}
  201. HANDLE get () const {return m_h;}
  202. HANDLE& get_ref () const {return m_h;}
  203. bool is_valid () const {return m_h != NULL_VALUE;}
  204. auto_handle<NULL_VALUE>& operator = (HANDLE h) {return reset(h);}
  205. auto_handle<NULL_VALUE>& operator = (auto_handle<NULL_VALUE>& other) {return reset(other);}
  206. bool operator == (HANDLE h) const {return m_h == h;}
  207. bool operator != (HANDLE h) const {return !(operator == (h)); }
  208. bool operator == (const auto_handle<NULL_VALUE>& other) const {return m_h == other.m_h;}
  209. bool operator != (const auto_handle<NULL_VALUE>& other) const {return !(operator == (other));}
  210. private:
  211. void set(HANDLE h) {m_h = h;}
  212. // ~! do not define these conversion, because it's very easy to making mistake !~
  213. template<HANDLE _Other> auto_handle(const auto_handle<_Other>&);
  214. template<HANDLE _Other> auto_handle<NULL_VALUE>& operator = (const auto_handle<_Other>&);
  215. private:
  216. HANDLE m_h;
  217. };
  218. typedef auto_handle<INVALID_HANDLE_VALUE> auto_file_handle; // 文件智能句柄
  219. typedef auto_handle<nullptr> auto_res_handle; // 普通资源智能句柄
  220. /************************************************************************/
  221. /* DC 智能包装器 */
  222. /************************************************************************/
  223. class auto_dc
  224. {
  225. public:
  226. auto_dc(HDC h = nullptr, HWND w = nullptr, bool is_create = false)
  227. {
  228. set(h, w, is_create);
  229. }
  230. auto_dc(auto_dc& other)
  231. {
  232. set(other.m_h, other.m_w, other.m_is_create);
  233. other.release();
  234. }
  235. ~auto_dc() {reset();}
  236. HDC GetDC(HWND hWnd)
  237. {
  238. HDC h = ::GetDC(hWnd);
  239. reset(h, hWnd, false);
  240. return h;
  241. }
  242. HDC GetWindowDC(HWND hWnd)
  243. {
  244. HDC h = ::GetWindowDC(hWnd);
  245. reset(h, hWnd, false);
  246. return h;
  247. }
  248. HDC GetDCEx(HWND hWnd, HRGN hrgnClip, DWORD flags)
  249. {
  250. HDC h = ::GetDCEx(hWnd, hrgnClip, flags);
  251. reset(h, hWnd, false);
  252. return h;
  253. }
  254. HDC CreateDC(LPCTSTR lpszDriver, LPCTSTR lpszDevice, LPCTSTR lpszOutput, CONST DEVMODE* lpInitData)
  255. {
  256. HDC h = ::CreateDC(lpszDriver, lpszDevice, lpszOutput, lpInitData);
  257. reset(h, nullptr, true);
  258. return h;
  259. }
  260. HDC CreateCompatibleDC(HDC hSrc)
  261. {
  262. HDC h = ::CreateCompatibleDC(hSrc);
  263. reset(h, nullptr, true);
  264. return h;
  265. }
  266. HGDIOBJ GetCurrentObject(UINT uObjectType)
  267. {
  268. return ::GetCurrentObject(m_h, uObjectType);
  269. }
  270. HBITMAP _GetCurrentBitmap()
  271. {
  272. return (HBITMAP)GetCurrentObject(OBJ_BITMAP);
  273. }
  274. HBRUSH _GetCurrentBrush()
  275. {
  276. return (HBRUSH)GetCurrentObject(OBJ_BRUSH);
  277. }
  278. HPALETTE _GetCurrentPalette()
  279. {
  280. return (HPALETTE)GetCurrentObject(OBJ_PAL);
  281. }
  282. HPEN _GetCurrentPen()
  283. {
  284. return (HPEN)GetCurrentObject(OBJ_PEN);
  285. }
  286. HFONT _GetCurrentFont()
  287. {
  288. return (HFONT)GetCurrentObject(OBJ_FONT);
  289. }
  290. int SelectClipRgn(HRGN hrgn)
  291. {
  292. return ::SelectClipRgn(m_h, hrgn);
  293. }
  294. int ExtSelectClipRgn(HRGN hrgn, int mode)
  295. {
  296. return ::ExtSelectClipRgn(m_h, hrgn, mode);
  297. }
  298. HGDIOBJ SelectObject(HGDIOBJ hgdiobj)
  299. {
  300. return ::SelectObject(m_h, hgdiobj);
  301. }
  302. HPALETTE SelectPalette(HPALETTE hpal, BOOL bForceBackground)
  303. {
  304. return ::SelectPalette(m_h, hpal, bForceBackground);
  305. }
  306. HBITMAP _SelectBitmap(HBITMAP hbm)
  307. {
  308. return (HBITMAP)SelectObject(hbm);
  309. }
  310. HBRUSH _SelectBrush(HBRUSH hbr)
  311. {
  312. return (HBRUSH)SelectObject(hbr);
  313. }
  314. HPEN _SelectPen(HPEN hpen)
  315. {
  316. return (HPEN)SelectObject(hpen);
  317. }
  318. HRGN _SelectRgn(HRGN hrgn)
  319. {
  320. return (HRGN)SelectObject(hrgn);
  321. }
  322. HFONT _SelectFont(HFONT hf)
  323. {
  324. return (HFONT)SelectObject(hf);
  325. }
  326. auto_dc& reset(HDC h = nullptr, HWND w = nullptr, bool is_create = false)
  327. {
  328. if(h != m_h || w != m_w)
  329. {
  330. if(is_valid())
  331. {
  332. if(m_is_create)
  333. VERIFY(::DeleteDC(m_h));
  334. else
  335. VERIFY(::ReleaseDC(m_w, m_h));
  336. }
  337. set(h, w, is_create);
  338. }
  339. return *this;
  340. }
  341. auto_dc& reset(auto_dc& other)
  342. {
  343. if(this != &other)
  344. {
  345. reset(other.m_h, other.m_w, other.m_is_create);
  346. other.release();
  347. }
  348. return *this;
  349. }
  350. HDC release()
  351. {
  352. HDC h = get_dc();
  353. set(nullptr, nullptr, false);
  354. return h;
  355. }
  356. /*
  357. auto_dc& operator = (HDC h)
  358. {
  359. return reset(h);
  360. }
  361. */
  362. auto_dc& operator = (auto_dc& other) {return reset(other);}
  363. operator HDC () const {return m_h;}
  364. HDC get_dc () const {return m_h;}
  365. const HDC& get_dc_ref () const {return m_h;}
  366. HWND get_wnd () const {return m_w;}
  367. bool is_valid () const {return m_h != nullptr;}
  368. bool is_create () const {return m_is_create;}
  369. private:
  370. void set(HDC h, HWND w, bool is_create)
  371. {
  372. m_h = h;
  373. m_w = w;
  374. m_is_create = is_create;
  375. }
  376. private:
  377. HDC m_h;
  378. HWND m_w;
  379. bool m_is_create;
  380. };
  381. class paint_dc
  382. {
  383. public:
  384. paint_dc(HWND hwnd) : m_hwnd(hwnd)
  385. {
  386. VERIFY(m_hdc = ::BeginPaint(m_hwnd, &m_ps));
  387. }
  388. ~paint_dc()
  389. {
  390. VERIFY(::EndPaint(m_hwnd, &m_ps));
  391. }
  392. operator HDC () const {return m_hdc;}
  393. bool is_valid() const {return m_hdc != nullptr;}
  394. public:
  395. PAINTSTRUCT m_ps;
  396. HWND m_hwnd;
  397. HDC m_hdc;
  398. };
  399. /************************************************************************/
  400. /* GDI Object 智能包装器 */
  401. /************************************************************************/
  402. template<class T>
  403. class auto_gdi_obj
  404. {
  405. public:
  406. auto_gdi_obj(T obj = nullptr) {set(obj);}
  407. auto_gdi_obj(auto_gdi_obj<T>& other) {set(other.release());}
  408. ~auto_gdi_obj() {reset();}
  409. auto_gdi_obj<T>& reset(T obj = nullptr)
  410. {
  411. if(obj != m_obj)
  412. {
  413. if(is_valid())
  414. {
  415. VERIFY(::DeleteObject(m_obj));
  416. }
  417. set(obj);
  418. }
  419. return *this;
  420. }
  421. auto_gdi_obj<T>& reset(auto_gdi_obj<T>& other)
  422. {
  423. if(this != &other)
  424. reset(other.release());
  425. return *this;
  426. }
  427. T release()
  428. {
  429. T obj = get();
  430. set(nullptr);
  431. return obj;
  432. }
  433. auto_gdi_obj<T>& operator = (T obj) {return reset(obj);}
  434. auto_gdi_obj<T>& operator = (auto_gdi_obj<T>& other) {return reset(other);}
  435. operator T () const {return m_obj;}
  436. T get () const {return m_obj;}
  437. const T& get_ref () const {return m_obj;}
  438. bool is_valid() const {return m_obj != nullptr;}
  439. private:
  440. void set(T obj) {m_obj = obj;}
  441. protected:
  442. T m_obj;
  443. };
  444. typedef auto_gdi_obj<HBITMAP> auto_bitmap_base;
  445. typedef auto_gdi_obj<HBRUSH> auto_brush_base;
  446. typedef auto_gdi_obj<HPALETTE> auto_palette_base;
  447. typedef auto_gdi_obj<HPEN> auto_pen_base;
  448. typedef auto_gdi_obj<HRGN> auto_rgn_base;
  449. typedef auto_gdi_obj<HFONT> auto_font_base;
  450. class auto_bitmap : public auto_bitmap_base
  451. {
  452. public:
  453. auto_bitmap(HBITMAP obj = nullptr) : auto_bitmap_base(obj) {}
  454. auto_bitmap& operator = (HBITMAP obj)
  455. {
  456. return (auto_bitmap&)reset(obj);
  457. }
  458. HBITMAP CreateBitmap(int nWidth, int nHeight, UINT cPlanes, UINT cBitsPerPel, CONST VOID* lpvBits)
  459. {
  460. HBITMAP obj = ::CreateBitmap(nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits);
  461. reset(obj);
  462. return obj;
  463. }
  464. HBITMAP CreateBitmapIndirect(CONST BITMAP *lpbm)
  465. {
  466. HBITMAP obj = ::CreateBitmapIndirect(lpbm);
  467. reset(obj);
  468. return obj;
  469. }
  470. HBITMAP CreateCompatibleBitmap(HDC hdc, int nWidth, int nHeight)
  471. {
  472. HBITMAP obj = ::CreateCompatibleBitmap(hdc, nWidth, nHeight);
  473. reset(obj);
  474. return obj;
  475. }
  476. HBITMAP CreateDIBSection(HDC hdc, const BITMAPINFO* pbmi, UINT iUsage, void** ppvBits, HANDLE hSection, DWORD dwOffset)
  477. {
  478. HBITMAP obj = ::CreateDIBSection(hdc, pbmi, iUsage, ppvBits, hSection, dwOffset);
  479. reset(obj);
  480. return obj;
  481. }
  482. HBITMAP LoadBitmap(HINSTANCE hInstance, LPCTSTR lpBitmapName)
  483. {
  484. HBITMAP obj = ::LoadBitmap(hInstance, lpBitmapName);
  485. reset(obj);
  486. return obj;
  487. }
  488. int GetBitmap(BITMAP* pBitMap)
  489. {
  490. ASSERT(m_obj != nullptr);
  491. return ::GetObject(m_obj, sizeof(BITMAP), pBitMap);
  492. }
  493. int GetDIBSection(DIBSECTION* pDIBSection)
  494. {
  495. ASSERT(m_obj != nullptr);
  496. return ::GetObject(m_obj, sizeof(DIBSECTION), pDIBSection);
  497. }
  498. };
  499. class auto_brush : public auto_brush_base
  500. {
  501. public:
  502. auto_brush(HBRUSH obj = nullptr) : auto_brush_base(obj) {}
  503. auto_brush& operator = (HBRUSH obj)
  504. {
  505. return (auto_brush&)reset(obj);
  506. }
  507. HBRUSH CreateDIBPatternBrushPt(const void* lpPackedDIB, UINT iUsage)
  508. {
  509. HBRUSH obj = ::CreateDIBPatternBrushPt(lpPackedDIB, iUsage);
  510. reset(obj);
  511. return obj;
  512. }
  513. HBRUSH CreatePatternBrush(HBITMAP hbmp)
  514. {
  515. HBRUSH obj = ::CreatePatternBrush(hbmp);
  516. reset(obj);
  517. return obj;
  518. }
  519. HBRUSH CreateSolidBrush(COLORREF crColor)
  520. {
  521. HBRUSH obj = ::CreateSolidBrush(crColor);
  522. reset(obj);
  523. return obj;
  524. }
  525. int GetLogBrush(LOGBRUSH* pLogBrush)
  526. {
  527. ASSERT(m_obj != nullptr);
  528. return ::GetObject(m_obj, sizeof(LOGBRUSH), pLogBrush);
  529. }
  530. };
  531. class auto_palette : public auto_palette_base
  532. {
  533. public:
  534. auto_palette(HPALETTE obj = nullptr) : auto_palette_base(obj) {}
  535. auto_palette& operator = (HPALETTE obj)
  536. {
  537. return (auto_palette&)reset(obj);
  538. }
  539. HPALETTE CreatePalette(CONST LOGPALETTE* lplgpl)
  540. {
  541. HPALETTE obj = ::CreatePalette(lplgpl);
  542. reset(obj);
  543. return obj;
  544. }
  545. int GetEntryCount()
  546. {
  547. ASSERT(m_obj != nullptr);
  548. WORD nEntries;
  549. ::GetObject(m_obj, sizeof(WORD), &nEntries);
  550. return (int)nEntries;
  551. }
  552. };
  553. class auto_pen : public auto_pen_base
  554. {
  555. public:
  556. auto_pen(HPEN obj = nullptr) : auto_pen_base(obj) {}
  557. auto_pen& operator = (HPEN obj)
  558. {
  559. return (auto_pen&)reset(obj);
  560. }
  561. HPEN CreatePen(int fnPenStyle, int nWidth, COLORREF crColor)
  562. {
  563. HPEN obj = ::CreatePen(fnPenStyle, nWidth, crColor);
  564. reset(obj);
  565. return obj;
  566. }
  567. HPEN CreatePenIndirect(const LOGPEN* lplgpn)
  568. {
  569. HPEN obj = ::CreatePenIndirect(lplgpn);
  570. reset(obj);
  571. return obj;
  572. }
  573. int GetLogPen(LOGPEN* pLogPen)
  574. {
  575. ASSERT(m_obj != nullptr);
  576. return ::GetObject(m_obj, sizeof(LOGPEN), pLogPen);
  577. }
  578. };
  579. class auto_rgn : public auto_rgn_base
  580. {
  581. public:
  582. auto_rgn(HRGN obj = nullptr) : auto_rgn_base(obj) {}
  583. auto_rgn& operator = (HRGN obj)
  584. {
  585. return (auto_rgn&)reset(obj);
  586. }
  587. HRGN CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect)
  588. {
  589. HRGN obj = ::CreateRectRgn(nLeftRect, nTopRect, nRightRect, nBottomRect);
  590. reset(obj);
  591. return obj;
  592. }
  593. HRGN CreateRectRgnIndirect(const RECT* lprc)
  594. {
  595. HRGN obj = ::CreateRectRgnIndirect(lprc);
  596. reset(obj);
  597. return obj;
  598. }
  599. };
  600. class auto_font : public auto_font_base
  601. {
  602. public:
  603. auto_font(HFONT obj = nullptr) : auto_font_base(obj) {}
  604. auto_font& operator = (HFONT obj)
  605. {
  606. return (auto_font&)reset(obj);
  607. }
  608. HFONT CreateFont(
  609. int nHeight, // height of font
  610. int nWidth, // average character width
  611. int nEscapement, // angle of escapement
  612. int nOrientation, // base-line orientation angle
  613. int fnWeight, // font weight
  614. DWORD bItalic, // italic attribute option
  615. DWORD bUnderline, // underline attribute option
  616. DWORD cStrikeOut, // strikeout attribute option
  617. DWORD nCharSet, // character set identifier
  618. DWORD nOutPrecision, // output precision
  619. DWORD nClipPrecision, // clipping precision
  620. DWORD nQuality, // output quality
  621. DWORD nPitchAndFamily, // pitch and family
  622. LPCTSTR lpszFace // typeface name
  623. )
  624. {
  625. HFONT obj = ::CreateFont(
  626. nHeight,
  627. nWidth,
  628. nEscapement,
  629. nOrientation,
  630. fnWeight,
  631. bItalic,
  632. bUnderline,
  633. cStrikeOut,
  634. nCharSet,
  635. nOutPrecision,
  636. nClipPrecision,
  637. nQuality,
  638. nPitchAndFamily,
  639. lpszFace
  640. );
  641. reset(obj);
  642. return obj;
  643. }
  644. HFONT CreateFontIndirect(const LOGFONT* lplf)
  645. {
  646. HFONT obj = ::CreateFontIndirect(lplf);
  647. reset(obj);
  648. return obj;
  649. }
  650. int GetLogFont(LOGFONT* pLogFont)
  651. {
  652. ASSERT(m_obj != nullptr);
  653. return ::GetObject(m_obj, sizeof(LOGFONT), pLogFont);
  654. }
  655. };