
HOWTO: Prevent flicker in TreeView control when changing the .Image property of a Node object
On a project with a listview in report mode, add a textbox (text1) and the
following. I assume you have the type declarations....
Dim LV As LVITEM
Const LVIF_TEXT = &H1
Const LVIF_IMAGE = &H2
Const LVM_FIRST = &H1000
Const LVM_SETITEM = (LVM_FIRST + 6)
'change the icon associated with item 2
With LV
.mask = LVIF_IMAGE
.iItem = 2
.iImage = 3 'index to a valid item in your imagelist
End With
Call SendMessage(ListView1.hwnd, LVM_SETITEM, 0&, ByVal LV)
'change the icon and text associated with item 3
With LV
.mask = LVIF_IMAGE Or LVIF_TEXT
.pszText = "This changed" & Chr$(0)
.cchTextMax = Len(.pszText)
.iItem = 3
.iImage = 4 'index to a valid item in your imagelist
End With
Call SendMessage(ListView1.hwnd, LVM_SETITEM, 0&, ByVal LV)
But to see the problems with changing via API, add this to the
ListView1_ItemClick sub ...
Dim msg as String
msg = "LV Internal text : " & item.Text & vbCrLf
Text1 = msg
Sample run:
LV Internal text : msdn
API returns: msdn
You'll see that although you changed the text, the collection maintained
internally returns the original text. To retrieve the changed code, you need
to use the API. Place this in the same itemclick sub beneath the above
code,then run, change and select the item.
Dim LV As LVITEM
With LV
.pszText = Space$(260) & Chr$(0)
.cchTextMax = Len(.pszText)
End With
'pass the item index. Remember the API is 0-based,
'but the collection is 1-based, so 1 needs to
'be subtracted.
Call SendMessageAny(ListView1.hwnd, LVM_GETITEMTEXT, item.Index - 1, LV)
'update the message to the textbox
msg = msg & "API returns: " & LV.pszText & vbCrLf
Text1 = msg
Sample run:
LV Internal text : msdn
API returns: This changed
--
Randy Birch, MVP Visual Basic
http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/
To assist in maintaining this thread for the benefit of
others, please post any response to this newsgroup.
Quote:
>> From limited testing, I've found that assigning new images (or test)
>> directly via the API tends not to cause the total redraw. But again, the
>> on-screen data is no longer in sync with what VB expects the control to
>> contain, so to date I've not done anything but play with this.
>I'm sure I might be able to come up with some idea. Do you know the API for
>assigning an image to a treeview control node. If so, could you please show
>me the API and source code to do this. I'm sure I could work out a way.
>What I think I could do would be to subclass the treeview with a special
>flag that permits refreshing or redrawing. But would that work? I could set
>the .Image property of a node, and then set the flag for not to redraw.
>Next time I get redraw message I reset the flag to false. Would this work?
>I'll try it.