CacheDC.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // CacheDC.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "HJHomework.h"
  5. #include "CacheDC.h"
  6. // CCacheDC
  7. CCacheDC::CCacheDC(CDC *pDC,CRect rect/* = CRect(0, 0, 0, 0)*/, BOOL bCopyFirst/* = TRUE*/)
  8. :CDC(), m_oldBitmap(NULL), m_pDC(pDC)
  9. {
  10. ASSERT(m_pDC != NULL); // 断言参数不为空
  11. m_bMemDC = !pDC->IsPrinting(); // 判断设备是否用于打印
  12. if (m_bMemDC)
  13. {
  14. CreateCompatibleDC(pDC); // 创建一个内存DC
  15. if (rect == CRect(0, 0, 0, 0)) // 判断绘画区域
  16. {
  17. pDC->GetClipBox(&m_rect); // 得到一个能够完整包含当前可见区域的最小矩形的大小
  18. }else
  19. {
  20. m_rect = rect;
  21. }
  22. //创建兼容位图
  23. m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
  24. m_oldBitmap = SelectObject(&m_bitmap); // 保留旧位图对象
  25. SetWindowOrg(m_rect.left, m_rect.top); // 移动窗口原点
  26. if (bCopyFirst) // 初次复制
  27. {
  28. this->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(), m_pDC, m_rect.left, m_rect.top, SRCCOPY);
  29. }
  30. }
  31. else
  32. {
  33. //创建一个跟当前DC绘制有关的副本
  34. m_bPrinting = pDC->m_bPrinting;
  35. m_hDC = pDC->m_hDC;
  36. m_hAttribDC = pDC->m_hAttribDC;
  37. }
  38. }
  39. CCacheDC::~CCacheDC()
  40. {
  41. if (m_bMemDC)
  42. {
  43. // 复制屏幕外位图到屏幕内
  44. m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(), this, m_rect.left, m_rect.top, SRCCOPY);
  45. SelectObject(m_oldBitmap); //交换原来的位图
  46. }else
  47. {
  48. m_hDC = m_hAttribDC = NULL;
  49. }
  50. }