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.

144 lines
2.7 KiB

  1. /*
  2. * @doc INTERNAL
  3. *
  4. * @module OSDC.CPP -- Off Screen DC class |
  5. *
  6. * This contains method used to implement the off screen
  7. * DC class
  8. *
  9. * Owner:<nl>
  10. * Rick Sailor
  11. *
  12. * Copyright (c) 1995-1998, Microsoft Corporation. All rights reserved.
  13. */
  14. #include "_common.h"
  15. #include "_osdc.h"
  16. ASSERTDATA
  17. /*
  18. * COffScreenDC::Init(hdc, xWidth, yHeight, crBackground)
  19. *
  20. * @mfunc
  21. * Initialize off screen DC with compatible bitmap
  22. *
  23. * @rdesc
  24. * HDC created
  25. */
  26. HDC COffScreenDC::Init(
  27. HDC hdc, //@parm DC to be compatible with
  28. LONG xWidth, //@parm Width of compatible bitmap
  29. LONG yHeight, //@parm Height of compatible bitmap
  30. COLORREF crBackground) //@parm Default background for bitmap
  31. {
  32. HDC hdcRet = NULL; // HDC to return to caller
  33. _hbmpOld = NULL; // Assume failure
  34. _hbmp = NULL;
  35. _hpalOld = NULL;
  36. // Create memory DC
  37. _hdc = CreateCompatibleDC(hdc);
  38. if(_hdc)
  39. {
  40. // Create bitmap based on size of client rectangle
  41. _hbmp = CreateCompatibleBitmap(hdc, xWidth, yHeight);
  42. if(_hbmp)
  43. {
  44. // Select bitmap into hdc
  45. _hbmpOld = (HBITMAP)SelectObject(_hdc, _hbmp);
  46. if(_hbmpOld && SetBkColor(_hdc, crBackground) != CLR_INVALID)
  47. hdcRet = _hdc;
  48. }
  49. }
  50. if(!hdcRet)
  51. FreeData();
  52. return hdcRet;
  53. }
  54. /*
  55. * COffScreenDC::SelectPalette(hpal)
  56. *
  57. * @mfunc
  58. * Set a new palette into the hdc
  59. */
  60. void COffScreenDC::SelectPalette(
  61. HPALETTE hpal) //@parm Handle to palette to set
  62. {
  63. #ifndef PEGASUS
  64. if(hpal)
  65. {
  66. _hpalOld = ::SelectPalette(_hdc, hpal, TRUE);
  67. RealizePalette(_hdc);
  68. }
  69. #endif
  70. }
  71. /*
  72. * COffScreenDC::FreeData()
  73. *
  74. * @mfunc
  75. * Free resources associated with bitmap
  76. */
  77. void COffScreenDC::FreeData()
  78. {
  79. if(_hdc)
  80. {
  81. #ifndef PEGASUS
  82. if(_hpalOld)
  83. ::SelectPalette(_hdc, _hpalOld, TRUE);
  84. #endif
  85. if(_hbmpOld)
  86. SelectObject(_hdc, _hbmpOld);
  87. if(_hbmp)
  88. DeleteObject(_hbmp);
  89. DeleteDC(_hdc);
  90. }
  91. }
  92. /*
  93. * COffScreenDC::Realloc(xWidth, yHeight)
  94. *
  95. * @mfunc
  96. * Reallocate bitmap
  97. *
  98. * @rdesc
  99. * TRUE - succeeded
  100. * FALSE - failed
  101. */
  102. BOOL COffScreenDC::Realloc(
  103. LONG xWidth, //@parm Width of new bitmap
  104. LONG yHeight) //@parm Height of new bitmap
  105. {
  106. // Create bitmap based on size of client rectangle
  107. HBITMAP hbmpNew = CreateCompatibleBitmap(_hdc, xWidth, yHeight);
  108. if(!hbmpNew)
  109. {
  110. AssertSz(FALSE,
  111. "COffScreenDC::Realloc CreateCompatibleBitmap failed");
  112. return FALSE;
  113. }
  114. // Select out old bitmap
  115. #if defined(DEBUG) || defined(_RELEASE_ASSERTS_)
  116. HBITMAP hbmpDebug = (HBITMAP)
  117. #endif // DEBUG
  118. SelectObject(_hdc, hbmpNew);
  119. AssertSz(hbmpDebug == _hbmp,
  120. "COffScreenDC::Realloc different bitmap");
  121. // Delete old bitmap
  122. DeleteObject(_hbmp);
  123. AssertSz(hbmpDebug == _hbmp,
  124. "COffScreenDC::Realloc Delete old bitmap failed");
  125. // Put in new bitmap
  126. _hbmp = hbmpNew;
  127. return TRUE;
  128. }