HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL,lpszName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
把hBitmap设为全局变量,在一个OPEN的按钮中打用上面一句打开,在OnPaint()里面用这个hBitmap来画图为什么画不出来

解决方案 »

  1.   

    // 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");
    转化,你懂的
    CBitmap::FromHandlestatic CBitmap* PASCAL FromHandle( HBITMAP hBitmap );
      

  2.   

    不是,我是问为什么在onpaint里打不开传进去的HBITMAP,画不了图,是不是他自动释放了
      

  3.   

    在OnPaint()里面:
    如果 m_hBitmap = NULL( 须初始化为NULL)
    if ( !m_hBitmap ) /* HBITMAP */ m_hBitmap = (HBITMAP)::LoadImage(NULL,lpszName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    else
    {// do others
    }
      

  4.   

    1。确定下LoadImage是否加载图片资源成功了
    2。如果hBitmap 定义成全局的,它不会自动释放,除非程序结束
      

  5.   

    类似这样的问题,最好是单步调试,最好先检查下LoadImage函数调用是否成功,可能是找不到图象文件的存放位置。
      

  6.   

    检查你用的哪个设备场景,去selectobject(hBitmap)。如果设备场景不对,或者没选进入,那么肯定不会画出来,
      

  7.   

    不是的,我是想用loadimage后把图放m_hBitmap里,弄成全局,不用每次都去打开图
      

  8.   

    你先看看LoadImage返回值对不对吧。
      

  9.   

    或者点按钮后有没有触发OnPaint。
    最有可能就是绘图部分代码问题
      

  10.   

    if ( !m_hBitmap )不是一次吗?
      

  11.   

    程序LoadImage一次之后,因为if ( !m_hBitmap )的判断导致以后都不会进入LoadImage,所以不会有图