需求是在sheet的左側停靠System.Windows.Forms.TreeView控件,實現相似資源瀏覽器的效果,另外,tree節點使用自定義的圖標,支持複選框。
首先準備好樹節點的圖標,使用visual studio 2010自帶的圖標能夠省去不少麻煩(在安裝目錄\Common7\VS2010ImageLibrary)。我挑選了4個16x16大小的圖標拷貝到vsto工程下的Resources目錄下。經過Project->xxx Properties->Resources->AddResource->Add Existing File添加圖標到工程中,代碼中就能夠用My.Resources.aaa的方式引用了(aaa爲圖標名稱)。瀏覽器
接下來,在sheet初始化函數中完成圖標與TreeView控件的初始化代碼,若是TreeView控件的內容須要隨sheet數據變化而刷新,還須要在sheet_change函數中添加代碼。函數
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
'
第一個參數爲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自動調整大小,須要在add到pane後再設置,不然不會生效
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