TTreeView - change expand and collapse image?

Is it possible to use the standard TTreeView to change the spread and smooth the image?

I do not mean the Node of the image, I mean the small arrows next to nodes that have children, for example:

enter image description here

Ideally, I would like the arrows to appear as + and - Symbols, as the tree structure of the Delphi components:

enter image description here

If this can be changed, how would I do it?

Working demo based on David's answer

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, Themes, uxTheme; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; type TMyTreeView = class(TTreeView) protected procedure CreateWnd; override; end; var Form1: TForm1; implementation {$R *.dfm} { TMyTreeView } procedure TMyTreeView.CreateWnd; begin inherited; if ThemeServices.Enabled and CheckWin32Version(6, 0) then SetWindowTheme(Handle, nil, nil); end; procedure TForm1.FormCreate(Sender: TObject); var MyTree: TMyTreeView; Node: TTreeNode; begin MyTree := TMyTreeView.Create(nil); with MyTree do begin Parent := Self; Height := 100; Width := 100; Left := 30; Top := 30; Node := Items.Add(nil, 'Item'); Items.AddChild(Node, 'Item'); Node := Items.AddChild(Node, 'Item'); Items.AddChild(Node, 'Item'); end; end; end. 

Result:

enter image description here

+6
source share
2 answers

There are two alternative themes in the tree view in Windows Vista. The theme you want to avoid is known as the Explorer theme. You want to use a standard theme. The control must choose to get the explorer theme. He does this through the SetWindowTheme API. The VCL tree control causes this to fail. He does this at the end of his CreateWnd method.

You can return to the standard theme by undoing this change as follows:

 type TMyTreeView = class(TTreeView) protected procedure CreateWnd; override; end; procedure TMyTreeView.CreateWnd; begin inherited; if StyleServices.Enabled and TOSVersion.Check(6) and StyleServices.IsSystemStyle then SetWindowTheme(Handle, nil, nil); end; 

This code is written for XE2. If you have an earlier Delphi, I think you want it to be like this:

  if ThemeServices.Enabled and CheckWin32Version(6, 0) then SetWindowTheme(Handle, nil, nil); 
+9
source

I will add Davids to the answer. Put the following code in some additional block and add it to use after the ComCtrls block. This way you can use the standard TTreeView and change the theme whenever you want. Or register it in your own package if you want.

 type TTreeView = class(ComCtrls.TTreeView) private procedure SetExplorerTheme(const Value: Boolean); public property ExplorerTheme: Boolean write SetExplorerTheme; end; procedure TTreeView.SetExplorerTheme(const Value: Boolean); begin if ThemeServices.ThemesEnabled and CheckWin32Version(6, 0) then if Value then SetWindowTheme(Handle, 'Explorer', nil) else SetWindowTheme(Handle, nil, nil); end; 

In never Delphi versions, you can also use the class helper to avoid extra inheritance.

+3
source

Source: https://habr.com/ru/post/903231/


All Articles