開發(fā)環(huán)境基于VSTO:visual studio 2010,,VB .Net,,excel 2007,文檔級(jí)別的定制程序,。 需求是在sheet的左側(cè)??縎ystem.Windows.Forms.TreeView控件,實(shí)現(xiàn)類似資源瀏覽器的效果,,另外,,tree節(jié)點(diǎn)使用自定義的圖標(biāo),支持復(fù)選框,。 首先準(zhǔn)備好樹節(jié)點(diǎn)的圖標(biāo),,使用visual studio 2010自帶的圖標(biāo)可以省去很多麻煩(在安裝目錄\Common7\VS2010ImageLibrary)。我挑選了4個(gè)16x16大小的圖標(biāo)拷貝到vsto工程下的Resources目錄下,。通過Project->xxx Properties->Resources->AddResource->Add Existing File添加圖標(biāo)到工程中,,代碼中就可以用My.Resources.aaa的方式引用了(aaa為圖標(biāo)名稱)。 接下來,,在sheet初始化函數(shù)中完成圖標(biāo)與TreeView控件的初始化代碼,,如果TreeView控件的內(nèi)容需要隨sheet數(shù)據(jù)變化而刷新,還需要在sheet_change函數(shù)中添加代碼,。
Public Class Sheet1 Public WithEvents m_tree As New TreeView Public m_imageList As New ImageList Private Sub Sheet1_Startup() Handles Me.Startup m_imageList.ColorDepth = ColorDepth.Depth32Bit '第一個(gè)參數(shù)為ImageKey,,類似Image名稱的作用 m_imageList.Images.Add("cfg", My.Resources.book_notebook) m_imageList.Images.Add("algo", My.Resources._075b_UpFolder_16x16_72) m_imageList.Images.Add("scen", My.Resources.PlayHS) m_imageList.Images.Add("warning", My.Resources._109_AllAnnotations_Warning_16x16_72) Globals.ThisWorkbook.ActionsPane.Controls.Add(m_tree) Application.CommandBars("Task Pane").Position = Microsoft.Office.Core.MsoBarPosition.msoBarLeft m_tree.ImageList = m_imageList m_tree.CheckBoxes = True 'tree的dock自動(dòng)調(diào)整大小,需要在add到pane后再設(shè)置,否則不會(huì)生效 m_tree.Dock = DockStyle.Fill With m_tree .BeginUpdate() .Nodes.Clear() .Nodes.Add(New TreeNode(Me.Name)) .Nodes(0).ImageKey = "cfg" .Nodes(0).SelectedImageKey = "cfg" Dim idx As Integer = .Nodes.Add(New TreeNode("Node1")) .Nodes(idx).ImageKey = "algo" .Nodes(idx).SelectedImageKey = "algo" Dim idx2 As Integer = .Nodes(idx).Nodes.Add(New TreeNode("case1_5M")) .Nodes(idx).Nodes(idx2).ImageKey = "scen" .Nodes(idx).Nodes(idx2).SelectedImageKey = "scen" .EndUpdate() End With End Sub Private Sub Sheet1_Change(Target As Microsoft.Office.Interop.Excel.Range) Handles Me.Change 'do Tree Refresh End Sub End Class
效果圖:
|