Windows.Forms.ListBox与OwnerDrawVariable错误? [英] Windows.Forms.ListBox with OwnerDrawVariable bug?

查看:438
本文介绍了Windows.Forms.ListBox与OwnerDrawVariable错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows.Forms.ListBox 与物业 DrawMode 设置为 OwnerDrawVariable 的ListBox 似乎缓存项目的高度,什么是好的。

In a Windows.Forms.ListBox with the property DrawMode set to OwnerDrawVariable, the ListBox seems to cache the height of the items, what is good.

但是,作为该项目的高度依赖的宽度,因为它使用 Graphics.MeasureString 做自动换行,需要计算项目的高度,如果的大小的ListBox 已经改变。然后还有一个问题。

BUT, being the item height dependent of the width, because it uses Graphics.MeasureString to do word wrap, needs to calculate the height of items if the size of the ListBox has changed. Then there's a problem.

的ListBox 默认不这样做,我找不到一个方法来清除缓存,迫使的ListBox 来提高itemheight事件。

The ListBox doesn't do this by default, and I can't find a method to clear the cache, forcing the ListBox to raise the itemheight event.

任何解决方案?我试图让为ListBox的来源,但没有找到关于任何事情,使派生类,并清除该缓存。

Any solution? I tried to get the source for the ListBox but don't find anything about that to make a derived class and clear this cache.

(复制尝试的项目到一个数组再次清除 ListBox.Items ,和TEM加入阵列。这甚至抛出异常的的ListBox 调用DRAWITEM或itemheight事件无效项指数)

(Tried copying the items to an array, clearing the ListBox.Items, and tem adding the array again. This even throw exceptions as the ListBox calling the drawitem or itemheight events with invalid item index)

推荐答案

据的此内容 MSDN

LB_SETITEMHEIGHT信息

设定的高度,以像素为单位,在一个列表框中的项目。如果列表框具有
LBS_OWNERDRAWVARIABLE 的风格,此消息设置由wParam参数指定的项目
的高度。否则,此消息在设置列表框中的所有项目的高度

Sets the height, in pixels, of items in a list box. If the list box has the LBS_OWNERDRAWVARIABLE style, this message sets the height of the item specified by the wParam parameter. Otherwise, this message sets the height of all items in the list box.

因此​​,这将做到这一点。

So this will do it

private const int LB_SETITEMHEIGHT = 0x01A0;

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

private void ListBoxExample_Resize(object sender, EventArgs e)
{
    for (int i = 0; i < ListBoxExample.Items.Count; i++)
    {

        MeasureItemEventArgs eArgs = new MeasureItemEventArgs(null, i);
        ListBoxExample_MeasureItem((object)ListBoxExample, eArgs);
        SendMessage((IntPtr) ListBoxExample.Handle, LB_SETITEMHEIGHT, (IntPtr) i, (IntPtr) e.ItemHeight);
    }
}



MeasureItemEventArgs 接受图形对象,如果有必要,创建一个从控制,并通过它的第一个参数。

The MeasureItemEventArgs accepts a Graphics object, if necessary, create one from the control and pass it in the first argument.

这篇关于Windows.Forms.ListBox与OwnerDrawVariable错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆