Tim8w

Is there a way to set the background image of a TreeView control like you used to be able to do in VB6 using the Sheridan controls

Sheridan VB6 SSTree control Properties:

PictureBackground
PictureBackgroundMaskColor
PictureBackgroundStyle
PictureBackgroundUseMask


Re: Visual Basic General TreeView Control - Background Image?

JohnWein

Use DrawMode OwnerDrawAll and you can do whatever you like.



Re: Visual Basic General TreeView Control - Background Image?

Tim8w

Is there any good examples out there I've done some searching and have not found any...




Re: Visual Basic General TreeView Control - Background Image?

JohnWein

In the "Search Microsoft.com for TextBox type: "ownerdrawall".

Place your Image in a Bitmap. In the Control's Paint event draw the Bitmap and then draw the rest of the Control on top of the Bitmap.





Re: Visual Basic General TreeView Control - Background Image?

Tim8w

I did this, but unfortunately, the TreeView Control doesn't contain an OnPaint event... I created one for it myself, but it never gets called...

Code Snippet

Private Sub tvRecipe_OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

e.Graphics.DrawImage(My.Resources.VeggieBag, 0, 0)

End Sub






Re: Visual Basic General TreeView Control - Background Image?

JohnWein

When I search I find that this Control's Paint event is called DrawNode.



Re: Visual Basic General TreeView Control - Background Image?

SJWhiteley

You will have to inherit from the treeview control:

Code Snippet

Public Class TreeEx : Inherits Windows.Forms.TreeView

Sub New()

'

MyBase.SetStyle(ControlStyles.DoubleBuffer _

Or ControlStyles.UserPaint _

Or ControlStyles.AllPaintingInWmPaint, _

True)

MyBase.UpdateStyles()

'

End Sub

Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)

MyBase.OnPaintBackground(pevent)

pevent.Graphics.DrawImage(My.Resources.MyBackground, New Rectangle(0, 0, ClientSize.Width, ClientSize.Height))

End Sub

End Class

Overiding the OnPaintBackground, and setting the style allows you to paint into the background. This may also change the way nodes are drawn, I'm not sure, as I draw all the nodes manually, anyway Smile






Re: Visual Basic General TreeView Control - Background Image?

Tim8w

SJWhiteley,

Could you send me a small example. I can't get the code above to compile. It keeps complaining about Inheriting from a different base...

Tim

TAlvord@kavlico.com






Re: Visual Basic General TreeView Control - Background Image?

rkimble

It sounds like you may have added a CustomControl or UserControl to your project and then tried to paste in SJ's code. This would cause the designer error you mention becuase there is already another partial class (created by the designer) that is inheriting from System.Windows.Forms.Control. To try SJ's code, add a plain Class file to the project and then paste his code over it.

You will have to handle all the drawing yourself if you use this snippet, and that may not be desireable for your need. I think there may be a better way... I'll experiment with it a bit later and post back here.






Re: Visual Basic General TreeView Control - Background Image?

Tim8w

rkimble,

Any luck with your experiment






Re: Visual Basic General TreeView Control - Background Image?

rkimble

Sorry, but no - this seems nearly impossible using the normal TreeView. I'd be curious to see SJ's code that does this... It appears that you would have to draw each node in the OnPaint override; and since the treeview would still be raising the DrawNode event it seems like there would be a lot of overhead and the control would be clunky. For instance, expanding a node causes the background image to redraw over itself leaving ugly chunks of left over image.

It looks like this is one for a third party custom control, perhaps something from the Infragistics control suite...






Re: Visual Basic General TreeView Control - Background Image?

SJWhiteley

That sounds like a challenge ...






Re: Visual Basic General TreeView Control - Background Image?

SJWhiteley

Actually, the DrawNode events don't fire: all painting of the control is done in the Paint Events (as set by the style of the control). What this means is that you have to do all the drawing yourself for each node. This includes drawing the expander, etc.

I had an example of this on gotdotnet - but that's dissappearing rapidly...

http://www.gotdotnet.com/codegallery/codegallery.aspx id=f5be1ee3-8f31-4ffe-8b00-c6dfad91fea3

It may seem a bit daunting if you haven't done much drawing, but if you really, really, need that background image, then you could use this as a starting point. Here's a screenshot of something I've been working on - I added a background image to the treeview on the left:

http://www.playinthesoup.com/soup/misc/treeviewbackground.png

This image just shows a few 'styles' added (the +/- node expanders are replaced with arrows):

http://www.playinthesoup.com/soup/misc/treeview.png

It demonstrates it can be done, but like a lot of things, it's a bit of work.






Re: Visual Basic General TreeView Control - Background Image?

rkimble

I was hoping you'd take it as one!




Re: Visual Basic General TreeView Control - Background Image?

rkimble

Ok, that makes sense. So it's a bit of work, but not nearly as bad as I first suspected.

Interesting stuff!!!