2010-07-03 60 views
7


我正在嘗試使用戶控件類似於Windows資源管理器中使用的Windows Vista/7 breadcrumb欄。使用.NET中的ScrollBar的DropDown菜單

但是,當我顯示包含多個子項目的麪包屑的下拉菜單時,會得到一個很長的列表,有時會超出屏幕大小。
但是,在Windows Vista/7示例中,每次最多顯示18個項目,並且當子項目數超過此數量(18)時,滾動條出現在右側。

我想知道有沒有人知道複製微軟的方法。
[也就是說,如何在具有自動滾動功能的控件中放置下拉菜單。]



謝謝。
Alex

+0

你是什麼意思準確的下拉菜單?哪個.net控制? (一個工具欄上的DropDownButton ??) – digEmAll 2010-07-03 14:06:22

+0

我的意思是一個ToolStripDropDown。 您在運行ContextMenu.Show或當您單擊ToolStripMenuItem時獲得的種類。 – 2010-07-03 14:25:48

回答

7

Windows 7/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); 
} 
+0

謝謝!我從來不知道你可以很容易地在ToolStripDropDown中嵌入一個Control [或其衍生物]。你真的很有幫助。 – 2010-07-03 23:21:11

+0

爲什麼選擇ListView而不是ComboBox? – 2011-04-05 11:56:30

+0

@Cody:因爲OP需要使用右鍵單擊來顯示列表框。使用組合將會有點不同... – digEmAll 2011-04-05 12:25:34

2

我推薦用Spy ++來看看它。所有東西都由標準的Windows控件組成,嵌套嚴格。下拉菜單是一個組合框,用於實現。這是一個很大程度上被遺忘的定製的,名爲ComboBoxEx。我從來沒有見過.NET包裝器,可能是因爲它的工作很容易通過Windows窗體中普通的舊ComboBox包裝器實現。

只需將其DrawMode屬性設置爲OwnerDrawFixed並實現DrawItem事件即可顯示圖標和文本。在MSDN Library article中有一個很好的例子。

2

如果要訪問Vista API來呈現欄,請查看Vista Bridge library。您可以在其中一個樣本中找到麪包屑控制棒樣本。

但我不確定它是否會在WinXP上呈現。

+0

 Your answer provided me with the Microsoft-built breadcrumb and I thank you for that. The solution, however did not take into account the scrollbar issue so I'll have to combine the your suggestion with digEmAll's Thanks. 
2010-07-03 23:17:50