How to disable a TreeView control without selecting all nodes?
Asked Answered
B

1

7

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.

enter image description here

Bondage answered 6/12, 2019 at 16:1 Comment(4)
Enable runtime themes?Valerie
They are enabled (in Delphi project options), but I have Windows 7 with classic theme. :)Bondage
Place the tree view in a panel and disable the panel instead.Demonic
@Peter - There will be no visual indication if the treeview is disabled or not.Valerie
V
6

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.

Valerie answered 6/12, 2019 at 17:24 Comment(1)
Great ! It's exactly what I needed. But I included in the test ...and not (cdsSelected in State) so the selected node remains grayed like it should. So, thanks a lot, Sertac Akyuz ! :)Bondage

© 2022 - 2024 — McMap. All rights reserved.