您的位置:首页 > 其它

从资源中加载显示位图,通过双缓冲的方法显示

2013-07-14 10:52 239 查看
从文件加载位图 看这篇 http://www.cppblog.com/alantop/archive/2008/03/14/44414.html

如何使用下面的代码。

1. 把位图导入资源。

资源->入->选bmp类型->导入->从硬盘中选择导入的bmp图即可。导入后,自动生成ID为IDB_BITMAP1的资源Bitmap.

2. 把下面代码复制到视图的OnDraw(CDC* pDC)函数



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;





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, bmpInfo.bmHeight, &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");

3. 代码的操作思路.

双缓冲,简单的说就是把画图的所有动作都在内存DC上完成,然后通过BitBlt拷贝到屏幕DC上。

a. 首先建立一个CBitmap对象

b. 然后,建立一个和显示设备兼容的内存DC

c. 然后把CBitmap放在建立的内存DC中

d. 然后,在内存DC上做一些画图的动作

e. 将内存DC中的东西,显示在屏幕上

4. 代码中的一些其他操作

a. 我们常用SelectObject把新的东西选入。把老的东西作为返回值,保存。再使用完毕后,在用SelectObject重新启用老的东西

b. 在代码里面有设置滚动条的地方。 使用CScrollView类作为基类。给SIZE结构体赋值,然后调用SetScrollSizes做设置,滚动条就会自动计算响应的。

转自(http://www.cppblog.com/alantop/archive/2008/03/13/44395.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: