平台:
windows ce
EVC (MFC)
代码:
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP_A);
... ...
bmp.DeleteObject()
bmp.LoadBitmap(IDB_BITMAP_B);
......
问题:
在用CBitmap bmp 对象来加载第二张图(IDB_BITMAP_B)的时候,必须先bmp.DeleteObject(),如果不加的话,在debug模式下会出错,出错的地方是:
BOOL CGdiObject::Attach(HGDIOBJ hObject)
{
ASSERT(m_hObject == NULL); // only attach once, detach on destroy
if (hObject == NULL)
return FALSE;
CHandleMap* pMap = afxMapHGDIOBJ(TRUE); // create map if not exist
ASSERT(pMap != NULL);
pMap->SetPermanent(m_hObject = hObject, this);
return TRUE;
}
起初以为是内存操作的原因要将bmp delete,后来看msdn:
If the bitmap identified by lpszResourceName does not exist or if there is insufficient memory to load the bitmap, the function returns zero.
You can use the CGdiObject::DeleteObject function to delete a bitmap loaded by the LoadBitmap function, or the CBitmap destructor deletes the object for you.
Before you delete the object, make sure it is not selected into a device context.
The following bitmaps are added to Windows versions 3.1 and later:
OBM_UPARRROWI
OBM_DNARROWI
OBM_RGARROWI
OBM_LFARROWI
These bitmaps are not found in device drivers for Windows versions 3.0 and earlier.
In Windows CE, when you use the CBitmap::LoadBitmap method to initialize a CBitmap object, the bitmap is read-only.
This is because Windows CE does not copy the bitmap into RAM, as the Windows-based desktop platforms do. In Windows CE, the bitmap only exists in a resource, which is part of the executable file of the program. When you select the bitmap into a device context, you cannot modify the device context—for example, by drawing text into it—because that would require the ability to write to the bitmap.
Windows CE does not support any predefined bitmaps, so you cannot use any OBM_* values in the lpszResourceName parameter.
Because Windows CE 1.0 only supports a 2 bit gray scale palette, use bitmaps that are one bit per pixel (monochrome .bmp) or two bits per pixel (.2bpp).
问题一
说明位图并不会加载到ram里面去的,是这样吗?
如果不是,就如msdn所说是exe模块的资源部分部分,
BOOL CGdiObject::Attach(HGDIOBJ hObject) 代码里面就是在做这种映射吧?
CHandleMap* pMap = afxMapHGDIOBJ(TRUE); // create map if not exist
必须是not exist才可以创建!
问题二:
如果不deleteobject(),在release 模式下,程序照样可以运行,界面显示不会受影响,但在debug模式下就会报错, 必须先delete才可以! 怎么解释? 如果不delete,程序运行会出现怎么样的异常? 或者说对软件的健壮性有怎样的影响?