2009-08-05 16 views

答えて

3

いいえ、しかし、私はあなたがそれが起こることができると確信していますTreeViewで、あまりにもトリッキーであってはいけない、またはListViewを使用することができますリストをしたい場合。ドライブを取得する

コードは次のようになります

//Get all Drives 
DriveInfo[] ListAllDrives = DriveInfo.GetDrives(); 

あなたはこのような何かを行うことができますのListViewItemまたはTreeViewNodesのアイコンを決定するには:

foreach (DriveInfo Drive in ListAllDrives) 
{ 
    //Create ListViewItem, give name etc. 
    ListViewItem NewItem = new ListViewItem(); 
    NewItem.Text = Drive.Name; 

    //Check type and get icon required. 
    if (Drive.DriveType.Removable) 
    { 
    //Set Icon as Removable Icon 
    }  
    //else if (Drive Type is other... etc. etc.) 
} 
0

お支払いをご希望の場合は、http://viewpack.qarchive.org/をご確認ください。

私はフリーコントロールについて認識していません。

0

やっときました私自身のコントロールを思いついた。

次のように私はドライブを搭載したリストビューを埋める:

GetFileIconはSHGetFileInfoを呼び出して自分の方法である
listView1.SmallImageList = new ImageList(); 
var drives = DriveInfo.GetDrives() 
         .Where(x => x.DriveType == DriveType.Removable) 
         .Select(x => x.Name.Replace("\\","")); 
foreach (var driveName in drives) 
{ 
    listView1.SmallImageList.Images.Add(driveName, GetFileIcon(driveName)); 
    listView1.Items.Add(driveName, driveName); 
} 

:フォームthis siteで、次のように見えるよう

IntPtr hImgSmall; //the handle to the system image list 
SHFILEINFO shinfo = new SHFILEINFO(); 
//Use this to get the small Icon 
hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo, 
           (uint)Marshal.SizeOf(shinfo), 
           Win32.SHGFI_ICON | 
           Win32.SHGFI_SMALLICON); 

return System.Drawing.Icon.FromHandle(shinfo.hIcon); 

のWin32クラスがコピーされます。

[StructLayout(LayoutKind.Sequential)] 
public struct SHFILEINFO 
{ 
    public IntPtr hIcon; 
    public IntPtr iIcon; 
    public uint dwAttributes; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 
    public string szDisplayName; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] 
    public string szTypeName; 
}; 

class Win32 
{ 
    public const uint SHGFI_ICON = 0x100; 
    public const uint SHGFI_LARGEICON = 0x0; // 'Large icon 
    public const uint SHGFI_SMALLICON = 0x1; // 'Small icon 

    [DllImport("shell32.dll")] 
    public static extern IntPtr SHGetFileInfo(string pszPath, 
     uint dwFileAttributes, 
     ref SHFILEINFO psfi, 
     uint cbSizeFileInfo, 
     uint uFlags); 
} 

私はそれが誰かを助けることを願っています。

+1

shell32.dllを使用しているときに注意してください。変更する可能性があります(あなたはMicrosoftのようなものを知っています!)、ここでアイコンの使用に関する質問をしました。http://stackoverflow.com/questions/949196/読み込み中のアイコンがshell32-dl​​l-win32-handle-not-valid-or-the-wrong-typeの場合 – ThePower

+0

これはVistaで動作し、XPでは動作しません –