public void BuildDriveList() { base.Items.Clear(); foreach (DriveInfo drive in DriveInfo.GetDrives()) { int imageIndex = 0; if (drive.DriveType == DriveType.CDRom) { imageIndex = 1; } else if (drive.DriveType == DriveType.Removable) { imageIndex = 2; } ImageComboItem item = new ImageComboItem($"{drive.Name} ({drive.DriveType})", imageIndex, false); item.ItemValue = drive.Name; base.Items.Add(item); } if (base.Items.Count != 0) { base.SelectedIndex = 0; } }
public void BuildDriveList() { base.Items.Clear(); ShellAPI.SHFILEINFO shInfo = new ShellAPI.SHFILEINFO(); ShellAPI.SHGFI dwAttribs = ShellAPI.SHGFI.SHGFI_ICON | ShellAPI.SHGFI.SHGFI_SMALLICON | ShellAPI.SHGFI.SHGFI_SYSICONINDEX | ShellAPI.SHGFI.SHGFI_DISPLAYNAME; ListDictionary _iconDict = new ListDictionary(); foreach (string drive in System.IO.Directory.GetLogicalDrives()) { IntPtr m_pHandle = ShellAPI.SHGetFileInfo(drive, ShellAPI.FILE_ATTRIBUTE_NORMAL, ref shInfo, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shInfo), dwAttribs); if (m_pHandle.Equals(IntPtr.Zero) == false) { int idxIcon = 0; if (_iconDict.Contains(shInfo.iIcon) == false) { base.ImageList.Images.Add(System.Drawing.Icon.FromHandle(shInfo.hIcon).Clone() as System.Drawing.Icon); User32API.DestroyIcon(shInfo.hIcon); _iconDict.Add(shInfo.iIcon, _iconDict.Count); idxIcon = _iconDict.Count - 1; } else { idxIcon = Convert.ToInt32(_iconDict[shInfo.iIcon]); } try { DriveInfo drv_info = new DriveInfo(shInfo.szDisplayName.Substring(shInfo.szDisplayName.IndexOf("(") + 1, 1)); if (drv_info.DriveType == DriveType.CDRom) { ImageComboItem item = new ImageComboItem(shInfo.szDisplayName, idxIcon, false); item.ItemValue = drive; base.Items.Add(item); } } catch { } } } if (base.Items.Count != 0) { base.SelectedIndex = 0; } }
// customized drawing process protected override void OnDrawItem(DrawItemEventArgs e) { // draw background & focus rect e.DrawBackground(); e.DrawFocusRectangle(); // check if it is an item from the Items collection if (e.Index < 0) { // not an item, draw the text (indented) e.Graphics.DrawString(this.Text, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top); } else { // check if item is an ImageComboItem if (this.Items[e.Index].GetType() == typeof(ImageComboItem)) { // get item to draw ImageComboItem item = (ImageComboItem)this.Items[e.Index]; // get forecolor & font Color forecolor = (item.ForeColor != Color.FromKnownColor(KnownColor.Transparent)) ? item.ForeColor : e.ForeColor; Font font = item.Mark ? new Font(e.Font, FontStyle.Bold) : e.Font; // -1: no image if (item.ImageIndex != -1) { // draw image, then draw text next to it this.ImageList.Draw(e.Graphics, e.Bounds.Left, e.Bounds.Top, item.ImageIndex); e.Graphics.DrawString(item.Text, font, new SolidBrush(forecolor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top); } else { // draw text (indented) e.Graphics.DrawString(item.Text, font, new SolidBrush(forecolor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top); } } else { // it is not an ImageComboItem, draw it e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top); } } base.OnDrawItem(e); }