【发布时间】:2012-10-23 10:06:19
【问题描述】:
我有一个自定义绘制方法,因此我可以将列表框中的项目设置为不同的颜色。问题是我每 500 毫秒重新绘制一次列表框以检查值是否已更改。这使列表框闪烁,我不知道如何双缓冲代码。有人可以帮忙吗?
private void listBox_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox sendingListBox = (ListBox)sender;
CustomListBoxItem item = sendingListBox.Items[e.Index] as CustomListBoxItem; // Get the current item and cast it to MyListBoxItem
if (item != null)
{
e.Graphics.DrawString( // Draw the appropriate text in the ListBox
item.Message, // The message linked to the item
zone1ListBox.Font, // Take the font from the listbox
new SolidBrush(item.ItemColor), // Set the color
0, // X pixel coordinate
e.Index * zone1ListBox.ItemHeight // Y pixel coordinate. Multiply the index by the ItemHeight defined in the listbox.
);
}
else
{
// The item isn't a MyListBoxItem, do something about it
}
}
【问题讨论】:
-
本机 ListBox 控件不支持双缓冲。使用 ListView 代替 View = List。这个代码:stackoverflow.com/a/8378761/17034
标签: c# listbox doublebuffered