Introduction
This is a simple TreeView
derived control that mimics the Windows Explorer interface. It provides no added functionality aside from the standard TreeView
control methods, properties, and events, however it does provide a ShellItem
class, which can be used to extend this basic example for your own needs, and it is also a good starting point for those wanting to get to grips with the system image list and Shell32 programming.
Background
Originally, I started researching this topic as I wanted to develop an FTP client in C# that would implement a local system explorer similar to that of the Windows Explorer, but it soon became apparent that it was not going to be an easy undertaking. Thanks to CodeProject and other code repositories, I came across some good examples of how to achieve what I wanted, however the best examples were written in VB.NET or Delphi, so I decided to write a simple C# control based on what I'd found.
Parts of the code are based on other CodeProject tutorials and code samples found elsewhere on the Internet. All of the code was written by myself but some of the concepts were "borrowed".
Helpful Hints
For each node in the TreeView
, an associated ShellItem object is created and stored in the node's "Tag
" property. The ShellItem
object allows you to retrieve information for the shell folder represented by the node, such as whether the folder has any sub-folders. For example, in the TreeView
's "OnBeforeCollapse
" event, we can obtain the ShellItem
object to perform a simple test...
ShellItem shNodeItem = (ShellItem)e.Node.Tag;
if (shNodeItem.HasSubFolder)
We can also get the folder's IShellFolder
interface from the ShellItem
object using the ShellFolder
property. Using this interface, we can do all sorts of nifty stuff, such as implementing shell context menus:
ShellItem shNodeItem = (ShellItem)e.Node.Tag;
IntPtr[] arrPidls = new IntPtr[0];
arrPidls[0] = shNodeItem.PIDL;
uint uRetVal =
shNodeItem.ShellFolder.GetUIObjectOf(IntPtr.Zero,
1, arrPidls, ref IID_IContextMenu,
IntPtr.Zero, out pCtx);
if (uRetVal != 0)
Marshal.ThrowExceptionForHR((int)uRetVal);
...
Marshal.ReleaseComObject(pCtx);
History
None yet.