Click to See Complete Forum and Search --> : background Image for treeview
callavin
July 25th, 2007, 01:57 AM
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
Tischnoetentoet
July 25th, 2007, 02:01 AM
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.
callavin
July 26th, 2007, 02:51 AM
I have not done drawing stuff programatically. It will be nice if u can refer me some URL or code snippet!!
Tischnoetentoet
July 26th, 2007, 03:07 AM
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.
callavin
July 26th, 2007, 07:31 AM
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.
Tischnoetentoet
July 26th, 2007, 08:55 AM
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));
callavin
July 27th, 2007, 02:31 AM
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.
Tischnoetentoet
July 27th, 2007, 03:02 AM
you have this code?:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
And you say it's not being called?
callavin
July 30th, 2007, 12:13 AM
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();
}
}
Tischnoetentoet
July 30th, 2007, 12:19 AM
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).
callavin
July 30th, 2007, 01:06 AM
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. :(
Mutant_Fruit
July 30th, 2007, 01:18 AM
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 ;)
callavin
July 30th, 2007, 01:35 AM
Are u sure that this property(TreeView.BackgroundImage) exists for Tree-View (Dot net framwork 2.0)? As I am unable to find that.
Mutant_Fruit
July 30th, 2007, 01:49 AM
I'm positive. Check MSDN. It's a get/set property that takes a bitmap.
Tischnoetentoet
July 30th, 2007, 01:54 AM
@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?
Mutant_Fruit
July 30th, 2007, 02:06 AM
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 ).
Tischnoetentoet
July 30th, 2007, 02:31 AM
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 CodeGuru (http://www.codeguru.com) or CodeProject (http://www.codeproject.com).
callavin
July 30th, 2007, 03:07 AM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.