WTL: Adding bitmaps to a CListViewCtrl using a CImageList

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I have been assigned to fix a bug in old code that I know very little
of =(

I have a list which should contain user defined color settings for
different categories. To help the user see what the settings are, each
row in the list should display a 16 x 16 bits square in the user
defined color, plus the name of the category. That is, it should look
something like this:

Color Category
[Blue] "Category1"
[Red] "Category2"
[Orange] "Category3"

.... where [Blue] represents a blue square etc.

The list is implemented using a ClistViewCtrl, which has a CImageList
connected to it. The squares are created as CBitmap:s, and to paint
them device contexts HDC and CDC are used.

The problem is that the squares are empty / have no color =(

The code that creates the list looks like this:


<code>
// Get HDC of screen
HDC hDC = GetDC();

// Create image list
BOOL bRes = m_ImageList.Create( 16, 16, ILC_COLORDDB, 0, 100 );
ATLASSERT( bRes );

// Put image list in list
m_List.SetImageList( m_ImageList, LVSIL_SMALL );

// Create all bitmaps used in image list
for( std::vector< CColoredItem >::iterator i=m_ColoredItems.begin();
i!=m_ColoredItems.end(); i++ )
{
// Create solid brush of items color
CBrush brush;
brush.CreateSolidBrush( i->color );

// Create bitmap and dc that paints in it
CDC bdc;
bdc.CreateCompatibleDC( hDC );
m_Bitmaps.push_back(CBitmap());
m_Bitmaps.back().CreateCompatibleBitmap( hDC, 16, 16 );

// Paint bitmap
HBITMAP hOldBitmap = bdc.SelectBitmap( m_Bitmaps.back() );
BOOL bRes = bdc.FillRect( CRect(0, 0, 16, 16), brush );
bdc.SelectBitmap(hOldBitmap);

// Add it to image list
int hItem = m_List.InsertItem( 0, i->label,
m_ImageList.Add( m_Bitmaps.back() ) );
m_List.SetItemData( hItem, i-m_ColoredItems.begin() );
}

// Release screen dc
ReleaseDC( hDC );
</code>

I can see no error in this, nor can my debugger. The use of the device
contexts and bitmaps (the calls to CreateCompatibleDC,
CreateCompatibleBitmap, GetDC etc) seem to be correct, from what I
have seen in articles / tutorials that I have found. The two lines
that add the bitmap to the image list are nasty to read, but they seem
to be correct. The coloritems stored in m_ColoredItems seem to be good
as well (i->color does provide valid COLORREFs (not white)).

Can anyone see anything wrong in the code above? Any help is deeply
appreciated =)

To complicate matters: the code is run as unmanaged code in a
managed .NET environment, and the program is a part of a plugin to a
host application.

.



Relevant Pages

  • Re: HDC
    ... I use HDC in OnDraw because pDC->m_hDC seems to me not doest work correctly ... for bitmap show when scroll bar is moved. ... I am trying to StretchBlt just Window size bitmap to ... why isn't your scrollbar logic using ...
    (microsoft.public.vc.mfc)
  • Re: CreateBitmap : How to draw on it ?
    ... You have to tell CreateBitmap() what display settings to create the bitmap ... Whatever HDC you select the bitmap into afterwards must match those ...
    (microsoft.public.win32.programmer.gdi)
  • Re: SHLoadDIBitmap Example?
    ... HDC hdc; ... HBITMAP hBmp, oBmp; ... // then do something to create bitmap ... should DeleteObject be called? ...
    (microsoft.public.pocketpc.developer)
  • Re: CreateBitmap : How to draw on it ?
    ... There is nothing in the bitmap specification that prevents 2-bit ... CreateBitmap result instead of the other way around), ... You have to tell CreateBitmapwhat display settings to create the bitmap ... An in-memory HDC that has a bitmap selected into it, ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Screen Capture with GDI+ to Bitmap
    ... // get the primary display monitor's working area. ... HDC hMemdc = CreateCompatibleDC; ... // create a bitmap to contain the screen image and select the bitmap into the mem dc ... HBITMAP hOldImage = SelectObject(hMemdc, hScreenImage); ...
    (microsoft.public.win32.programmer.gdi)