/// <summary>
        /// Initializes a new instance of the <see cref="ShellTreeView"/> class.
        /// </summary>
        public ShellTreeView()
        {
            m_TreeView               = new TreeViewBase();
            m_TreeView.Dock          = DockStyle.Fill;
            m_TreeView.BackColor     = Color.White;
            m_TreeView.HideSelection = false;
            m_TreeView.HotTracking   = true;
            m_TreeView.Parent        = this;
            m_TreeView.ShowRootLines = false;
            m_TreeView.AfterSelect  += new TreeViewEventHandler(m_TreeView_AfterSelect);
            m_TreeView.BeforeExpand += new TreeViewCancelEventHandler(m_TreeView_BeforeExpand);
            m_TreeView.ItemDrag     += new ItemDragEventHandler(m_TreeView_ItemDrag);
            m_TreeView.MouseDown    += new MouseEventHandler(m_TreeView_MouseDown);
            m_TreeView.MouseUp      += new MouseEventHandler(m_TreeView_MouseUp);
            m_TreeView.BorderStyle   = BorderStyle.None;
            m_ScrollTimer.Interval   = 250;
            m_ScrollTimer.Tick      += new EventHandler(m_ScrollTimer_Tick);
            Size = new System.Drawing.Size(120, 100);
            SystemImageList.UseSystemImageList(m_TreeView);

            m_ShellListener.DriveAdded     += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
            m_ShellListener.DriveRemoved   += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
            m_ShellListener.FolderCreated  += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
            m_ShellListener.FolderDeleted  += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
            m_ShellListener.FolderRenamed  += new ShellItemChangeEventHandler(m_ShellListener_ItemRenamed);
            m_ShellListener.FolderUpdated  += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
            m_ShellListener.ItemCreated    += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
            m_ShellListener.ItemDeleted    += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
            m_ShellListener.ItemRenamed    += new ShellItemChangeEventHandler(m_ShellListener_ItemRenamed);
            m_ShellListener.ItemUpdated    += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
            m_ShellListener.SharingChanged += new ShellItemEventHandler(m_ShellListener_ItemUpdated);

            // Setting AllowDrop to true then false makes sure OleInitialize()
            // is called for the thread: it must be called before we can use
            // RegisterDragDrop. There is probably a neater way of doing this.
            m_TreeView.AllowDrop = true;
            m_TreeView.AllowDrop = false;
            this.DoubleBuffered  = true;
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            CreateItems();
        }
Пример #2
0
        void m_Combo_DrawItem(object sender, DrawItemEventArgs e)
        {
            int iconWidth = SystemInformation.SmallIconSize.Width;
            int indent    = ((e.State & DrawItemState.ComboBoxEdit) == 0) ? (iconWidth / 2) : 0;

            if (e.Index != -1)
            {
                string    display;
                ComboItem item      = (ComboItem)m_Combo.Items[e.Index];
                Color     textColor = SystemColors.WindowText;
                Rectangle textRect;
                int       textOffset;
                SizeF     size;

                if ((e.State & DrawItemState.ComboBoxEdit) != 0)
                {
                    // Don't draw the folder location in the edit box when
                    // the control is Editable as the edit control will
                    // take care of that.
                    display = m_Editable ? string.Empty : GetEditString();
                }
                else
                {
                    display = item.Folder.DisplayName;
                }

                size = TextRenderer.MeasureText(display, m_Combo.Font);

                textRect   = new Rectangle(e.Bounds.Left + iconWidth + (item.Indent * indent) + 3, e.Bounds.Y, (int)size.Width, e.Bounds.Height);
                textOffset = (int)((e.Bounds.Height - size.Height) / 2);

                // If the text is being drawin in the main combo box edit area,
                // draw the text 1 pixel higher - this is how it looks in Windows.
                if ((e.State & DrawItemState.ComboBoxEdit) != 0)
                {
                    textOffset -= 1;
                }

                if ((e.State & DrawItemState.Selected) != 0)
                {
                    e.Graphics.FillRectangle(SystemBrushes.Highlight, textRect);
                    textColor = SystemColors.HighlightText;
                }
                else
                {
                    e.DrawBackground();
                }

                if ((e.State & DrawItemState.Focus) != 0)
                {
                    ControlPaint.DrawFocusRectangle(e.Graphics, textRect);
                }

                SystemImageList.DrawSmallImage(
                    e.Graphics,
                    new Point(e.Bounds.Left + (item.Indent * indent), e.Bounds.Top),
                    item.Folder.GetSystemImageListIndex(ShellIconType.SmallIcon,
                                                        ShellIconFlags.OverlayIndex),
                    (e.State & DrawItemState.Selected) != 0
                    );

                TextRenderer.DrawText(e.Graphics, display, m_Combo.Font, new Point(textRect.Left, textRect.Top + textOffset), textColor);
            }
        }