在MFC中显示多个位图

时间:2015-03-07 10:20:27

标签: c++ visual-c++ mfc

我正在尝试在不同位置的视图中显示同一图像的两个位图,如下所示,但它只显示第一个。如果我注释掉第一个,那么显示另一个。

void CChildView::OnPaint() 
{
  // Load the bitmap
  CBitmap BmpLady;
  // Load the bitmap from the resource
  BmpLady.LoadBitmap(IDB_MB);

  CPaintDC dc(this); // device context for painting

  // Create a memory device compatible with the above CPaintDC variable
  CDC MemDCLady;
  MemDCLady.CreateCompatibleDC(&dc);

  // Select the new bitmap
  CBitmap *BmpPrevious = MemDCLady.SelectObject(&BmpLady);
  // Copy the bits from the memory DC into the current dc
  dc.BitBlt(20, 10, 436, 364, &MemDCLady, 0, 0, SRCCOPY);
  // Restore the old bitmap
  dc.SelectObject(BmpPrevious);

  // Draw another bitmap for same image.
  CPaintDC dc1(this);

  CDC MemDCLady1;
  MemDCLady1.CreateCompatibleDC(&dc1);
  CBitmap *BmpPrevious1 = MemDCLady1.SelectObject(&BmpLady);
  dc1.BitBlt(200, 100, 436, 364, &MemDCLady1, 0, 0, SRCCOPY);
  dc1.SelectObject(BmpPrevious1);
}

如何同时显示两个图像?请帮忙。提前谢谢。

P.S:我是MFC的新手。

1 个答案:

答案 0 :(得分:2)

第二个位图无需再次使用CreateCompatibleDC。通过以下更改,我可以同时显示两个位图

void CChildView::OnPaint() 
{
  CBitmap BmpLady;
  // Load the bitmap from the resource
  BmpLady.LoadBitmap(IDB_MB);

  CPaintDC dc(this);
  CDC MemDCLady;

  // Create a memory device compatible with the above CPaintDC variable
  MemDCLady.CreateCompatibleDC(&dc);
  // Select the new bitmap
  //CBitmap *BmpPrevious = MemDCLady.SelectObject(&BmpLady);
   MemDCLady.SelectObject(&BmpLady);
  // Copy the bits from the memory DC into the current dc
  dc.BitBlt(20, 10, 436, 364, &MemDCLady, 0, 0, SRCCOPY);

  // MemDCLady.SelectObject(&BmpLady);
  // Copy the bits from the memory DC into the current dc
  dc.BitBlt(200, 100, 436, 364, &MemDCLady, 0, 0, SRCCOPY);
}
相关问题