TransparentPicView.cpp
上传用户:aokegd
上传日期:2009-12-14
资源大小:1276k
文件大小:4k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // TransparentPicView.cpp : implementation of the CTransparentPicView class
  2. //
  3. #include "stdafx.h"
  4. #include "TransparentPic.h"
  5. #include "TransparentPicDoc.h"
  6. #include "TransparentPicView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CTransparentPicView
  14. IMPLEMENT_DYNCREATE(CTransparentPicView, CView)
  15. BEGIN_MESSAGE_MAP(CTransparentPicView, CView)
  16. //{{AFX_MSG_MAP(CTransparentPicView)
  17. // NOTE - the ClassWizard will add and remove mapping macros here.
  18. //    DO NOT EDIT what you see in these blocks of generated code!
  19. //}}AFX_MSG_MAP
  20. // Standard printing commands
  21. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  22. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  23. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  24. END_MESSAGE_MAP()
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CTransparentPicView construction/destruction
  27. CTransparentPicView::CTransparentPicView()
  28. {
  29. // TODO: add construction code here
  30. }
  31. CTransparentPicView::~CTransparentPicView()
  32. {
  33. }
  34. BOOL CTransparentPicView::PreCreateWindow(CREATESTRUCT& cs)
  35. {
  36. // TODO: Modify the Window class or styles here by modifying
  37. //  the CREATESTRUCT cs
  38. return CView::PreCreateWindow(cs);
  39. }
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CTransparentPicView drawing
  42. void CTransparentPicView::OnDraw(CDC* pDC)
  43. {
  44. CTransparentPicDoc* pDoc = GetDocument();
  45. ASSERT_VALID(pDoc);
  46. //画底层位图
  47. CBitmap bmp;
  48. bmp.LoadBitmap(IDB_BITMAP1);
  49. BITMAP bm;
  50. bmp.GetBitmap(&bm);
  51. int nWidth=bm.bmWidth,nHeight=bm.bmHeight;
  52. CDC MemDC;
  53. MemDC.CreateCompatibleDC(pDC);
  54. CBitmap* pOldBmp = MemDC.SelectObject(&bmp);
  55. pDC->BitBlt(0,0,nWidth,nHeight,&MemDC,0,0,SRCCOPY);
  56. MemDC.SelectObject(pOldBmp);
  57. //画上层透明位图
  58. DrawTransparent(pDC,0,0,RGB(0,0,0));
  59. // TODO: add draw code for native data here
  60. }
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CTransparentPicView printing
  63. BOOL CTransparentPicView::OnPreparePrinting(CPrintInfo* pInfo)
  64. {
  65. // default preparation
  66. return DoPreparePrinting(pInfo);
  67. }
  68. void CTransparentPicView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  69. {
  70. // TODO: add extra initialization before printing
  71. }
  72. void CTransparentPicView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  73. {
  74. // TODO: add cleanup after printing
  75. }
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CTransparentPicView diagnostics
  78. #ifdef _DEBUG
  79. void CTransparentPicView::AssertValid() const
  80. {
  81. CView::AssertValid();
  82. }
  83. void CTransparentPicView::Dump(CDumpContext& dc) const
  84. {
  85. CView::Dump(dc);
  86. }
  87. CTransparentPicDoc* CTransparentPicView::GetDocument() // non-debug version is inline
  88. {
  89. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTransparentPicDoc)));
  90. return (CTransparentPicDoc*)m_pDocument;
  91. }
  92. #endif //_DEBUG
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CTransparentPicView message handlers
  95. void CTransparentPicView::DrawTransparent(CDC *pDC, int x, int y, COLORREF crColour)
  96. {
  97. COLORREF crOldBack=pDC->SetBkColor(RGB(255,255,255));
  98. COLORREF crOldText=pDC->SetTextColor(RGB(0,0,0));
  99. CDC dcImage, dcMask;
  100. CBitmap bmp;
  101. bmp.LoadBitmap(IDB_BITMAP2);
  102. //IDB_BITMAP1是待显示位图的资源ID
  103. BITMAP bm;bmp.GetBitmap(&bm);
  104. int nWidth=bm.bmWidth,nHeight=bm.bmHeight;
  105. //为图像及mask各创建一个DC
  106. dcImage.CreateCompatibleDC(pDC);
  107. dcMask.CreateCompatibleDC(pDC);
  108. //把图像装入image DC
  109. CBitmap* pOldBitmapImage=dcImage.SelectObject(&bmp);
  110. //为“掩码”位图创建一个单色bitmap
  111. CBitmap bitmapMask;
  112. bitmapMask.CreateBitmap(nWidth, nHeight, 1, 1, NULL); //把mask位图装入mask DC
  113. CBitmap* pOldBitmapMask = dcMask.SelectObject(&bitmapMask); //用透明色创建“掩码”位图
  114. dcImage.SetBkColor(crColour);//crColor是位图中的透明色
  115. dcMask.BitBlt(0, 0, nWidth, nHeight, &dcImage, 0, 0, SRCCOPY);//分3步进行实际的绘制
  116. pDC->BitBlt(x, y, nWidth, nHeight, &dcImage, 0, 0, SRCINVERT);
  117. pDC->BitBlt(x, y, nWidth, nHeight, &dcMask, 0, 0, SRCAND);
  118. pDC->BitBlt(x, y, nWidth, nHeight, &dcImage, 0, 0, SRCINVERT); //恢复原先设置
  119. dcImage.SelectObject(pOldBitmapImage);
  120. dcMask.SelectObject(pOldBitmapMask);
  121. pDC->SetBkColor(crOldBack);
  122. pDC->SetTextColor(crOldText);
  123. }