Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2615 lines
76 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. #ifndef __ATLGDI_H__
  11. #define __ATLGDI_H__
  12. #ifndef __cplusplus
  13. #error ATL requires C++ compilation (use a .cpp suffix)
  14. #endif
  15. #ifndef __ATLBASE_H__
  16. #error atlgdi.h requires atlbase.h to be included first
  17. #endif
  18. #include <commctrl.h>
  19. #ifdef UNDER_CE
  20. #ifdef TrackPopupMenu
  21. #undef TrackPopupMenu
  22. #endif //TrackPopupMenu
  23. //REVIEW
  24. BOOL IsMenu(HMENU hMenu)
  25. {
  26. return (hMenu != NULL);
  27. }
  28. #endif //UNDER_CE
  29. namespace ATL
  30. {
  31. /////////////////////////////////////////////////////////////////////////////
  32. // Forward declarations
  33. class CDC;
  34. class CPaintDC;
  35. class CClientDC;
  36. class CWindowDC;
  37. class CMenu;
  38. class CPen;
  39. class CBrush;
  40. class CFont;
  41. class CBitmap;
  42. class CPalette;
  43. class CRgn;
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CDC - The device context class
  46. class CDC
  47. {
  48. public:
  49. // Attributes
  50. HDC m_hDC;
  51. BOOL m_bAutoRestore;
  52. HPEN m_hOriginalPen;
  53. HBRUSH m_hOriginalBrush;
  54. // HPALETTE m_hOriginalPalette;
  55. HFONT m_hOriginalFont;
  56. HBITMAP m_hOriginalBitmap;
  57. void RestoreAllObjects()
  58. {
  59. if(m_hOriginalPen != NULL)
  60. {
  61. #ifndef UNDER_CE
  62. ATLASSERT(::GetObjectType(m_hOriginalPen) == OBJ_PEN || ::GetObjectType(m_hOriginalPen) == OBJ_EXTPEN);
  63. #else // CE specific
  64. ATLASSERT(::GetObjectType(m_hOriginalPen) == OBJ_PEN);
  65. #endif //!UNDER_CE
  66. ::SelectObject(m_hDC, m_hOriginalPen);
  67. m_hOriginalPen = NULL;
  68. }
  69. if(m_hOriginalBrush != NULL)
  70. {
  71. ATLASSERT(::GetObjectType(m_hOriginalBrush) == OBJ_BRUSH);
  72. ::SelectObject(m_hDC, m_hOriginalBrush);
  73. m_hOriginalBrush = NULL;
  74. }
  75. // if(m_hOriginalPalette != NULL)
  76. // {
  77. // ATLASSERT(::GetObjectType(m_hOriginalPalette) == OBJ_PAL);
  78. // ::SelectPalette(m_hDC, m_hOriginalPalette, FALSE);
  79. // m_hOriginalPalette = NULL;
  80. // }
  81. if(m_hOriginalFont != NULL)
  82. {
  83. ATLASSERT(::GetObjectType(m_hOriginalFont) == OBJ_FONT);
  84. ::SelectObject(m_hDC, m_hOriginalFont);
  85. m_hOriginalFont = NULL;
  86. }
  87. if(m_hOriginalBitmap != NULL)
  88. {
  89. ATLASSERT(::GetObjectType(m_hOriginalBitmap) == OBJ_BITMAP);
  90. ::SelectObject(m_hDC, m_hOriginalBitmap);
  91. m_hOriginalBitmap = NULL;
  92. }
  93. }
  94. CDC(HDC hDC = NULL, BOOL bAutoRestore = TRUE) : m_hDC(hDC), m_bAutoRestore(bAutoRestore),
  95. m_hOriginalPen(NULL), m_hOriginalBrush(NULL), m_hOriginalFont(NULL), m_hOriginalBitmap(NULL)
  96. {
  97. }
  98. ~CDC()
  99. {
  100. if(m_hDC != NULL)
  101. {
  102. if(m_bAutoRestore)
  103. RestoreAllObjects();
  104. ::DeleteDC(Detach());
  105. }
  106. }
  107. CDC& operator=(HDC hDC)
  108. {
  109. m_hDC = hDC;
  110. return *this;
  111. }
  112. void Attach(HDC hDC)
  113. {
  114. m_hDC = hDC;
  115. }
  116. HDC Detach()
  117. {
  118. HDC hDC = m_hDC;
  119. m_hDC = NULL;
  120. return hDC;
  121. }
  122. operator HDC() const { return m_hDC; }
  123. #ifndef UNDER_CE
  124. HWND WindowFromDC() const
  125. {
  126. ATLASSERT(m_hDC != NULL);
  127. return ::WindowFromDC(m_hDC);
  128. }
  129. #endif //!UNDER_CE
  130. HPEN GetCurrentPen() const
  131. {
  132. ATLASSERT(m_hDC != NULL);
  133. return (HPEN)::GetCurrentObject(m_hDC, OBJ_PEN);
  134. }
  135. HBRUSH GetCurrentBrush() const
  136. {
  137. ATLASSERT(m_hDC != NULL);
  138. return (HBRUSH)::GetCurrentObject(m_hDC, OBJ_BRUSH);
  139. }
  140. HPALETTE GetCurrentPalette() const
  141. {
  142. ATLASSERT(m_hDC != NULL);
  143. return (HPALETTE)::GetCurrentObject(m_hDC, OBJ_PAL);
  144. }
  145. HFONT GetCurrentFont() const
  146. {
  147. ATLASSERT(m_hDC != NULL);
  148. return (HFONT)::GetCurrentObject(m_hDC, OBJ_FONT);
  149. }
  150. HBITMAP GetCurrentBitmap() const
  151. {
  152. ATLASSERT(m_hDC != NULL);
  153. return (HBITMAP)::GetCurrentObject(m_hDC, OBJ_BITMAP);
  154. }
  155. HDC CreateDC(LPCTSTR lpszDriverName, LPCTSTR lpszDeviceName,
  156. LPCTSTR lpszOutput, const DEVMODE* lpInitData)
  157. {
  158. ATLASSERT(m_hDC == NULL);
  159. m_hDC = ::CreateDC(lpszDriverName, lpszDeviceName, lpszOutput, lpInitData);
  160. return m_hDC;
  161. }
  162. HDC CreateCompatibleDC(HDC hDC = NULL)
  163. {
  164. ATLASSERT(m_hDC == NULL);
  165. m_hDC = ::CreateCompatibleDC(hDC);
  166. return m_hDC;
  167. }
  168. BOOL DeleteDC()
  169. {
  170. if(m_hDC == NULL)
  171. return FALSE;
  172. if(m_bAutoRestore)
  173. RestoreAllObjects();
  174. return ::DeleteDC(Detach());
  175. }
  176. // Device-Context Functions
  177. int SaveDC()
  178. {
  179. ATLASSERT(m_hDC != NULL);
  180. return ::SaveDC(m_hDC);
  181. }
  182. BOOL RestoreDC(int nSavedDC)
  183. {
  184. ATLASSERT(m_hDC != NULL);
  185. return ::RestoreDC(m_hDC, nSavedDC);
  186. }
  187. int GetDeviceCaps(int nIndex) const
  188. {
  189. ATLASSERT(m_hDC != NULL);
  190. return ::GetDeviceCaps(m_hDC, nIndex);
  191. }
  192. #ifndef UNDER_CE
  193. UINT SetBoundsRect(LPCRECT lpRectBounds, UINT flags)
  194. {
  195. ATLASSERT(m_hDC != NULL);
  196. return ::SetBoundsRect(m_hDC, lpRectBounds, flags);
  197. }
  198. UINT GetBoundsRect(LPRECT lpRectBounds, UINT flags)
  199. {
  200. ATLASSERT(m_hDC != NULL);
  201. return ::GetBoundsRect(m_hDC, lpRectBounds, flags);
  202. }
  203. BOOL ResetDC(const DEVMODE* lpDevMode)
  204. {
  205. ATLASSERT(m_hDC != NULL);
  206. return ::ResetDC(m_hDC, lpDevMode) != NULL;
  207. }
  208. #endif //!UNDER_CE
  209. // Drawing-Tool Functions
  210. #ifndef UNDER_CE
  211. BOOL GetBrushOrg(LPPOINT lpPoint) const
  212. {
  213. ATLASSERT(m_hDC != NULL);
  214. return ::GetBrushOrgEx(m_hDC, lpPoint);
  215. }
  216. #endif //!UNDER_CE
  217. BOOL SetBrushOrg(int x, int y, LPPOINT lpPoint = NULL)
  218. {
  219. ATLASSERT(m_hDC != NULL);
  220. return ::SetBrushOrgEx(m_hDC, x, y, lpPoint);
  221. }
  222. BOOL SetBrushOrg(POINT point, LPPOINT lpPointRet = NULL)
  223. {
  224. ATLASSERT(m_hDC != NULL);
  225. return ::SetBrushOrgEx(m_hDC, point.x, point.y, lpPointRet);
  226. }
  227. #ifndef UNDER_CE
  228. int EnumObjects(int nObjectType, int (CALLBACK* lpfn)(LPVOID, LPARAM), LPARAM lpData)
  229. {
  230. ATLASSERT(m_hDC != NULL);
  231. #ifdef STRICT
  232. return ::EnumObjects(m_hDC, nObjectType, (GOBJENUMPROC)lpfn, lpData);
  233. #else
  234. return ::EnumObjects(m_hDC, nObjectType, (GOBJENUMPROC)lpfn, (LPVOID)lpData);
  235. #endif
  236. }
  237. #endif //!UNDER_CE
  238. // Type-safe selection helpers
  239. HPEN SelectPen(HPEN hPen)
  240. {
  241. ATLASSERT(m_hDC != NULL);
  242. #ifndef UNDER_CE
  243. ATLASSERT(::GetObjectType(hPen) == OBJ_PEN || ::GetObjectType(hPen) == OBJ_EXTPEN);
  244. #else // CE specific
  245. ATLASSERT(::GetObjectType(hPen) == OBJ_PEN);
  246. #endif //!UNDER_CE
  247. HPEN hOldPen = (HPEN)::SelectObject(m_hDC, hPen);
  248. if(m_hOriginalPen == NULL)
  249. m_hOriginalPen = hOldPen;
  250. return hOldPen;
  251. }
  252. HBRUSH SelectBrush(HBRUSH hBrush)
  253. {
  254. ATLASSERT(m_hDC != NULL);
  255. ATLASSERT(::GetObjectType(hBrush) == OBJ_BRUSH);
  256. HBRUSH hOldBrush = (HBRUSH)::SelectObject(m_hDC, hBrush);
  257. if(m_hOriginalBrush == NULL)
  258. m_hOriginalBrush = hOldBrush;
  259. return hOldBrush;
  260. }
  261. HFONT SelectFont(HFONT hFont)
  262. {
  263. ATLASSERT(m_hDC != NULL);
  264. ATLASSERT(::GetObjectType(hFont) == OBJ_FONT);
  265. HFONT hOldFont = (HFONT)::SelectObject(m_hDC, hFont);
  266. if(m_hOriginalFont == NULL)
  267. m_hOriginalFont = hOldFont;
  268. return hOldFont;
  269. }
  270. HBITMAP SelectBitmap(HBITMAP hBitmap)
  271. {
  272. ATLASSERT(m_hDC != NULL);
  273. ATLASSERT(::GetObjectType(hBitmap) == OBJ_BITMAP);
  274. HBITMAP hOldBitmap = (HBITMAP)::SelectObject(m_hDC, hBitmap);
  275. if(m_hOriginalBitmap == NULL)
  276. m_hOriginalBitmap = hOldBitmap;
  277. return hOldBitmap;
  278. }
  279. int SelectRgn(HRGN hRgn) // special return for regions
  280. {
  281. ATLASSERT(m_hDC != NULL);
  282. ATLASSERT(::GetObjectType(hRgn) == OBJ_REGION);
  283. return (int)(INT_PTR)::SelectObject(m_hDC, hRgn);
  284. }
  285. HGDIOBJ SelectStockObject(int nIndex)
  286. {
  287. ATLASSERT(m_hDC != NULL);
  288. HGDIOBJ hObject = ::GetStockObject(nIndex);
  289. ATLASSERT(hObject != NULL);
  290. switch(::GetObjectType(hObject))
  291. {
  292. case OBJ_PEN:
  293. #ifndef UNDER_CE
  294. /*?*/ case OBJ_EXTPEN:
  295. #endif //!UNDER_CE
  296. return SelectPen((HPEN)hObject);
  297. case OBJ_BRUSH:
  298. return SelectBrush((HBRUSH)hObject);
  299. case OBJ_FONT:
  300. return SelectFont((HFONT)hObject);
  301. default:
  302. return NULL;
  303. }
  304. }
  305. // Color and Color Palette Functions
  306. COLORREF GetNearestColor(COLORREF crColor) const
  307. {
  308. ATLASSERT(m_hDC != NULL);
  309. return ::GetNearestColor(m_hDC, crColor);
  310. }
  311. HPALETTE SelectPalette(HPALETTE hPalette, BOOL bForceBackground)
  312. {
  313. ATLASSERT(m_hDC != NULL);
  314. HPALETTE hOldPal = ::SelectPalette(m_hDC, hPalette, bForceBackground);
  315. // if(/*m_bAutoRestore && */m_hOriginalPal == NULL)
  316. // m_hOriginalPal = hOldPal;
  317. return hOldPal;
  318. }
  319. UINT RealizePalette()
  320. {
  321. ATLASSERT(m_hDC != NULL);
  322. return ::RealizePalette(m_hDC);
  323. }
  324. #ifndef UNDER_CE
  325. void UpdateColors()
  326. {
  327. ATLASSERT(m_hDC != NULL);
  328. ::UpdateColors(m_hDC);
  329. }
  330. #endif //!UNDER_CE
  331. // Drawing-Attribute Functions
  332. COLORREF GetBkColor() const
  333. {
  334. ATLASSERT(m_hDC != NULL);
  335. return ::GetBkColor(m_hDC);
  336. }
  337. int GetBkMode() const
  338. {
  339. ATLASSERT(m_hDC != NULL);
  340. return ::GetBkMode(m_hDC);
  341. }
  342. #ifndef UNDER_CE
  343. int GetPolyFillMode() const
  344. {
  345. ATLASSERT(m_hDC != NULL);
  346. return ::GetPolyFillMode(m_hDC);
  347. }
  348. int GetROP2() const
  349. {
  350. ATLASSERT(m_hDC != NULL);
  351. return ::GetROP2(m_hDC);
  352. }
  353. int GetStretchBltMode() const
  354. {
  355. ATLASSERT(m_hDC != NULL);
  356. return ::GetStretchBltMode(m_hDC);
  357. }
  358. #endif //!UNDER_CE
  359. COLORREF GetTextColor() const
  360. {
  361. ATLASSERT(m_hDC != NULL);
  362. return ::GetTextColor(m_hDC);
  363. }
  364. COLORREF SetBkColor(COLORREF crColor)
  365. {
  366. ATLASSERT(m_hDC != NULL);
  367. return ::SetBkColor(m_hDC, crColor);
  368. }
  369. int SetBkMode(int nBkMode)
  370. {
  371. ATLASSERT(m_hDC != NULL);
  372. return ::SetBkMode(m_hDC, nBkMode);
  373. }
  374. #ifndef UNDER_CE
  375. int SetPolyFillMode(int nPolyFillMode)
  376. {
  377. ATLASSERT(m_hDC != NULL);
  378. return ::SetPolyFillMode(m_hDC, nPolyFillMode);
  379. }
  380. #endif //!UNDER_CE
  381. int SetROP2(int nDrawMode)
  382. {
  383. ATLASSERT(m_hDC != NULL);
  384. return ::SetROP2(m_hDC, nDrawMode);
  385. }
  386. #ifndef UNDER_CE
  387. int SetStretchBltMode(int nStretchMode)
  388. {
  389. ATLASSERT(m_hDC != NULL);
  390. return ::SetStretchBltMode(m_hDC, nStretchMode);
  391. }
  392. #endif //!UNDER_CE
  393. COLORREF SetTextColor(COLORREF crColor)
  394. {
  395. ATLASSERT(m_hDC != NULL);
  396. return ::SetTextColor(m_hDC, crColor);
  397. }
  398. #ifndef UNDER_CE
  399. BOOL GetColorAdjustment(LPCOLORADJUSTMENT lpColorAdjust) const
  400. {
  401. ATLASSERT(m_hDC != NULL);
  402. return ::GetColorAdjustment(m_hDC, lpColorAdjust);
  403. }
  404. BOOL SetColorAdjustment(const COLORADJUSTMENT* lpColorAdjust)
  405. {
  406. ATLASSERT(m_hDC != NULL);
  407. return ::SetColorAdjustment(m_hDC, lpColorAdjust);
  408. }
  409. #endif //!UNDER_CE
  410. // Mapping Functions
  411. #ifndef UNDER_CE
  412. int GetMapMode() const
  413. {
  414. ATLASSERT(m_hDC != NULL);
  415. return ::GetMapMode(m_hDC);
  416. }
  417. BOOL GetViewportOrg(LPPOINT lpPoint) const
  418. {
  419. ATLASSERT(m_hDC != NULL);
  420. return ::GetViewportOrgEx(m_hDC, lpPoint);
  421. }
  422. int SetMapMode(int nMapMode)
  423. {
  424. ATLASSERT(m_hDC != NULL);
  425. return ::SetMapMode(m_hDC, nMapMode);
  426. }
  427. // Viewport Origin
  428. BOOL SetViewportOrg(int x, int y, LPPOINT lpPoint = NULL)
  429. {
  430. ATLASSERT(m_hDC != NULL);
  431. return ::SetViewportOrgEx(m_hDC, x, y, lpPoint);
  432. }
  433. BOOL SetViewportOrg(POINT point, LPPOINT lpPointRet = NULL)
  434. {
  435. ATLASSERT(m_hDC != NULL);
  436. return SetViewportOrg(point.x, point.y, lpPointRet);
  437. }
  438. BOOL OffsetViewportOrg(int nWidth, int nHeight, LPPOINT lpPoint = NULL)
  439. {
  440. ATLASSERT(m_hDC != NULL);
  441. return ::OffsetViewportOrgEx(m_hDC, nWidth, nHeight, lpPoint);
  442. }
  443. // Viewport Extent
  444. BOOL GetViewportExt(LPSIZE lpSize) const
  445. {
  446. ATLASSERT(m_hDC != NULL);
  447. return ::GetViewportExtEx(m_hDC, lpSize);
  448. }
  449. BOOL SetViewportExt(int x, int y, LPSIZE lpSize = NULL)
  450. {
  451. ATLASSERT(m_hDC != NULL);
  452. return ::SetViewportExtEx(m_hDC, x, y, lpSize);
  453. }
  454. BOOL SetViewportExt(SIZE size, LPSIZE lpSizeRet = NULL)
  455. {
  456. ATLASSERT(m_hDC != NULL);
  457. return SetViewportExt(size.cx, size.cy, lpSizeRet);
  458. }
  459. BOOL ScaleViewportExt(int xNum, int xDenom, int yNum, int yDenom, LPSIZE lpSize = NULL)
  460. {
  461. ATLASSERT(m_hDC != NULL);
  462. return ::ScaleViewportExtEx(m_hDC, xNum, xDenom, yNum, yDenom, lpSize);
  463. }
  464. // Window Origin
  465. BOOL GetWindowOrg(LPPOINT lpPoint) const
  466. {
  467. ATLASSERT(m_hDC != NULL);
  468. return ::GetWindowOrgEx(m_hDC, lpPoint);
  469. }
  470. BOOL SetWindowOrg(int x, int y, LPPOINT lpPoint = NULL)
  471. {
  472. ATLASSERT(m_hDC != NULL);
  473. return ::SetWindowOrgEx(m_hDC, x, y, lpPoint);
  474. }
  475. BOOL SetWindowOrg(POINT point, LPPOINT lpPointRet = NULL)
  476. {
  477. ATLASSERT(m_hDC != NULL);
  478. return SetWindowOrg(point.x, point.y, lpPointRet);
  479. }
  480. BOOL OffsetWindowOrg(int nWidth, int nHeight, LPPOINT lpPoint = NULL)
  481. {
  482. ATLASSERT(m_hDC != NULL);
  483. return ::OffsetWindowOrgEx(m_hDC, nWidth, nHeight, lpPoint);
  484. }
  485. // Window extent
  486. BOOL GetWindowExt(LPSIZE lpSize) const
  487. {
  488. ATLASSERT(m_hDC != NULL);
  489. return ::GetWindowExtEx(m_hDC, lpSize);
  490. }
  491. BOOL SetWindowExt(int x, int y, LPSIZE lpSize = NULL)
  492. {
  493. ATLASSERT(m_hDC != NULL);
  494. return ::SetWindowExtEx(m_hDC, x, y, lpSize);
  495. }
  496. BOOL SetWindowExt(SIZE size, LPSIZE lpSizeRet)
  497. {
  498. ATLASSERT(m_hDC != NULL);
  499. return SetWindowExt(size.cx, size.cy, lpSizeRet);
  500. }
  501. BOOL ScaleWindowExt(int xNum, int xDenom, int yNum, int yDenom, LPSIZE lpSize = NULL)
  502. {
  503. ATLASSERT(m_hDC != NULL);
  504. return ::ScaleWindowExtEx(m_hDC, xNum, xDenom, yNum, yDenom, lpSize);
  505. }
  506. // Coordinate Functions
  507. BOOL DPtoLP(LPPOINT lpPoints, int nCount = 1) const
  508. {
  509. ATLASSERT(m_hDC != NULL);
  510. return ::DPtoLP(m_hDC, lpPoints, nCount);
  511. }
  512. BOOL DPtoLP(LPRECT lpRect) const
  513. {
  514. ATLASSERT(m_hDC != NULL);
  515. return ::DPtoLP(m_hDC, (LPPOINT)lpRect, 2);
  516. }
  517. BOOL DPtoLP(LPSIZE lpSize) const
  518. {
  519. SIZE sizeWinExt;
  520. if(!GetWindowExt(&sizeWinExt))
  521. return FALSE;
  522. SIZE sizeVpExt;
  523. if(!GetViewportExt(&sizeVpExt))
  524. return FALSE;
  525. lpSize->cx = MulDiv(lpSize->cx, abs(sizeWinExt.cx), abs(sizeVpExt.cx));
  526. lpSize->cy = MulDiv(lpSize->cy, abs(sizeWinExt.cy), abs(sizeVpExt.cy));
  527. return TRUE;
  528. }
  529. BOOL LPtoDP(LPPOINT lpPoints, int nCount = 1) const
  530. {
  531. ATLASSERT(m_hDC != NULL);
  532. return ::LPtoDP(m_hDC, lpPoints, nCount);
  533. }
  534. BOOL LPtoDP(LPRECT lpRect) const
  535. {
  536. ATLASSERT(m_hDC != NULL);
  537. return ::LPtoDP(m_hDC, (LPPOINT)lpRect, 2);
  538. }
  539. BOOL LPtoDP(LPSIZE lpSize) const
  540. {
  541. SIZE sizeWinExt;
  542. if(!GetWindowExt(&sizeWinExt))
  543. return FALSE;
  544. SIZE sizeVpExt;
  545. if(!GetViewportExt(&sizeVpExt))
  546. return FALSE;
  547. lpSize->cx = MulDiv(lpSize->cx, abs(sizeVpExt.cx), abs(sizeWinExt.cx));
  548. lpSize->cy = MulDiv(lpSize->cy, abs(sizeVpExt.cy), abs(sizeWinExt.cy));
  549. return TRUE;
  550. }
  551. // Special Coordinate Functions (useful for dealing with metafiles and OLE)
  552. #define HIMETRIC_INCH 2540 // HIMETRIC units per inch
  553. void DPtoHIMETRIC(LPSIZE lpSize) const
  554. {
  555. ATLASSERT(m_hDC != NULL);
  556. int nMapMode;
  557. if((nMapMode = GetMapMode()) < MM_ISOTROPIC && nMapMode != MM_TEXT)
  558. {
  559. // when using a constrained map mode, map against physical inch
  560. ((CDC*)this)->SetMapMode(MM_HIMETRIC);
  561. DPtoLP(lpSize);
  562. ((CDC*)this)->SetMapMode(nMapMode);
  563. }
  564. else
  565. {
  566. // map against logical inch for non-constrained mapping modes
  567. int cxPerInch = GetDeviceCaps(LOGPIXELSX);
  568. int cyPerInch = GetDeviceCaps(LOGPIXELSY);
  569. ATLASSERT(cxPerInch != 0 && cyPerInch != 0);
  570. lpSize->cx = MulDiv(lpSize->cx, HIMETRIC_INCH, cxPerInch);
  571. lpSize->cy = MulDiv(lpSize->cy, HIMETRIC_INCH, cyPerInch);
  572. }
  573. }
  574. void HIMETRICtoDP(LPSIZE lpSize) const
  575. {
  576. ATLASSERT(m_hDC != NULL);
  577. int nMapMode;
  578. if((nMapMode = GetMapMode()) < MM_ISOTROPIC && nMapMode != MM_TEXT)
  579. {
  580. // when using a constrained map mode, map against physical inch
  581. ((CDC*)this)->SetMapMode(MM_HIMETRIC);
  582. LPtoDP(lpSize);
  583. ((CDC*)this)->SetMapMode(nMapMode);
  584. }
  585. else
  586. {
  587. // map against logical inch for non-constrained mapping modes
  588. int cxPerInch = GetDeviceCaps(LOGPIXELSX);
  589. int cyPerInch = GetDeviceCaps(LOGPIXELSY);
  590. ATLASSERT(cxPerInch != 0 && cyPerInch != 0);
  591. lpSize->cx = MulDiv(lpSize->cx, cxPerInch, HIMETRIC_INCH);
  592. lpSize->cy = MulDiv(lpSize->cy, cyPerInch, HIMETRIC_INCH);
  593. }
  594. }
  595. void LPtoHIMETRIC(LPSIZE lpSize) const
  596. {
  597. LPtoDP(lpSize);
  598. DPtoHIMETRIC(lpSize);
  599. }
  600. void HIMETRICtoLP(LPSIZE lpSize) const
  601. {
  602. HIMETRICtoDP(lpSize);
  603. DPtoLP(lpSize);
  604. }
  605. #endif //!UNDER_CE
  606. // Region Functions
  607. BOOL FillRgn(HRGN hRgn, HBRUSH hBrush)
  608. {
  609. ATLASSERT(m_hDC != NULL);
  610. return ::FillRgn(m_hDC, hRgn, hBrush);
  611. }
  612. #ifndef UNDER_CE
  613. BOOL FrameRgn(HRGN hRgn, HBRUSH hBrush, int nWidth, int nHeight)
  614. {
  615. ATLASSERT(m_hDC != NULL);
  616. return ::FrameRgn(m_hDC, hRgn, hBrush, nWidth, nHeight);
  617. }
  618. BOOL InvertRgn(HRGN hRgn)
  619. {
  620. ATLASSERT(m_hDC != NULL);
  621. return ::InvertRgn(m_hDC, hRgn);
  622. }
  623. BOOL PaintRgn(HRGN hRgn)
  624. {
  625. ATLASSERT(m_hDC != NULL);
  626. return ::PaintRgn(m_hDC, hRgn);
  627. }
  628. #endif //!UNDER_CE
  629. // Clipping Functions
  630. int GetClipBox(LPRECT lpRect) const
  631. {
  632. ATLASSERT(m_hDC != NULL);
  633. return ::GetClipBox(m_hDC, lpRect);
  634. }
  635. #ifndef UNDER_CE
  636. BOOL PtVisible(int x, int y) const
  637. {
  638. ATLASSERT(m_hDC != NULL);
  639. return ::PtVisible(m_hDC, x, y);
  640. }
  641. BOOL PtVisible(POINT point) const
  642. {
  643. ATLASSERT(m_hDC != NULL);
  644. return ::PtVisible(m_hDC, point.x, point.y);
  645. }
  646. #endif //!UNDER_CE
  647. BOOL RectVisible(LPCRECT lpRect) const
  648. {
  649. ATLASSERT(m_hDC != NULL);
  650. return ::RectVisible(m_hDC, lpRect);
  651. }
  652. int SelectClipRgn(HRGN hRgn)
  653. {
  654. ATLASSERT(m_hDC != NULL);
  655. return ::SelectClipRgn(m_hDC, (HRGN)hRgn);
  656. }
  657. int ExcludeClipRect(int x1, int y1, int x2, int y2)
  658. {
  659. ATLASSERT(m_hDC != NULL);
  660. return ::ExcludeClipRect(m_hDC, x1, y1, x2, y2);
  661. }
  662. int ExcludeClipRect(LPCRECT lpRect)
  663. {
  664. ATLASSERT(m_hDC != NULL);
  665. return ::ExcludeClipRect(m_hDC, lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
  666. }
  667. #ifndef UNDER_CE
  668. int ExcludeUpdateRgn(HWND hWnd)
  669. {
  670. ATLASSERT(m_hDC != NULL);
  671. return ::ExcludeUpdateRgn(m_hDC, hWnd);
  672. }
  673. #endif //!UNDER_CE
  674. int IntersectClipRect(int x1, int y1, int x2, int y2)
  675. {
  676. ATLASSERT(m_hDC != NULL);
  677. return ::IntersectClipRect(m_hDC, x1, y1, x2, y2);
  678. }
  679. int IntersectClipRect(LPCRECT lpRect)
  680. {
  681. ATLASSERT(m_hDC != NULL);
  682. return ::IntersectClipRect(m_hDC, lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
  683. }
  684. #ifndef UNDER_CE
  685. int OffsetClipRgn(int x, int y)
  686. {
  687. ATLASSERT(m_hDC != NULL);
  688. return ::OffsetClipRgn(m_hDC, x, y);
  689. }
  690. int OffsetClipRgn(SIZE size)
  691. {
  692. ATLASSERT(m_hDC != NULL);
  693. return ::OffsetClipRgn(m_hDC, size.cx, size.cy);
  694. }
  695. int SelectClipRgn(HRGN hRgn, int nMode)
  696. {
  697. ATLASSERT(m_hDC != NULL);
  698. return ::ExtSelectClipRgn(m_hDC, hRgn, nMode);
  699. }
  700. #endif //!UNDER_CE
  701. // Line-Output Functions
  702. #ifndef UNDER_CE
  703. //REVIEW
  704. BOOL GetCurrentPosition(LPPOINT lpPoint) const
  705. {
  706. ATLASSERT(m_hDC != NULL);
  707. return ::GetCurrentPositionEx(m_hDC, lpPoint);
  708. }
  709. BOOL MoveTo(int x, int y, LPPOINT lpPoint = NULL)
  710. {
  711. ATLASSERT(m_hDC != NULL);
  712. return ::MoveToEx(m_hDC, x, y, lpPoint);
  713. }
  714. BOOL MoveTo(POINT point, LPPOINT lpPointRet = NULL)
  715. {
  716. ATLASSERT(m_hDC != NULL);
  717. return MoveTo(point.x, point.y, lpPointRet);
  718. }
  719. BOOL LineTo(int x, int y)
  720. {
  721. ATLASSERT(m_hDC != NULL);
  722. return ::LineTo(m_hDC, x, y);
  723. }
  724. BOOL LineTo(POINT point)
  725. {
  726. ATLASSERT(m_hDC != NULL);
  727. return LineTo(point.x, point.y);
  728. }
  729. BOOL Arc(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  730. {
  731. ATLASSERT(m_hDC != NULL);
  732. return ::Arc(m_hDC, x1, y1, x2, y2, x3, y3, x4, y4);
  733. }
  734. BOOL Arc(LPCRECT lpRect, POINT ptStart, POINT ptEnd)
  735. {
  736. ATLASSERT(m_hDC != NULL);
  737. return ::Arc(m_hDC, lpRect->left, lpRect->top,
  738. lpRect->right, lpRect->bottom, ptStart.x, ptStart.y,
  739. ptEnd.x, ptEnd.y);
  740. }
  741. #endif //!UNDER_CE
  742. BOOL Polyline(LPPOINT lpPoints, int nCount)
  743. {
  744. ATLASSERT(m_hDC != NULL);
  745. return ::Polyline(m_hDC, lpPoints, nCount);
  746. }
  747. #ifndef UNDER_CE
  748. BOOL AngleArc(int x, int y, int nRadius, float fStartAngle, float fSweepAngle)
  749. {
  750. ATLASSERT(m_hDC != NULL);
  751. return ::AngleArc(m_hDC, x, y, nRadius, fStartAngle, fSweepAngle);
  752. }
  753. BOOL ArcTo(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  754. {
  755. ATLASSERT(m_hDC != NULL);
  756. return ::ArcTo(m_hDC, x1, y1, x2, y2, x3, y3, x4, y4);
  757. }
  758. BOOL ArcTo(LPCRECT lpRect, POINT ptStart, POINT ptEnd)
  759. {
  760. ATLASSERT(m_hDC != NULL);
  761. return ArcTo(lpRect->left, lpRect->top, lpRect->right,
  762. lpRect->bottom, ptStart.x, ptStart.y, ptEnd.x, ptEnd.y);
  763. }
  764. int GetArcDirection() const
  765. {
  766. ATLASSERT(m_hDC != NULL);
  767. return ::GetArcDirection(m_hDC);
  768. }
  769. int SetArcDirection(int nArcDirection)
  770. {
  771. ATLASSERT(m_hDC != NULL);
  772. return ::SetArcDirection(m_hDC, nArcDirection);
  773. }
  774. BOOL PolyDraw(const POINT* lpPoints, const BYTE* lpTypes, int nCount)
  775. {
  776. ATLASSERT(m_hDC != NULL);
  777. return ::PolyDraw(m_hDC, lpPoints, lpTypes, nCount);
  778. }
  779. BOOL PolylineTo(const POINT* lpPoints, int nCount)
  780. {
  781. ATLASSERT(m_hDC != NULL);
  782. return ::PolylineTo(m_hDC, lpPoints, nCount);
  783. }
  784. BOOL PolyPolyline(const POINT* lpPoints,
  785. const DWORD* lpPolyPoints, int nCount)
  786. {
  787. ATLASSERT(m_hDC != NULL);
  788. return ::PolyPolyline(m_hDC, lpPoints, lpPolyPoints, nCount);
  789. }
  790. BOOL PolyBezier(const POINT* lpPoints, int nCount)
  791. {
  792. ATLASSERT(m_hDC != NULL);
  793. return ::PolyBezier(m_hDC, lpPoints, nCount);
  794. }
  795. BOOL PolyBezierTo(const POINT* lpPoints, int nCount)
  796. {
  797. ATLASSERT(m_hDC != NULL);
  798. return ::PolyBezierTo(m_hDC, lpPoints, nCount);
  799. }
  800. #endif //!UNDER_CE
  801. // Simple Drawing Functions
  802. BOOL FillRect(LPCRECT lpRect, HBRUSH hBrush)
  803. {
  804. ATLASSERT(m_hDC != NULL);
  805. return ::FillRect(m_hDC, lpRect, hBrush);
  806. }
  807. #ifndef UNDER_CE
  808. BOOL FrameRect(LPCRECT lpRect, HBRUSH hBrush)
  809. {
  810. ATLASSERT(m_hDC != NULL);
  811. return ::FrameRect(m_hDC, lpRect, hBrush);
  812. }
  813. BOOL InvertRect(LPCRECT lpRect)
  814. {
  815. ATLASSERT(m_hDC != NULL);
  816. return ::InvertRect(m_hDC, lpRect);
  817. }
  818. BOOL DrawIcon(int x, int y, HICON hIcon)
  819. {
  820. ATLASSERT(m_hDC != NULL);
  821. return ::DrawIcon(m_hDC, x, y, hIcon);
  822. }
  823. BOOL DrawIcon(POINT point, HICON hIcon)
  824. {
  825. ATLASSERT(m_hDC != NULL);
  826. return ::DrawIcon(m_hDC, point.x, point.y, hIcon);
  827. }
  828. BOOL DrawState(POINT pt, SIZE size, HBITMAP hBitmap, UINT nFlags, HBRUSH hBrush = NULL)
  829. {
  830. ATLASSERT(m_hDC != NULL);
  831. return ::DrawState(m_hDC, hBrush, NULL, (LPARAM)hBitmap, 0, pt.x, pt.y, size.cx, size.cy, nFlags | DST_BITMAP);
  832. }
  833. BOOL DrawState(POINT pt, SIZE size, LPCTSTR lpszText, UINT nFlags,
  834. BOOL bPrefixText = TRUE, int nTextLen = 0, HBRUSH hBrush = NULL)
  835. {
  836. ATLASSERT(m_hDC != NULL);
  837. return ::DrawState(m_hDC, hBrush, NULL, (LPARAM)lpszText, (WPARAM)nTextLen, pt.x, pt.y, size.cx, size.cy, nFlags | (bPrefixText ? DST_PREFIXTEXT : DST_TEXT));
  838. }
  839. BOOL DrawState(POINT pt, SIZE size, DRAWSTATEPROC lpDrawProc,
  840. LPARAM lData, UINT nFlags, HBRUSH hBrush = NULL)
  841. {
  842. ATLASSERT(m_hDC != NULL);
  843. return ::DrawState(m_hDC, hBrush, lpDrawProc, lData, 0, pt.x, pt.y, size.cx, size.cy, nFlags | DST_COMPLEX);
  844. }
  845. #endif //!UNDER_CE
  846. // Ellipse and Polygon Functions
  847. #ifndef UNDER_CE
  848. BOOL Chord(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  849. {
  850. ATLASSERT(m_hDC != NULL);
  851. return ::Chord(m_hDC, x1, y1, x2, y2, x3, y3, x4, y4);
  852. }
  853. BOOL Chord(LPCRECT lpRect, POINT ptStart, POINT ptEnd)
  854. {
  855. ATLASSERT(m_hDC != NULL);
  856. return ::Chord(m_hDC, lpRect->left, lpRect->top,
  857. lpRect->right, lpRect->bottom, ptStart.x, ptStart.y,
  858. ptEnd.x, ptEnd.y);
  859. }
  860. #endif //!UNDER_CE
  861. void DrawFocusRect(LPCRECT lpRect)
  862. {
  863. ATLASSERT(m_hDC != NULL);
  864. ::DrawFocusRect(m_hDC, lpRect);
  865. }
  866. BOOL Ellipse(int x1, int y1, int x2, int y2)
  867. {
  868. ATLASSERT(m_hDC != NULL);
  869. return ::Ellipse(m_hDC, x1, y1, x2, y2);
  870. }
  871. BOOL Ellipse(LPCRECT lpRect)
  872. {
  873. ATLASSERT(m_hDC != NULL);
  874. return ::Ellipse(m_hDC, lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
  875. }
  876. #ifndef UNDER_CE
  877. BOOL Pie(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  878. {
  879. ATLASSERT(m_hDC != NULL);
  880. return ::Pie(m_hDC, x1, y1, x2, y2, x3, y3, x4, y4);
  881. }
  882. BOOL Pie(LPCRECT lpRect, POINT ptStart, POINT ptEnd)
  883. {
  884. ATLASSERT(m_hDC != NULL);
  885. return ::Pie(m_hDC, lpRect->left, lpRect->top,
  886. lpRect->right, lpRect->bottom, ptStart.x, ptStart.y,
  887. ptEnd.x, ptEnd.y);
  888. }
  889. #endif //!UNDER_CE
  890. BOOL Polygon(LPPOINT lpPoints, int nCount)
  891. {
  892. ATLASSERT(m_hDC != NULL);
  893. return ::Polygon(m_hDC, lpPoints, nCount);
  894. }
  895. #ifndef UNDER_CE
  896. BOOL PolyPolygon(LPPOINT lpPoints, LPINT lpPolyCounts, int nCount)
  897. {
  898. ATLASSERT(m_hDC != NULL);
  899. return ::PolyPolygon(m_hDC, lpPoints, lpPolyCounts, nCount);
  900. }
  901. #endif //!UNDER_CE
  902. BOOL Rectangle(int x1, int y1, int x2, int y2)
  903. {
  904. ATLASSERT(m_hDC != NULL);
  905. return ::Rectangle(m_hDC, x1, y1, x2, y2);
  906. }
  907. BOOL Rectangle(LPCRECT lpRect)
  908. {
  909. ATLASSERT(m_hDC != NULL);
  910. return ::Rectangle(m_hDC, lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
  911. }
  912. BOOL RoundRect(int x1, int y1, int x2, int y2, int x3, int y3)
  913. {
  914. ATLASSERT(m_hDC != NULL);
  915. return ::RoundRect(m_hDC, x1, y1, x2, y2, x3, y3);
  916. }
  917. BOOL RoundRect(LPCRECT lpRect, POINT point)
  918. {
  919. ATLASSERT(m_hDC != NULL);
  920. return ::RoundRect(m_hDC, lpRect->left, lpRect->top, lpRect->right, lpRect->bottom, point.x, point.y);
  921. }
  922. // Bitmap Functions
  923. BOOL PatBlt(int x, int y, int nWidth, int nHeight, DWORD dwRop)
  924. {
  925. ATLASSERT(m_hDC != NULL);
  926. return ::PatBlt(m_hDC, x, y, nWidth, nHeight, dwRop);
  927. }
  928. BOOL BitBlt(int x, int y, int nWidth, int nHeight, HDC hSrcDC,
  929. int xSrc, int ySrc, DWORD dwRop)
  930. {
  931. ATLASSERT(m_hDC != NULL);
  932. return ::BitBlt(m_hDC, x, y, nWidth, nHeight, hSrcDC, xSrc, ySrc, dwRop);
  933. }
  934. BOOL StretchBlt(int x, int y, int nWidth, int nHeight, HDC hSrcDC,
  935. int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, DWORD dwRop)
  936. {
  937. ATLASSERT(m_hDC != NULL);
  938. return ::StretchBlt(m_hDC, x, y, nWidth, nHeight, hSrcDC, xSrc, ySrc, nSrcWidth, nSrcHeight, dwRop);
  939. }
  940. COLORREF GetPixel(int x, int y) const
  941. {
  942. ATLASSERT(m_hDC != NULL);
  943. return ::GetPixel(m_hDC, x, y);
  944. }
  945. COLORREF GetPixel(POINT point) const
  946. {
  947. ATLASSERT(m_hDC != NULL);
  948. return ::GetPixel(m_hDC, point.x, point.y);
  949. }
  950. COLORREF SetPixel(int x, int y, COLORREF crColor)
  951. {
  952. ATLASSERT(m_hDC != NULL);
  953. return ::SetPixel(m_hDC, x, y, crColor);
  954. }
  955. COLORREF SetPixel(POINT point, COLORREF crColor)
  956. {
  957. ATLASSERT(m_hDC != NULL);
  958. return ::SetPixel(m_hDC, point.x, point.y, crColor);
  959. }
  960. #ifndef UNDER_CE
  961. BOOL FloodFill(int x, int y, COLORREF crColor)
  962. {
  963. ATLASSERT(m_hDC != NULL);
  964. return ::FloodFill(m_hDC, x, y, crColor);
  965. }
  966. BOOL ExtFloodFill(int x, int y, COLORREF crColor, UINT nFillType)
  967. {
  968. ATLASSERT(m_hDC != NULL);
  969. return ::ExtFloodFill(m_hDC, x, y, crColor, nFillType);
  970. }
  971. #endif //!UNDER_CE
  972. BOOL MaskBlt(int x, int y, int nWidth, int nHeight, HDC hSrcDC,
  973. int xSrc, int ySrc, HBITMAP hMaskBitmap, int xMask, int yMask,
  974. DWORD dwRop)
  975. {
  976. ATLASSERT(m_hDC != NULL);
  977. return ::MaskBlt(m_hDC, x, y, nWidth, nHeight, hSrcDC, xSrc, ySrc, hMaskBitmap, xMask, yMask, dwRop);
  978. }
  979. #ifndef UNDER_CE
  980. BOOL PlgBlt(LPPOINT lpPoint, HDC hSrcDC, int xSrc, int ySrc,
  981. int nWidth, int nHeight, HBITMAP hMaskBitmap, int xMask, int yMask)
  982. {
  983. ATLASSERT(m_hDC != NULL);
  984. return ::PlgBlt(m_hDC, lpPoint, hSrcDC, xSrc, ySrc, nWidth, nHeight, hMaskBitmap, xMask, yMask);
  985. }
  986. BOOL SetPixelV(int x, int y, COLORREF crColor)
  987. {
  988. ATLASSERT(m_hDC != NULL);
  989. return ::SetPixelV(m_hDC, x, y, crColor);
  990. }
  991. BOOL SetPixelV(POINT point, COLORREF crColor)
  992. {
  993. ATLASSERT(m_hDC != NULL);
  994. return ::SetPixelV(m_hDC, point.x, point.y, crColor);
  995. }
  996. #endif //!UNDER_CE
  997. // Text Functions
  998. #ifndef UNDER_CE
  999. BOOL TextOut(int x, int y, LPCTSTR lpszString, int nCount = -1)
  1000. {
  1001. ATLASSERT(m_hDC != NULL);
  1002. if(nCount == -1)
  1003. nCount = lstrlen(lpszString);
  1004. return ::TextOut(m_hDC, x, y, lpszString, nCount);
  1005. }
  1006. #endif //!UNDER_CE
  1007. BOOL ExtTextOut(int x, int y, UINT nOptions, LPCRECT lpRect,
  1008. LPCTSTR lpszString, UINT nCount = -1, LPINT lpDxWidths = NULL)
  1009. {
  1010. ATLASSERT(m_hDC != NULL);
  1011. if(nCount == -1)
  1012. nCount = lstrlen(lpszString);
  1013. return ::ExtTextOut(m_hDC, x, y, nOptions, lpRect, lpszString, nCount, lpDxWidths);
  1014. }
  1015. #ifndef UNDER_CE
  1016. SIZE TabbedTextOut(int x, int y, LPCTSTR lpszString, int nCount = -1,
  1017. int nTabPositions = 0, LPINT lpnTabStopPositions = NULL, int nTabOrigin = 0)
  1018. {
  1019. ATLASSERT(m_hDC != NULL);
  1020. if(nCount == -1)
  1021. nCount = lstrlen(lpszString);
  1022. SIZE size;
  1023. LONG lRes = ::TabbedTextOut(m_hDC, x, y, lpszString, nCount, nTabPositions, lpnTabStopPositions, nTabOrigin);
  1024. size.cx = LOWORD(lRes);
  1025. size.cy = HIWORD(lRes);
  1026. return size;
  1027. }
  1028. #endif //!UNDER_CE
  1029. int DrawText(LPCTSTR lpszString, int nCount, LPRECT lpRect, UINT nFormat)
  1030. {
  1031. ATLASSERT(m_hDC != NULL);
  1032. return ::DrawText(m_hDC, lpszString, nCount, lpRect, nFormat);
  1033. }
  1034. BOOL GetTextExtent(LPCTSTR lpszString, int nCount, LPSIZE lpSize) const
  1035. {
  1036. ATLASSERT(m_hDC != NULL);
  1037. if(nCount == -1)
  1038. nCount = lstrlen(lpszString);
  1039. return ::GetTextExtentPoint32(m_hDC, lpszString, nCount, lpSize);
  1040. }
  1041. #ifndef UNDER_CE
  1042. BOOL GetTabbedTextExtent(LPCTSTR lpszString, int nCount,
  1043. int nTabPositions, LPINT lpnTabStopPositions) const
  1044. {
  1045. ATLASSERT(m_hDC != NULL);
  1046. if(nCount == -1)
  1047. nCount = lstrlen(lpszString);
  1048. return ::GetTabbedTextExtent(m_hDC, lpszString, nCount, nTabPositions, lpnTabStopPositions);
  1049. }
  1050. BOOL GrayString(HBRUSH hBrush,
  1051. BOOL (CALLBACK* lpfnOutput)(HDC, LPARAM, int), LPARAM lpData,
  1052. int nCount, int x, int y, int nWidth, int nHeight)
  1053. {
  1054. ATLASSERT(m_hDC != NULL);
  1055. return ::GrayString(m_hDC, hBrush, (GRAYSTRINGPROC)lpfnOutput, lpData, nCount, x, y, nWidth, nHeight);
  1056. }
  1057. UINT GetTextAlign() const
  1058. {
  1059. ATLASSERT(m_hDC != NULL);
  1060. return ::GetTextAlign(m_hDC);
  1061. }
  1062. UINT SetTextAlign(UINT nFlags)
  1063. {
  1064. ATLASSERT(m_hDC != NULL);
  1065. return ::SetTextAlign(m_hDC, nFlags);
  1066. }
  1067. #endif //!UNDER_CE
  1068. int GetTextFace(LPTSTR lpszFacename, int nCount) const
  1069. {
  1070. ATLASSERT(m_hDC != NULL);
  1071. return ::GetTextFace(m_hDC, nCount, lpszFacename);
  1072. }
  1073. int GetTextFaceLen() const
  1074. {
  1075. ATLASSERT(m_hDC != NULL);
  1076. return ::GetTextFace(m_hDC, 0, NULL);
  1077. }
  1078. #ifndef _ATL_NO_COM
  1079. BOOL GetTextFace(BSTR& bstrFace) const
  1080. {
  1081. USES_CONVERSION;
  1082. ATLASSERT(m_hDC != NULL);
  1083. ATLASSERT(bstrFace == NULL);
  1084. int nLen = GetTextFaceLen();
  1085. if(nLen == 0)
  1086. return FALSE;
  1087. LPTSTR lpszText = (LPTSTR)_alloca(nLen * sizeof(TCHAR));
  1088. if(!GetTextFace(lpszText, nLen))
  1089. return FALSE;
  1090. bstrFace = ::SysAllocString(T2OLE(lpszText));
  1091. return (bstrFace != NULL) ? TRUE : FALSE;
  1092. }
  1093. #endif //!_ATL_NO_COM
  1094. BOOL GetTextMetrics(LPTEXTMETRIC lpMetrics) const
  1095. {
  1096. ATLASSERT(m_hDC != NULL);
  1097. return ::GetTextMetrics(m_hDC, lpMetrics);
  1098. }
  1099. #ifndef UNDER_CE
  1100. int SetTextJustification(int nBreakExtra, int nBreakCount)
  1101. {
  1102. ATLASSERT(m_hDC != NULL);
  1103. return ::SetTextJustification(m_hDC, nBreakExtra, nBreakCount);
  1104. }
  1105. int GetTextCharacterExtra() const
  1106. {
  1107. ATLASSERT(m_hDC != NULL);
  1108. return ::GetTextCharacterExtra(m_hDC);
  1109. }
  1110. int SetTextCharacterExtra(int nCharExtra)
  1111. {
  1112. ATLASSERT(m_hDC != NULL);
  1113. return ::SetTextCharacterExtra(m_hDC, nCharExtra);
  1114. }
  1115. #endif //!UNDER_CE
  1116. // Advanced Drawing
  1117. BOOL DrawEdge(LPRECT lpRect, UINT nEdge, UINT nFlags)
  1118. {
  1119. ATLASSERT(m_hDC != NULL);
  1120. return ::DrawEdge(m_hDC, lpRect, nEdge, nFlags);
  1121. }
  1122. BOOL DrawFrameControl(LPRECT lpRect, UINT nType, UINT nState)
  1123. {
  1124. ATLASSERT(m_hDC != NULL);
  1125. return ::DrawFrameControl(m_hDC, lpRect, nType, nState);
  1126. }
  1127. // Scrolling Functions
  1128. BOOL ScrollDC(int dx, int dy, LPCRECT lpRectScroll, LPCRECT lpRectClip,
  1129. HRGN hRgnUpdate, LPRECT lpRectUpdate)
  1130. {
  1131. ATLASSERT(m_hDC != NULL);
  1132. return ::ScrollDC(m_hDC, dx, dy, lpRectScroll, lpRectClip, hRgnUpdate, lpRectUpdate);
  1133. }
  1134. // Font Functions
  1135. #ifndef UNDER_CE
  1136. BOOL GetCharWidth(UINT nFirstChar, UINT nLastChar, LPINT lpBuffer) const
  1137. {
  1138. ATLASSERT(m_hDC != NULL);
  1139. return ::GetCharWidth(m_hDC, nFirstChar, nLastChar, lpBuffer);
  1140. }
  1141. DWORD SetMapperFlags(DWORD dwFlag)
  1142. {
  1143. ATLASSERT(m_hDC != NULL);
  1144. return ::SetMapperFlags(m_hDC, dwFlag);
  1145. }
  1146. BOOL GetAspectRatioFilter(LPSIZE lpSize) const
  1147. {
  1148. ATLASSERT(m_hDC != NULL);
  1149. return ::GetAspectRatioFilterEx(m_hDC, lpSize);
  1150. }
  1151. BOOL GetCharABCWidths(UINT nFirstChar, UINT nLastChar, LPABC lpabc) const
  1152. {
  1153. ATLASSERT(m_hDC != NULL);
  1154. return ::GetCharABCWidths(m_hDC, nFirstChar, nLastChar, lpabc);
  1155. }
  1156. DWORD GetFontData(DWORD dwTable, DWORD dwOffset, LPVOID lpData, DWORD cbData) const
  1157. {
  1158. ATLASSERT(m_hDC != NULL);
  1159. return ::GetFontData(m_hDC, dwTable, dwOffset, lpData, cbData);
  1160. }
  1161. int GetKerningPairs(int nPairs, LPKERNINGPAIR lpkrnpair) const
  1162. {
  1163. ATLASSERT(m_hDC != NULL);
  1164. return ::GetKerningPairs(m_hDC, nPairs, lpkrnpair);
  1165. }
  1166. UINT GetOutlineTextMetrics(UINT cbData, LPOUTLINETEXTMETRIC lpotm) const
  1167. {
  1168. ATLASSERT(m_hDC != NULL);
  1169. return ::GetOutlineTextMetrics(m_hDC, cbData, lpotm);
  1170. }
  1171. DWORD GetGlyphOutline(UINT nChar, UINT nFormat, LPGLYPHMETRICS lpgm,
  1172. DWORD cbBuffer, LPVOID lpBuffer, const MAT2* lpmat2) const
  1173. {
  1174. ATLASSERT(m_hDC != NULL);
  1175. return ::GetGlyphOutline(m_hDC, nChar, nFormat, lpgm, cbBuffer, lpBuffer, lpmat2);
  1176. }
  1177. BOOL GetCharABCWidths(UINT nFirstChar, UINT nLastChar,
  1178. LPABCFLOAT lpABCF) const
  1179. {
  1180. ATLASSERT(m_hDC != NULL);
  1181. return ::GetCharABCWidthsFloat(m_hDC, nFirstChar, nLastChar, lpABCF);
  1182. }
  1183. BOOL GetCharWidth(UINT nFirstChar, UINT nLastChar,
  1184. float* lpFloatBuffer) const
  1185. {
  1186. ATLASSERT(m_hDC != NULL);
  1187. return ::GetCharWidthFloat(m_hDC, nFirstChar, nLastChar, lpFloatBuffer);
  1188. }
  1189. #endif //!UNDER_CE
  1190. // Printer/Device Escape Functions
  1191. #ifndef UNDER_CE
  1192. int Escape(int nEscape, int nCount, LPCSTR lpszInData, LPVOID lpOutData)
  1193. {
  1194. ATLASSERT(m_hDC != NULL);
  1195. return ::Escape(m_hDC, nEscape, nCount, lpszInData, lpOutData);
  1196. }
  1197. int Escape(int nEscape, int nInputSize, LPCSTR lpszInputData,
  1198. int nOutputSize, LPSTR lpszOutputData)
  1199. {
  1200. ATLASSERT(m_hDC != NULL);
  1201. return ::ExtEscape(m_hDC, nEscape, nInputSize, lpszInputData, nOutputSize, lpszOutputData);
  1202. }
  1203. int DrawEscape(int nEscape, int nInputSize, LPCSTR lpszInputData)
  1204. {
  1205. ATLASSERT(m_hDC != NULL);
  1206. return ::DrawEscape(m_hDC, nEscape, nInputSize, lpszInputData);
  1207. }
  1208. #endif //!UNDER_CE
  1209. // Escape helpers
  1210. int StartDoc(LPCTSTR lpszDocName) // old Win3.0 version
  1211. {
  1212. DOCINFO di;
  1213. memset(&di, 0, sizeof(DOCINFO));
  1214. di.cbSize = sizeof(DOCINFO);
  1215. di.lpszDocName = lpszDocName;
  1216. return StartDoc(&di);
  1217. }
  1218. int StartDoc(LPDOCINFO lpDocInfo)
  1219. {
  1220. ATLASSERT(m_hDC != NULL);
  1221. return ::StartDoc(m_hDC, lpDocInfo);
  1222. }
  1223. int StartPage()
  1224. {
  1225. ATLASSERT(m_hDC != NULL);
  1226. return ::StartPage(m_hDC);
  1227. }
  1228. int EndPage()
  1229. {
  1230. ATLASSERT(m_hDC != NULL);
  1231. return ::EndPage(m_hDC);
  1232. }
  1233. int SetAbortProc(BOOL (CALLBACK* lpfn)(HDC, int))
  1234. {
  1235. ATLASSERT(m_hDC != NULL);
  1236. return ::SetAbortProc(m_hDC, (ABORTPROC)lpfn);
  1237. }
  1238. int AbortDoc()
  1239. {
  1240. ATLASSERT(m_hDC != NULL);
  1241. return ::AbortDoc(m_hDC);
  1242. }
  1243. int EndDoc()
  1244. {
  1245. ATLASSERT(m_hDC != NULL);
  1246. return ::EndDoc(m_hDC);
  1247. }
  1248. // MetaFile Functions
  1249. #ifndef UNDER_CE
  1250. BOOL PlayMetaFile(HMETAFILE hMF)
  1251. {
  1252. ATLASSERT(m_hDC != NULL);
  1253. if(::GetDeviceCaps(m_hDC, TECHNOLOGY) == DT_METAFILE)
  1254. {
  1255. // playing metafile in metafile, just use core windows API
  1256. return ::PlayMetaFile(m_hDC, hMF);
  1257. }
  1258. // for special playback, lParam == pDC
  1259. return ::EnumMetaFile(m_hDC, hMF, EnumMetaFileProc, (LPARAM)this);
  1260. }
  1261. BOOL PlayMetaFile(HENHMETAFILE hEnhMetaFile, LPCRECT lpBounds)
  1262. {
  1263. ATLASSERT(m_hDC != NULL);
  1264. return ::PlayEnhMetaFile(m_hDC, hEnhMetaFile, lpBounds);
  1265. }
  1266. BOOL AddMetaFileComment(UINT nDataSize, const BYTE* pCommentData) // can be used for enhanced metafiles only
  1267. {
  1268. ATLASSERT(m_hDC != NULL);
  1269. return ::GdiComment(m_hDC, nDataSize, pCommentData);
  1270. }
  1271. // Special handling for metafile playback
  1272. static int CALLBACK EnumMetaFileProc(HDC hDC, HANDLETABLE* pHandleTable, METARECORD* pMetaRec, int nHandles, LPARAM lParam)
  1273. {
  1274. CDC* pDC = (CDC*)lParam;
  1275. switch (pMetaRec->rdFunction)
  1276. {
  1277. case META_SETMAPMODE:
  1278. pDC->SetMapMode((int)(short)pMetaRec->rdParm[0]);
  1279. break;
  1280. case META_SETWINDOWEXT:
  1281. pDC->SetWindowExt((int)(short)pMetaRec->rdParm[1], (int)(short)pMetaRec->rdParm[0]);
  1282. break;
  1283. case META_SETWINDOWORG:
  1284. pDC->SetWindowOrg((int)(short)pMetaRec->rdParm[1], (int)(short)pMetaRec->rdParm[0]);
  1285. break;
  1286. case META_SETVIEWPORTEXT:
  1287. pDC->SetViewportExt((int)(short)pMetaRec->rdParm[1], (int)(short)pMetaRec->rdParm[0]);
  1288. break;
  1289. case META_SETVIEWPORTORG:
  1290. pDC->SetViewportOrg((int)(short)pMetaRec->rdParm[1], (int)(short)pMetaRec->rdParm[0]);
  1291. break;
  1292. case META_SCALEWINDOWEXT:
  1293. pDC->ScaleWindowExt((int)(short)pMetaRec->rdParm[3], (int)(short)pMetaRec->rdParm[2],
  1294. (int)(short)pMetaRec->rdParm[1], (int)(short)pMetaRec->rdParm[0]);
  1295. break;
  1296. case META_SCALEVIEWPORTEXT:
  1297. pDC->ScaleViewportExt((int)(short)pMetaRec->rdParm[3], (int)(short)pMetaRec->rdParm[2],
  1298. (int)(short)pMetaRec->rdParm[1], (int)(short)pMetaRec->rdParm[0]);
  1299. break;
  1300. case META_OFFSETVIEWPORTORG:
  1301. pDC->OffsetViewportOrg((int)(short)pMetaRec->rdParm[1], (int)(short)pMetaRec->rdParm[0]);
  1302. break;
  1303. case META_SAVEDC:
  1304. pDC->SaveDC();
  1305. break;
  1306. case META_RESTOREDC:
  1307. pDC->RestoreDC((int)(short)pMetaRec->rdParm[0]);
  1308. break;
  1309. case META_SETBKCOLOR:
  1310. pDC->SetBkColor(*(UNALIGNED COLORREF*)&pMetaRec->rdParm[0]);
  1311. break;
  1312. case META_SETTEXTCOLOR:
  1313. pDC->SetTextColor(*(UNALIGNED COLORREF*)&pMetaRec->rdParm[0]);
  1314. break;
  1315. // need to watch out for SelectObject(HFONT), for custom font mapping
  1316. case META_SELECTOBJECT:
  1317. {
  1318. HGDIOBJ hObject = pHandleTable->objectHandle[pMetaRec->rdParm[0]];
  1319. UINT nObjType = ::GetObjectType(hObject);
  1320. if(nObjType == 0)
  1321. {
  1322. // object type is unknown, determine if it is a font
  1323. HFONT hStockFont = (HFONT)::GetStockObject(SYSTEM_FONT);
  1324. /**/ HFONT hFontOld = (HFONT)::SelectObject(pDC->m_hDC, hStockFont);
  1325. /**/ HGDIOBJ hObjOld = ::SelectObject(pDC->m_hDC, hObject);
  1326. if(hObjOld == hStockFont)
  1327. {
  1328. // got the stock object back, so must be selecting a font
  1329. pDC->SelectFont((HFONT)hObject);
  1330. break; // don't play the default record
  1331. }
  1332. else
  1333. {
  1334. // didn't get the stock object back, so restore everything
  1335. /**/ ::SelectObject(pDC->m_hDC, hFontOld);
  1336. /**/ ::SelectObject(pDC->m_hDC, hObjOld);
  1337. }
  1338. // and fall through to PlayMetaFileRecord...
  1339. }
  1340. else if(nObjType == OBJ_FONT)
  1341. {
  1342. // play back as CDC::SelectFont(HFONT)
  1343. pDC->SelectFont((HFONT)hObject);
  1344. break; // don't play the default record
  1345. }
  1346. }
  1347. // fall through...
  1348. default:
  1349. ::PlayMetaFileRecord(hDC, pHandleTable, pMetaRec, nHandles);
  1350. break;
  1351. }
  1352. return 1;
  1353. }
  1354. #endif //!UNDER_CE
  1355. // Path Functions
  1356. #ifndef UNDER_CE
  1357. BOOL AbortPath()
  1358. {
  1359. ATLASSERT(m_hDC != NULL);
  1360. return ::AbortPath(m_hDC);
  1361. }
  1362. BOOL BeginPath()
  1363. {
  1364. ATLASSERT(m_hDC != NULL);
  1365. return ::BeginPath(m_hDC);
  1366. }
  1367. BOOL CloseFigure()
  1368. {
  1369. ATLASSERT(m_hDC != NULL);
  1370. return ::CloseFigure(m_hDC);
  1371. }
  1372. BOOL EndPath()
  1373. {
  1374. ATLASSERT(m_hDC != NULL);
  1375. return ::EndPath(m_hDC);
  1376. }
  1377. BOOL FillPath()
  1378. {
  1379. ATLASSERT(m_hDC != NULL);
  1380. return ::FillPath(m_hDC);
  1381. }
  1382. BOOL FlattenPath()
  1383. {
  1384. ATLASSERT(m_hDC != NULL);
  1385. return ::FlattenPath(m_hDC);
  1386. }
  1387. BOOL StrokeAndFillPath()
  1388. {
  1389. ATLASSERT(m_hDC != NULL);
  1390. return ::StrokeAndFillPath(m_hDC);
  1391. }
  1392. BOOL StrokePath()
  1393. {
  1394. ATLASSERT(m_hDC != NULL);
  1395. return ::StrokePath(m_hDC);
  1396. }
  1397. BOOL WidenPath()
  1398. {
  1399. ATLASSERT(m_hDC != NULL);
  1400. return ::WidenPath(m_hDC);
  1401. }
  1402. BOOL GetMiterLimit(PFLOAT pfMiterLimit) const
  1403. {
  1404. ATLASSERT(m_hDC != NULL);
  1405. return ::GetMiterLimit(m_hDC, pfMiterLimit);
  1406. }
  1407. BOOL SetMiterLimit(float fMiterLimit)
  1408. {
  1409. ATLASSERT(m_hDC != NULL);
  1410. return ::SetMiterLimit(m_hDC, fMiterLimit, NULL);
  1411. }
  1412. int GetPath(LPPOINT lpPoints, LPBYTE lpTypes, int nCount) const
  1413. {
  1414. ATLASSERT(m_hDC != NULL);
  1415. return ::GetPath(m_hDC, lpPoints, lpTypes, nCount);
  1416. }
  1417. BOOL SelectClipPath(int nMode)
  1418. {
  1419. ATLASSERT(m_hDC != NULL);
  1420. return ::SelectClipPath(m_hDC, nMode);
  1421. }
  1422. #endif //!UNDER_CE
  1423. // Misc Helper Functions
  1424. static HBRUSH PASCAL GetHalftoneBrush()
  1425. {
  1426. HBRUSH halftoneBrush;
  1427. WORD grayPattern[8];
  1428. for(int i = 0; i < 8; i++)
  1429. grayPattern[i] = (WORD)(0x5555 << (i & 1));
  1430. HBITMAP grayBitmap = CreateBitmap(8, 8, 1, 1, &grayPattern);
  1431. if(grayBitmap != NULL)
  1432. {
  1433. halftoneBrush = ::CreatePatternBrush(grayBitmap);
  1434. DeleteObject(grayBitmap);
  1435. }
  1436. return halftoneBrush;
  1437. }
  1438. void DrawDragRect(LPCRECT lpRect, SIZE size, LPCRECT lpRectLast, SIZE sizeLast, HBRUSH hBrush = NULL, HBRUSH hBrushLast = NULL)
  1439. {
  1440. // first, determine the update region and select it
  1441. HRGN hRgnNew;
  1442. HRGN hRgnOutside, hRgnInside;
  1443. hRgnOutside = ::CreateRectRgnIndirect(lpRect);
  1444. RECT rect = *lpRect;
  1445. ::InflateRect(&rect, -size.cx, -size.cy);
  1446. ::IntersectRect(&rect, &rect, lpRect);
  1447. hRgnInside = ::CreateRectRgnIndirect(&rect);
  1448. hRgnNew = ::CreateRectRgn(0, 0, 0, 0);
  1449. ::CombineRgn(hRgnNew, hRgnOutside, hRgnInside, RGN_XOR);
  1450. HBRUSH hBrushOld = NULL;
  1451. if(hBrush == NULL)
  1452. hBrush = CDC::GetHalftoneBrush();
  1453. if(hBrushLast == NULL)
  1454. hBrushLast = hBrush;
  1455. HRGN hRgnLast, hRgnUpdate;
  1456. if(lpRectLast != NULL)
  1457. {
  1458. // find difference between new region and old region
  1459. hRgnLast = ::CreateRectRgn(0, 0, 0, 0);
  1460. ::SetRectRgn(hRgnOutside, lpRectLast->left, lpRectLast->top, lpRectLast->right, lpRectLast->bottom);
  1461. rect = *lpRectLast;
  1462. ::InflateRect(&rect, -sizeLast.cx, -sizeLast.cy);
  1463. ::IntersectRect(&rect, &rect, lpRectLast);
  1464. ::SetRectRgn(hRgnInside, rect.left, rect.top, rect.right, rect.bottom);
  1465. ::CombineRgn(hRgnLast, hRgnOutside, hRgnInside, RGN_XOR);
  1466. // only diff them if brushes are the same
  1467. if(hBrush == hBrushLast)
  1468. {
  1469. hRgnUpdate = ::CreateRectRgn(0, 0, 0, 0);
  1470. ::CombineRgn(hRgnUpdate, hRgnLast, hRgnNew, RGN_XOR);
  1471. }
  1472. }
  1473. if(hBrush != hBrushLast && lpRectLast != NULL)
  1474. {
  1475. // brushes are different -- erase old region first
  1476. SelectClipRgn(hRgnLast);
  1477. GetClipBox(&rect);
  1478. hBrushOld = SelectBrush(hBrushLast);
  1479. PatBlt(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, PATINVERT);
  1480. SelectBrush(hBrushOld);
  1481. hBrushOld = NULL;
  1482. }
  1483. // draw into the update/new region
  1484. SelectClipRgn(hRgnUpdate != NULL ? hRgnUpdate : hRgnNew);
  1485. GetClipBox(&rect);
  1486. hBrushOld = SelectBrush(hBrush);
  1487. PatBlt(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, PATINVERT);
  1488. // cleanup DC
  1489. if(hBrushOld != NULL)
  1490. SelectBrush(hBrushOld);
  1491. SelectClipRgn(NULL);
  1492. }
  1493. void FillSolidRect(LPCRECT lpRect, COLORREF clr)
  1494. {
  1495. ATLASSERT(m_hDC != NULL);
  1496. ::SetBkColor(m_hDC, clr);
  1497. ::ExtTextOut(m_hDC, 0, 0, ETO_OPAQUE, lpRect, NULL, 0, NULL);
  1498. }
  1499. void FillSolidRect(int x, int y, int cx, int cy, COLORREF clr)
  1500. {
  1501. ATLASSERT(m_hDC != NULL);
  1502. ::SetBkColor(m_hDC, clr);
  1503. RECT rect = { x, y, x + cx, y + cy };
  1504. ::ExtTextOut(m_hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
  1505. }
  1506. void Draw3dRect(LPCRECT lpRect, COLORREF clrTopLeft, COLORREF clrBottomRight)
  1507. {
  1508. Draw3dRect(lpRect->left, lpRect->top, lpRect->right - lpRect->left,
  1509. lpRect->bottom - lpRect->top, clrTopLeft, clrBottomRight);
  1510. }
  1511. void Draw3dRect(int x, int y, int cx, int cy, COLORREF clrTopLeft, COLORREF clrBottomRight)
  1512. {
  1513. FillSolidRect(x, y, cx - 1, 1, clrTopLeft);
  1514. FillSolidRect(x, y, 1, cy - 1, clrTopLeft);
  1515. FillSolidRect(x + cx, y, -1, cy, clrBottomRight);
  1516. FillSolidRect(x, y + cy, cx, -1, clrBottomRight);
  1517. }
  1518. };
  1519. /////////////////////////////////////////////////////////////////////////////
  1520. // CDC Helpers
  1521. class CPaintDC : public CDC
  1522. {
  1523. public:
  1524. HWND m_hWnd;
  1525. PAINTSTRUCT m_ps;
  1526. CPaintDC(HWND hWnd, BOOL bAutoRestore = TRUE) : CDC(NULL, bAutoRestore)
  1527. {
  1528. ATLASSERT(::IsWindow(hWnd));
  1529. m_hWnd = hWnd;
  1530. m_hDC = ::BeginPaint(hWnd, &m_ps);
  1531. }
  1532. ~CPaintDC()
  1533. {
  1534. ATLASSERT(m_hDC != NULL);
  1535. ATLASSERT(::IsWindow(m_hWnd));
  1536. if(m_bAutoRestore)
  1537. RestoreAllObjects();
  1538. ::EndPaint(m_hWnd, &m_ps);
  1539. Detach();
  1540. }
  1541. };
  1542. class CClientDC : public CDC
  1543. {
  1544. public:
  1545. HWND m_hWnd;
  1546. CClientDC(HWND hWnd, BOOL bAutoRestore = TRUE) : CDC(NULL, bAutoRestore)
  1547. {
  1548. ATLASSERT(hWnd == NULL || ::IsWindow(hWnd));
  1549. m_hWnd = hWnd;
  1550. m_hDC = ::GetDC(hWnd);
  1551. }
  1552. ~CClientDC()
  1553. {
  1554. ATLASSERT(m_hDC != NULL);
  1555. if(m_bAutoRestore)
  1556. RestoreAllObjects();
  1557. ::ReleaseDC(m_hWnd, Detach());
  1558. }
  1559. };
  1560. class CWindowDC : public CDC
  1561. {
  1562. public:
  1563. HWND m_hWnd;
  1564. CWindowDC(HWND hWnd, BOOL bAutoRestore = TRUE) : CDC(NULL, bAutoRestore)
  1565. {
  1566. ATLASSERT(hWnd == NULL || ::IsWindow(hWnd));
  1567. m_hWnd = hWnd;
  1568. m_hDC = ::GetWindowDC(hWnd);
  1569. }
  1570. ~CWindowDC()
  1571. {
  1572. ATLASSERT(m_hDC != NULL);
  1573. if(m_bAutoRestore)
  1574. RestoreAllObjects();
  1575. ::ReleaseDC(m_hWnd, Detach());
  1576. }
  1577. };
  1578. /////////////////////////////////////////////////////////////////////////////
  1579. // CMenu
  1580. class CMenu
  1581. {
  1582. public:
  1583. HMENU m_hMenu;
  1584. CMenu(HMENU hMenu = NULL) : m_hMenu(hMenu) { }
  1585. ~CMenu()
  1586. {
  1587. if(m_hMenu != NULL)
  1588. DestroyMenu();
  1589. }
  1590. CMenu& operator=(HMENU hMenu)
  1591. {
  1592. m_hMenu = hMenu;
  1593. return *this;
  1594. }
  1595. void Attach(HMENU hMenuNew)
  1596. {
  1597. ATLASSERT(::IsMenu(hMenuNew));
  1598. m_hMenu = hMenuNew;
  1599. }
  1600. HMENU Detach()
  1601. {
  1602. HMENU hMenu = m_hMenu;
  1603. m_hMenu = NULL;
  1604. return hMenu;
  1605. }
  1606. operator HMENU() const { return m_hMenu; }
  1607. BOOL CreateMenu()
  1608. {
  1609. ATLASSERT(m_hMenu == NULL);
  1610. m_hMenu = ::CreateMenu();
  1611. return (m_hMenu != NULL) ? TRUE : FALSE;
  1612. }
  1613. BOOL CreatePopupMenu()
  1614. {
  1615. ATLASSERT(m_hMenu == NULL);
  1616. m_hMenu = ::CreatePopupMenu();
  1617. return (m_hMenu != NULL) ? TRUE : FALSE;
  1618. }
  1619. BOOL LoadMenu(LPCTSTR lpszResourceName)
  1620. {
  1621. ATLASSERT(m_hMenu == NULL);
  1622. m_hMenu = ::LoadMenu(_Module.GetResourceInstance(), lpszResourceName);
  1623. return (m_hMenu != NULL) ? TRUE : FALSE;
  1624. }
  1625. BOOL LoadMenu(UINT nIDResource)
  1626. {
  1627. ATLASSERT(m_hMenu == NULL);
  1628. m_hMenu = ::LoadMenu(_Module.GetResourceInstance(), MAKEINTRESOURCE(nIDResource));
  1629. return (m_hMenu != NULL) ? TRUE : FALSE;
  1630. }
  1631. #ifndef UNDER_CE
  1632. BOOL LoadMenuIndirect(const void* lpMenuTemplate)
  1633. {
  1634. ATLASSERT(m_hMenu == NULL);
  1635. m_hMenu = ::LoadMenuIndirect(lpMenuTemplate);
  1636. return (m_hMenu != NULL) ? TRUE : FALSE;
  1637. }
  1638. #endif //!UNDER_CE
  1639. BOOL DestroyMenu()
  1640. {
  1641. if (m_hMenu == NULL)
  1642. return FALSE;
  1643. return ::DestroyMenu(Detach());
  1644. }
  1645. // Menu Operations
  1646. BOOL DeleteMenu(UINT nPosition, UINT nFlags)
  1647. {
  1648. ATLASSERT(::IsMenu(m_hMenu));
  1649. return ::DeleteMenu(m_hMenu, nPosition, nFlags);
  1650. }
  1651. BOOL TrackPopupMenu(UINT nFlags, int x, int y, HWND hWnd, LPCRECT lpRect = NULL)
  1652. {
  1653. ATLASSERT(::IsMenu(m_hMenu));
  1654. #ifndef UNDER_CE
  1655. return ::TrackPopupMenu(m_hMenu, nFlags, x, y, 0, hWnd, lpRect);
  1656. #else // CE specific
  1657. return ::TrackPopupMenuEx(m_hMenu, nFlags, x, y, hWnd, NULL);
  1658. #endif //!UNDER_CE
  1659. }
  1660. BOOL TrackPopupMenuEx(UINT uFlags, int x, int y, HWND hWnd, LPTPMPARAMS lptpm = NULL)
  1661. {
  1662. ATLASSERT(::IsMenu(m_hMenu));
  1663. return ::TrackPopupMenuEx(m_hMenu, uFlags, x, y, hWnd, lptpm);
  1664. }
  1665. // Menu Item Operations
  1666. BOOL AppendMenu(UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
  1667. {
  1668. ATLASSERT(::IsMenu(m_hMenu));
  1669. return ::AppendMenu(m_hMenu, nFlags, nIDNewItem, lpszNewItem);
  1670. }
  1671. #ifndef UNDER_CE
  1672. BOOL AppendMenu(UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
  1673. {
  1674. ATLASSERT(::IsMenu(m_hMenu));
  1675. return ::AppendMenu(m_hMenu, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
  1676. }
  1677. #endif //!UNDER_CE
  1678. UINT CheckMenuItem(UINT nIDCheckItem, UINT nCheck)
  1679. {
  1680. ATLASSERT(::IsMenu(m_hMenu));
  1681. return (UINT)::CheckMenuItem(m_hMenu, nIDCheckItem, nCheck);
  1682. }
  1683. UINT EnableMenuItem(UINT nIDEnableItem, UINT nEnable)
  1684. {
  1685. ATLASSERT(::IsMenu(m_hMenu));
  1686. return ::EnableMenuItem(m_hMenu, nIDEnableItem, nEnable);
  1687. }
  1688. #ifndef UNDER_CE
  1689. UINT GetMenuItemCount() const
  1690. {
  1691. ATLASSERT(::IsMenu(m_hMenu));
  1692. return ::GetMenuItemCount(m_hMenu);
  1693. }
  1694. UINT GetMenuItemID(int nPos) const
  1695. {
  1696. ATLASSERT(::IsMenu(m_hMenu));
  1697. return ::GetMenuItemID(m_hMenu, nPos);
  1698. }
  1699. UINT GetMenuState(UINT nID, UINT nFlags) const
  1700. {
  1701. ATLASSERT(::IsMenu(m_hMenu));
  1702. return ::GetMenuState(m_hMenu, nID, nFlags);
  1703. }
  1704. int GetMenuString(UINT nIDItem, LPTSTR lpString, int nMaxCount, UINT nFlags) const
  1705. {
  1706. ATLASSERT(::IsMenu(m_hMenu));
  1707. return ::GetMenuString(m_hMenu, nIDItem, lpString, nMaxCount, nFlags);
  1708. }
  1709. int GetMenuStringLen(UINT nIDItem, UINT nFlags) const
  1710. {
  1711. ATLASSERT(::IsMenu(m_hMenu));
  1712. return ::GetMenuString(m_hMenu, nIDItem, NULL, 0, nFlags);
  1713. }
  1714. #ifndef _ATL_NO_COM
  1715. BOOL GetMenuString(UINT nIDItem, BSTR& bstrText, UINT nFlags) const
  1716. {
  1717. USES_CONVERSION;
  1718. ATLASSERT(::IsMenu(m_hMenu));
  1719. ATLASSERT(bstrText == NULL);
  1720. int nLen = GetMenuStringLen(nIDItem, nFlags);
  1721. {
  1722. bstrText = ::SysAllocString(OLESTR(""));
  1723. return (bstrText != NULL) ? TRUE : FALSE;
  1724. }
  1725. LPTSTR lpszText = (LPTSTR)_alloca((nLen + 1) * sizeof(TCHAR));
  1726. if(!GetMenuString(nIDItem, lpszText, nLen, nFlags))
  1727. return FALSE;
  1728. bstrText = ::SysAllocString(T2OLE(lpszText));
  1729. return (bstrText != NULL) ? TRUE : FALSE;
  1730. }
  1731. #endif //!_ATL_NO_COM
  1732. #endif //!UNDER_CE
  1733. HMENU GetSubMenu(int nPos) const
  1734. {
  1735. ATLASSERT(::IsMenu(m_hMenu));
  1736. return ::GetSubMenu(m_hMenu, nPos);
  1737. }
  1738. #ifndef UNDER_CE
  1739. BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
  1740. {
  1741. ATLASSERT(::IsMenu(m_hMenu));
  1742. return ::InsertMenu(m_hMenu, nPosition, nFlags, nIDNewItem, lpszNewItem);
  1743. }
  1744. BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
  1745. {
  1746. ATLASSERT(::IsMenu(m_hMenu));
  1747. return ::InsertMenu(m_hMenu, nPosition, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
  1748. }
  1749. #endif //!UNDER_CE
  1750. #ifndef UNDER_CE
  1751. BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
  1752. {
  1753. ATLASSERT(::IsMenu(m_hMenu));
  1754. return ::ModifyMenu(m_hMenu, nPosition, nFlags, nIDNewItem, lpszNewItem);
  1755. }
  1756. BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
  1757. {
  1758. ATLASSERT(::IsMenu(m_hMenu));
  1759. return ::ModifyMenu(m_hMenu, nPosition, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
  1760. }
  1761. #endif //!UNDER_CE
  1762. BOOL RemoveMenu(UINT nPosition, UINT nFlags)
  1763. {
  1764. ATLASSERT(::IsMenu(m_hMenu));
  1765. return ::RemoveMenu(m_hMenu, nPosition, nFlags);
  1766. }
  1767. #ifndef UNDER_CE
  1768. BOOL SetMenuItemBitmaps(UINT nPosition, UINT nFlags, HBITMAP hBmpUnchecked, HBITMAP hBmpChecked)
  1769. {
  1770. ATLASSERT(::IsMenu(m_hMenu));
  1771. return ::SetMenuItemBitmaps(m_hMenu, nPosition, nFlags, hBmpUnchecked, hBmpChecked);
  1772. }
  1773. #endif //!UNDER_CE
  1774. BOOL CheckMenuRadioItem(UINT nIDFirst, UINT nIDLast, UINT nIDItem, UINT nFlags)
  1775. {
  1776. ATLASSERT(::IsMenu(m_hMenu));
  1777. return ::CheckMenuRadioItem(m_hMenu, nIDFirst, nIDLast, nIDItem, nFlags);
  1778. }
  1779. BOOL GetMenuItemInfo(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii)
  1780. {
  1781. ATLASSERT(::IsMenu(m_hMenu));
  1782. return (BOOL)::GetMenuItemInfo(m_hMenu, uItem, bByPosition, lpmii);
  1783. }
  1784. BOOL SetMenuItemInfo(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii)
  1785. {
  1786. ATLASSERT(::IsMenu(m_hMenu));
  1787. return (BOOL)::SetMenuItemInfo(m_hMenu, uItem, bByPosition, lpmii);
  1788. }
  1789. // Context Help Functions
  1790. #ifndef UNDER_CE
  1791. BOOL SetMenuContextHelpId(DWORD dwContextHelpId)
  1792. {
  1793. ATLASSERT(::IsMenu(m_hMenu));
  1794. return ::SetMenuContextHelpId(m_hMenu, dwContextHelpId);
  1795. }
  1796. DWORD GetMenuContextHelpId() const
  1797. {
  1798. ATLASSERT(::IsMenu(m_hMenu));
  1799. return ::GetMenuContextHelpId(m_hMenu);
  1800. }
  1801. #endif //!UNDER_CE
  1802. };
  1803. /////////////////////////////////////////////////////////////////////////////
  1804. // CPen
  1805. class CPen
  1806. {
  1807. public:
  1808. HPEN m_hPen;
  1809. CPen(HPEN hPen = NULL) : m_hPen(hPen)
  1810. { }
  1811. ~CPen()
  1812. {
  1813. if(m_hPen != NULL)
  1814. DeleteObject();
  1815. }
  1816. CPen& operator=(HPEN hPen)
  1817. {
  1818. m_hPen = hPen;
  1819. return *this;
  1820. }
  1821. void Attach(HPEN hPen)
  1822. {
  1823. m_hPen = hPen;
  1824. }
  1825. HPEN Detach()
  1826. {
  1827. HPEN hPen = m_hPen;
  1828. m_hPen = NULL;
  1829. return hPen;
  1830. }
  1831. operator HPEN() const { return m_hPen; }
  1832. HPEN CreatePen(int nPenStyle, int nWidth, COLORREF crColor)
  1833. {
  1834. ATLASSERT(m_hPen == NULL);
  1835. m_hPen = ::CreatePen(nPenStyle, nWidth, crColor);
  1836. return m_hPen;
  1837. }
  1838. #ifndef UNDER_CE
  1839. HPEN CreatePen(int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL)
  1840. {
  1841. ATLASSERT(m_hPen == NULL);
  1842. m_hPen = ::ExtCreatePen(nPenStyle, nWidth, pLogBrush, nStyleCount, lpStyle);
  1843. return m_hPen;
  1844. }
  1845. #endif //!UNDER_CE
  1846. HPEN CreatePenIndirect(LPLOGPEN lpLogPen)
  1847. {
  1848. ATLASSERT(m_hPen == NULL);
  1849. m_hPen = ::CreatePenIndirect(lpLogPen);
  1850. return m_hPen;
  1851. }
  1852. BOOL DeleteObject()
  1853. {
  1854. ATLASSERT(m_hPen != NULL);
  1855. BOOL bRet = ::DeleteObject(m_hPen);
  1856. if(bRet)
  1857. m_hPen = NULL;
  1858. return bRet;
  1859. }
  1860. // Attributes
  1861. int GetLogPen(LOGPEN* pLogPen)
  1862. {
  1863. ATLASSERT(m_hPen != NULL);
  1864. return ::GetObject(m_hPen, sizeof(LOGPEN), pLogPen);
  1865. }
  1866. #ifndef UNDER_CE
  1867. int GetExtLogPen(EXTLOGPEN* pLogPen)
  1868. {
  1869. ATLASSERT(m_hPen != NULL);
  1870. return ::GetObject(m_hPen, sizeof(EXTLOGPEN), pLogPen);
  1871. }
  1872. #endif //!UNDER_CE
  1873. };
  1874. /////////////////////////////////////////////////////////////////////////////
  1875. // CBrush
  1876. class CBrush
  1877. {
  1878. public:
  1879. HBRUSH m_hBrush;
  1880. CBrush(HBRUSH hBrush = NULL) : m_hBrush(hBrush)
  1881. { }
  1882. ~CBrush()
  1883. {
  1884. if(m_hBrush != NULL)
  1885. DeleteObject();
  1886. }
  1887. CBrush& operator=(HBRUSH hBrush)
  1888. {
  1889. m_hBrush = hBrush;
  1890. return *this;
  1891. }
  1892. void Attach(HBRUSH hBrush)
  1893. {
  1894. m_hBrush = hBrush;
  1895. }
  1896. HBRUSH Detach()
  1897. {
  1898. HBRUSH hBrush = m_hBrush;
  1899. m_hBrush = NULL;
  1900. return hBrush;
  1901. }
  1902. operator HBRUSH() const { return m_hBrush; }
  1903. HBRUSH CreateSolidBrush(COLORREF crColor)
  1904. {
  1905. ATLASSERT(m_hBrush == NULL);
  1906. m_hBrush = ::CreateSolidBrush(crColor);
  1907. return m_hBrush;
  1908. }
  1909. #ifndef UNDER_CE
  1910. HBRUSH CreateHatchBrush(int nIndex, COLORREF crColor)
  1911. {
  1912. ATLASSERT(m_hBrush == NULL);
  1913. m_hBrush = ::CreateHatchBrush(nIndex, crColor);
  1914. return m_hBrush;
  1915. }
  1916. HBRUSH CreateBrushIndirect(const LOGBRUSH* lpLogBrush)
  1917. {
  1918. ATLASSERT(m_hBrush == NULL);
  1919. m_hBrush = ::CreateBrushIndirect(lpLogBrush);
  1920. return m_hBrush;
  1921. }
  1922. #endif //!UNDER_CE
  1923. HBRUSH CreatePatternBrush(HBITMAP hBitmap)
  1924. {
  1925. ATLASSERT(m_hBrush == NULL);
  1926. m_hBrush = ::CreatePatternBrush(hBitmap);
  1927. return m_hBrush;
  1928. }
  1929. #ifndef UNDER_CE
  1930. //REVIEW
  1931. HBRUSH CreateDIBPatternBrush(HGLOBAL hPackedDIB, UINT nUsage)
  1932. {
  1933. ATLASSERT(hPackedDIB != NULL);
  1934. const void* lpPackedDIB = ::GlobalLock(hPackedDIB);
  1935. ATLASSERT(lpPackedDIB != NULL);
  1936. m_hBrush = ::CreateDIBPatternBrushPt(lpPackedDIB, nUsage);
  1937. ::GlobalUnlock(hPackedDIB);
  1938. return m_hBrush;
  1939. }
  1940. #endif //!UNDER_CE
  1941. HBRUSH CreateDIBPatternBrush(const void* lpPackedDIB, UINT nUsage)
  1942. {
  1943. ATLASSERT(m_hBrush == NULL);
  1944. m_hBrush = ::CreateDIBPatternBrushPt(lpPackedDIB, nUsage);
  1945. return m_hBrush;
  1946. }
  1947. HBRUSH CreateSysColorBrush(int nIndex)
  1948. {
  1949. ATLASSERT(m_hBrush == NULL);
  1950. m_hBrush = ::GetSysColorBrush(nIndex);
  1951. return m_hBrush;
  1952. }
  1953. BOOL DeleteObject()
  1954. {
  1955. ATLASSERT(m_hBrush != NULL);
  1956. BOOL bRet = ::DeleteObject(m_hBrush);
  1957. if(bRet)
  1958. m_hBrush = NULL;
  1959. return bRet;
  1960. }
  1961. // Attributes
  1962. int GetLogBrush(LOGBRUSH* pLogBrush)
  1963. {
  1964. ATLASSERT(m_hBrush != NULL);
  1965. return ::GetObject(m_hBrush, sizeof(LOGBRUSH), pLogBrush);
  1966. }
  1967. };
  1968. /////////////////////////////////////////////////////////////////////////////
  1969. // CFont
  1970. class CFont
  1971. {
  1972. public:
  1973. HFONT m_hFont;
  1974. CFont(HFONT hFont = NULL) : m_hFont(hFont)
  1975. { }
  1976. ~CFont()
  1977. {
  1978. if(m_hFont != NULL)
  1979. DeleteObject();
  1980. }
  1981. CFont& operator=(HFONT hFont)
  1982. {
  1983. m_hFont = hFont;
  1984. return *this;
  1985. }
  1986. void Attach(HFONT hFont)
  1987. {
  1988. m_hFont = hFont;
  1989. }
  1990. HFONT Detach()
  1991. {
  1992. HFONT hFont = m_hFont;
  1993. m_hFont = NULL;
  1994. return hFont;
  1995. }
  1996. operator HFONT() const { return m_hFont; }
  1997. HFONT CreateFontIndirect(const LOGFONT* lpLogFont)
  1998. {
  1999. ATLASSERT(m_hFont == NULL);
  2000. m_hFont = ::CreateFontIndirect(lpLogFont);
  2001. return m_hFont;
  2002. }
  2003. HFONT CreateFont(int nHeight, int nWidth, int nEscapement,
  2004. int nOrientation, int nWeight, BYTE bItalic, BYTE bUnderline,
  2005. BYTE cStrikeOut, BYTE nCharSet, BYTE nOutPrecision,
  2006. BYTE nClipPrecision, BYTE nQuality, BYTE nPitchAndFamily,
  2007. LPCTSTR lpszFacename)
  2008. {
  2009. ATLASSERT(m_hFont == NULL);
  2010. #ifndef UNDER_CE
  2011. m_hFont = ::CreateFont(nHeight, nWidth, nEscapement,
  2012. #else // CE specific
  2013. m_hFont = CreateFont(nHeight, nWidth, nEscapement,
  2014. #endif //!UNDER_CE
  2015. nOrientation, nWeight, bItalic, bUnderline, cStrikeOut,
  2016. nCharSet, nOutPrecision, nClipPrecision, nQuality,
  2017. nPitchAndFamily, lpszFacename);
  2018. return m_hFont;
  2019. }
  2020. #ifndef UNDER_CE
  2021. HFONT CreatePointFont(int nPointSize, LPCTSTR lpszFaceName, HDC hDC = NULL)
  2022. {
  2023. LOGFONT logFont;
  2024. memset(&logFont, 0, sizeof(LOGFONT));
  2025. logFont.lfCharSet = DEFAULT_CHARSET;
  2026. logFont.lfHeight = nPointSize;
  2027. lstrcpyn(logFont.lfFaceName, lpszFaceName, sizeof(logFont.lfFaceName)/sizeof(TCHAR));
  2028. return CreatePointFontIndirect(&logFont, hDC);
  2029. }
  2030. HFONT CreatePointFontIndirect(const LOGFONT* lpLogFont, HDC hDC = NULL)
  2031. {
  2032. HDC hDC1 = (hDC != NULL) ? hDC : (::GetDC(NULL));
  2033. // convert nPointSize to logical units based on hDC
  2034. LOGFONT logFont = *lpLogFont;
  2035. POINT pt;
  2036. pt.y = ::GetDeviceCaps(hDC1, LOGPIXELSY) * logFont.lfHeight;
  2037. pt.y /= 720; // 72 points/inch, 10 decipoints/point
  2038. ::DPtoLP(hDC1, &pt, 1);
  2039. POINT ptOrg = { 0, 0 };
  2040. ::DPtoLP(hDC1, &ptOrg, 1);
  2041. logFont.lfHeight = -abs(pt.y - ptOrg.y);
  2042. if(hDC == NULL)
  2043. ::ReleaseDC(NULL, hDC1);
  2044. return CreateFontIndirect(&logFont);
  2045. }
  2046. #endif //!UNDER_CE
  2047. BOOL DeleteObject()
  2048. {
  2049. ATLASSERT(m_hFont != NULL);
  2050. BOOL bRet = ::DeleteObject(m_hFont);
  2051. if(bRet)
  2052. m_hFont = NULL;
  2053. return bRet;
  2054. }
  2055. // Attributes
  2056. int GetLogFont(LOGFONT* pLogFont)
  2057. {
  2058. ATLASSERT(m_hFont != NULL);
  2059. return ::GetObject(m_hFont, sizeof(LOGFONT), pLogFont);
  2060. }
  2061. };
  2062. /////////////////////////////////////////////////////////////////////////////
  2063. // CBitmap
  2064. class CBitmap
  2065. {
  2066. public:
  2067. HBITMAP m_hBitmap;
  2068. CBitmap(HBITMAP hBitmap = NULL) : m_hBitmap(hBitmap)
  2069. { }
  2070. ~CBitmap()
  2071. {
  2072. if(m_hBitmap != NULL)
  2073. DeleteObject();
  2074. }
  2075. CBitmap& operator=(HBITMAP hBitmap)
  2076. {
  2077. m_hBitmap = hBitmap;
  2078. return *this;
  2079. }
  2080. void Attach(HBITMAP hBitmap)
  2081. {
  2082. m_hBitmap = hBitmap;
  2083. }
  2084. HBITMAP Detach()
  2085. {
  2086. HBITMAP hBitmap = m_hBitmap;
  2087. m_hBitmap = NULL;
  2088. return hBitmap;
  2089. }
  2090. operator HBITMAP() const { return m_hBitmap; }
  2091. HBITMAP LoadBitmap(LPCTSTR lpszResourceName)
  2092. {
  2093. ATLASSERT(m_hBitmap == NULL);
  2094. m_hBitmap = ::LoadBitmap(_Module.GetResourceInstance(), lpszResourceName);
  2095. return m_hBitmap;
  2096. }
  2097. HBITMAP LoadBitmap(UINT nIDResource)
  2098. {
  2099. ATLASSERT(m_hBitmap == NULL);
  2100. m_hBitmap = ::LoadBitmap(_Module.GetResourceInstance(), MAKEINTRESOURCE(nIDResource));
  2101. return m_hBitmap;
  2102. }
  2103. HBITMAP LoadOEMBitmap(UINT nIDBitmap) // for OBM_/OCR_/OIC_
  2104. {
  2105. ATLASSERT(m_hBitmap == NULL);
  2106. m_hBitmap = ::LoadBitmap(NULL, MAKEINTRESOURCE(nIDBitmap));
  2107. return m_hBitmap;
  2108. }
  2109. HBITMAP LoadMappedBitmap(UINT nIDBitmap, UINT nFlags = 0, LPCOLORMAP lpColorMap = NULL, int nMapSize = 0)
  2110. {
  2111. ATLASSERT(m_hBitmap == NULL);
  2112. m_hBitmap = ::CreateMappedBitmap(_Module.GetResourceInstance(), nIDBitmap, (WORD)nFlags, lpColorMap, nMapSize);
  2113. return m_hBitmap;
  2114. }
  2115. HBITMAP CreateBitmap(int nWidth, int nHeight, UINT nPlanes, UINT nBitcount, const void* lpBits)
  2116. {
  2117. ATLASSERT(m_hBitmap == NULL);
  2118. m_hBitmap = ::CreateBitmap(nWidth, nHeight, nPlanes, nBitcount, lpBits);
  2119. return m_hBitmap;
  2120. }
  2121. #ifndef UNDER_CE
  2122. HBITMAP CreateBitmapIndirect(LPBITMAP lpBitmap)
  2123. {
  2124. ATLASSERT(m_hBitmap == NULL);
  2125. m_hBitmap = ::CreateBitmapIndirect(lpBitmap);
  2126. return m_hBitmap;
  2127. }
  2128. #endif //!UNDER_CE
  2129. HBITMAP CreateCompatibleBitmap(HDC hDC, int nWidth, int nHeight)
  2130. {
  2131. ATLASSERT(m_hBitmap == NULL);
  2132. m_hBitmap = ::CreateCompatibleBitmap(hDC, nWidth, nHeight);
  2133. return m_hBitmap;
  2134. }
  2135. #ifndef UNDER_CE
  2136. HBITMAP CreateDiscardableBitmap(HDC hDC, int nWidth, int nHeight)
  2137. {
  2138. ATLASSERT(m_hBitmap == NULL);
  2139. m_hBitmap = ::CreateDiscardableBitmap(hDC, nWidth, nHeight);
  2140. return m_hBitmap;
  2141. }
  2142. #endif //!UNDER_CE
  2143. BOOL DeleteObject()
  2144. {
  2145. ATLASSERT(m_hBitmap != NULL);
  2146. BOOL bRet = ::DeleteObject(m_hBitmap);
  2147. if(bRet)
  2148. m_hBitmap = NULL;
  2149. return bRet;
  2150. }
  2151. // Attributes
  2152. int GetBitmap(BITMAP* pBitMap)
  2153. {
  2154. ATLASSERT(m_hBitmap != NULL);
  2155. return ::GetObject(m_hBitmap, sizeof(BITMAP), pBitMap);
  2156. }
  2157. // Operations
  2158. #ifndef UNDER_CE
  2159. //REVIEW
  2160. DWORD SetBitmapBits(DWORD dwCount, const void* lpBits)
  2161. {
  2162. ATLASSERT(m_hBitmap != NULL);
  2163. return ::SetBitmapBits(m_hBitmap, dwCount, lpBits);
  2164. }
  2165. DWORD GetBitmapBits(DWORD dwCount, LPVOID lpBits) const
  2166. {
  2167. ATLASSERT(m_hBitmap != NULL);
  2168. return ::GetBitmapBits(m_hBitmap, dwCount, lpBits);
  2169. }
  2170. BOOL SetBitmapDimension(int nWidth, int nHeight, LPSIZE lpSize = NULL)
  2171. {
  2172. ATLASSERT(m_hBitmap != NULL);
  2173. return ::SetBitmapDimensionEx(m_hBitmap, nWidth, nHeight, lpSize);
  2174. }
  2175. BOOL GetBitmapDimension(LPSIZE lpSize) const
  2176. {
  2177. ATLASSERT(m_hBitmap != NULL);
  2178. return ::GetBitmapDimensionEx(m_hBitmap, lpSize);
  2179. }
  2180. #endif //!UNDER_CE
  2181. };
  2182. /////////////////////////////////////////////////////////////////////////////
  2183. // CPalette
  2184. class CPalette
  2185. {
  2186. public:
  2187. HPALETTE m_hPalette;
  2188. CPalette(HPALETTE hPalette = NULL) : m_hPalette(hPalette)
  2189. { }
  2190. ~CPalette()
  2191. {
  2192. if(m_hPalette != NULL)
  2193. DeleteObject();
  2194. }
  2195. CPalette& operator=(HPALETTE hPalette)
  2196. {
  2197. m_hPalette = hPalette;
  2198. return *this;
  2199. }
  2200. void Attach(HPALETTE hPalette)
  2201. {
  2202. m_hPalette = hPalette;
  2203. }
  2204. HPALETTE Detach()
  2205. {
  2206. HPALETTE hPalette = m_hPalette;
  2207. m_hPalette = NULL;
  2208. return hPalette;
  2209. }
  2210. operator HPALETTE() const { return m_hPalette; }
  2211. HPALETTE CreatePalette(LPLOGPALETTE lpLogPalette)
  2212. {
  2213. ATLASSERT(m_hPalette == NULL);
  2214. m_hPalette = ::CreatePalette(lpLogPalette);
  2215. return m_hPalette;
  2216. }
  2217. #ifndef UNDER_CE
  2218. HPALETTE CreateHalftonePalette(HDC hDC)
  2219. {
  2220. ATLASSERT(m_hPalette == NULL);
  2221. ATLASSERT(hDC != NULL);
  2222. m_hPalette = ::CreateHalftonePalette(hDC);
  2223. return m_hPalette;
  2224. }
  2225. #endif //!UNDER_CE
  2226. BOOL DeleteObject()
  2227. {
  2228. ATLASSERT(m_hPalette != NULL);
  2229. BOOL bRet = ::DeleteObject(m_hPalette);
  2230. if(bRet)
  2231. m_hPalette = NULL;
  2232. return bRet;
  2233. }
  2234. // Attributes
  2235. int GetEntryCount()
  2236. {
  2237. ATLASSERT(m_hPalette != NULL);
  2238. WORD nEntries;
  2239. ::GetObject(m_hPalette, sizeof(WORD), &nEntries);
  2240. return (int)nEntries;
  2241. }
  2242. UINT GetPaletteEntries(UINT nStartIndex, UINT nNumEntries, LPPALETTEENTRY lpPaletteColors) const
  2243. {
  2244. ATLASSERT(m_hPalette != NULL);
  2245. return ::GetPaletteEntries(m_hPalette, nStartIndex, nNumEntries, lpPaletteColors);
  2246. }
  2247. UINT SetPaletteEntries(UINT nStartIndex, UINT nNumEntries, LPPALETTEENTRY lpPaletteColors)
  2248. {
  2249. ATLASSERT(m_hPalette != NULL);
  2250. return ::SetPaletteEntries(m_hPalette, nStartIndex, nNumEntries, lpPaletteColors);
  2251. }
  2252. // Operations
  2253. #ifndef UNDER_CE
  2254. void AnimatePalette(UINT nStartIndex, UINT nNumEntries, LPPALETTEENTRY lpPaletteColors)
  2255. {
  2256. ATLASSERT(m_hPalette != NULL);
  2257. ::AnimatePalette(m_hPalette, nStartIndex, nNumEntries, lpPaletteColors);
  2258. }
  2259. BOOL ResizePalette(UINT nNumEntries)
  2260. {
  2261. ATLASSERT(m_hPalette != NULL);
  2262. return ::ResizePalette(m_hPalette, nNumEntries);
  2263. }
  2264. #endif //!UNDER_CE
  2265. UINT GetNearestPaletteIndex(COLORREF crColor) const
  2266. {
  2267. ATLASSERT(m_hPalette != NULL);
  2268. return ::GetNearestPaletteIndex(m_hPalette, crColor);
  2269. }
  2270. };
  2271. /////////////////////////////////////////////////////////////////////////////
  2272. // CRgn
  2273. class CRgn
  2274. {
  2275. public:
  2276. HRGN m_hRgn;
  2277. CRgn(HRGN hRgn = NULL) : m_hRgn(hRgn)
  2278. { }
  2279. ~CRgn()
  2280. {
  2281. if(m_hRgn != NULL)
  2282. DeleteObject();
  2283. }
  2284. CRgn& operator=(HRGN hRgn)
  2285. {
  2286. m_hRgn = hRgn;
  2287. return *this;
  2288. }
  2289. void Attach(HRGN hRgn)
  2290. {
  2291. m_hRgn = hRgn;
  2292. }
  2293. HRGN Detach()
  2294. {
  2295. HRGN hRgn = m_hRgn;
  2296. m_hRgn = NULL;
  2297. return hRgn;
  2298. }
  2299. operator HRGN() const { return m_hRgn; }
  2300. HRGN CreateRectRgn(int x1, int y1, int x2, int y2)
  2301. {
  2302. ATLASSERT(m_hRgn == NULL);
  2303. m_hRgn = ::CreateRectRgn(x1, y1, x2, y2);
  2304. return m_hRgn;
  2305. }
  2306. HRGN CreateRectRgnIndirect(LPCRECT lpRect)
  2307. {
  2308. ATLASSERT(m_hRgn == NULL);
  2309. m_hRgn = ::CreateRectRgnIndirect(lpRect);
  2310. return m_hRgn;
  2311. }
  2312. #ifndef UNDER_CE
  2313. HRGN CreateEllipticRgn(int x1, int y1, int x2, int y2)
  2314. {
  2315. ATLASSERT(m_hRgn == NULL);
  2316. m_hRgn = ::CreateEllipticRgn(x1, y1, x2, y2);
  2317. return m_hRgn;
  2318. }
  2319. HRGN CreateEllipticRgnIndirect(LPCRECT lpRect)
  2320. {
  2321. ATLASSERT(m_hRgn == NULL);
  2322. m_hRgn = ::CreateEllipticRgnIndirect(lpRect);
  2323. return m_hRgn;
  2324. }
  2325. HRGN CreatePolygonRgn(LPPOINT lpPoints, int nCount, int nMode)
  2326. {
  2327. ATLASSERT(m_hRgn == NULL);
  2328. m_hRgn = ::CreatePolygonRgn(lpPoints, nCount, nMode);
  2329. return m_hRgn;
  2330. }
  2331. HRGN CreatePolyPolygonRgn(LPPOINT lpPoints, LPINT lpPolyCounts, int nCount, int nPolyFillMode)
  2332. {
  2333. ATLASSERT(m_hRgn == NULL);
  2334. m_hRgn = ::CreatePolyPolygonRgn(lpPoints, lpPolyCounts, nCount, nPolyFillMode);
  2335. return m_hRgn;
  2336. }
  2337. HRGN CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3)
  2338. {
  2339. ATLASSERT(m_hRgn == NULL);
  2340. m_hRgn = ::CreateRoundRectRgn(x1, y1, x2, y2, x3, y3);
  2341. return m_hRgn;
  2342. }
  2343. HRGN CreateFromPath(HDC hDC)
  2344. {
  2345. ATLASSERT(m_hRgn == NULL);
  2346. ATLASSERT(hDC != NULL);
  2347. m_hRgn = ::PathToRegion(hDC);
  2348. return m_hRgn;
  2349. }
  2350. HRGN CreateFromData(const XFORM* lpXForm, int nCount, const RGNDATA* pRgnData)
  2351. {
  2352. ATLASSERT(m_hRgn == NULL);
  2353. m_hRgn = ::ExtCreateRegion(lpXForm, nCount, pRgnData);
  2354. return m_hRgn;
  2355. }
  2356. #endif //!UNDER_CE
  2357. BOOL DeleteObject()
  2358. {
  2359. ATLASSERT(m_hRgn != NULL);
  2360. BOOL bRet = ::DeleteObject(m_hRgn);
  2361. if(bRet)
  2362. m_hRgn = NULL;
  2363. return bRet;
  2364. }
  2365. // Operations
  2366. void SetRectRgn(int x1, int y1, int x2, int y2)
  2367. {
  2368. ATLASSERT(m_hRgn != NULL);
  2369. ::SetRectRgn(m_hRgn, x1, y1, x2, y2);
  2370. }
  2371. void SetRectRgn(LPCRECT lpRect)
  2372. {
  2373. ATLASSERT(m_hRgn != NULL);
  2374. ::SetRectRgn(m_hRgn, lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
  2375. }
  2376. int CombineRgn(HRGN hRgnSrc1, HRGN hRgnSrc2, int nCombineMode)
  2377. {
  2378. ATLASSERT(m_hRgn != NULL);
  2379. return ::CombineRgn(m_hRgn, hRgnSrc1, hRgnSrc2, nCombineMode);
  2380. }
  2381. int CombineRgn(HRGN hRgnSrc, int nCombineMode)
  2382. {
  2383. ATLASSERT(m_hRgn != NULL);
  2384. return ::CombineRgn(m_hRgn, m_hRgn, hRgnSrc, nCombineMode);
  2385. }
  2386. int CopyRgn(HRGN hRgnSrc)
  2387. {
  2388. ATLASSERT(m_hRgn != NULL);
  2389. return ::CombineRgn(m_hRgn, hRgnSrc, NULL, RGN_COPY);
  2390. }
  2391. BOOL EqualRgn(HRGN hRgn) const
  2392. {
  2393. ATLASSERT(m_hRgn != NULL);
  2394. return ::EqualRgn(m_hRgn, hRgn);
  2395. }
  2396. int OffsetRgn(int x, int y)
  2397. {
  2398. ATLASSERT(m_hRgn != NULL);
  2399. return ::OffsetRgn(m_hRgn, x, y);
  2400. }
  2401. int OffsetRgn(POINT point)
  2402. {
  2403. ATLASSERT(m_hRgn != NULL);
  2404. return ::OffsetRgn(m_hRgn, point.x, point.y);
  2405. }
  2406. int GetRgnBox(LPRECT lpRect) const
  2407. {
  2408. ATLASSERT(m_hRgn != NULL);
  2409. return ::GetRgnBox(m_hRgn, lpRect);
  2410. }
  2411. BOOL PtInRegion(int x, int y) const
  2412. {
  2413. ATLASSERT(m_hRgn != NULL);
  2414. return ::PtInRegion(m_hRgn, x, y);
  2415. }
  2416. BOOL PtInRegion(POINT point) const
  2417. {
  2418. ATLASSERT(m_hRgn != NULL);
  2419. return ::PtInRegion(m_hRgn, point.x, point.y);
  2420. }
  2421. BOOL RectInRegion(LPCRECT lpRect) const
  2422. {
  2423. ATLASSERT(m_hRgn != NULL);
  2424. return ::RectInRegion(m_hRgn, lpRect);
  2425. }
  2426. int GetRegionData(LPRGNDATA lpRgnData, int nDataSize) const
  2427. {
  2428. ATLASSERT(m_hRgn != NULL);
  2429. return (int)::GetRegionData(m_hRgn, nDataSize, lpRgnData);
  2430. }
  2431. };
  2432. }; //namespace ATL
  2433. #endif // __ATLGDI_H__