WTL: Adding bitmaps to a CListViewCtrl using a CImageList
- From: JagdPanther <david.rundqvist@xxxxxxxxx>
- Date: Fri, 20 Jul 2007 14:39:37 -0000
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.
.
- Prev by Date: Re: COM DLL does not appear in References dialog
- Next by Date: KBBar problem
- Previous by thread: Re: Inheriting Events interface in client
- Next by thread: KBBar problem
- Index(es):
Relevant Pages
|