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.

217 lines
5.2 KiB

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