In a Windows.Forms.ListBox
with the property DrawMode
set to OwnerDrawVariable
, the ListBox
seems to cache the height of
According this MSDN
LB_SETITEMHEIGHT message
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);
}
}
The MeasureItemEventArgs
accepts a Graphics
object, if necessary, create one from the control and pass it in the first argument.