gpt4 book ai didi

c# - 如何让组合框重新测量项目高度?

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:32 24 4
gpt4 key购买 nike

我正在创建一个可以绘制分隔符的自定义组合框。因此,我覆盖了 OnDrawItem() 和 OnMeasureItem() 方法。
问题是 OnMeasureItem() 在数据源更改时仅被调用一次。所以如果我想稍后指定一个分隔项,我需要重新测量它的高度(因为带有分隔符的项目应该更高),但似乎所有可以导致项目高度被重新测量的方法都是私有(private)的,所以我不能调用它们.
我不知道我上面写的是否容易理解,所以我会重复我需要的:
每次指定应使用分隔符绘制项目时,我都需要重新测量项目高度(必须调用 OnMeasureItem())。

separatorableComboBox.DataSource = customers;
// and now I want the third customer in the list to be drawn with a separator,
// so it needs to be taller and therefore OnMeasureItem() should be called
separatorableComboBox.SpecifySeparatorItem(customers[2]);

UPD. 伙计们,调用 RefreshItems() 有效,但它非常慢(在我的机器上 > 20 毫秒),有更快的方法吗?
UPD2. 现在我正在使用 SendMessage(..., CB_SETITEMHEIGHT, ...); serge_gubenko 建议的方法。但我很好奇是否有一种快速的方法可以使用 .NET(或者更具体地说,使用 ComboBox 类本身)完成任务?

最佳答案

澄清一下,我假设您正在为您的组合框使用 OwnerDrawVariable 样式。如果我正确理解你的问题,有几种方法可以满足你的需要:

  • 调用组合框的 RefreshItems 方法,该方法将重新创建项目并触发每个项目的 onMeasureItem 事件。此方法受 ComboBox 类的保护,因此下面是一个关于如何使用反射执行此操作的示例:
    MethodInfo method = comboBox1.GetType().GetMethod(
"RefreshItems", BindingFlags.Instance | BindingFlags.NonPublic);
if (method != null) method.Invoke(comboBox1, null);
  • 发送CB_SETITEMHEIGHT每当您想要更改它时,使用项目的新高度向控件发送消息:
    public const int CB_SETITEMHEIGHT = 0x0153;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
...
// this will set height to 100 for the item with index 2
SendMessage(comboBox1.Handle, CB_SETITEMHEIGHT, 2, 100);

希望这对你有帮助,问候

关于c# - 如何让组合框重新测量项目高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2098725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com