CBitmap MyBitmap;
MyBitmap.LoadBitmap(IDB_BITMAP1);
??总是失败返回0为什么?

解决方案 »

  1.   

    CBitmap MyBitmap;
    设为类的成员
      

  2.   

    为什么会错啊,光这两句是没有错的啊,除非IDB_BITMAP1不存在!如果存在的话,你就要看看是不是其他地方出错了
      

  3.   

    CBitmap MyBitmap;应该是类成员,否则在函数执行完之后资源就被释放了
      

  4.   

    我是想画一个与门,代码如下:
    void CMyView::DrawAndGate(CPoint point)
    {
    CDC dc;

    CClientDC ClientDC(this);
    dc.CreateCompatibleDC (&ClientDC);

    CBitmap BitMap_AndGate;
    CBitmap* pOldBitmap; BitMap_AndGate.LoadBitmap (IDB_BITMAP1);
    //下断点到这句 ,LoadBitmap总是返回0,就是不成功。
    //IDB_BITMAP1是我画的与门图
    BITMAP bmpInfo; BitMap_AndGate.GetBitmap (&bmpInfo); CRect rect;
    GetClientRect(&rect);
    pOldBitmap=dc.SelectObject (&BitMap_AndGate);
    ClientDC.BitBlt (point.x ,point.y,55,30,&dc,0,0,SRCCOPY);
    dc.SelectObject (pOldBitmap);}
      

  5.   

    这是MSDN的例子:
    // This OnDraw() handler loads a bitmap from system resources,
    // centers it in the view, and uses BitBlt() to paint the bitmap
    // bits.void CBlat2View::OnDraw(CDC* pDC)
    {
       CBlat2Doc* pDoc = GetDocument();
       ASSERT_VALID(pDoc);   // load IDB_BITMAP1 from our resources
       CBitmap bmp;
       if (bmp.LoadBitmap(IDB_BITMAP1))
       {      // 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, SRCCOPY);      dcMemory.SelectObject(pOldBitmap);
       }
       else
          TRACE0("ERROR: Where's IDB_BITMAP1?\n");
    }