求助:關於TreeView構件的使用——當單擊(Click)某個結點(Node)時,該結點的imageindex會自動變成0,如何才能不讓結點的imageind

  • 主题发起人 主题发起人 gy87sz
  • 开始时间 开始时间
G

gy87sz

Unregistered / Unconfirmed
GUEST, unregistred user!
求助:關於TreeView構件的使用——當單擊(Click)某個結點(Node)時,該結點的imageindex會自動變成0,如何才能不讓結點的imageindex自動改變?(50分)<br />求助:關於TreeView構件的使用——當單擊(Click)某個結點(Node)時,該結
點的imageindex會自動變成0,如何才能不讓結點的imageindex自動改變?
=====================================================================
unit selectdir;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, imglist, shellApi;

type
TForm2 = class(TForm)
TreeView1: TTreeView;
procedure FormActivate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
Images:TImageList;
end;

var
Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.FormActivate(Sender: TObject);
var treenode1,treenode2:TTreeNode;
tmpicon :TIcon;
begin
tmpIcon:=TIcon.Create;
images:=TImageList.Create(self);
with images do
begin
tmpIcon.Handle:=ExtractIcon(handle,PChar('c:/windows/explorer.exe'),3);
addIcon(tmpIcon);
tmpIcon.Handle:=ExtractIcon(handle,PChar('c:/windows/explorer.exe'),0);
addIcon(tmpIcon);
tmpIcon.Handle:=ExtractIcon(handle,PChar('c:/windows/system/mydocs.dll'),0);
addIcon(tmpIcon);
tmpIcon.Handle:=ExtractIcon(handle,PChar('c:/windows/system/shell32.dll'),6);
addIcon(tmpIcon);
tmpIcon.Handle:=ExtractIcon(handle,PChar('c:/windows/system/shell32.dll'),8);
addIcon(tmpIcon);
tmpIcon.Handle:=ExtractIcon(handle,PChar('c:/windows/system/shell32.dll'),11);
addIcon(tmpIcon);
tmpIcon.Handle:=ExtractIcon(handle,PChar('c:/windows/system/shell32.dll'),3);
addIcon(tmpIcon);
tmpIcon.Handle:=ExtractIcon(handle,PChar('c:/windows/system/shell32.dll'),4);
addIcon(tmpIcon);
end;
tmpIcon.Free;
treeview1.Images:=Images;
// treeview1.StateImages:=Images;
with treeview1.Items do
begin
clear;
treenode1:=add(nil,'桌面');
treenode1.ImageIndex:=0;
treenode2:=addchild(treenode1,'我的電腦');
treenode2.ImageIndex:=1;
treenode2:=addchild(treenode1,'我的文檔');
treenode2.ImageIndex:=2;
end;

end;


procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Images.Free;
end;

end.
 
在treeview1中,items除了有ImageIndex属性外,还有selected index属性,
ImageIndex表示显示时的图标,selected index表示选中时的图标,它的默认值是0,
所以如果你没有设置,选中时就会显示imageindex=0的图标,所以你的程序应该这样写:
treeview1.Images:=Images;
with treeview1.Items do
begin
clear;
treenode1:=add(nil,'桌面');
treenode1.ImageIndex:=0;
treenode1.SelectedIndex:=0;
treenode2:=addchild(treenode1,'我的电脑');
treenode2.ImageIndex:=1;
treenode2.SelectedIndex:=2;
treenode3:=addchild(treenode1,'我的文档');
treenode3.ImageIndex:=2;
treenode3.SelectedIndex:=2;
end;
当然,如果你希望在节点选中时显示其他图标,也可以给出别的SelectedIndex。
 
后退
顶部