Thursday, September 13, 2007

What happened to the Treeview Icons?

As part of the Extensibility implementation we have been looking into a bug in the last day or so, related to Icon images in a treeview. The bug is that in Windows XP the icon images didn't appear, in Vista they did, for the same EXE.  The odd part?  It was working in both until just recently.

So what happened?

As part of doing a review, and some related bug fixes, I noticed that the visual styles weren't enabled, so I 'fixed' them.  Now I'm running Windows Vista, so simply putting Application.EnableVisualStyles before Application.Run worked beautifully.  You might ask why this hadn't been done earlier, well it had, sort of.  The application starts multiple threads, with a message loop and form on each, there are reasons for this that I won't go into here.  The bug I fixed was when I notice we'd only called Application.EnableVisualStyles on the initial thread, and I added the call into each new thread.

As I said, this worked swimmingly for me, but a while later when the other Devs got the latest code (they're using Windows XP) things got interesting.  There is a treeview on the form and it's icon had disappeared!  But only on the Windows XP machines??  I tried running the EXE on my Vista machine and the images appeared, so it's obviously a runtime issue and not a compile time one.

The answer, a lesson learned long ago but unfortunately forgotten.  Under Windows XP the Application.EnableVisualStyles exhibits some interesting behaviour.  I'm not going to try to explain why, I'll defer to those more knowledgeable in this area.  I found a Code Project article and an old but informative blog that I think do a good job of explaining the issue.

Note that as part of the fix for this you must call Application.EnableVisualStyles and Application.DoEvents BEFORE you call the constructor for the form.  This is due to the message loop and is explained in the blog article referred to above.

 

No comments: