即使我在 MeasureItem() 方法中指定了大小,当更改列表框的大小时,CListBox 的项目大小也会更改吗?
我使用了一个从 CListBox
派生的类,并使用以下内容创建它:
style:WS_CHILD|WS_VISIBLE |LBS_OWNERDRAWFIXED | WS_VSCROLL | WS_HSCROLL
我希望 ListBox 的项目具有固定大小,不受列表框大小的影响。 因此,我重写了 MeasureItem() 方法,在该方法中我指定了项目的大小,如下所示:
void CMyListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
{
lpMIS->itemHeight = ALBUM_ITEM_HEIGHT;
lpMIS->itemWidth = ALBUM_ITEM_WIDTH;
}
但项目的大小会根据列表框大小的变化而变化。 我的方法有什么问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
参考文献中没有提到的是,每次调整
*_OWNERDRAWFIXED
控件大小时都会调用WM_MEASUREITEM
。然而,我不知道这种行为有多正式以及是否应该依赖它,但它已在 CodeGuru 以及在 Google 上找到的一些论坛帖子。
如果您不想处理该消息,则只需在第一个
OnMeasureItem()
调用中的某个位置设置一个私有标志,并在下次检查它是否已设置时立即返回。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.如果您查看
CListBox::MeasureItem
您将看到它仅被调用一次,除非设置了LBS_OWNERDRAWVARIABLE
(不是LBS_OWNERDRAWFIXED
)样式。 如果我理解正确,那么这将解释您所看到的行为,因为每次控件大小更改时都需要调用MeasureItem
。另外,您是否考虑过 MFC 技术说明 14 中提出的观点:自定义控件?
If you look at the
MSDN
entry forCListBox::MeasureItem
you'll see that it's only called once unless theLBS_OWNERDRAWVARIABLE
(notLBS_OWNERDRAWFIXED
) style is set. If I understand correctly then this would explain the behaviour you're seeing becauseMeasureItem
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?