How to disable a TreeView control without selecting all nodes?

久未见 提交于 2021-01-27 05:01:13

问题


I don't know if this is a bug or something, but if I try to disable a TTreeView control, all the nodes become selected (grayed out)... Can anything be done to just disable the input for this control without changing the selection ? Of course, the node are not really selected, they are just visually selected, but this is annoying.


回答1:


That's how the disabled control looks like when no theme is applied. You can modify it with little intervention to item drawing:

procedure TForm1.TreeView1AdvancedCustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; Stage: TCustomDrawStage;
  var PaintImages, DefaultDraw: Boolean);
begin
  if (not TreeView1.Enabled) and
      (GetWindowTheme(TreeView1.Handle) = 0) and (Stage = cdPrePaint) then begin
    TreeView1.Canvas.Brush.Color := clWindow; // or TreeView1.Color
    TreeView1.Canvas.Font.Color := clGrayText;
  end;
end;

Unfortunately the State never includes 'cdsDisabled' or 'cdsGrayed' (which I didn't investigate), so the code tests if the treeview is enabled or not.



来源:https://stackoverflow.com/questions/59216513/how-to-disable-a-treeview-control-without-selecting-all-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!