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

上一篇讲了如何从资源中加载位图,这里讲如何从文件中加载。
资源加载位图 http://www.cppblog.com/alantop/archive/2008/03/13/44395.html

整个程序的流程是一样的,不明白的,可以看上一篇。
这里主要的 不同是怎样产生CBitmap对象。
从资源中加载是:
None.gif     CBitmap bmp;
None.gif    bmp
. LoadBitmap(IDB_BITMAP1)
从文件中加载是:
None.gif     CBitmap bitmap;
None.gif    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 DeleteDC function.

完全从文件加载位图,双缓冲代码如下,放在OnDraw(CDC* pDC)函数中即可。
None.gif     CBitmap bitmap;
None.gif    bitmap
. m_hObject = (HBITMAP) :: LoadImage( NULL , " test.bmp " , IMAGE_BITMAP , 500 , 400 , LR_LOADFROMFILE);
None.gif    
if  (bitmap . m_hObject)
None.gif    {
None.gif        
//  Get the size of the bitmap
None.gif
        BITMAP bmpInfo;
None.gif        bitmap
. GetBitmap( & bmpInfo);
None.gif        
None.gif        
//  Create an in-memory DC compatible with the
None.gif        // display DC we're using to paint

None.gif
        CDC dcMemory;
None.gif        dcMemory
. CreateCompatibleDC(pDC);
None.gif        
None.gif        
//  Select the bitmap into the in-memory DC
None.gif
        CBitmap *  pOldBitmap  =  dcMemory . SelectObject( & bitmap);
None.gif        
None.gif        
//  Find a centerpoint for the bitmap in the client area
None.gif
        CRect rect;
None.gif        GetClientRect(
& rect);
None.gif        int nX 
=  rect . left  +  (rect . Width()  -  bmpInfo . bmWidth)  /   2 ;
None.gif        int nY 
=  rect . top  +  (rect . Height()  -  bmpInfo . bmHeight)  /   2 ;
None.gif        
None.gif        POINT ptstart
,  ptend;
None.gif        ptstart
. =  ptstart . =   0 ;
None.gif        ptend
. =  ptend . =   500 ;
None.gif        
None.gif        
None.gif        dcMemory
. MoveTo(ptstart);
None.gif        dcMemory
. LineTo(ptend);
None.gif            
None.gif        
//  Copy the bits from the in-memory DC into the on-
None.gif        // screen DC to actually do the painting. Use the centerpoint
None.gif        // we computed for the target offset.

None.gif
        pDC -> BitBlt( 0 ,   0 ,  bmpInfo . bmWidth  *   2 ,  bmpInfo . bmHeight  *   2 ,   & dcMemory ,  
None.gif            
0 ,   0 ,  SRCCOPY);
None.gif        
None.gif        dcMemory
. SelectObject(pOldBitmap);
None.gif        
None.gif        SIZE si;
None.gif        si
. cx  =  bmpInfo . bmWidth;
None.gif        si
. cy  =  bmpInfo . bmHeight;
None.gif        SetScrollSizes(MM_TEXT
,  si);
None.gif    }
None.gif    
else
None.gif        TRACE0(
" ERROR: Where's IDB_BITMAP1?\n " );
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值