2012-04-25 279 views
0

我有一个ListBox,它有一个DrawItem事件定义如下。 ListBox的以下属性被设置为 - DrawMode = OwnerDrawFixed和FormattingEnabled = ture。为什么在执行DrawItem事件时ListBox闪烁?

当我运行程序并添加多个项目或对象到ListBox时,它闪烁真的很糟糕。我不确定问题到底是什么。我有其他WinForms上非常类似的设置ListBoxes,他们不闪烁。我尝试捕获闪烁ListBox的Winform图像,但捕获的图像并未每次都显示ListBox闪烁。

method HTrendFrm.AGroupList_DrawItem(sender: System.Object; e: System.Windows.Forms.DrawItemEventArgs); 
    var 
     lb:ListBox; 
     tg:TTrendGroup; 
    begin 
     if e.Index = -1 then exit; 
     lb := (sender as ListBox); 
     tg := TTrendGroup(LoggingGroup.Item[e.Index]); 
     if tg.Enabled then 
     begin 
     if ((e.State and DrawItemState.Selected) = DrawItemState.Selected) then 
     begin 
      lb.ForeColor:=Color.White; 
      e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); 
     end 
     else 
     lb.ForeColor := Color.Black; 
     end 
     else 
     lb.ForeColor := Color.LightGray; 

     lb.CreateGraphics.DrawString(tg.name,new Font('Arial',9,FontStyle.Bold),new SolidBrush(lb.ForeColor),e.Bounds.Left+5,e.Bounds.Top); 

     if ((e.State and DrawItemState.Focus) <> DrawItemState.Checked) then 
      e.DrawFocusRectangle(); 
    end; 

那么,是什么导致我的ListBox闪烁?

在此先感谢,

+0

您需要设置双缓冲区样式:http://msdn.microsoft.com/en-us/library/b367a457.aspx – asawyer 2012-04-25 13:21:32

+1

我不知道Delphi,但我认为你应该使用'e.Graphics '而不是'lb.CreateGraphics'。 – LarsTech 2012-04-25 13:22:12

回答

1

我遵循LarsTech的建议,它按预期工作。

我完全删除了lb listbox,并用e.Graphics替换了它。现在,它不再闪烁。