Change Expand and Collapse Image?

Is it possible using the standard TTreeView to change the Expand and Collapse Image?

I don't mean Node images, I mean the little arrows next to Nodes that have children, like so:

Ideally I would like the arrows to show as + and - Symbols, like the Delphi component structure tree:

If it is possible to change this, how would I go about doing 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.

The Result:


Tree views in post-Vista Windows have two alternative themes. The theme that you are wanting to avoid is known as the explorer theme. You want to use the standard theme. A control has to opt-in to get the explorer theme. It does so via the SetWindowTheme API. The VCL tree view control calls this to opt-in. It does so at the end of its CreateWnd method.

You can revert to the standard theme by undoing the change like this:

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 then I think you want it like this:

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

I addition to Davids answer. Put the following code in some extra unit and add it in the uses after the ComCtrls unit. That way you can use the standard TTreeView and change the theme whenever you like. Or register it in your own package if you like.

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 could also use a class helper to avoid the extra inheritance.

链接地址: http://www.djcxy.com/p/34986.html

上一篇: 从Delphi WebBrowser扩展obout.com ASP.NET TreeView

下一篇: 更改展开和折叠图像?