views:

479

answers:

2

I used a class which derives from CListBox, and create it with following style:WS_CHILD|WS_VISIBLE |LBS_OWNERDRAWFIXED | WS_VSCROLL | WS_HSCROLL

I expect the ListBox's item to be have a fixed size, not affected by the size of the list box. So I override the MeasureItem() method, in which I specify the item's size like below:

void CMyListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS) { lpMIS->itemHeight = ALBUM_ITEM_HEIGHT; lpMIS->itemWidth = ALBUM_ITEM_WIDTH; }

But the item's size changes according to the List box's size changing. is there anything wrong with my approach? Thank you!

A: 

If you look at the MSDN entry for CListBox::MeasureItem you'll see that it's only called once unless the LBS_OWNERDRAWVARIABLE (not LBS_OWNERDRAWFIXED) style is set. If I understand correctly then this would explain the behaviour you're seeing because MesuareItem would need to be called each time the control's size changes.

Also, have you considered the points made in MFC Technical Note 14 : Custom Controls?

Stu Mackellar
A: 

What's not mentioned in the reference is that WM_MEASUREITEM is called every time the *_OWNERDRAWFIXED control is resized.

I don't know however, how official this behavior is and whether it should be relied on, but it has been verified at CodeGuru and several forum posts found on the Google thing.

If you don't want to process the message, then just set a private flag somewhere in the first OnMeasureItem() call and return from it as soon as you check that it's set next time.

macbirdie