Source code of Windows XP (NT5)
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.

225 lines
5.6 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998, 1999, 2000
  4. *
  5. * TITLE: SIMDC.H
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 1/19/1999
  12. *
  13. * DESCRIPTION: Simple DC class. Cleans up DCs in order to simplify code.
  14. *
  15. *******************************************************************************/
  16. #ifndef __SIMDC_H_INCLUDED
  17. #define __SIMDC_H_INCLUDED
  18. #include <windows.h>
  19. #include <uicommon.h>
  20. #include "ssmprsrc.h"
  21. extern HINSTANCE g_hInstance;
  22. class CSimpleDC
  23. {
  24. public:
  25. // Where'd we get it from?
  26. enum CContextSource
  27. {
  28. FromWindowDC,
  29. FromClientDC,
  30. FromPaintDC,
  31. FromCompatibleDC,
  32. FromCreateDC,
  33. FromNullDC
  34. };
  35. private:
  36. HDC m_hDC;
  37. HWND m_hWnd;
  38. CContextSource m_nSource;
  39. PAINTSTRUCT m_PaintStruct;
  40. HBITMAP m_hOriginalBitmap;
  41. HBRUSH m_hOriginalBrush;
  42. HFONT m_hOriginalFont;
  43. HPEN m_hOriginalPen;
  44. HPALETTE m_hOriginalPalette;
  45. private:
  46. // No implementation
  47. CSimpleDC &operator=( const CSimpleDC &other );
  48. CSimpleDC( const CSimpleDC &other );
  49. private:
  50. void SaveState(void)
  51. {
  52. if (m_hDC)
  53. {
  54. HBITMAP hTempBitmap = LoadBitmap( g_hInstance, MAKEINTRESOURCE(IDB_1) );
  55. if (hTempBitmap)
  56. {
  57. m_hOriginalBitmap = reinterpret_cast<HBITMAP>(SelectObject(m_hDC,hTempBitmap));
  58. if (m_hOriginalBitmap)
  59. {
  60. SelectObject( m_hDC, m_hOriginalBitmap );
  61. }
  62. DeleteObject(hTempBitmap);
  63. }
  64. m_hOriginalBrush = reinterpret_cast<HBRUSH>(SelectObject(m_hDC,GetStockObject(NULL_BRUSH)));
  65. m_hOriginalFont = reinterpret_cast<HFONT>(SelectObject(m_hDC,GetStockObject(SYSTEM_FONT)));
  66. m_hOriginalPen = reinterpret_cast<HPEN>(SelectObject(m_hDC,GetStockObject(NULL_PEN)));
  67. m_hOriginalPalette = reinterpret_cast<HPALETTE>(SelectPalette(m_hDC,reinterpret_cast<HPALETTE>(GetStockObject(DEFAULT_PALETTE)),TRUE));
  68. }
  69. }
  70. void RestoreState(void)
  71. {
  72. if (m_hDC)
  73. {
  74. if (m_hOriginalBitmap)
  75. SelectObject( m_hDC, m_hOriginalBitmap );
  76. if (m_hOriginalBrush)
  77. SelectObject( m_hDC, m_hOriginalBrush );
  78. if (m_hOriginalFont)
  79. SelectObject( m_hDC, m_hOriginalFont );
  80. if (m_hOriginalPen)
  81. SelectObject( m_hDC, m_hOriginalPen );
  82. if (m_hOriginalPalette)
  83. SelectPalette( m_hDC, m_hOriginalPalette, TRUE );
  84. }
  85. }
  86. public:
  87. CSimpleDC(void)
  88. : m_hDC(NULL),
  89. m_hWnd(NULL),
  90. m_nSource(FromNullDC),
  91. m_hOriginalBitmap(NULL),
  92. m_hOriginalBrush(NULL),
  93. m_hOriginalFont(NULL),
  94. m_hOriginalPen(NULL),
  95. m_hOriginalPalette(NULL)
  96. {
  97. ::ZeroMemory(&m_PaintStruct,sizeof(m_PaintStruct));
  98. }
  99. virtual ~CSimpleDC(void)
  100. {
  101. Release();
  102. }
  103. void Release(void)
  104. {
  105. RestoreState();
  106. if (m_hDC)
  107. {
  108. switch (m_nSource)
  109. {
  110. case FromWindowDC:
  111. if (m_hDC)
  112. ReleaseDC( m_hWnd, m_hDC );
  113. break;
  114. case FromClientDC:
  115. if (m_hDC)
  116. ReleaseDC( m_hWnd, m_hDC );
  117. break;
  118. case FromPaintDC:
  119. if (m_hDC)
  120. EndPaint( m_hWnd, &m_PaintStruct );
  121. break;
  122. case FromCompatibleDC:
  123. if (m_hDC)
  124. DeleteDC( m_hDC );
  125. break;
  126. case FromCreateDC:
  127. if (m_hDC)
  128. DeleteDC( m_hDC );
  129. break;
  130. case FromNullDC:
  131. break;
  132. }
  133. }
  134. ZeroMemory( &m_PaintStruct, sizeof(m_PaintStruct));
  135. m_hDC = NULL;
  136. m_hWnd = NULL;
  137. m_nSource = FromNullDC;
  138. }
  139. bool GetWindowDC( HWND hWnd )
  140. {
  141. Release();
  142. if (m_hDC = ::GetWindowDC(hWnd))
  143. {
  144. m_hWnd = hWnd;
  145. m_nSource = FromWindowDC;
  146. SaveState();
  147. }
  148. return (m_hDC != NULL);
  149. }
  150. bool GetDC( HWND hWnd )
  151. {
  152. Release();
  153. if (m_hDC = ::GetDC(hWnd))
  154. {
  155. m_hWnd = hWnd;
  156. m_nSource = FromClientDC;
  157. SaveState();
  158. }
  159. return (m_hDC != NULL);
  160. }
  161. bool BeginPaint( HWND hWnd )
  162. {
  163. Release();
  164. m_hDC = ::BeginPaint( hWnd, &m_PaintStruct );
  165. if (m_hDC)
  166. {
  167. m_hWnd = hWnd;
  168. m_nSource = FromPaintDC;
  169. SaveState();
  170. }
  171. return (m_hDC != NULL);
  172. }
  173. bool CreateCompatibleDC( HDC hDC )
  174. {
  175. Release();
  176. m_hDC = ::CreateCompatibleDC( hDC );
  177. if (m_hDC)
  178. {
  179. m_nSource = FromCompatibleDC;
  180. SaveState();
  181. }
  182. return (m_hDC != NULL);
  183. }
  184. bool CreateDC( LPCTSTR lpszDriver, LPCTSTR lpszDevice, LPCTSTR lpszOutput, CONST DEVMODE *lpInitData )
  185. {
  186. Release();
  187. m_hDC = ::CreateDC( lpszDriver, lpszDevice, lpszOutput, lpInitData );
  188. if (m_hDC)
  189. {
  190. m_nSource = FromCreateDC;
  191. SaveState();
  192. }
  193. return (m_hDC != NULL);
  194. }
  195. bool IsValid(void) const
  196. {
  197. return (m_hDC != NULL && m_nSource != FromNullDC);
  198. }
  199. operator HDC(void)
  200. {
  201. return m_hDC;
  202. }
  203. };
  204. #endif // __SIMDC_H_INCLUDED