从文件中加载位图,双缓冲显示

整个程序的流程是一样的,不明白的,可以看上一篇。
这里主要的 不同是怎样产生CBitmap对象。
从资源中加载是:
     CBitmap bmp;
     bmp.LoadBitmap(IDB_BITMAP1)从文件中加载是:
     CBitmap bitmap;
     bitmap.m_hObject=(HBITMAP)::LoadImage(NULL,"test.bmp",IMAGE_BITMAP,500,400,LR_LOADFROMFILE);
这里需要注意的是从文件加载位图,并不是通过调用CBitmap的成员函数完成。
而是使用SDK函数LoadImage,通过将其返回值赋值给CBitmap的成员变量m_hObject而完成对CBitmap的对象的赋值过程。
此处的强制类型可以不使用,使用是强调的意思。

注意: When you no longer need the memory DC, call the function. 

完全从文件加载位图,双缓冲代码如下,放在OnDraw(CDC* pDC)函数中即可。      CBitmap bitmap;
     bitmap.m_hObject=(HBITMAP)::LoadImage(NULL,"test.bmp",IMAGE_BITMAP,500,400,LR_LOADFROMFILE);
    if (bitmap.m_hObject)
     {
        // Get the size of the bitmap
         BITMAP bmpInfo;
         bitmap.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(&bitmap);
        
        // 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;
        
         POINT ptstart, ptend;
         ptstart.x = ptstart.y = 0;
         ptend.x = ptend.y = 500;
        
        
         dcMemory.MoveTo(ptstart);
         dcMemory.LineTo(ptend);
            
        // 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(0, 0, bmpInfo.bmWidth * 2, bmpInfo.bmHeight * 2, &dcMemory, 
            0, 0, SRCCOPY);
        
         dcMemory.SelectObject(pOldBitmap);
        
         SIZE si;
         si.cx = bmpInfo.bmWidth;
         si.cy = bmpInfo.bmHeight;
         SetScrollSizes(MM_TEXT, si);
     }
    else
         TRACE0("ERROR: Where's IDB_BITMAP1?\n");转自( http://www.cppblog.com/alantop/archive/2008/03/14/44414.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值