代码之家  ›  专栏  ›  技术社区  ›  Alex Essilfie

NET中带滚动条的下拉菜单

  •  7
  • Alex Essilfie  · 技术社区  · 15 年前


    我正在尝试创建一个类似于Windows资源管理器中使用的Windows Vista/7面包屑栏的用户控件。

    但是,当我显示包含许多子项的breadcrumb的下拉菜单时,会得到一个非常长的列表,有时会超过屏幕大小。
    但是,在WindowsVista/7示例中,一次最多显示18个项目,当子项目数超过此数字(18)时,右侧会显示一个滚动条。


    [即,如何在具有自动滚动功能的控件中放置下拉菜单。]



    谢谢。

    3 回复  |  直到 15 年前
        1
  •  9
  •   Alex Essilfie    9 年前

    Windows7/Vista面包屑看起来类似于列表视图。 下面的图片给出了一个示例(在windows xp上),说明了我的意思(单击按钮将显示列表):

    Windows 7 breadcrumb sample

    下面是代码:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            var button = sender as Button;
    
            // create fake items list
            List<string> strings = new List<string>();
            for (int i = 0; i < 36; i++)
                strings.Add("ITEM " + (i+1));
            var listViewItems = strings.Select(x => new ListViewItem(x, 0)).ToArray();
    
            // create a new list view
            ListView listView = new ListView();
            listView.View = View.SmallIcon;
            listView.SmallImageList = imageList1;
            listView.MultiSelect = false;
    
            // add items to listview
            listView.Items.AddRange(listViewItems);
    
            // calculate size of list from the listViewItems' height
            int itemToShow = 18;
            var lastItemToShow = listViewItems.Take(itemToShow).Last();
            int height = lastItemToShow.Bounds.Bottom + listView.Margin.Top;
            listView.Height = height;
    
            // create a new popup and add the list view to it
            var popup = new ToolStripDropDown();
            popup.AutoSize = false;
            popup.Margin = Padding.Empty;
            popup.Padding = Padding.Empty;
            ToolStripControlHost host = new ToolStripControlHost(listView);
            host.Margin = Padding.Empty;
            host.Padding = Padding.Empty;
            host.AutoSize = false;
            host.Size = listView.Size;
            popup.Size = listView.Size;
            popup.Items.Add(host);
    
            // show the popup
            popup.Show(this, button.Left, button.Bottom);
        }
    }
    

    编辑:

    要获取所选项目,一种方法如下:

    // change some properties (for selection) and subscribe the ItemActivate 
    // event of the listView
    listView.HotTracking = true;
    listView.Activation = ItemActivation.OneClick;
    listView.ItemActivate += new EventHandler(listView_ItemActivate);
    
    
    // the click on the item invokes this method
    void listView_ItemActivate(object sender, EventArgs e)
    {
        var listview = sender as ListView;
        var item = listview.SelectedItems[0].ToString();
        var dropdown = listview.Parent as ToolStripDropDown;
        // unsubscribe the event (to avoid memory leaks)
        listview.SelectedIndexChanged -= listView_ItemActivate;
        // close the dropdown (if you want)
        dropdown.Close();
    
        // do whatever you want with the item
        MessageBox.Show("Selected item is: " + item);
    }
    
        2
  •  2
  •   Hans Passant    15 年前

    我建议用Spy++来看看。所有的东西都是由标准的Windows控件组成的,嵌套得很重。下拉列表实现为drumroll,一个组合框。这是一个很大程度上被遗忘的定制产品,名为 ComboBoxEx . 我从来没有见过它的.NET包装器,可能是因为它所做的工作很容易由Windows窗体中普通的旧组合框包装器实现。

    MSDN Library article

        3
  •  2
  •   vgru    15 年前

    如果您想访问vistaapi来呈现工具栏,请查看 Vista Bridge library . 您可以在其中一个示例中找到面包屑条控件的示例。

    不过,我不确定它是否会在WinXP上呈现。

    推荐文章