从资源中加载显示位图,通过双缓冲的方法显示
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)
如何使用下面的代码。
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)
相关文章推荐
- 从资源中加载显示位图,通过双缓冲的方法显示
- 关于CI框架通过修改.htaccess文件的办法解决无法加载资源文件的方法
- ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,下面就说实现方法,先贴上主方法的代码:
- android中java代码中通过资源id加载String方法
- vc 加载bmp位图并显示的方法
- Tomcat6.0通过映射路径加载资源的方法
- VC++图片控件(Picture Control)显示资源位图(BMP)、文件位图(BMP)、其它格式文件图片(JPG\PNG\BMP)的方法
- vc 加载bmp位图并显示的方法
- unity 安卓热更新代码的最新方法: 通过Mono加载新的重新编译的dll
- Unity3d发布Android版通过WWW加载本地AssetBundle资源
- WinCE显示资源中BMP图片的几种方法
- c#通过资源加载字体供winform使用
- WinCE中加载位图的方法
- JNI:通过函数名对应表的方式来加载对应的native方法
- 【Unity3d】关于AssetBundle.CreateFromFile()方法加载资源失败解决方法
- mfc 利用HBITMAP 和CBITMAP 显示位图文件(双缓冲)
- 通过请求不同的url返回Json对象并在前端显示的解决方法
- 调用JS时显示“数据加载中...”的方法
- MFC显示位图的几种方法
- WinCE显示资源中BMP图片的几种方法附自己个人实验数据