Option Explicit ' User-defined enum values returned by GetTVItemButtonState Public Enum TVItemOverlayIndices tvoNoOverlay = 0 tvoShareOverlay = 1 tvoShortcutOverlay = 2 End Enum Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hWnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long Declare Function ImageList_SetOverlayImage Lib "comctl32.dll" (ByVal himl As Long, ByVal iImage As Long, ByVal iOverlay As Long) As Boolean Public Const TV_FIRST = &H1100 Public Const TVM_GETNEXTITEM = (TV_FIRST + 10) Public Const TVM_GETITEM = (TV_FIRST + 12) Public Const TVM_SETITEM = (TV_FIRST + 13) ' TVM_GETNEXTITEM wParam Public Const TVGN_CARET = &H9 ' TVM_GET/SETITEM lParam Public Type TVITEM ' was TV_ITEM mask As Long hItem As Long state As Long stateMask As Long pszText As Long ' pointer cchTextMax As Long iImage As Long iSelectedImage As Long cChildren As Long lParam As Long End Type ' TVITEM mask Public Const TVIF_STATE = &H8 ' TVITEM state, stateMask Public Const TVIS_OVERLAYMASK = &HF00 ' Returns the overlay index of the specified treeview item ' hwndTV - treeview's window handle ' hItem - item's handle we're checking Public Function GetTVItemOverlayIndex(hwndTV As Long, hItem As Long) As TVItemOverlayIndices Dim tvi As TVITEM tvi.mask = TVIF_STATE tvi.hItem = hItem tvi.stateMask = TVIS_OVERLAYMASK If TreeView_GetItem(hwndTV, tvi) Then GetTVItemOverlayIndex = OVERLAYMASKTOINDEX(tvi.state And TVIS_OVERLAYMASK) End If End Function ' Sets the overlay index of the specified treeview item ' hwndTV - treeview's window handle ' hItem - item's handle we're checking Public Function SetTVItemOverlayIndex(hwndTV As Long, hItem As Long, dwIndex As TVItemOverlayIndices) As Boolean Dim tvi As TVITEM tvi.mask = TVIF_STATE tvi.hItem = hItem tvi.state = INDEXTOOVERLAYMASK(dwIndex) tvi.stateMask = TVIS_OVERLAYMASK SetTVItemOverlayIndex = TreeView_SetItem(hwndTV, tvi) End Function ' Retrieves the tree-view item that bears the specified relationship to a specified item. ' Returns the handle to the item if successful or 0 otherwise. Public Function TreeView_GetNextItem(hWnd As Long, hItem As Long, flag As Long) As Long TreeView_GetNextItem = SendMessage(hWnd, TVM_GETNEXTITEM, ByVal flag, ByVal hItem) End Function ' Retrieves the currently selected item. ' Returns the handle to the item if successful or 0 otherwise. Public Function TreeView_GetSelection(hWnd As Long) As Long TreeView_GetSelection = TreeView_GetNextItem(hWnd, 0, TVGN_CARET) End Function ' Retrieves some or all of a tree-view item's attributes. ' Returns TRUE if successful or FALSE otherwise. Public Function TreeView_GetItem(hWnd As Long, pitem As TVITEM) As Boolean TreeView_GetItem = SendMessage(hWnd, TVM_GETITEM, 0, pitem) End Function ' Sets some or all of a tree-view item's attributes. ' Old docs say returns zero if successful or - 1 otherwise. ' New docs say returns TRUE if successful, or FALSE otherwise Public Function TreeView_SetItem(hWnd As Long, pitem As TVITEM) As Boolean TreeView_SetItem = SendMessage(hWnd, TVM_SETITEM, 0, pitem) End Function ' Returns the one-based overlay mask from the specifed zero-based overlay ' image index, shifted left eight bits. A common control utility macro. Public Function INDEXTOOVERLAYMASK(iIndex As Long) As Long ' INDEXTOOVERLAYMASK(i) ((i) << 8) INDEXTOOVERLAYMASK = iIndex * (2 ^ 8) End Function ' Returns the zero-based overlay image index from the specified one-based ' overlay mask. The inverse of INDEXTOOVERLAYMASK. ' A user-defined function (not in Commctrl.h) Public Function OVERLAYMASKTOINDEX(iOverlay As Long) As Long OVERLAYMASKTOINDEX = iOverlay / (2 ^ 8) End Function
Option Explicit ' Demonstrates how to add and remove TreeView Node overlay icons. Private m_hwndTV As Long ' TreeView1.hWnd Private m_fNoClick As BooleanPrivate Sub Form_Load() Dim nod As Node Dim i As Integer Dim j As Integer
' Initialize the Imagelist With ImageList1 .ImageWidth = 16 .ImageHeight = 16 .ListImages.Add Picture:=LoadPicture("icons\drive16.ico") ' index 0 .ListImages.Add Picture:=LoadPicture("icons\fldclosed16.ico") ' index 1 .ListImages.Add Picture:=LoadPicture("icons\fldopen16.ico") ' index 2 .ListImages.Add Picture:=LoadPicture("icons\share16.ico") ' index 3 .ListImages.Add Picture:=LoadPicture("icons\shortcut16.ico") ' index 4
' Establish the indices of our overlay icons in the ImageList. ' The Indices of the Option1 control array correspond to the ' TVItemOverlayIndices Enum member values. Call ImageList_SetOverlayImage(.hImageList, 3, tvoShareOverlay) Call ImageList_SetOverlayImage(.hImageList, 4, tvoShortcutOverlay) End With
' Fill up the treeview... For i = 1 To 2 Set nod = .Nodes.Add(, , , "Drive" & i, 1, 1) For j = 1 To 4 Call .Nodes.Add(nod.Index, tvwChild, , "Folder" & j, 2, 3) Next nod.Expanded = True Next
End Sub' Sets the value of the OptionButton whose Index corresponds to the ' overlay index of the selected NodePrivate Sub TreeView1_NodeClick(ByVal Node As Node) Dim hItem As Long
hItem = TreeView_GetSelection(m_hwndTV) If hItem Then m_fNoClick = True Option1(GetTVItemOverlayIndex(m_hwndTV, hItem)) = True ' invokes Option1_Click m_fNoClick = False End If
End Sub' Sets the overlay index of the selected Node to the value of the ' selected OptionButton's Index.Private Sub Option1_Click(Index As Integer) Dim hItem As Long
If m_fNoClick Then Exit Sub
hItem = TreeView_GetSelection(m_hwndTV) If hItem Then Call SetTVItemOverlayIndex(m_hwndTV, hItem, (Index)) End If
End Sub
一个节点添加两个并列的图标--- Add OverlayIcon for a Treeview Node
to:VBAdvisor 请加我的qq或告诉我您的qq好吗? 我想详细请教一下
Sorry,I don't own a QQ account.
By the way,the code I provided is very clear and straight away.Good luck!
' TreeView 的名称是“tvwData”。
' 添加一个节点并设置其 Image 属性。
' 图像的 Key 值为“leaf”
tvwData.Nodes.Add , ,"1 node","Top","leaf"
End Sub
可以告诉我你的qq吗?
treeview控件,一个节点添加两个并列的图标,
例如一个节点: pic1|pic2 父节点;两个图标同时显示
' User-defined enum values returned by GetTVItemButtonState
Public Enum TVItemOverlayIndices
tvoNoOverlay = 0
tvoShareOverlay = 1
tvoShortcutOverlay = 2
End Enum
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Declare Function ImageList_SetOverlayImage Lib "comctl32.dll" (ByVal himl As Long, ByVal iImage As Long, ByVal iOverlay As Long) As Boolean
Public Const TV_FIRST = &H1100
Public Const TVM_GETNEXTITEM = (TV_FIRST + 10)
Public Const TVM_GETITEM = (TV_FIRST + 12)
Public Const TVM_SETITEM = (TV_FIRST + 13)
' TVM_GETNEXTITEM wParam
Public Const TVGN_CARET = &H9
' TVM_GET/SETITEM lParam
Public Type TVITEM ' was TV_ITEM
mask As Long
hItem As Long
state As Long
stateMask As Long
pszText As Long ' pointer
cchTextMax As Long
iImage As Long
iSelectedImage As Long
cChildren As Long
lParam As Long
End Type
' TVITEM mask
Public Const TVIF_STATE = &H8
' TVITEM state, stateMask
Public Const TVIS_OVERLAYMASK = &HF00
' Returns the overlay index of the specified treeview item
' hwndTV - treeview's window handle
' hItem - item's handle we're checking
Public Function GetTVItemOverlayIndex(hwndTV As Long, hItem As Long) As TVItemOverlayIndices
Dim tvi As TVITEM
tvi.mask = TVIF_STATE
tvi.hItem = hItem
tvi.stateMask = TVIS_OVERLAYMASK
If TreeView_GetItem(hwndTV, tvi) Then
GetTVItemOverlayIndex = OVERLAYMASKTOINDEX(tvi.state And TVIS_OVERLAYMASK)
End If
End Function
' Sets the overlay index of the specified treeview item
' hwndTV - treeview's window handle
' hItem - item's handle we're checking
Public Function SetTVItemOverlayIndex(hwndTV As Long, hItem As Long, dwIndex As TVItemOverlayIndices) As Boolean
Dim tvi As TVITEM
tvi.mask = TVIF_STATE
tvi.hItem = hItem
tvi.state = INDEXTOOVERLAYMASK(dwIndex)
tvi.stateMask = TVIS_OVERLAYMASK
SetTVItemOverlayIndex = TreeView_SetItem(hwndTV, tvi)
End Function
' Retrieves the tree-view item that bears the specified relationship to a specified item.
' Returns the handle to the item if successful or 0 otherwise.
Public Function TreeView_GetNextItem(hWnd As Long, hItem As Long, flag As Long) As Long
TreeView_GetNextItem = SendMessage(hWnd, TVM_GETNEXTITEM, ByVal flag, ByVal hItem)
End Function
' Retrieves the currently selected item.
' Returns the handle to the item if successful or 0 otherwise.
Public Function TreeView_GetSelection(hWnd As Long) As Long
TreeView_GetSelection = TreeView_GetNextItem(hWnd, 0, TVGN_CARET)
End Function
' Retrieves some or all of a tree-view item's attributes.
' Returns TRUE if successful or FALSE otherwise.
Public Function TreeView_GetItem(hWnd As Long, pitem As TVITEM) As Boolean
TreeView_GetItem = SendMessage(hWnd, TVM_GETITEM, 0, pitem)
End Function
' Sets some or all of a tree-view item's attributes.
' Old docs say returns zero if successful or - 1 otherwise.
' New docs say returns TRUE if successful, or FALSE otherwise
Public Function TreeView_SetItem(hWnd As Long, pitem As TVITEM) As Boolean
TreeView_SetItem = SendMessage(hWnd, TVM_SETITEM, 0, pitem)
End Function
' Returns the one-based overlay mask from the specifed zero-based overlay
' image index, shifted left eight bits. A common control utility macro.
Public Function INDEXTOOVERLAYMASK(iIndex As Long) As Long
' INDEXTOOVERLAYMASK(i) ((i) << 8)
INDEXTOOVERLAYMASK = iIndex * (2 ^ 8)
End Function
' Returns the zero-based overlay image index from the specified one-based
' overlay mask. The inverse of INDEXTOOVERLAYMASK.
' A user-defined function (not in Commctrl.h)
Public Function OVERLAYMASKTOINDEX(iOverlay As Long) As Long
OVERLAYMASKTOINDEX = iOverlay / (2 ^ 8)
End Function
' Demonstrates how to add and remove TreeView Node overlay icons.
Private m_hwndTV As Long ' TreeView1.hWnd
Private m_fNoClick As BooleanPrivate Sub Form_Load()
Dim nod As Node
Dim i As Integer
Dim j As Integer
' Initialize the Imagelist
With ImageList1
.ImageWidth = 16
.ImageHeight = 16
.ListImages.Add Picture:=LoadPicture("icons\drive16.ico") ' index 0
.ListImages.Add Picture:=LoadPicture("icons\fldclosed16.ico") ' index 1
.ListImages.Add Picture:=LoadPicture("icons\fldopen16.ico") ' index 2
.ListImages.Add Picture:=LoadPicture("icons\share16.ico") ' index 3
.ListImages.Add Picture:=LoadPicture("icons\shortcut16.ico") ' index 4
' Establish the indices of our overlay icons in the ImageList.
' The Indices of the Option1 control array correspond to the
' TVItemOverlayIndices Enum member values.
Call ImageList_SetOverlayImage(.hImageList, 3, tvoShareOverlay)
Call ImageList_SetOverlayImage(.hImageList, 4, tvoShortcutOverlay)
End With
With TreeView1
' Initialize the TreeView
.HideSelection = False
.ImageList = ImageList1
.Indentation = 19 * Screen.TwipsPerPixelX
.LabelEdit = tvwManual
.LineStyle = tvwRootLines
m_hwndTV = .hWnd
' Fill up the treeview...
For i = 1 To 2
Set nod = .Nodes.Add(, , , "Drive" & i, 1, 1)
For j = 1 To 4
Call .Nodes.Add(nod.Index, tvwChild, , "Folder" & j, 2, 3)
Next
nod.Expanded = True
Next
.Nodes(1).Selected = True
End With ' TreeView1
m_fNoClick = True
Option1(0) = True ' invokes Option1_Click
m_fNoClick = False
End Sub' Sets the value of the OptionButton whose Index corresponds to the
' overlay index of the selected NodePrivate Sub TreeView1_NodeClick(ByVal Node As Node)
Dim hItem As Long
hItem = TreeView_GetSelection(m_hwndTV)
If hItem Then
m_fNoClick = True
Option1(GetTVItemOverlayIndex(m_hwndTV, hItem)) = True ' invokes Option1_Click
m_fNoClick = False
End If
End Sub' Sets the overlay index of the selected Node to the value of the
' selected OptionButton's Index.Private Sub Option1_Click(Index As Integer)
Dim hItem As Long
If m_fNoClick Then Exit Sub
hItem = TreeView_GetSelection(m_hwndTV)
If hItem Then
Call SetTVItemOverlayIndex(m_hwndTV, hItem, (Index))
End If
End Sub
请加我的qq或告诉我您的qq好吗?
我想详细请教一下
msn有吗?邮箱什么的?