为什么在DrawItem事件执行时ListBox会闪烁?

时间:2012-04-25 13:16:53

标签: .net winforms listbox flicker delphi-prism

我有一个ListBox,它有一个如下定义的DrawItem事件。 ListBox的以下属性设置如下 - DrawMode = OwnerDrawFixed和FormattingEnabled = ture。

当我运行程序并向ListBox添加多个项目或对象时,它会非常糟糕。我不确定究竟是什么问题。我有ListBoxes与其他winforms非常相似的设置,他们不闪烁。我尝试用闪烁的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闪烁?

提前致谢,

1 个答案:

答案 0 :(得分:1)

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

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