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.

2617 lines
66 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. HGDIOBJ SelectRgn(HRGN hRgn) // special return for regions
  280. {
  281. ATLASSERT(m_hDC != NULL);
  282. ATLASSERT(::GetObjectType(hRgn) == OBJ_REGION);
  283. return ::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 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. //REVIEW
  1673. BOOL AppendMenu(UINT nFlags, UINT nIDNewItem, HBITMAP hBmp)
  1674. {
  1675. ATLASSERT(::IsMenu(m_hMenu));
  1676. return ::AppendMenu(m_hMenu, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
  1677. }
  1678. #endif //!UNDER_CE
  1679. UINT CheckMenuItem(UINT nIDCheckItem, UINT nCheck)
  1680. {
  1681. ATLASSERT(::IsMenu(m_hMenu));
  1682. return (UINT)::CheckMenuItem(m_hMenu, nIDCheckItem, nCheck);
  1683. }
  1684. UINT EnableMenuItem(UINT nIDEnableItem, UINT nEnable)
  1685. {
  1686. ATLASSERT(::IsMenu(m_hMenu));
  1687. return ::EnableMenuItem(m_hMenu, nIDEnableItem, nEnable);
  1688. }
  1689. #ifndef UNDER_CE
  1690. UINT GetMenuItemCount() const
  1691. {
  1692. ATLASSERT(::IsMenu(m_hMenu));
  1693. return ::GetMenuItemCount(m_hMenu);
  1694. }
  1695. UINT GetMenuItemID(int nPos) const
  1696. {
  1697. ATLASSERT(::IsMenu(m_hMenu));
  1698. return ::GetMenuItemID(m_hMenu, nPos);
  1699. }
  1700. UINT GetMenuState(UINT nID, UINT nFlags) const
  1701. {
  1702. ATLASSERT(::IsMenu(m_hMenu));
  1703. return ::GetMenuState(m_hMenu, nID, nFlags);
  1704. }
  1705. int GetMenuString(UINT nIDItem, LPTSTR lpString, int nMaxCount, UINT nFlags) const
  1706. {
  1707. ATLASSERT(::IsMenu(m_hMenu));
  1708. return ::GetMenuString(m_hMenu, nIDItem, lpString, nMaxCount, nFlags);
  1709. }
  1710. int GetMenuStringLen(UINT nIDItem, UINT nFlags) const
  1711. {
  1712. ATLASSERT(::IsMenu(m_hMenu));
  1713. return ::GetMenuString(m_hMenu, nIDItem, NULL, 0, nFlags);
  1714. }
  1715. #ifndef _ATL_NO_COM
  1716. BOOL GetMenuString(UINT nIDItem, BSTR& bstrText, UINT nFlags) const
  1717. {
  1718. USES_CONVERSION;
  1719. ATLASSERT(::IsMenu(m_hMenu));
  1720. ATLASSERT(bstrText == NULL);
  1721. int nLen = GetMenuStringLen(nIDItem, nFlags);
  1722. {
  1723. bstrText = ::SysAllocString(OLESTR(""));
  1724. return (bstrText != NULL) ? TRUE : FALSE;
  1725. }
  1726. LPTSTR lpszText = (LPTSTR)_alloca((nLen + 1) * sizeof(TCHAR));
  1727. if(!GetMenuString(nIDItem, lpszText, nLen, nFlags))
  1728. return FALSE;
  1729. bstrText = ::SysAllocString(T2OLE(lpszText));
  1730. return (bstrText != NULL) ? TRUE : FALSE;
  1731. }
  1732. #endif //!_ATL_NO_COM
  1733. #endif //!UNDER_CE
  1734. HMENU GetSubMenu(int nPos) const
  1735. {
  1736. ATLASSERT(::IsMenu(m_hMenu));
  1737. return ::GetSubMenu(m_hMenu, nPos);
  1738. }
  1739. #ifndef UNDER_CE
  1740. //REVIEW
  1741. BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
  1742. {
  1743. ATLASSERT(::IsMenu(m_hMenu));
  1744. return ::InsertMenu(m_hMenu, nPosition, nFlags, nIDNewItem, lpszNewItem);
  1745. }
  1746. BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem, HBITMAP hBmp)
  1747. {
  1748. ATLASSERT(::IsMenu(m_hMenu));
  1749. return ::InsertMenu(m_hMenu, nPosition, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
  1750. }
  1751. #endif //!UNDER_CE
  1752. #ifndef UNDER_CE
  1753. BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
  1754. {
  1755. ATLASSERT(::IsMenu(m_hMenu));
  1756. return ::ModifyMenu(m_hMenu, nPosition, nFlags, nIDNewItem, lpszNewItem);
  1757. }
  1758. BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem, HBITMAP hBmp)
  1759. {
  1760. ATLASSERT(::IsMenu(m_hMenu));
  1761. return ::ModifyMenu(m_hMenu, nPosition, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
  1762. }
  1763. #endif //!UNDER_CE
  1764. BOOL RemoveMenu(UINT nPosition, UINT nFlags)
  1765. {
  1766. ATLASSERT(::IsMenu(m_hMenu));
  1767. return ::RemoveMenu(m_hMenu, nPosition, nFlags);
  1768. }
  1769. #ifndef UNDER_CE
  1770. BOOL SetMenuItemBitmaps(UINT nPosition, UINT nFlags, HBITMAP hBmpUnchecked, HBITMAP hBmpChecked)
  1771. {
  1772. ATLASSERT(::IsMenu(m_hMenu));
  1773. return ::SetMenuItemBitmaps(m_hMenu, nPosition, nFlags, hBmpUnchecked, hBmpChecked);
  1774. }
  1775. #endif //!UNDER_CE
  1776. BOOL CheckMenuRadioItem(UINT nIDFirst, UINT nIDLast, UINT nIDItem, UINT nFlags)
  1777. {
  1778. ATLASSERT(::IsMenu(m_hMenu));
  1779. return ::CheckMenuRadioItem(m_hMenu, nIDFirst, nIDLast, nIDItem, nFlags);
  1780. }
  1781. BOOL GetMenuItemInfo(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii)
  1782. {
  1783. ATLASSERT(::IsMenu(m_hMenu));
  1784. return (BOOL)::GetMenuItemInfo(m_hMenu, uItem, bByPosition, lpmii);
  1785. }
  1786. BOOL SetMenuItemInfo(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii)
  1787. {
  1788. ATLASSERT(::IsMenu(m_hMenu));
  1789. return (BOOL)::SetMenuItemInfo(m_hMenu, uItem, bByPosition, lpmii);
  1790. }
  1791. // Context Help Functions
  1792. #ifndef UNDER_CE
  1793. BOOL SetMenuContextHelpId(DWORD dwContextHelpId)
  1794. {
  1795. ATLASSERT(::IsMenu(m_hMenu));
  1796. return ::SetMenuContextHelpId(m_hMenu, dwContextHelpId);
  1797. }
  1798. DWORD GetMenuContextHelpId() const
  1799. {
  1800. ATLASSERT(::IsMenu(m_hMenu));
  1801. return ::GetMenuContextHelpId(m_hMenu);
  1802. }
  1803. #endif //!UNDER_CE
  1804. };
  1805. /////////////////////////////////////////////////////////////////////////////
  1806. // CPen
  1807. class CPen
  1808. {
  1809. public:
  1810. HPEN m_hPen;
  1811. CPen(HPEN hPen = NULL) : m_hPen(hPen)
  1812. { }
  1813. ~CPen()
  1814. {
  1815. if(m_hPen != NULL)
  1816. DeleteObject();
  1817. }
  1818. CPen& operator=(HPEN hPen)
  1819. {
  1820. m_hPen = hPen;
  1821. return *this;
  1822. }
  1823. void Attach(HPEN hPen)
  1824. {
  1825. m_hPen = hPen;
  1826. }
  1827. HPEN Detach()
  1828. {
  1829. HPEN hPen = m_hPen;
  1830. m_hPen = NULL;
  1831. return hPen;
  1832. }
  1833. operator HPEN() const { return m_hPen; }
  1834. HPEN CreatePen(int nPenStyle, int nWidth, COLORREF crColor)
  1835. {
  1836. ATLASSERT(m_hPen == NULL);
  1837. m_hPen = ::CreatePen(nPenStyle, nWidth, crColor);
  1838. return m_hPen;
  1839. }
  1840. #ifndef UNDER_CE
  1841. HPEN CreatePen(int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL)
  1842. {
  1843. ATLASSERT(m_hPen == NULL);
  1844. m_hPen = ::ExtCreatePen(nPenStyle, nWidth, pLogBrush, nStyleCount, lpStyle);
  1845. return m_hPen;
  1846. }
  1847. #endif //!UNDER_CE
  1848. HPEN CreatePenIndirect(LPLOGPEN lpLogPen)
  1849. {
  1850. ATLASSERT(m_hPen == NULL);
  1851. m_hPen = ::CreatePenIndirect(lpLogPen);
  1852. return m_hPen;
  1853. }
  1854. BOOL DeleteObject()
  1855. {
  1856. ATLASSERT(m_hPen != NULL);
  1857. BOOL bRet = ::DeleteObject(m_hPen);
  1858. if(bRet)
  1859. m_hPen = NULL;
  1860. return bRet;
  1861. }
  1862. // Attributes
  1863. int GetLogPen(LOGPEN* pLogPen)
  1864. {
  1865. ATLASSERT(m_hPen != NULL);
  1866. return ::GetObject(m_hPen, sizeof(LOGPEN), pLogPen);
  1867. }
  1868. #ifndef UNDER_CE
  1869. int GetExtLogPen(EXTLOGPEN* pLogPen)
  1870. {
  1871. ATLASSERT(m_hPen != NULL);
  1872. return ::GetObject(m_hPen, sizeof(EXTLOGPEN), pLogPen);
  1873. }
  1874. #endif //!UNDER_CE
  1875. };
  1876. /////////////////////////////////////////////////////////////////////////////
  1877. // CBrush
  1878. class CBrush
  1879. {
  1880. public:
  1881. HBRUSH m_hBrush;
  1882. CBrush(HBRUSH hBrush = NULL) : m_hBrush(hBrush)
  1883. { }
  1884. ~CBrush()
  1885. {
  1886. if(m_hBrush != NULL)
  1887. DeleteObject();
  1888. }
  1889. CBrush& operator=(HBRUSH hBrush)
  1890. {
  1891. m_hBrush = hBrush;
  1892. return *this;
  1893. }
  1894. void Attach(HBRUSH hBrush)
  1895. {
  1896. m_hBrush = hBrush;
  1897. }
  1898. HBRUSH Detach()
  1899. {
  1900. HBRUSH hBrush = m_hBrush;
  1901. m_hBrush = NULL;
  1902. return hBrush;
  1903. }
  1904. operator HBRUSH() const { return m_hBrush; }
  1905. HBRUSH CreateSolidBrush(COLORREF crColor)
  1906. {
  1907. ATLASSERT(m_hBrush == NULL);
  1908. m_hBrush = ::CreateSolidBrush(crColor);
  1909. return m_hBrush;
  1910. }
  1911. #ifndef UNDER_CE
  1912. HBRUSH CreateHatchBrush(int nIndex, COLORREF crColor)
  1913. {
  1914. ATLASSERT(m_hBrush == NULL);
  1915. m_hBrush = ::CreateHatchBrush(nIndex, crColor);
  1916. return m_hBrush;
  1917. }
  1918. HBRUSH CreateBrushIndirect(const LOGBRUSH* lpLogBrush)
  1919. {
  1920. ATLASSERT(m_hBrush == NULL);
  1921. m_hBrush = ::CreateBrushIndirect(lpLogBrush);
  1922. return m_hBrush;
  1923. }
  1924. #endif //!UNDER_CE
  1925. HBRUSH CreatePatternBrush(HBITMAP hBitmap)
  1926. {
  1927. ATLASSERT(m_hBrush == NULL);
  1928. m_hBrush = ::CreatePatternBrush(hBitmap);
  1929. return m_hBrush;
  1930. }
  1931. #ifndef UNDER_CE
  1932. //REVIEW
  1933. HBRUSH CreateDIBPatternBrush(HGLOBAL hPackedDIB, UINT nUsage)
  1934. {
  1935. ATLASSERT(hPackedDIB != NULL);
  1936. const void* lpPackedDIB = ::GlobalLock(hPackedDIB);
  1937. ATLASSERT(lpPackedDIB != NULL);
  1938. m_hBrush = ::CreateDIBPatternBrushPt(lpPackedDIB, nUsage);
  1939. ::GlobalUnlock(hPackedDIB);
  1940. return m_hBrush;
  1941. }
  1942. #endif //!UNDER_CE
  1943. HBRUSH CreateDIBPatternBrush(const void* lpPackedDIB, UINT nUsage)
  1944. {
  1945. ATLASSERT(m_hBrush == NULL);
  1946. m_hBrush = ::CreateDIBPatternBrushPt(lpPackedDIB, nUsage);
  1947. return m_hBrush;
  1948. }
  1949. HBRUSH CreateSysColorBrush(int nIndex)
  1950. {
  1951. ATLASSERT(m_hBrush == NULL);
  1952. m_hBrush = ::GetSysColorBrush(nIndex);
  1953. return m_hBrush;
  1954. }
  1955. BOOL DeleteObject()
  1956. {
  1957. ATLASSERT(m_hBrush != NULL);
  1958. BOOL bRet = ::DeleteObject(m_hBrush);
  1959. if(bRet)
  1960. m_hBrush = NULL;
  1961. return bRet;
  1962. }
  1963. // Attributes
  1964. int GetLogBrush(LOGBRUSH* pLogBrush)
  1965. {
  1966. ATLASSERT(m_hBrush != NULL);
  1967. return ::GetObject(m_hBrush, sizeof(LOGBRUSH), pLogBrush);
  1968. }
  1969. };
  1970. /////////////////////////////////////////////////////////////////////////////
  1971. // CFont
  1972. class CFont
  1973. {
  1974. public:
  1975. HFONT m_hFont;
  1976. CFont(HFONT hFont = NULL) : m_hFont(hFont)
  1977. { }
  1978. ~CFont()
  1979. {
  1980. if(m_hFont != NULL)
  1981. DeleteObject();
  1982. }
  1983. CFont& operator=(HFONT hFont)
  1984. {
  1985. m_hFont = hFont;
  1986. return *this;
  1987. }
  1988. void Attach(HFONT hFont)
  1989. {
  1990. m_hFont = hFont;
  1991. }
  1992. HFONT Detach()
  1993. {
  1994. HFONT hFont = m_hFont;
  1995. m_hFont = NULL;
  1996. return hFont;
  1997. }
  1998. operator HFONT() const { return m_hFont; }
  1999. HFONT CreateFontIndirect(const LOGFONT* lpLogFont)
  2000. {
  2001. ATLASSERT(m_hFont == NULL);
  2002. m_hFont = ::CreateFontIndirect(lpLogFont);
  2003. return m_hFont;
  2004. }
  2005. HFONT CreateFont(int nHeight, int nWidth, int nEscapement,
  2006. int nOrientation, int nWeight, BYTE bItalic, BYTE bUnderline,
  2007. BYTE cStrikeOut, BYTE nCharSet, BYTE nOutPrecision,
  2008. BYTE nClipPrecision, BYTE nQuality, BYTE nPitchAndFamily,
  2009. LPCTSTR lpszFacename)
  2010. {
  2011. ATLASSERT(m_hFont == NULL);
  2012. #ifndef UNDER_CE
  2013. m_hFont = ::CreateFont(nHeight, nWidth, nEscapement,
  2014. #else // CE specific
  2015. m_hFont = CreateFont(nHeight, nWidth, nEscapement,
  2016. #endif //!UNDER_CE
  2017. nOrientation, nWeight, bItalic, bUnderline, cStrikeOut,
  2018. nCharSet, nOutPrecision, nClipPrecision, nQuality,
  2019. nPitchAndFamily, lpszFacename);
  2020. return m_hFont;
  2021. }
  2022. #ifndef UNDER_CE
  2023. HFONT CreatePointFont(int nPointSize, LPCTSTR lpszFaceName, HDC hDC = NULL)
  2024. {
  2025. LOGFONT logFont;
  2026. memset(&logFont, 0, sizeof(LOGFONT));
  2027. logFont.lfCharSet = DEFAULT_CHARSET;
  2028. logFont.lfHeight = nPointSize;
  2029. lstrcpyn(logFont.lfFaceName, lpszFaceName, sizeof(logFont.lfFaceName)/sizeof(TCHAR));
  2030. return CreatePointFontIndirect(&logFont, hDC);
  2031. }
  2032. HFONT CreatePointFontIndirect(const LOGFONT* lpLogFont, HDC hDC = NULL)
  2033. {
  2034. HDC hDC1 = (hDC != NULL) ? hDC : (::GetDC(NULL));
  2035. // convert nPointSize to logical units based on hDC
  2036. LOGFONT logFont = *lpLogFont;
  2037. POINT pt;
  2038. pt.y = ::GetDeviceCaps(hDC1, LOGPIXELSY) * logFont.lfHeight;
  2039. pt.y /= 720; // 72 points/inch, 10 decipoints/point
  2040. ::DPtoLP(hDC1, &pt, 1);
  2041. POINT ptOrg = { 0, 0 };
  2042. ::DPtoLP(hDC1, &ptOrg, 1);
  2043. logFont.lfHeight = -abs(pt.y - ptOrg.y);
  2044. if(hDC == NULL)
  2045. ::ReleaseDC(NULL, hDC1);
  2046. return CreateFontIndirect(&logFont);
  2047. }
  2048. #endif //!UNDER_CE
  2049. BOOL DeleteObject()
  2050. {
  2051. ATLASSERT(m_hFont != NULL);
  2052. BOOL bRet = ::DeleteObject(m_hFont);
  2053. if(bRet)
  2054. m_hFont = NULL;
  2055. return bRet;
  2056. }
  2057. // Attributes
  2058. int GetLogFont(LOGFONT* pLogFont)
  2059. {
  2060. ATLASSERT(m_hFont != NULL);
  2061. return ::GetObject(m_hFont, sizeof(LOGFONT), pLogFont);
  2062. }
  2063. };
  2064. /////////////////////////////////////////////////////////////////////////////
  2065. // CBitmap
  2066. class CBitmap
  2067. {
  2068. public:
  2069. HBITMAP m_hBitmap;
  2070. CBitmap(HBITMAP hBitmap = NULL) : m_hBitmap(hBitmap)
  2071. { }
  2072. ~CBitmap()
  2073. {
  2074. if(m_hBitmap != NULL)
  2075. DeleteObject();
  2076. }
  2077. CBitmap& operator=(HBITMAP hBitmap)
  2078. {
  2079. m_hBitmap = hBitmap;
  2080. return *this;
  2081. }
  2082. void Attach(HBITMAP hBitmap)
  2083. {
  2084. m_hBitmap = hBitmap;
  2085. }
  2086. HBITMAP Detach()
  2087. {
  2088. HBITMAP hBitmap = m_hBitmap;
  2089. m_hBitmap = NULL;
  2090. return hBitmap;
  2091. }
  2092. operator HBITMAP() const { return m_hBitmap; }
  2093. HBITMAP LoadBitmap(LPCTSTR lpszResourceName)
  2094. {
  2095. ATLASSERT(m_hBitmap == NULL);
  2096. m_hBitmap = ::LoadBitmap(_Module.GetResourceInstance(), lpszResourceName);
  2097. return m_hBitmap;
  2098. }
  2099. HBITMAP LoadBitmap(UINT nIDResource)
  2100. {
  2101. ATLASSERT(m_hBitmap == NULL);
  2102. m_hBitmap = ::LoadBitmap(_Module.GetResourceInstance(), MAKEINTRESOURCE(nIDResource));
  2103. return m_hBitmap;
  2104. }
  2105. HBITMAP LoadOEMBitmap(UINT nIDBitmap) // for OBM_/OCR_/OIC_
  2106. {
  2107. ATLASSERT(m_hBitmap == NULL);
  2108. m_hBitmap = ::LoadBitmap(NULL, MAKEINTRESOURCE(nIDBitmap));
  2109. return m_hBitmap;
  2110. }
  2111. HBITMAP LoadMappedBitmap(UINT nIDBitmap, UINT nFlags = 0, LPCOLORMAP lpColorMap = NULL, int nMapSize = 0)
  2112. {
  2113. ATLASSERT(m_hBitmap == NULL);
  2114. m_hBitmap = ::CreateMappedBitmap(_Module.GetResourceInstance(), nIDBitmap, (WORD)nFlags, lpColorMap, nMapSize);
  2115. return m_hBitmap;
  2116. }
  2117. HBITMAP CreateBitmap(int nWidth, int nHeight, UINT nPlanes, UINT nBitcount, const void* lpBits)
  2118. {
  2119. ATLASSERT(m_hBitmap == NULL);
  2120. m_hBitmap = ::CreateBitmap(nWidth, nHeight, nPlanes, nBitcount, lpBits);
  2121. return m_hBitmap;
  2122. }
  2123. #ifndef UNDER_CE
  2124. HBITMAP CreateBitmapIndirect(LPBITMAP lpBitmap)
  2125. {
  2126. ATLASSERT(m_hBitmap == NULL);
  2127. m_hBitmap = ::CreateBitmapIndirect(lpBitmap);
  2128. return m_hBitmap;
  2129. }
  2130. #endif //!UNDER_CE
  2131. HBITMAP CreateCompatibleBitmap(HDC hDC, int nWidth, int nHeight)
  2132. {
  2133. ATLASSERT(m_hBitmap == NULL);
  2134. m_hBitmap = ::CreateCompatibleBitmap(hDC, nWidth, nHeight);
  2135. return m_hBitmap;
  2136. }
  2137. #ifndef UNDER_CE
  2138. HBITMAP CreateDiscardableBitmap(HDC hDC, int nWidth, int nHeight)
  2139. {
  2140. ATLASSERT(m_hBitmap == NULL);
  2141. m_hBitmap = ::CreateDiscardableBitmap(hDC, nWidth, nHeight);
  2142. return m_hBitmap;
  2143. }
  2144. #endif //!UNDER_CE
  2145. BOOL DeleteObject()
  2146. {
  2147. ATLASSERT(m_hBitmap != NULL);
  2148. BOOL bRet = ::DeleteObject(m_hBitmap);
  2149. if(bRet)
  2150. m_hBitmap = NULL;
  2151. return bRet;
  2152. }
  2153. // Attributes
  2154. int GetBitmap(BITMAP* pBitMap)
  2155. {
  2156. ATLASSERT(m_hBitmap != NULL);
  2157. return ::GetObject(m_hBitmap, sizeof(BITMAP), pBitMap);
  2158. }
  2159. // Operations
  2160. #ifndef UNDER_CE
  2161. //REVIEW
  2162. DWORD SetBitmapBits(DWORD dwCount, const void* lpBits)
  2163. {
  2164. ATLASSERT(m_hBitmap != NULL);
  2165. return ::SetBitmapBits(m_hBitmap, dwCount, lpBits);
  2166. }
  2167. DWORD GetBitmapBits(DWORD dwCount, LPVOID lpBits) const
  2168. {
  2169. ATLASSERT(m_hBitmap != NULL);
  2170. return ::GetBitmapBits(m_hBitmap, dwCount, lpBits);
  2171. }
  2172. BOOL SetBitmapDimension(int nWidth, int nHeight, LPSIZE lpSize = NULL)
  2173. {
  2174. ATLASSERT(m_hBitmap != NULL);
  2175. return ::SetBitmapDimensionEx(m_hBitmap, nWidth, nHeight, lpSize);
  2176. }
  2177. BOOL GetBitmapDimension(LPSIZE lpSize) const
  2178. {
  2179. ATLASSERT(m_hBitmap != NULL);
  2180. return ::GetBitmapDimensionEx(m_hBitmap, lpSize);
  2181. }
  2182. #endif //!UNDER_CE
  2183. };
  2184. /////////////////////////////////////////////////////////////////////////////
  2185. // CPalette
  2186. class CPalette
  2187. {
  2188. public:
  2189. HPALETTE m_hPalette;
  2190. CPalette(HPALETTE hPalette = NULL) : m_hPalette(hPalette)
  2191. { }
  2192. ~CPalette()
  2193. {
  2194. if(m_hPalette != NULL)
  2195. DeleteObject();
  2196. }
  2197. CPalette& operator=(HPALETTE hPalette)
  2198. {
  2199. m_hPalette = hPalette;
  2200. return *this;
  2201. }
  2202. void Attach(HPALETTE hPalette)
  2203. {
  2204. m_hPalette = hPalette;
  2205. }
  2206. HPALETTE Detach()
  2207. {
  2208. HPALETTE hPalette = m_hPalette;
  2209. m_hPalette = NULL;
  2210. return hPalette;
  2211. }
  2212. operator HPALETTE() const { return m_hPalette; }
  2213. HPALETTE CreatePalette(LPLOGPALETTE lpLogPalette)
  2214. {
  2215. ATLASSERT(m_hPalette == NULL);
  2216. m_hPalette = ::CreatePalette(lpLogPalette);
  2217. return m_hPalette;
  2218. }
  2219. #ifndef UNDER_CE
  2220. HPALETTE CreateHalftonePalette(HDC hDC)
  2221. {
  2222. ATLASSERT(m_hPalette == NULL);
  2223. ATLASSERT(hDC != NULL);
  2224. m_hPalette = ::CreateHalftonePalette(hDC);
  2225. return m_hPalette;
  2226. }
  2227. #endif //!UNDER_CE
  2228. BOOL DeleteObject()
  2229. {
  2230. ATLASSERT(m_hPalette != NULL);
  2231. BOOL bRet = ::DeleteObject(m_hPalette);
  2232. if(bRet)
  2233. m_hPalette = NULL;
  2234. return bRet;
  2235. }
  2236. // Attributes
  2237. int GetEntryCount()
  2238. {
  2239. ATLASSERT(m_hPalette != NULL);
  2240. WORD nEntries;
  2241. ::GetObject(m_hPalette, sizeof(WORD), &nEntries);
  2242. return (int)nEntries;
  2243. }
  2244. UINT GetPaletteEntries(UINT nStartIndex, UINT nNumEntries, LPPALETTEENTRY lpPaletteColors) const
  2245. {
  2246. ATLASSERT(m_hPalette != NULL);
  2247. return ::GetPaletteEntries(m_hPalette, nStartIndex, nNumEntries, lpPaletteColors);
  2248. }
  2249. UINT SetPaletteEntries(UINT nStartIndex, UINT nNumEntries, LPPALETTEENTRY lpPaletteColors)
  2250. {
  2251. ATLASSERT(m_hPalette != NULL);
  2252. return ::SetPaletteEntries(m_hPalette, nStartIndex, nNumEntries, lpPaletteColors);
  2253. }
  2254. // Operations
  2255. #ifndef UNDER_CE
  2256. void AnimatePalette(UINT nStartIndex, UINT nNumEntries, LPPALETTEENTRY lpPaletteColors)
  2257. {
  2258. ATLASSERT(m_hPalette != NULL);
  2259. ::AnimatePalette(m_hPalette, nStartIndex, nNumEntries, lpPaletteColors);
  2260. }
  2261. BOOL ResizePalette(UINT nNumEntries)
  2262. {
  2263. ATLASSERT(m_hPalette != NULL);
  2264. return ::ResizePalette(m_hPalette, nNumEntries);
  2265. }
  2266. #endif //!UNDER_CE
  2267. UINT GetNearestPaletteIndex(COLORREF crColor) const
  2268. {
  2269. ATLASSERT(m_hPalette != NULL);
  2270. return ::GetNearestPaletteIndex(m_hPalette, crColor);
  2271. }
  2272. };
  2273. /////////////////////////////////////////////////////////////////////////////
  2274. // CRgn
  2275. class CRgn
  2276. {
  2277. public:
  2278. HRGN m_hRgn;
  2279. CRgn(HRGN hRgn = NULL) : m_hRgn(hRgn)
  2280. { }
  2281. ~CRgn()
  2282. {
  2283. if(m_hRgn != NULL)
  2284. DeleteObject();
  2285. }
  2286. CRgn& operator=(HRGN hRgn)
  2287. {
  2288. m_hRgn = hRgn;
  2289. return *this;
  2290. }
  2291. void Attach(HRGN hRgn)
  2292. {
  2293. m_hRgn = hRgn;
  2294. }
  2295. HRGN Detach()
  2296. {
  2297. HRGN hRgn = m_hRgn;
  2298. m_hRgn = NULL;
  2299. return hRgn;
  2300. }
  2301. operator HRGN() const { return m_hRgn; }
  2302. HRGN CreateRectRgn(int x1, int y1, int x2, int y2)
  2303. {
  2304. ATLASSERT(m_hRgn == NULL);
  2305. m_hRgn = ::CreateRectRgn(x1, y1, x2, y2);
  2306. return m_hRgn;
  2307. }
  2308. HRGN CreateRectRgnIndirect(LPCRECT lpRect)
  2309. {
  2310. ATLASSERT(m_hRgn == NULL);
  2311. m_hRgn = ::CreateRectRgnIndirect(lpRect);
  2312. return m_hRgn;
  2313. }
  2314. #ifndef UNDER_CE
  2315. HRGN CreateEllipticRgn(int x1, int y1, int x2, int y2)
  2316. {
  2317. ATLASSERT(m_hRgn == NULL);
  2318. m_hRgn = ::CreateEllipticRgn(x1, y1, x2, y2);
  2319. return m_hRgn;
  2320. }
  2321. HRGN CreateEllipticRgnIndirect(LPCRECT lpRect)
  2322. {
  2323. ATLASSERT(m_hRgn == NULL);
  2324. m_hRgn = ::CreateEllipticRgnIndirect(lpRect);
  2325. return m_hRgn;
  2326. }
  2327. HRGN CreatePolygonRgn(LPPOINT lpPoints, int nCount, int nMode)
  2328. {
  2329. ATLASSERT(m_hRgn == NULL);
  2330. m_hRgn = ::CreatePolygonRgn(lpPoints, nCount, nMode);
  2331. return m_hRgn;
  2332. }
  2333. HRGN CreatePolyPolygonRgn(LPPOINT lpPoints, LPINT lpPolyCounts, int nCount, int nPolyFillMode)
  2334. {
  2335. ATLASSERT(m_hRgn == NULL);
  2336. m_hRgn = ::CreatePolyPolygonRgn(lpPoints, lpPolyCounts, nCount, nPolyFillMode);
  2337. return m_hRgn;
  2338. }
  2339. HRGN CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3)
  2340. {
  2341. ATLASSERT(m_hRgn == NULL);
  2342. m_hRgn = ::CreateRoundRectRgn(x1, y1, x2, y2, x3, y3);
  2343. return m_hRgn;
  2344. }
  2345. HRGN CreateFromPath(HDC hDC)
  2346. {
  2347. ATLASSERT(m_hRgn == NULL);
  2348. ATLASSERT(hDC != NULL);
  2349. m_hRgn = ::PathToRegion(hDC);
  2350. return m_hRgn;
  2351. }
  2352. HRGN CreateFromData(const XFORM* lpXForm, int nCount, const RGNDATA* pRgnData)
  2353. {
  2354. ATLASSERT(m_hRgn == NULL);
  2355. m_hRgn = ::ExtCreateRegion(lpXForm, nCount, pRgnData);
  2356. return m_hRgn;
  2357. }
  2358. #endif //!UNDER_CE
  2359. BOOL DeleteObject()
  2360. {
  2361. ATLASSERT(m_hRgn != NULL);
  2362. BOOL bRet = ::DeleteObject(m_hRgn);
  2363. if(bRet)
  2364. m_hRgn = NULL;
  2365. return bRet;
  2366. }
  2367. // Operations
  2368. void SetRectRgn(int x1, int y1, int x2, int y2)
  2369. {
  2370. ATLASSERT(m_hRgn != NULL);
  2371. ::SetRectRgn(m_hRgn, x1, y1, x2, y2);
  2372. }
  2373. void SetRectRgn(LPCRECT lpRect)
  2374. {
  2375. ATLASSERT(m_hRgn != NULL);
  2376. ::SetRectRgn(m_hRgn, lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
  2377. }
  2378. int CombineRgn(HRGN hRgnSrc1, HRGN hRgnSrc2, int nCombineMode)
  2379. {
  2380. ATLASSERT(m_hRgn != NULL);
  2381. return ::CombineRgn(m_hRgn, hRgnSrc1, hRgnSrc2, nCombineMode);
  2382. }
  2383. int CombineRgn(HRGN hRgnSrc, int nCombineMode)
  2384. {
  2385. ATLASSERT(m_hRgn != NULL);
  2386. return ::CombineRgn(m_hRgn, m_hRgn, hRgnSrc, nCombineMode);
  2387. }
  2388. int CopyRgn(HRGN hRgnSrc)
  2389. {
  2390. ATLASSERT(m_hRgn != NULL);
  2391. return ::CombineRgn(m_hRgn, hRgnSrc, NULL, RGN_COPY);
  2392. }
  2393. BOOL EqualRgn(HRGN hRgn) const
  2394. {
  2395. ATLASSERT(m_hRgn != NULL);
  2396. return ::EqualRgn(m_hRgn, hRgn);
  2397. }
  2398. int OffsetRgn(int x, int y)
  2399. {
  2400. ATLASSERT(m_hRgn != NULL);
  2401. return ::OffsetRgn(m_hRgn, x, y);
  2402. }
  2403. int OffsetRgn(POINT point)
  2404. {
  2405. ATLASSERT(m_hRgn != NULL);
  2406. return ::OffsetRgn(m_hRgn, point.x, point.y);
  2407. }
  2408. int GetRgnBox(LPRECT lpRect) const
  2409. {
  2410. ATLASSERT(m_hRgn != NULL);
  2411. return ::GetRgnBox(m_hRgn, lpRect);
  2412. }
  2413. BOOL PtInRegion(int x, int y) const
  2414. {
  2415. ATLASSERT(m_hRgn != NULL);
  2416. return ::PtInRegion(m_hRgn, x, y);
  2417. }
  2418. BOOL PtInRegion(POINT point) const
  2419. {
  2420. ATLASSERT(m_hRgn != NULL);
  2421. return ::PtInRegion(m_hRgn, point.x, point.y);
  2422. }
  2423. BOOL RectInRegion(LPCRECT lpRect) const
  2424. {
  2425. ATLASSERT(m_hRgn != NULL);
  2426. return ::RectInRegion(m_hRgn, lpRect);
  2427. }
  2428. int GetRegionData(LPRGNDATA lpRgnData, int nDataSize) const
  2429. {
  2430. ATLASSERT(m_hRgn != NULL);
  2431. return (int)::GetRegionData(m_hRgn, nDataSize, lpRgnData);
  2432. }
  2433. };
  2434. }; //namespace ATL
  2435. #endif // __ATLGDI_H__