请教,我想在视图区中显示一个位图,为什么这样什么都不现实呢?(源码在里面)

时间:2022-01-31 14:01:28
void CThunder1View::OnDraw(CDC* pDC)
{
CThunder1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here

  CBitmap bmp;
   if (bmp.LoadBitmap(IDI_ICON1))
   {
 
      // Get the size of the bitmap
      BITMAP bmpInfo;
      bmp.GetBitmap(&bmpInfo);

      // Create an in-memory DC compatible with the
      // display DC we're using to paint
      CDC dcMemory;
      dcMemory.CreateCompatibleDC(pDC);

      // Select the bitmap into the in-memory DC
      CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);

      // Find a centerpoint for the bitmap in the client area
      CRect rect;
      GetClientRect(&rect);
      int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
      int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;

      // Copy the bits from the in-memory DC into the on-
      // screen DC to actually do the painting. Use the centerpoint
      // we computed for the target offset.
      pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 
         0, 0, SRCPAINT );

      dcMemory.SelectObject(pOldBitmap);
   }
   else
      TRACE0("ERROR: Where's IDB_BITMAP1?\n");


}

3 个解决方案

#1


放在OnEraseBkgnd或OnPaint里试一下,还有看一看图是不是真的已经载入了

#2


如何判断位图是否真的载入了?

#3


下面的语句一定可以用,我已经用了的
CDrawScrollBmpView::CDrawScrollBmpView()
{
// TODO: add construction code here
    HBITMAP hBmp=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"C:\\My Documents\\image2.bmp",
                             IMAGE_BITMAP,0,0,
LR_CREATEDIBSECTION|LR_LOADFROMFILE);
m_picture.Attach(hBmp); 
}

void CDrawScrollBmpView::OnDraw(CDC* pDC)
{
CDrawScrollBmpDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here

CDC dc;
dc.CreateCompatibleDC(pDC);
dc.SelectObject (&m_picture);
BITMAP bmp;
m_picture.GetBitmap (&bmp);
pDC->BitBlt(300,0,bmp.bmWidth ,bmp.bmHeight,&dc,0,0,SRCCOPY);  

}

#1


放在OnEraseBkgnd或OnPaint里试一下,还有看一看图是不是真的已经载入了

#2


如何判断位图是否真的载入了?

#3


下面的语句一定可以用,我已经用了的
CDrawScrollBmpView::CDrawScrollBmpView()
{
// TODO: add construction code here
    HBITMAP hBmp=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"C:\\My Documents\\image2.bmp",
                             IMAGE_BITMAP,0,0,
LR_CREATEDIBSECTION|LR_LOADFROMFILE);
m_picture.Attach(hBmp); 
}

void CDrawScrollBmpView::OnDraw(CDC* pDC)
{
CDrawScrollBmpDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here

CDC dc;
dc.CreateCompatibleDC(pDC);
dc.SelectObject (&m_picture);
BITMAP bmp;
m_picture.GetBitmap (&bmp);
pDC->BitBlt(300,0,bmp.bmWidth ,bmp.bmHeight,&dc,0,0,SRCCOPY);  

}