background Image for treeview
Hello,
How to put some bitmap image as the background for the treeview. I checked that control and there is no property which lets me to set the background image for the control.
Thanks
[198 byte] By [
callavin] at [2007-11-20 9:35:13]

# 1 Re: background Image for treeview
Derive a custom class from TreeView and use the Paint event (or OnPaint function) to draw the background image yourself.
Simply create a property called BackgroundImage which you can use to set the background image and draw the image in the function mentioned above.
# 2 Re: background Image for treeview
I have not done drawing stuff programatically. It will be nice if u can refer me some URL or code snippet!!
# 3 Re: background Image for treeview
Are you able to derive your class from TreeView? If so, continue.
Simply type in: public override, then you will get a list of functions that you can override. I bet the OnPaint function will be there.
Then, use the Graphics object to draw anything you like (shouldn't be too hard).
Anyway, see this article (http://www.c-sharpcorner.com/UploadFile/mahesh/gdi_plus12092005070041AM/gdi_plus.aspx) for more information.
# 4 Re: background Image for treeview
I have written the following code in the Backimage property
Graphics obj = this.CreateGraphics();
obj.DrawImage(this.imgTemp, new Point(this.Top, this.Left));
obj.Dispose();
But when it repaints itself that image goes off.
os i wrote this code in the following method and in the property i am calling this.invalidate()
protected override void OnPaint(PaintEventArgs e)
But now that image does not come up.
# 5 Re: background Image for treeview
You can get the graphics object fomr the PaintEventArgs!
base.OnPaint(e);
Graphics obj = e.Graphics;
obj.DrawImage(this.imgTemp, new Point(this.Top, this.Left));
# 6 Re: background Image for treeview
i have written the code in onPaint overridden mehtod. But i am not sure why this the control is not coming to this method. I have put breakpoint inside this method.
# 7 Re: background Image for treeview
you have this code?:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
And you say it's not being called?
# 8 Re: background Image for treeview
this is code which i have written and didn't get any thing till now :(
public partial class TreeComponent : TreeView
{
public TreeComponent()
: base()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.imgTemp != null)
{
Graphics obj = e.Graphics;
obj.DrawImage(this.imgTemp, new Point(this.Top, this.Left));
obj.Dispose();
}
}
Image imgTemp;
public Image BackGroundImage
{
get
{
return this.imgTemp;
}
set
{
if (value != null)
{
this.imgTemp = value;
}
}
}
public TreeComponent(IContainer container)
{
container.Add(this);
InitializeComponent();
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
}
# 9 Re: background Image for treeview
Remove functions:
public TreeComponent(IContainer container)
and
private void InitializeComponent()
You don't need a partial class to derive from a TreeView (since there is no additional component initialization).
But, I don't think this will solve your drawing problem. Maybe you should try the paint event (go to your designer of the custom class, view the properties and check out the Paint event).
# 10 Re: background Image for treeview
Container class I was using so that i can drag n drop the custom-control from the tool box. And i dont think it realted to anything with the Drawing of the control.
Moreover I am unable to find the "Paint" event as such for the Tree-View class.
and so unable to write the code in corresponding event-handler. :(
# 11 Re: background Image for treeview
If he paints his own stuff into the graphics object given to him in the OnPaint method, he won't get the normal treeview items rendering.
TreeView.BackgroundImage might be the property you're looking for ;)
# 12 Re: background Image for treeview
Are u sure that this property(TreeView.BackgroundImage) exists for Tree-View (Dot net framwork 2.0)? As I am unable to find that.
# 13 Re: background Image for treeview
I'm positive. Check MSDN. It's a get/set property that takes a bitmap.
# 14 Re: background Image for treeview
@Mutant Fruit:
TreeView.BackgroundImage Property
This property supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Remarks
This property is not relevant for this class.
I guess it's not applicable for the TreeView class?
# 15 Re: background Image for treeview
That'd be one very good reason not to use the property. Thats something i didn't know ;) In this case your best bet would be to override OnPaint and all that and do custom rendering. It might be the only way other than writing your own control from scratch (not recommended :P ).
# 16 Re: background Image for treeview
I have researched this problem a bit since I got interested in this problem.
You can't simply use the OnPaint function in a TreeView since it is not supported. To enable the OnPaint function, you have to set the style to UserPaint. Here is a class that supports a custom background image (see attached file).
This class draws a background image. However, since you set the style to UserPaint, you have to draw all the lines and nodes yourself too! But I bet there are lots of classes on the internet already providing this functionality, see dev-archive (http://www.dev-archive.com) or CodeProject (http://www.codeproject.com).
# 17 Re: background Image for treeview
After googling on msdn I found this discussion thread:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=325184&SiteID=1
which says its not there in VS2005 also.