-3

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

我将代码从 NoteApp 复制/粘贴到 TextApp,并将组件(一个 TTreeView、一个 TRichEdit 和一个 TActionToolbar)放在 TextApp.Form_Main 上。TTreeView 的 OnCustomDrawItem 事件设置为根据对应的注释项的 NoteType 更改每个 Node 的 FontStyle,NoteType 是一个简单记录的数组:

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 中的 top 项是可以的,但是越往下,刷新率越低。
所有组件的属性设置相同。我怀疑我在某个地方犯了错误。我将 TextApp 上所有其他组件的可见性设置为 false,但 TTreeView 仍然非常缓慢。如果我删除上面的代码,它会再次变快。我不在 TextApp 中使用运行时主题。

4

1 回答 1

-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 属性中,而不是单独的数组中。奇迹般有效。

于 2018-03-31T14:24:52.217 回答