tags:

views:

128

answers:

2

Hi, anyone know how to modify ShellCtrls.pas? Actually I want to add some items in the top of explorer tree.

For example :

-myitem    
-miitemtoo
-mycomputer
-c:
-d:

Or maybe modifying this enhanced treeview : http://delphi.about.com/library/weekly/code/gtrocheckshelltreeview.zip

But I think ShellCtrls is the main file to be modified.

+1  A: 

Well, to strictly answer your question, to modify ShellCtrls.pas, you open it in the Code Editor, make the changes that you want there, and then compile it into your app.

But, I'd strongly recommend against doing that. The best way to make changes to it is to create a descendant component. That's the way things are done in the OOP world.

So instead of modifying existing classes, create a descendant class.

Nick Hodges
A: 

I don't think you need to modify the sources. The stock TShellTreeView is a descendant of TCustomTreeView, hence you can add items as in a TreeView.

The below code is not throughly thought out or tested, but it might get you started. It inserts at the top of the ShellTreeView an item with display name as the executable name and with path as the path to the executable.

uses
  shlobj, activex, shellapi;

function InsertToSTV(STV: TShellTreeView; Pos: Integer;
    Path, DisplayName: string): Boolean;
var
  ShellFolderInterface, NodeShellFolder: IShellFolder;
  ItemIDList: PItemIDList;
  CharsParsed, Attributes: ULONG;
  wPath: PWideChar;
  Node: TTreeNode;
  FileInfo: TSHFileInfo;
begin
  Result:= False;
  if (SHGetDesktopFolder(ShellFolderInterface) = NOERROR) then begin
    wPath:= StringToOleStr(Path);
    if wPath <> nil then
      try
        if ShellFolderInterface.ParseDisplayName(0, nil, wPath, CharsParsed,
            ItemIDList, Attributes) = NO_ERROR then

          ShellFolderInterface.BindToObject(ItemIDList, nil, IID_IShellFolder,
              NodeShellFolder);
          Node := STV.Items.Insert(STV.Items[Pos], DisplayName);
          Node.Data := TShellFolder.Create(nil, ItemIDList, NodeShellFolder);

          if STV.UseShellImages and not Assigned(STV.Images) then begin
            SHGetFileInfo(PChar(ItemIDList), 0,
                          FileInfo,
                          SizeOf(FileInfo),
                          SHGFI_PIDL or SHGFI_SYSICONINDEX);
            Node.ImageIndex := FileInfo.iIcon;
            SHGetFileInfo(PChar(ItemIDList), 0,
                          FileInfo,
                          SizeOf(FileInfo),
                          SHGFI_PIDL or SHGFI_SYSICONINDEX or SHGFI_OPENICON);
            Node.SelectedIndex := FileInfo.iIcon;
          end;
        finally
          SysFreeString(wPath);
        end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  InsertToSTV(ShellTreeView1,
             0,
             ExtractFilePath(Application.ExeName),
             ExtractFileName(Application.ExeName));
end;
Sertac Akyuz