【问题标题】:double buffer customer drawn list box双缓冲客户绘制列表框
【发布时间】: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
    }
}

【问题讨论】:

标签: c# listbox doublebuffered


【解决方案1】:

如果你需要启用ListBox的双缓冲你应该继承一个类,因为属性是private并设置它为true,或者使用SetStyle()方法并应用WS_EX_COMPOSITED标志:

public class DoubleBufferedListBox : ListBox {
    protected override CreateParams CreateParams {
        get {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;   // WS_EX_COMPOSITED
            return cp;
        }
    }

    public DoubleBufferedListBox( ) {
        this.SetStyle(ControlStyles.DoubleBuffer, true);         
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
    }
}

【讨论】:

  • 感谢您的回复。列表框是一个 Windows 控件,所以我不确定如何将它重新定义为 doubleBufferedListBox?
  • 你的意思是从设计器窗口设置吗?
  • 是的,我从设计器窗口设置控件并简单地将其放置在表单上,​​因此它被定义为 ListBox,我不知道如何使用自定义列表框调用您的代码。有没有办法让图形对象在我的代码中双缓冲?
  • 如果你想在windows设计器中使用当前的listbox,你可以打开Form.Designer.cs找到listbox声明的位置,并设为private DoubleBufferedListBox listBoxName;,然后找到它的初始化位置并初始化它是listBoxName = new DoubleBufferedListBox()。否则,如果您想从工具箱中添加一个新的 DoubleBufferedListBox,请编译一次代码,然后您就会找到它。
  • 如果有人收到ControlStyles.DoubleBuffered 未找到的错误,请使用ControlStyles.DoubleBuffer
猜你喜欢
  • 2010-11-11
  • 2012-02-11
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多