HOWTO: Prevent flicker in TreeView control when changing the .Image property of a Node object 
Author Message
 HOWTO: Prevent flicker in TreeView control when changing the .Image property of a Node object

Hello everybody.

I have a treeview control. I fill it with the appropriate nodes and set the
appropriate images as I add the nodes.

If I change a node's .Image property. the treeview often flickers. This is
quite annoying, as it does not do it with the Policy Editor (in the
treeview window there) - you know, the checkboxes on and off states do not
cause it to flicker, and I've seen many apps that have treeviews that
change images for nodes and do not flicker (at all).

It must be something in the treeview in commoncontrol in VB?

How can I prevent the flicker. It is very irritating. Is there a way to
direct the treeview drawing to an offscreen DC and then bitblt it to the
treeview screen DC?

Thanks for your help.



Wed, 02 May 2001 03:00:00 GMT  
 HOWTO: Prevent flicker in TreeView control when changing the .Image property of a Node object

Quote:

> I have a treeview control. I fill it with the appropriate nodes and set the
> appropriate images as I add the nodes.

> If I change a node's .Image property. the treeview often flickers. This is
> quite annoying, as it does not do it with the Policy Editor (in the
> treeview window there) - you know, the checkboxes on and off states do not
> cause it to flicker, and I've seen many apps that have treeviews that
> change images for nodes and do not flicker (at all).

> It must be something in the treeview in commoncontrol in VB?

> How can I prevent the flicker. It is very irritating. Is there a way to
> direct the treeview drawing to an offscreen DC and then bitblt it to the
> treeview screen DC?

Maybe I'm off here, but you might want to check out the LockWindowUpdate
API.

It works with forms, but also some controls (listboxes for instance).

Declare Function LockWindowUpdate Lib "user32" Alias "LockWindowUpdate"
(ByVal hwndLock As Long) As Long

Usage:
        lRet = LockWindowUpdate(TreeView1.hWnd)
        'do things
        lRet = LockWindowUpdate(0&)

Hope this is of some help,

Trond Solberg
Systems Consultant



Wed, 02 May 2001 03:00:00 GMT  
 HOWTO: Prevent flicker in TreeView control when changing the .Image property of a Node object
No, LockWindowUpdate will prevent the new image from displaying until the
control is again refreshed. As for Bitblt'ing the image, it won't do any
good. VB keeps the reference to the assigned image in its control's internal
collections, so simply changing the screen representation does not alter
VB's knowledge of the change, hence the previous image will again appear on
a refresh of the item or control.

--

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:


>> I have a treeview control. I fill it with the appropriate nodes and set
the
>> appropriate images as I add the nodes.

>> If I change a node's .Image property. the treeview often flickers. This
is
>> quite annoying, as it does not do it with the Policy Editor (in the
>> treeview window there) - you know, the checkboxes on and off states do
not
>> cause it to flicker, and I've seen many apps that have treeviews that
>> change images for nodes and do not flicker (at all).

>> It must be something in the treeview in commoncontrol in VB?

>> How can I prevent the flicker. It is very irritating. Is there a way to
>> direct the treeview drawing to an offscreen DC and then bitblt it to the
>> treeview screen DC?

>Maybe I'm off here, but you might want to check out the LockWindowUpdate
>API.

>It works with forms, but also some controls (listboxes for instance).

>Declare Function LockWindowUpdate Lib "user32" Alias "LockWindowUpdate"
>(ByVal hwndLock As Long) As Long

>Usage:
> lRet = LockWindowUpdate(TreeView1.hWnd)
> 'do things
> lRet = LockWindowUpdate(0&)

>Hope this is of some help,

>Trond Solberg
>Systems Consultant



Wed, 02 May 2001 03:00:00 GMT  
 HOWTO: Prevent flicker in TreeView control when changing the .Image property of a Node object
Thanks for that information, Randy.

But I really want to prevent this disgusting flicker. It is really driving
me nuts. I can't see why it should do it when I've seen *other* treeview
controls without the flicker. Such as that example in policy editor. Go and
check it out (i might figure you have already), and it does not flicker.
Hey, perhaps subclassing is my only hope. What's your opinion on this? Do
you have any good knowledge about treeview/listview control message/type
structures? What I was asking was if you could trap the drawing, let the
drawing go to an offscreenDC and the bitblt it back when the paint message
is finished. I'm sure you could, or can't you? So trap the WM_TVPAINT
message and then reset the lParam type structure point to an OffscreenDC,
and then paint it back. Also, eat the WM_ERASEBKGND messages...Hmmm. Damn
that flicker.

Any help from you would be greatly appreciated.

Thanks.



Thu, 03 May 2001 03:00:00 GMT  
 HOWTO: Prevent flicker in TreeView control when changing the .Image property of a Node object
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.

Now if you want to change icons of items which are not visible in the
listview window, then do try the LockWindowUpdate or a SendMessage with the
redraw flag off while you update the contents. Once you enable updating
again, and scroll the item into view, it should have the image/text you
desired. I wonder too if the LV could be subclassed  to trap the redraw
messages, then use an InvalidateRect just for the screen area that actually
was updated.  I've not tried .. just firing thoughts.

--

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:
>Thanks for that information, Randy.

>But I really want to prevent this disgusting flicker. It is really driving
>me nuts. I can't see why it should do it when I've seen *other* treeview
>controls without the flicker. Such as that example in policy editor. Go and
>check it out (i might figure you have already), and it does not flicker.
>Hey, perhaps subclassing is my only hope. What's your opinion on this? Do
>you have any good knowledge about treeview/listview control message/type
>structures? What I was asking was if you could trap the drawing, let the
>drawing go to an offscreenDC and the bitblt it back when the paint message
>is finished. I'm sure you could, or can't you? So trap the WM_TVPAINT
>message and then reset the lParam type structure point to an OffscreenDC,
>and then paint it back. Also, eat the WM_ERASEBKGND messages...Hmmm. Damn
>that flicker.

>Any help from you would be greatly appreciated.

>Thanks.



Thu, 03 May 2001 03:00:00 GMT  
 HOWTO: Prevent flicker in TreeView control when changing the .Image property of a Node object

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.



Fri, 04 May 2001 03:00:00 GMT  
 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.



Fri, 04 May 2001 03:00:00 GMT  
 HOWTO: Prevent flicker in TreeView control when changing the .Image property of a Node object

All of this seems more trouble than it's worth.  You might as well use
createwindowex. I know one of you guys has an example on your site that
illustrates this with the added bonus of using the system image list.  This
is the way to go IMO.



Sun, 06 May 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. HOWTO: Prevent flicker in TreeView control when changing the .Image property of a Node object

2. HOWTO: Prevent flicker in TreeView control when changing the .Image property of a Node object

3. TreeView :Flicker in TreeView-control

4. Preventing Flicker in a listview control

5. HowTO: Eliminate flicker when redrawing the Chart (timer controled) with MSChart control

6. Toolbar flickering when animating (changing) images on a button

7. Flickers filling the TreeView Control

8. Control Flicker (Treeview/Listview)

9. How do I avoid Flickering in TreeView control

10. HowTo: Changing a files property pages

11. Image Control- how to stop flickering?

12. Remove flickering in Image Control

 

 
Powered by phpBB® Forum Software