Friday, September 16, 2005

Solving mysterious drawing problems associated with ImageList in VC++

Consider the following scenerio - You need combine a system icon and a bitmap together and then insert the composite image to an ImageList ("ImageList" form the MS ImageList API.. i.e., ImageList_Create(), ImageList_AddImage()). What you will probably do is something like this:

1. create a bitmap handle
2. create a memory device context (dc) and then SelectObject() to select the bitmap into the dc
3. call DrawIconEx() to draw the provided HICON to the dc
4. call BitBlt() to blt the provided HBITMAP to the dc
5. call DeleteDC() to free the dc
6. call ImageList_Add() and pass the bitmap handle you created on step 1

Everything seems straightforward; however, there is one crucial part which, if done incorrectly, may fail the whole process, ending up with an ImageList with a corrupted bitmap.

If you initialize the ImageList with anything other than ILC_COLORDDB as the 3rd paramter, such as this:

_imageList = ImageList_Create(width, height, ILC_COLOR32|ILC_MASK, 8, 8);

Then the resulting image will be problematic. However, if you create the ImageList with ILC_COLORDDB instead:

_imageList = ImageList_Create(width, height, ILC_COLORDDB|ILC_MASK, 8, 8);

Then everything works fine. I have yet to figure out exactly why this happens, but I suspect this has something to do with the way Windows handles DIB and DDB differently. Icons might be considered as a "DIB" when being used to DrawIcon() to an HDC. Since your bitmap is a DDB, when you draw a DIB object and a DDB object into the HDC, this will corrupt the image. This is really annoying because the whole process does not flag any errors.

1 comment:

farryahbacker said...

The best slots, roulette, and video slots in Australia at The
Top casinos, slot machines & video 화성 출장샵 poker. Australian casino sites offer some 보령 출장마사지 of 제주 출장마사지 the best live dealer games online. 통영 출장샵 Whether you're looking for a new 사천 출장샵