Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have some nodes binded to my Treeview Control.
i don't want to show imagekey or imageindex to each of my node.

i want an image which acts as an backgroud for entire TreeView Control.

how to set background image to Treeview control?Possible or not?

if Possible
do let me know.
if not try to suggest me the other way..

Purely Windows Application only C#.Net or VB.Net.

Sincerely,
Pawan.
Posted

Yes it is possible - I think. It certainly isn't going to be easy, and I don't think the effort is going to be worth the results...

TreeView does have a property BackgroundImage which you can set - it isn't listed in intellisense so you have to type the whole thing. Unfortunately, it does nothing, for reasons I cannot fathom. The "official" MS explanation that TreeView is derived from control does not really hold water, as the property exists, but ho hum.

You can provide your own background by overriding WndProc and trapping WM_ERASEBKGND in a derived class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace GUITester
    {
    public partial class MyTreeView : TreeView
        {
        private Image background;

        public Image Background
            {
            get { return background; }
            set { background = value; }
            }

        public MyTreeView()
            {
            InitializeComponent();
            background = Bitmap.FromFile(@"F:\Temp\XXX.jpg");
            }

        const int WM_ERASEBKGND = 20;

        protected override void WndProc(ref Message m)
            {
            if (m.Msg == WM_ERASEBKGND)
                {
                using (Graphics g = CreateGraphics())
                    {
                    g.DrawImage(background, new Point(0, 0));
                    }
                }
            else
                {
                base.WndProc(ref m);
                }
            }
        }
    }
And this works - right up until you add nodes. Try it - it looks pretty horrible. So the obvious thing to do is to derive from TreeNode and pull the same trick. Only you can't - TreeNode doesn't expose WndProc. So you will have to get really clever and find some other way!

If you really must do this, then either write your own TreeView, or buy one in - I am sure there is such a thing out there!

If you do write it yourself, you could get a good article out of it!
 
Share this answer
 
Comments
Simon Dufour 15-Jul-10 12:09pm    
Reason for my vote of 5
Didn't even think it was possible!
Pawan Kiran 16-Jul-10 2:07am    
Reason for my vote of 5
i have learned some thing new from your msg.
In WPF, it's really easy to do. If it's really critical to your project, you might want to reconsider the technology you use. Using the right technology is an important part of a project analysis phase.
 
Share this answer
 
Comments
Pawan Kiran 16-Jul-10 1:30am    
Yes, what ever you said is right but in some cases we are restricted to do the things with limited resources. Now i am in a restricted zone.
Pawan Kiran 16-Jul-10 2:04am    
Reason for my vote of 5
your words are True.
Simon Dufour 16-Jul-10 8:13am    
You have project constraints then that's how it usually turn up. Good luck :).
Yes, It is possible.

You need to create a stylesheet
For eg.
.logo
{
background-image:url('images/logo.gif');
width:180px;
height:50px;
z-index:1;
}


and on your treeview control

Add div tag and call class =logo

your treeview control

end div tag

This will add background image to your treeview control.
 
Share this answer
 
v2
Comments
Pawan Kiran 15-Jul-10 9:16am    
Thanks to you.
When i use asp.net Treeview, i will make use of ur code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900