Delphi TTreeView OnCustomDrawItem事件将其降低

时间:2018-03-30 19:01:23

标签: performance delphi treeview custom-draw

我有一个用于记笔记的大纲应用程序(在Delphi 10.2东京)(我称之为NoteApp)。我有另一个用于编辑纯文本的应用程序(TextApp)。由于我在这些应用程序之间切换很多,我决定在TextApp中集成笔记记录功能。

我将代码从NoteApp复制/粘贴到TextApp,然后将组件(一个TTreeView,一个TRichEdit和一个TActionToolbar)放在TextApp.Form_Main上。 TTreeView的OnCustomDrawItem事件设置为根据相应音符项的NoteType更改每个节点的FontStyle,该音符项是一组简单记录:

type

  ///
  ///  Note types
  ///
  TNoteType = (ntNote, ntTodo, ntDone, ntNext, ntTitle) ;
  ///
  ///
  ///
  TNote = Record
    Text       ,
    Attachment ,
    Properties ,
    CloseDate  : String      ;
    NoteType   : TNoteType   ;
  End;

我们的阵列:

var
  Notes: Array of TNote ;

事件:

procedure TForm_Main.TreeView_NotesCustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  ///
  ///  First check to see if the application allows us to change visuals. If the
  ///  application is in processing mode, visual updates are not allowed.
  ///
  if ChangeAllowed AND Node.IsVisible then
    begin
      ///
      ///  Check NoteType of the corresponding note:
      ///
      case Notes[Node.AbsoluteIndex].NoteType of
        ntTitle:
          begin
            TreeView_Notes.Canvas.Font.Style := [fsBold] ;
          end;
        //ntNote:
        //  begin
        //  end;
        ntTodo:
          begin
            TreeView_Notes.Canvas.Font.Style := [fsBold] ;
          end;
        ntNext:
          begin
            TreeView_Notes.Canvas.Font.Style := [fsUnderline] ;
          end;
        ntDone:
          begin
            TreeView_Notes.Canvas.Font.Style := [fsStrikeOut] ;
          end;
      end;
    end;
end;

当我在NoteApp中打开一个笔记文件时,它完美无缺。当我在TextApp中打开相同的文件时,TTreeView会慢慢刷新。 TTreeView中的顶级项目没问题,但是越低,刷新率就越低 所有组件的属性都是相同的。 我怀疑我在某个地方犯了一个错误。我将TextApp上所有其他组件的可见性设置为false,但TTreeView仍然非常慢。 如果我删除上面的代码,它会再次变快。我不在TextApp中使用运行时主题。

1 个答案:

答案 0 :(得分:-1)

好的,我找到了问题的答案。

答案隐藏在上面的代码中,我发布的内容足以回答这个问题。事实证明,我发布的代码是MCVE。我发布答案,以防万一发生在其他人身上。

<强>答案:

结果Node.AbsoluteIndex非常缓慢。它不应该用作索引。

解决方案1:

我使用Node.Data作为索引,现在它非常快。

解决方案2:

我尝试和工作的替代解决方案:

  TTreeNodeNote = class(TTreeNode)
  public
    Note: TNote;
  end;

procedure TForm_Main.TreeView_NotesCreateNodeClass(Sender: TCustomTreeView;
  var NodeClass: TTreeNodeClass);
begin
  NodeClass := TTreeNodeNote;
end;

然后我们将数据存储在每个Node的Note属性中,而不是单独的数组中。像魅力一样。