0

リストボックスのアイテムを異なる色にできるように、カスタム描画メソッドがあります。問題は、値が変更されたかどうかを確認するために、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
    }
}
4

1 に答える 1

4

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);
    }
}
于 2012-11-02T17:49:53.270 に答える