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

来源:互联网 发布:数据科学入门 pdf下载 编辑:程序博客网 时间:2024/06/05 00:46
整个程序的流程是一样的,不明白的,可以看上一篇。
这里主要的 不同是怎样产生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
原创粉丝点击