关于CreateBitmap和CreateCompatibleBitmap
首先现象是这样的:
我的程序突然有一天,一个Create出来的窗口画不出东西,开了天窗!跟进去一看,是调用CBitmap::CreateBitmap失败(返回值为FALSE)。用GetLastError查到的原因是“ERROR_NOT_ENOUGH_MEMORY”。
很奇怪,这个view的代码我并没有动啊!我试着改成CBitmap::CreateCompatibleBitmap,好了!算了,不管了,就这么着吧~
谁知,过了些时日,别的窗口又开天窗了!
这次我把CreateBitmap改成CreateCompatibleBitmap,也不是每次都成功,时不时的就会创建失败!
查msdn,关于CreateBitmap API有如下这一段话:
The CreateBitmap function can be used to create color bitmaps.
However, for performance reasons applications should use CreateBitmap to create monochrome bitmaps and CreateCompatibleBitmap to create color bitmaps.
Whenever a color bitmap returned from CreateBitmap is selected into a device context, the system checks that the bitmap matches the format of the device context it is being selected into.
Because CreateCompatibleBitmap takes a device context, it returns a bitmap that has the same format as the specified device context.
Thus, subsequent calls to SelectObject are faster with a color bitmap from CreateCompatibleBitmap than with a color bitmap returned from CreateBitmap.
这段话看着挺绕,说CreateCompatibleBitmap API比CreateBitmap API的效率更高一些。那么,我的问题之一是:
CBitmap的这两个属性,是否也如API一样,CreateCompatibleBitmap比CreateBitmap的效率高??
我的问题之二:搜了一下,我的程序里,有二十多处地方(17个窗口)调用CreateBitmap,10处地方调用CreateCompatibleBitmap,且这些占用bitmap的窗口只在最后退出的时候才销毁。有经验的同仁看看,调用的地方是不是太多了?如果都换成CreateCompatibleBitmap,资源占用情况是否会有所改观?